mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 12:23:15 +00:00
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.
This commit is contained in:
parent
0cb16ffe08
commit
d6a4c0c79e
|
@ -98,22 +98,23 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
|
||||||
|
|
||||||
Optional<int> convert_to_int(const StringView& str)
|
Optional<int> convert_to_int(const StringView& str)
|
||||||
{
|
{
|
||||||
if (str.is_empty())
|
auto str_trimmed = str.trim_whitespace();
|
||||||
|
if (str_trimmed.is_empty())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
bool negative = false;
|
bool negative = false;
|
||||||
size_t i = 0;
|
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 (characters[0] == '-' || characters[0] == '+') {
|
||||||
if (str.length() == 1)
|
if (str_trimmed.length() == 1)
|
||||||
return {};
|
return {};
|
||||||
i++;
|
i++;
|
||||||
negative = (characters[0] == '-');
|
negative = (characters[0] == '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
int value = 0;
|
int value = 0;
|
||||||
for (; i < str.length(); i++) {
|
for (; i < str_trimmed.length(); i++) {
|
||||||
if (characters[i] < '0' || characters[i] > '9')
|
if (characters[i] < '0' || characters[i] > '9')
|
||||||
return {};
|
return {};
|
||||||
value = value * 10;
|
value = value * 10;
|
||||||
|
@ -124,13 +125,14 @@ Optional<int> convert_to_int(const StringView& str)
|
||||||
|
|
||||||
Optional<unsigned> convert_to_uint(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 {};
|
return {};
|
||||||
|
|
||||||
unsigned value = 0;
|
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')
|
if (characters[i] < '0' || characters[i] > '9')
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
@ -142,14 +144,15 @@ Optional<unsigned> convert_to_uint(const StringView& str)
|
||||||
|
|
||||||
Optional<unsigned> convert_to_uint_from_hex(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 {};
|
return {};
|
||||||
|
|
||||||
unsigned value = 0;
|
unsigned value = 0;
|
||||||
const auto count = str.length();
|
const auto count = str_trimmed.length();
|
||||||
|
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
char digit = str[i];
|
char digit = str_trimmed[i];
|
||||||
u8 digit_val;
|
u8 digit_val;
|
||||||
|
|
||||||
if (digit >= '0' && digit <= '9') {
|
if (digit >= '0' && digit <= '9') {
|
||||||
|
|
|
@ -130,6 +130,10 @@ TEST_CASE(convert_to_int)
|
||||||
actual = AK::StringUtils::convert_to_int("-12345");
|
actual = AK::StringUtils::convert_to_int("-12345");
|
||||||
EXPECT_EQ(actual.has_value(), true);
|
EXPECT_EQ(actual.has_value(), true);
|
||||||
EXPECT_EQ(actual.value(), -12345);
|
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)
|
TEST_CASE(convert_to_uint)
|
||||||
|
@ -170,6 +174,10 @@ TEST_CASE(convert_to_uint)
|
||||||
actual = AK::StringUtils::convert_to_uint("12345");
|
actual = AK::StringUtils::convert_to_uint("12345");
|
||||||
EXPECT_EQ(actual.has_value(), true);
|
EXPECT_EQ(actual.has_value(), true);
|
||||||
EXPECT_EQ(actual.value(), 12345u);
|
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)
|
TEST_CASE(ends_with)
|
||||||
|
|
Loading…
Reference in a new issue