summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-11-16 18:30:22 +0000
committerAndreas Kling <kling@serenityos.org>2020-11-17 09:48:35 +0100
commitd6a4c0c79e47d90d9c49dd45f555609bc49ef5d4 (patch)
treece96c3c8b772f7ec0eb2accd479e7c7ef2b27d51
parent0cb16ffe08c029013bb18689d46d9bb02a5a4e7b (diff)
downloadserenity-d6a4c0c79e47d90d9c49dd45f555609bc49ef5d4.zip
AK: Trim whitespace in StringUtils::convert_to_{int,uint,uint_from_hex}()
Personally I found this unintuitive at first, but it is in line with strtol(), Python's int() or JavaScript's parseInt(), so I guess it makes sense. Fixes #4097.
-rw-r--r--AK/StringUtils.cpp23
-rw-r--r--AK/Tests/TestStringUtils.cpp8
2 files changed, 21 insertions, 10 deletions
diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp
index 531b1ff505..adca8ca11f 100644
--- a/AK/StringUtils.cpp
+++ b/AK/StringUtils.cpp
@@ -98,22 +98,23 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
Optional<int> convert_to_int(const StringView& str)
{
- if (str.is_empty())
+ auto str_trimmed = str.trim_whitespace();
+ if (str_trimmed.is_empty())
return {};
bool negative = false;
size_t i = 0;
- const auto characters = str.characters_without_null_termination();
+ const auto characters = str_trimmed.characters_without_null_termination();
if (characters[0] == '-' || characters[0] == '+') {
- if (str.length() == 1)
+ if (str_trimmed.length() == 1)
return {};
i++;
negative = (characters[0] == '-');
}
int value = 0;
- for (; i < str.length(); i++) {
+ for (; i < str_trimmed.length(); i++) {
if (characters[i] < '0' || characters[i] > '9')
return {};
value = value * 10;
@@ -124,13 +125,14 @@ Optional<int> convert_to_int(const StringView& str)
Optional<unsigned> convert_to_uint(const StringView& str)
{
- if (str.is_empty())
+ auto str_trimmed = str.trim_whitespace();
+ if (str_trimmed.is_empty())
return {};
unsigned value = 0;
- const auto characters = str.characters_without_null_termination();
+ const auto characters = str_trimmed.characters_without_null_termination();
- for (size_t i = 0; i < str.length(); i++) {
+ for (size_t i = 0; i < str_trimmed.length(); i++) {
if (characters[i] < '0' || characters[i] > '9')
return {};
@@ -142,14 +144,15 @@ Optional<unsigned> convert_to_uint(const StringView& str)
Optional<unsigned> convert_to_uint_from_hex(const StringView& str)
{
- if (str.is_empty())
+ auto str_trimmed = str.trim_whitespace();
+ if (str_trimmed.is_empty())
return {};
unsigned value = 0;
- const auto count = str.length();
+ const auto count = str_trimmed.length();
for (size_t i = 0; i < count; i++) {
- char digit = str[i];
+ char digit = str_trimmed[i];
u8 digit_val;
if (digit >= '0' && digit <= '9') {
diff --git a/AK/Tests/TestStringUtils.cpp b/AK/Tests/TestStringUtils.cpp
index 6b608859ca..8b99ff39d4 100644
--- a/AK/Tests/TestStringUtils.cpp
+++ b/AK/Tests/TestStringUtils.cpp
@@ -130,6 +130,10 @@ TEST_CASE(convert_to_int)
actual = AK::StringUtils::convert_to_int("-12345");
EXPECT_EQ(actual.has_value(), true);
EXPECT_EQ(actual.value(), -12345);
+
+ actual = AK::StringUtils::convert_to_int(" \t-12345 \n\n");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), -12345);
}
TEST_CASE(convert_to_uint)
@@ -170,6 +174,10 @@ TEST_CASE(convert_to_uint)
actual = AK::StringUtils::convert_to_uint("12345");
EXPECT_EQ(actual.has_value(), true);
EXPECT_EQ(actual.value(), 12345u);
+
+ actual = AK::StringUtils::convert_to_uint(" \t12345 \n\n");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 12345u);
}
TEST_CASE(ends_with)