Skip to content

Commit

Permalink
AK: Implement String::to_int (SerenityOS#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloo authored and awesomekling committed May 26, 2019
1 parent 79dba9a commit 411cdf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool String::operator==(const String& other) const

if (length() != other.length())
return false;

return !memcmp(characters(), other.characters(), length());
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion LibGUI/GVariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class GVariant {
RawPoint as_point;
RawSize as_size;
RawRect as_rect;
} m_value;
} m_value;

Type m_type { Type::Invalid };
};

0 comments on commit 411cdf0

Please sign in to comment.