1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

AK: Move to_int(), to_uint() implementations to StringUtils (#1338)

Provide wrappers in String and StringView. Add some tests for the
implementations.
This commit is contained in:
howar6hill 2020-03-02 21:19:33 +08:00 committed by GitHub
parent 918ebabf60
commit d75fa80a7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 141 additions and 73 deletions

View file

@ -174,45 +174,12 @@ StringView StringView::substring_view_starting_after_substring(const StringView&
int StringView::to_int(bool& ok) const
{
bool negative = false;
int value = 0;
size_t i = 0;
if (is_empty()) {
ok = false;
return 0;
}
if (characters_without_null_termination()[0] == '-') {
i++;
negative = true;
}
for (; i < length(); i++) {
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters_without_null_termination()[i] - '0';
}
ok = true;
return negative ? -value : value;
return StringUtils::convert_to_int(*this, ok);
}
unsigned StringView::to_uint(bool& ok) const
{
unsigned value = 0;
for (size_t i = 0; i < length(); ++i) {
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters_without_null_termination()[i] - '0';
}
ok = true;
return value;
return StringUtils::convert_to_uint(*this, ok);
}
unsigned StringView::hash() const