From 637105947a6902813bdf829abb39077312b99f36 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Thu, 5 Dec 2019 16:16:03 +0100 Subject: Fix handling of multiple flags --- cxxformat.hpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'cxxformat.hpp') diff --git a/cxxformat.hpp b/cxxformat.hpp index 1dd919e..79f16e5 100644 --- a/cxxformat.hpp +++ b/cxxformat.hpp @@ -287,20 +287,24 @@ namespace detail checkFmtLength(); std::string specification{"%"}; - const auto flag = fmt[++i]; - if (flag == '%') // handle %% here + if (fmt[i + 1] == '%') // handle %% here { return std::string{fmt.substr(0, directiveStart + 1)} + format(fmt.substr(i + 1), argumentIndex, std::forward(arg), std::forward(args)...); } - switch (flag) + while(i < fmtSize - 1) { - case '#': case '0': case '-': case ' ': case '+': - specification.push_back(flag); - checkFmtLength(); - break; - default: - --i; + switch (const auto flag = fmt[++i]) + { + case '#': case '0': case '-': case ' ': case '+': + specification.push_back(flag); + checkFmtLength(); + break; + default: + --i; + goto afterFlagLoop; + } } + afterFlagLoop: std::string tempNumber; // field width @@ -570,12 +574,12 @@ namespace detail else return format_s(); } - template - constexpr std::pair getFlag() + template + constexpr size_t getFlagEnd() { - constexpr char next = get(fmt); - if constexpr (next == '#' || next == '0' || next == '-' || next == ' ' || next == '+') return {next, i + 1}; - else return {0, i}; + constexpr char next = get(fmt); + if constexpr (next == '#' || next == '0' || next == '-' || next == ' ' || next == '+') return getFlagEnd(); + else return i + offset; } template @@ -618,9 +622,7 @@ namespace detail { if constexpr (get(fmt) == '%') return substr<0, i, true>(fmt).s + format_s(fmt), 0, argumentIndex>(std::forward(arg), std::forward(args)...); - constexpr auto flagData = getFlag(); - constexpr char flag = flagData.first; - constexpr char iAfterFlag = flagData.second; + constexpr char iAfterFlag = getFlagEnd(); constexpr size_t iAfterFieldWidth = getNumberEnd(); constexpr auto havePrecision = get(fmt) == '.'; -- cgit v1.2.3-54-g00ecf