aboutsummaryrefslogtreecommitdiff
path: root/src/common/StringToNumberConverter.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-03-10 16:43:04 +0800
committercrupest <crupest@outlook.com>2022-03-10 16:43:04 +0800
commit51f87e3ff980e62f9cb5ee656e5591412e2766eb (patch)
tree4101a0c00fd77dfc694c377ae2b69178a77cdb63 /src/common/StringToNumberConverter.cpp
parent0b5c16f6b35f7144b34996d8c77f370bcbcf150c (diff)
downloadcru-51f87e3ff980e62f9cb5ee656e5591412e2766eb.tar.gz
cru-51f87e3ff980e62f9cb5ee656e5591412e2766eb.tar.bz2
cru-51f87e3ff980e62f9cb5ee656e5591412e2766eb.zip
...
Diffstat (limited to 'src/common/StringToNumberConverter.cpp')
-rw-r--r--src/common/StringToNumberConverter.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/common/StringToNumberConverter.cpp b/src/common/StringToNumberConverter.cpp
index 526c2712..e20e436c 100644
--- a/src/common/StringToNumberConverter.cpp
+++ b/src/common/StringToNumberConverter.cpp
@@ -10,20 +10,24 @@ static bool IsSpace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
-StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
- const char* const str, const Index size,
- Index* processed_characters_count) const {
+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 (!CheckParams()) throw std::invalid_argument("Invalid parsing flags.");
+ if (!converter->CheckParams())
+ throw std::invalid_argument("Invalid parsing flags.");
- const bool throw_on_error = (flags & StringToNumberFlags::kThrowOnError) != 0;
+ const bool throw_on_error =
+ (converter->flags & StringToNumberFlags::kThrowOnError) != 0;
- const char* const end = str + size;
+ auto const end = str + size;
- const char* current = str;
+ auto current = str;
- if (flags & StringToNumberFlags::kAllowLeadingSpaces) {
+ if (converter->flags & StringToNumberFlags::kAllowLeadingSpaces) {
while (current != end && IsSpace(*current)) {
current++;
}
@@ -60,7 +64,7 @@ StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
}
}
- int base = this->base;
+ int base = converter->base;
if (base == 0) {
if (*current == '0') {
@@ -96,7 +100,7 @@ StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
}
const bool allow_leading_zero =
- flags & StringToNumberFlags::kAllowLeadingZeroForInteger;
+ converter->flags & StringToNumberFlags::kAllowLeadingZeroForInteger;
while (current != end && *current == '0') {
current++;
@@ -110,9 +114,9 @@ StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
}
const bool allow_trailing_junk =
- flags & StringToNumberFlags::kAllowTrailingJunk;
+ converter->flags & StringToNumberFlags::kAllowTrailingJunk;
const bool allow_trailing_spaces =
- flags & StringToNumberFlags::kAllowTrailingSpaces;
+ converter->flags & StringToNumberFlags::kAllowTrailingSpaces;
unsigned long long result = 0;
while (current != end) {
@@ -165,4 +169,17 @@ StringToIntegerConverterImplResult StringToIntegerConverterImpl::Parse(
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