Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "format_spec.h" | ||
2 | #include <algorithm> | ||
3 | |||
4 | namespace lython { | ||
5 | |||
6 | String FormatSpecifier::__str__() const { | ||
7 |
1/1✓ Branch 1 taken 7 times.
|
7 | StringStream ss; |
8 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (fill != '\0') |
9 |
1/1✓ Branch 1 taken 1 times.
|
1 | ss << fill; |
10 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (align != '\0') |
11 |
1/1✓ Branch 1 taken 3 times.
|
3 | ss << align; |
12 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (sign != '\0') |
13 |
1/1✓ Branch 1 taken 1 times.
|
1 | ss << sign; |
14 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (alternate != '\0') |
15 |
1/1✓ Branch 1 taken 1 times.
|
1 | ss << alternate; |
16 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if (pad != '\0') |
17 |
1/1✓ Branch 1 taken 2 times.
|
2 | ss << pad; |
18 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | if (width != '\0') |
19 |
1/1✓ Branch 1 taken 6 times.
|
6 | ss << width; |
20 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (precision != '\0') |
21 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | ss << "." << precision; |
22 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
|
7 | if (type != '\0') |
23 |
1/1✓ Branch 1 taken 4 times.
|
4 | ss << type; |
24 |
1/1✓ Branch 1 taken 7 times.
|
14 | return ss.str(); |
25 | 7 | } | |
26 | |||
27 | String FormatSpecifier::__repr__() const { | ||
28 | ✗ | StringStream ss; | |
29 | ✗ | bool needs_comma = false; | |
30 | |||
31 | auto p = [&](String name, auto val) { | ||
32 | ✗ | if (needs_comma) { | |
33 | ✗ | ss << ", "; | |
34 | } | ||
35 | ✗ | ss << name << "=" << val; | |
36 | ✗ | needs_comma = true; | |
37 | ✗ | }; | |
38 | |||
39 | ✗ | ss << "Fmt("; | |
40 | |||
41 | ✗ | if (fill != '\0') | |
42 | ✗ | p("fill", fill); | |
43 | |||
44 | ✗ | if (align != '\0') | |
45 | ✗ | p("align", align); | |
46 | |||
47 | ✗ | if (sign != '\0') | |
48 | ✗ | p("sign", sign); | |
49 | ✗ | if (alternate != '\0') | |
50 | ✗ | p("alternate", alternate); | |
51 | ✗ | if (pad != '\0') | |
52 | ✗ | p("pad", pad); | |
53 | ✗ | if (width != '\0') | |
54 | ✗ | p("width", width); | |
55 | ✗ | if (precision != '\0') | |
56 | ✗ | p("precision", precision); | |
57 | ✗ | if (type != '\0') | |
58 | ✗ | p("type", type); | |
59 | |||
60 | ✗ | ss << ")"; | |
61 | ✗ | return ss.str(); | |
62 | ✗ | } | |
63 | |||
64 | bool contains(Array<char> const& arr, char target) { | ||
65 |
2/2✓ Branch 5 taken 144 times.
✓ Branch 6 taken 13 times.
|
157 | for (auto& val: arr) { |
66 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 127 times.
|
144 | if (val == target) { |
67 | 17 | return true; | |
68 | } | ||
69 | } | ||
70 | 13 | return false; | |
71 | } | ||
72 | |||
73 | bool FormatSpecifier::is_float() const { | ||
74 |
1/1✓ Branch 2 taken 1 times.
|
1 | return contains({'e', 'E', 'f', 'F', 'g', 'G', 'n', '%'}, type); |
75 | } | ||
76 | bool FormatSpecifier::is_integer() const { | ||
77 |
1/1✓ Branch 2 taken 1 times.
|
1 | return contains({'b', 'c', 'd', 'o', 'x', 'X', 'n'}, type); |
78 | } | ||
79 | bool FormatSpecifier::is_undefined() const { return type == '\0'; } | ||
80 | |||
81 | struct FormatSpecParser { | ||
82 | enum class ParsingStep | ||
83 | { | ||
84 | Fill, | ||
85 | Align, | ||
86 | Sign, | ||
87 | Alternate, | ||
88 | Pad, | ||
89 | Width, | ||
90 | Precision, | ||
91 | Type, | ||
92 | }; | ||
93 | |||
94 | ParsingStep step = ParsingStep::Type; | ||
95 | |||
96 | inline static Array<char> valid_align = {'<', '>', '=', '^'}; | ||
97 | inline static Array<char> valid_sign = {'+', '-', ' '}; | ||
98 | inline static Array<char> valid_alternate = {'#'}; | ||
99 | inline static Array<char> valid_pad = {'0'}; | ||
100 | inline static Array<char> valid_type = { | ||
101 | 'b', 'c', 'd', 'o', 'x', 'X', 'n', 'e', 'E', 'f', 'F', 'g', 'G', 'n', '%'}; | ||
102 | |||
103 | FormatSpecifier parse(String const& buffer) { | ||
104 | 11 | int i = int(buffer.size()) - 1; | |
105 | 11 | FormatSpecifier spec; | |
106 | 11 | String temp; | |
107 | |||
108 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 7 times.
|
54 | while (i >= 0) { |
109 | 47 | char c = buffer[i]; | |
110 | |||
111 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 43 times.
|
47 | if (step == ParsingStep::Fill) { |
112 | 4 | spec.fill = c; | |
113 | 4 | break; | |
114 | } | ||
115 | |||
116 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 37 times.
|
43 | if (step == ParsingStep::Align) { |
117 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (contains(valid_align, c)) { |
118 | 4 | spec.align = c; | |
119 | 4 | i -= 1; | |
120 | } | ||
121 | |||
122 | 6 | step = ParsingStep::Fill; | |
123 | 6 | continue; | |
124 | } | ||
125 | |||
126 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 31 times.
|
37 | if (step == ParsingStep::Sign) { |
127 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
|
6 | if (contains(valid_sign, c)) { |
128 | 2 | spec.sign = c; | |
129 | 2 | i -= 1; | |
130 | } | ||
131 | |||
132 | 6 | step = ParsingStep::Align; | |
133 | 6 | continue; | |
134 | } | ||
135 | |||
136 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 25 times.
|
31 | if (step == ParsingStep::Alternate) { |
137 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
|
6 | if (contains(valid_alternate, c)) { |
138 | 2 | spec.alternate = c; | |
139 | 2 | i -= 1; | |
140 | } | ||
141 | |||
142 | 6 | step = ParsingStep::Sign; | |
143 | 6 | continue; | |
144 | } | ||
145 | |||
146 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
|
25 | if (step == ParsingStep::Width) { |
147 | 5 | temp.clear(); | |
148 |
3/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
16 | while (std::isdigit(c) && i >= 0) { |
149 |
1/1✓ Branch 1 taken 13 times.
|
13 | temp.push_back(c); |
150 | 13 | i -= 1; | |
151 | |||
152 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
|
13 | if (i >= 0) { |
153 | 11 | c = buffer[i]; | |
154 | } else { | ||
155 | 2 | break; | |
156 | } | ||
157 | } | ||
158 | |||
159 |
1/1✓ Branch 3 taken 5 times.
|
5 | reverse(temp.begin(), temp.end()); |
160 | 5 | spec.width = std::strtoull(temp.c_str(), nullptr, 10); | |
161 | |||
162 |
3/3✓ Branch 1 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
|
5 | if (temp[0] == '0') { |
163 | 3 | spec.pad = '0'; | |
164 | } | ||
165 | 5 | step = ParsingStep::Alternate; | |
166 | 5 | continue; | |
167 | } | ||
168 | |||
169 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (step == ParsingStep::Precision) { |
170 | 10 | temp.clear(); | |
171 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
|
20 | while (std::isdigit(c)) { |
172 |
1/1✓ Branch 1 taken 12 times.
|
12 | temp.push_back(c); |
173 | 12 | i -= 1; | |
174 | |||
175 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | if (i >= 0) { |
176 | 10 | c = buffer[i]; | |
177 | } else { | ||
178 | 2 | break; | |
179 | } | ||
180 | } | ||
181 | |||
182 |
1/1✓ Branch 3 taken 10 times.
|
10 | reverse(temp.begin(), temp.end()); |
183 | 10 | uint64_t val = std::strtoull(temp.c_str(), nullptr, 10); | |
184 | |||
185 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (c == '.') { |
186 | 5 | i -= 1; | |
187 | 5 | spec.precision = val; | |
188 | 5 | step = ParsingStep::Width; | |
189 | } else { | ||
190 | 5 | spec.width = val; | |
191 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (temp[0] == '0') { |
192 | ✗ | spec.pad = '0'; | |
193 | } | ||
194 | 5 | step = ParsingStep::Alternate; | |
195 | } | ||
196 | 10 | continue; | |
197 | 10 | } | |
198 | |||
199 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (step == ParsingStep::Type) { |
200 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
|
10 | if (contains(valid_type, c)) { |
201 | 8 | spec.type = c; | |
202 | 8 | i -= 1; | |
203 | } | ||
204 | 10 | step = ParsingStep::Precision; | |
205 | 10 | continue; | |
206 | } | ||
207 | } | ||
208 | |||
209 | 11 | spec.valid = true; | |
210 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
|
11 | if (i > 0) { |
211 | 3 | spec.valid = false; | |
212 |
1/1✓ Branch 1 taken 3 times.
|
3 | spec.unparsed = buffer.substr(0, i); |
213 | } | ||
214 | |||
215 | 22 | return spec; | |
216 | 11 | } | |
217 | }; | ||
218 | |||
219 | FormatSpecifier FormatSpecifier::parse(String const& buffer) { | ||
220 |
1/1✓ Branch 2 taken 11 times.
|
22 | return FormatSpecParser().parse(buffer); |
221 | } | ||
222 | |||
223 | } // namespace lython | ||
224 |