fix: Fix SmallString numeric parsing, make more robust

* Fix by parsing numbers from left to right instead of right to left
  (previously, would parse "17" to 71)
* Support leading +/- signs
* Throw if unexpected char encountered
* Unit tests
This commit is contained in:
John Colvin committed 2025-06-13 20:50:30 +00:00
1 parent ca27dd3329
commit db5dbd3666
2 files changed
+101 -10

No files matched your search

+75 -10
View File
@@ -46,6 +46,7 @@ struct SmallString {
void copy_from(std::string_view str);
std::int32_t toi() const;
std::uint32_t tou() const;
std::int64_t tol() const;
std::uint64_t toul() const;
@@ -183,10 +184,49 @@ std::int32_t SmallString<N>::toi() const {
"SmallString<N>::toi failed because string is empty");
}
std::int32_t rv = 0;
std::size_t i = length_ - 1;
std::size_t i = 0;
std::int32_t digit;
bool negate = false;
do {
rv = rv * 10 + (data_[i] - '0');
} while (i-- > 0);
digit = (data_[i] - '0');
if (digit < 0 || digit > 9) {
if (i == 0 && length_ > 1) {
switch (data_[i]) {
case '+':
continue;
case '-':
negate = true;
continue;
}
}
throw std::logic_error("SmallString<N>::toi failed due to invalid "
"format, must match ^(+-)?[0-9]+$");
}
rv = rv * 10 + digit;
} while (++i < length_);
return negate ? -rv : rv;
}
template <std::size_t N>
std::uint32_t SmallString<N>::tou() const {
if (length_ == 0) {
throw std::logic_error(
"SmallString<N>::tou failed because string is empty");
}
std::uint32_t rv = 0;
std::size_t i = 0;
std::int32_t digit;
do {
digit = (data_[i] - '0');
if (digit < 0 || digit > 9) {
if (i == 0 && length_ > 1 && data_[i] == '+') {
continue;
}
throw std::logic_error("SmallString<N>::tou failed due to invalid "
"format, must match ^+?[0-9]+$");
}
rv = rv * 10u + static_cast<std::uint32_t>(digit);
} while (++i < length_);
return rv;
}
@@ -197,11 +237,27 @@ std::int64_t SmallString<N>::tol() const {
"SmallString<N>::tol failed because string is empty");
}
std::int64_t rv = 0;
std::size_t i = length_ - 1;
std::size_t i = 0;
std::int32_t digit;
bool negate = false;
do {
rv = rv * 10 + (data_[i] - '0');
} while (i-- > 0);
return rv;
digit = (data_[i] - '0');
if (digit < 0 || digit > 9) {
if (i == 0 && length_ > 1) {
switch (data_[i]) {
case '+':
continue;
case '-':
negate = true;
continue;
}
}
throw std::logic_error("SmallString<N>::tol failed due to invalid "
"format, must match ^(+-)?[0-9]+$");
}
rv = rv * 10 + digit;
} while (++i < length_);
return negate ? -rv : rv;
}
template <std::size_t N>
@@ -211,10 +267,19 @@ std::uint64_t SmallString<N>::toul() const {
"SmallString<N>::toul failed because string is empty");
}
std::uint64_t rv = 0;
std::size_t i = length_ - 1;
std::size_t i = 0;
std::int32_t digit;
do {
rv = rv * 10ul + static_cast<std::uint32_t>(data_[i] - '0');
} while (i-- > 0);
digit = (data_[i] - '0');
if (digit < 0 || digit > 9) {
if (i == 0 && length_ > 1 && data_[i] == '+') {
continue;
}
throw std::logic_error("SmallString<N>::toul failed due to invalid "
"format, must match ^+?[0-9]+$");
}
rv = rv * 10ul + static_cast<std::uint32_t>(digit);
} while (++i < length_);
return rv;
}
@@ -30,6 +30,32 @@ TEST_CASE("small_string", "[core]") {
small_str.clear();
REQUIRE(small_str.empty());
}
SECTION("numeric_parsing") {
// int
REQUIRE(SmallString<8>{"17"}.toi() == 17);
REQUIRE(SmallString<8>{"+17"}.toi() == 17);
REQUIRE(SmallString<8>{"-17"}.toi() == -17);
REQUIRE(SmallString<8>{"0"}.toi() == 0);
REQUIRE(SmallString<8>{"+0"}.toi() == 0);
REQUIRE(SmallString<8>{"-0"}.toi() == 0);
// uint
REQUIRE(SmallString<8>{"17"}.tou() == 17);
REQUIRE(SmallString<8>{"+17"}.tou() == 17);
REQUIRE(SmallString<8>{"0"}.tou() == 0);
REQUIRE(SmallString<8>{"+0"}.tou() == 0);
// long
REQUIRE(SmallString<8>{"17"}.tol() == 17);
REQUIRE(SmallString<8>{"+17"}.tol() == 17);
REQUIRE(SmallString<8>{"-17"}.tol() == -17);
REQUIRE(SmallString<8>{"0"}.tol() == 0);
REQUIRE(SmallString<8>{"+0"}.tol() == 0);
REQUIRE(SmallString<8>{"-0"}.tol() == 0);
// ulong
REQUIRE(SmallString<8>{"17"}.toul() == 17);
REQUIRE(SmallString<8>{"+17"}.toul() == 17);
REQUIRE(SmallString<8>{"0"}.toul() == 0);
REQUIRE(SmallString<8>{"+0"}.toul() == 0);
}
}
} // namespace bts::core::tests