| 31 | } |
| 32 | |
| 33 | std::string ParseFormat(std::string fmt, const std::vector<Printer>& printers) { |
| 34 | enum class S { |
| 35 | normal, |
| 36 | curly, |
| 37 | givenindex, |
| 38 | colon, |
| 39 | width, |
| 40 | precision, |
| 41 | type, |
| 42 | write |
| 43 | }; |
| 44 | auto toint = [&fmt](std::string str) { |
| 45 | std::stringstream ss(str); |
| 46 | int a = 0; |
| 47 | ss >> a; |
| 48 | fassert( |
| 49 | !ss.fail(), |
| 50 | FILELINE + ": Can't parse '" + str + "' as int in '" + fmt + "'"); |
| 51 | return a; |
| 52 | }; |
| 53 | auto match = [](char c, std::string s) { |
| 54 | return s.find(c) != std::string::npos; |
| 55 | }; |
| 56 | const std::string digits = "0123456789"; |
| 57 | S state = S::normal; |
| 58 | std::string res; |
| 59 | struct Mod { |
| 60 | std::string givenindex; |
| 61 | std::string precision; |
| 62 | std::string width; |
| 63 | bool leadzero = false; |
| 64 | char type = 0; |
| 65 | }; |
| 66 | Mod mod; |
| 67 | size_t autoindex = 0; // current index in strs with automatic numbering |
| 68 | for (size_t i = 0; i < fmt.length(); ++i) { |
| 69 | char c = fmt[i]; |
| 70 | auto peek = [&i, &fmt]() -> char { |
| 71 | return i + 1 < fmt.length() ? fmt[i + 1] : 0; |
| 72 | }; |
| 73 | auto report = [&]() { |
| 74 | return std::string() + "got '" + c + "' at position " + |
| 75 | std::to_string(i) + " in \"" + Escape(fmt) + "\""; |
| 76 | }; |
| 77 | auto reportpeek = [&]() { |
| 78 | return std::string() + "got '" + peek() + "' at position " + |
| 79 | std::to_string(i) + " in \"" + Escape(fmt) + "\""; |
| 80 | }; |
| 81 | switch (state) { |
| 82 | case S::normal: |
| 83 | if (c == '{') { |
| 84 | if (peek() == '{') { // repeated '{{' |
| 85 | res += c; |
| 86 | ++i; |
| 87 | } else { |
| 88 | mod = Mod(); |
| 89 | state = S::curly; |
| 90 | } |