1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include "cru/common/StringToNumberConverter.h"
#include "cru/common/Exception.h"
namespace cru {
bool StringToIntegerConverterImpl::CheckParams() const {
return base == 0 || base >= 2 & base <= 36;
}
static bool IsSpace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
namespace {
template <typename T>
StringToIntegerConverterImplResult GenericParseInteger(
const StringToIntegerConverterImpl* converter, const T* const str,
const Index size, Index* processed_characters_count) {
if (str == nullptr) throw std::invalid_argument("Invalid str.");
if (size < 0) throw std::invalid_argument("Invalid size.");
if (!converter->CheckParams())
throw std::invalid_argument("Invalid parsing flags.");
const bool throw_on_error =
(converter->flags & StringToNumberFlags::kThrowOnError) != 0;
auto const end = str + size;
auto current = str;
if (converter->flags & StringToNumberFlags::kAllowLeadingSpaces) {
while (current != end && IsSpace(*current)) {
current++;
}
}
if (current == end) {
if (processed_characters_count) {
*processed_characters_count = 0;
}
if (throw_on_error) {
throw Exception(u"Empty string (after reading leading spaces).");
} else {
return {false, 0};
}
}
bool negate = false;
if (*current == '-') {
++current;
negate = true;
} else if (*current == '+') {
++current;
}
if (current == end) {
if (processed_characters_count) {
*processed_characters_count = 0;
}
if (throw_on_error) {
throw Exception(u"Empty string (after reading sign).");
} else {
return {false, 0};
}
}
int base = converter->base;
if (base == 0) {
if (*current == '0') {
++current;
if (current == end) {
if (processed_characters_count) {
*processed_characters_count = current - str;
}
return {negate, 0};
} else if (*current == 'x' || *current == 'X') {
++current;
base = 16;
} else if (*current == 'b' || *current == 'B') {
++current;
base = 2;
} else {
base = 8;
}
} else {
base = 10;
}
}
if (current == end) {
if (processed_characters_count) {
*processed_characters_count = 0;
}
if (throw_on_error) {
throw Exception(u"Empty string (after reading head base indicator).");
} else {
return {false, 0};
}
}
const bool allow_leading_zero =
converter->flags & StringToNumberFlags::kAllowLeadingZeroForInteger;
while (current != end && *current == '0') {
current++;
}
if (current == end) {
if (processed_characters_count) {
*processed_characters_count = current - str;
}
return {negate, 0};
}
const bool allow_trailing_junk =
converter->flags & StringToNumberFlags::kAllowTrailingJunk;
const bool allow_trailing_spaces =
converter->flags & StringToNumberFlags::kAllowTrailingSpaces;
unsigned long long result = 0;
while (current != end) {
const char c = *current;
if (c >= '0' && c <= (base > 10 ? '9' : base + '0' - 1)) {
result = result * base + c - '0';
current++;
} else if (base > 10 && c >= 'a' && c <= (base + 'a' - 10 - 1)) {
result = result * base + c - 'a' + 10;
current++;
} else if (base > 10 && c >= 'A' && c <= (base + 'A' - 10 - 1)) {
result = result * base + c - 'A' + 10;
current++;
} else if (allow_trailing_junk) {
break;
} else if (allow_trailing_spaces && IsSpace(c)) {
break;
} else {
if (processed_characters_count) {
*processed_characters_count = 0;
}
if (throw_on_error) {
throw Exception(String(u"Read invalid character '") + c + u"'.");
} else {
return {false, 0};
}
}
}
if (allow_trailing_spaces) {
while (current != end && IsSpace(*current)) {
current++;
}
if (current != end) {
if (processed_characters_count) {
*processed_characters_count = 0;
}
if (throw_on_error) {
throw Exception(u"There is trailing junk.");
} else {
return {false, 0};
}
}
}
if (processed_characters_count) {
*processed_characters_count = current - str;
}
return {negate, result};
}
} // namespace
StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
const char* const str, const Index size,
Index* processed_characters_count) const {
return GenericParseInteger(this, str, size, processed_characters_count);
}
StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
const char16_t* const str, const Index size,
Index* processed_characters_count) const {
return GenericParseInteger(this, str, size, processed_characters_count);
}
} // namespace cru
|