diff options
author | Faissal Bensefia <faissaloo@gmail.com> | 2019-05-26 03:08:36 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-26 04:08:36 +0200 |
commit | 411cdf067b5b82c96fca5880c790931818f0d757 (patch) | |
tree | 88e3d62ab56154bbbcc42fb0da301fcd213f747b | |
parent | 79dba9a545b6b7f54dbbee0c718019c7d50b5380 (diff) | |
download | serenity-411cdf067b5b82c96fca5880c790931818f0d757.zip |
AK: Implement String::to_int (#99)
-rw-r--r-- | AK/String.cpp | 30 | ||||
-rw-r--r-- | LibGUI/GVariant.h | 2 |
2 files changed, 26 insertions, 6 deletions
diff --git a/AK/String.cpp b/AK/String.cpp index beacc5b881..3298e833aa 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -15,7 +15,7 @@ bool String::operator==(const String& other) const if (length() != other.length()) return false; - + 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()); } -// FIXME: Duh. int String::to_int(bool& ok) const { - unsigned value = to_uint(ok); - ASSERT(ok); - return (int)value; + bool negative = false; + int value = 0; + 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 diff --git a/LibGUI/GVariant.h b/LibGUI/GVariant.h index 5fee08ef9d..38cdfd713b 100644 --- a/LibGUI/GVariant.h +++ b/LibGUI/GVariant.h @@ -194,7 +194,7 @@ private: RawPoint as_point; RawSize as_size; RawRect as_rect; - } m_value; + } m_value; Type m_type { Type::Invalid }; }; |