mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +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:
parent
918ebabf60
commit
d75fa80a7b
7 changed files with 141 additions and 73 deletions
|
@ -189,45 +189,12 @@ ByteBuffer String::to_byte_buffer() const
|
|||
|
||||
int String::to_int(bool& ok) const
|
||||
{
|
||||
bool negative = false;
|
||||
int value = 0;
|
||||
size_t i = 0;
|
||||
|
||||
if (is_empty()) {
|
||||
ok = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (characters()[0] == '-') {
|
||||
i++;
|
||||
negative = true;
|
||||
}
|
||||
for (; i < length(); i++) {
|
||||
if (characters()[i] < '0' || characters()[i] > '9') {
|
||||
ok = false;
|
||||
return 0;
|
||||
}
|
||||
value = value * 10;
|
||||
value += characters()[i] - '0';
|
||||
}
|
||||
ok = true;
|
||||
|
||||
return negative ? -value : value;
|
||||
return StringUtils::convert_to_int(this->view(), ok);
|
||||
}
|
||||
|
||||
unsigned String::to_uint(bool& ok) const
|
||||
{
|
||||
unsigned value = 0;
|
||||
for (size_t i = 0; i < length(); ++i) {
|
||||
if (characters()[i] < '0' || characters()[i] > '9') {
|
||||
ok = false;
|
||||
return 0;
|
||||
}
|
||||
value = value * 10;
|
||||
value += characters()[i] - '0';
|
||||
}
|
||||
ok = true;
|
||||
return value;
|
||||
return StringUtils::convert_to_uint(this->view(), ok);
|
||||
}
|
||||
|
||||
String String::number(unsigned long long value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue