mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +00:00
AK: Implement String::to_int (#99)
This commit is contained in:
parent
79dba9a545
commit
411cdf067b
2 changed files with 26 additions and 6 deletions
|
@ -15,7 +15,7 @@ bool String::operator==(const String& other) const
|
||||||
|
|
||||||
if (length() != other.length())
|
if (length() != other.length())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return !memcmp(characters(), other.characters(), length());
|
return !memcmp(characters(), other.characters(), length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,12 +122,32 @@ ByteBuffer String::to_byte_buffer() const
|
||||||
return ByteBuffer::copy(reinterpret_cast<const byte*>(characters()), length());
|
return ByteBuffer::copy(reinterpret_cast<const byte*>(characters()), length());
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Duh.
|
|
||||||
int String::to_int(bool& ok) const
|
int String::to_int(bool& ok) const
|
||||||
{
|
{
|
||||||
unsigned value = to_uint(ok);
|
bool negative = false;
|
||||||
ASSERT(ok);
|
int value = 0;
|
||||||
return (int)value;
|
ssize_t i = 0;
|
||||||
|
|
||||||
|
if (is_null()) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned String::to_uint(bool& ok) const
|
unsigned String::to_uint(bool& ok) const
|
||||||
|
|
|
@ -194,7 +194,7 @@ private:
|
||||||
RawPoint as_point;
|
RawPoint as_point;
|
||||||
RawSize as_size;
|
RawSize as_size;
|
||||||
RawRect as_rect;
|
RawRect as_rect;
|
||||||
} m_value;
|
} m_value;
|
||||||
|
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue