Skip to content

Commit

Permalink
AK: Move to_int(), to_uint() implementations to StringUtils (Serenity…
Browse files Browse the repository at this point in the history
…OS#1338)

Provide wrappers in String and StringView. Add some tests for the
implementations.
  • Loading branch information
howar6hill committed Mar 2, 2020
1 parent 918ebab commit d75fa80
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 73 deletions.
37 changes: 2 additions & 35 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class String {
static String repeated(char, size_t count);
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;

// FIXME: These should be shared between String and StringView somehow!
int to_int(bool& ok) const;
unsigned to_uint(bool& ok) const;

Expand Down
57 changes: 57 additions & 0 deletions AK/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,63 @@ namespace StringUtils {
return (mask_ptr == mask_end) && string_ptr == string_end;
}

int convert_to_int(const StringView& str, bool& ok)
{
if (str.is_empty()) {
ok = false;
return 0;
}

bool negative = false;
size_t i = 0;
const auto characters = str.characters_without_null_termination();

if (characters[0] == '-' || characters[0] == '+') {
if (str.length() == 1) {
ok = false;
return 0;
}
i++;
negative = (characters[0] == '-');
}

int value = 0;
for (; i < str.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 convert_to_uint(const StringView& str, bool& ok)
{
if (str.is_empty()) {
ok = false;
return 0;
}

unsigned value = 0;
const auto characters = str.characters_without_null_termination();

for (size_t i = 0; i < str.length(); i++) {
if (characters[i] < '0' || characters[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters[i] - '0';
}
ok = true;

return value;
}

}

}
2 changes: 2 additions & 0 deletions AK/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum class CaseSensitivity {
namespace StringUtils {

bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive);
int convert_to_int(const StringView&, bool& ok);
unsigned convert_to_uint(const StringView&, bool& ok);

}

Expand Down
37 changes: 2 additions & 35 deletions AK/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ class StringView {
// following newline.".
Vector<StringView> lines(bool consider_cr = true) const;

// FIXME: These should be shared between String and StringView somehow!
unsigned to_uint(bool& ok) const;
int to_int(bool& ok) const;
unsigned to_uint(bool& ok) const;

// Create a new substring view of this string view, starting either at the beginning of
// the given substring view, or after its end, and continuing until the end of this string
Expand Down
77 changes: 77 additions & 0 deletions AK/Tests/TestStringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,81 @@ TEST_CASE(matches_case_insensitive)
EXPECT(!AK::StringUtils::matches("acdcb", "a*c?b"));
}

TEST_CASE(convert_to_int)
{
bool ok = false;
AK::StringUtils::convert_to_int(StringView(), ok);
EXPECT(!ok);

AK::StringUtils::convert_to_int("", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_int("a", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_int("+", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_int("-", ok);
EXPECT(!ok);

int actual = actual = AK::StringUtils::convert_to_int("0", ok);
EXPECT(ok && actual == 0);

actual = AK::StringUtils::convert_to_int("1", ok);
EXPECT(ok && actual == 1);

actual = AK::StringUtils::convert_to_int("+1", ok);
EXPECT(ok && actual == 1);

actual = AK::StringUtils::convert_to_int("-1", ok);
EXPECT(ok && actual == -1);

actual = AK::StringUtils::convert_to_int("01", ok);
EXPECT(ok && actual == 1);

actual = AK::StringUtils::convert_to_int("12345", ok);
EXPECT(ok && actual == 12345);

actual = AK::StringUtils::convert_to_int("-12345", ok);
EXPECT(ok && actual == -12345);
}

TEST_CASE(convert_to_uint)
{
bool ok = false;
AK::StringUtils::convert_to_uint(StringView(), ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("a", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("+", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("-", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("+1", ok);
EXPECT(!ok);

AK::StringUtils::convert_to_uint("-1", ok);
EXPECT(!ok);

uint actual = AK::StringUtils::convert_to_uint("0", ok);
EXPECT(ok && actual == 0u);

actual = AK::StringUtils::convert_to_uint("1", ok);
EXPECT(ok && actual == 1u);

actual = AK::StringUtils::convert_to_uint("01", ok);
EXPECT(ok && actual == 1u);

actual = AK::StringUtils::convert_to_uint("12345", ok);
EXPECT(ok && actual == 12345u);
}

TEST_MAIN(StringUtils)

0 comments on commit d75fa80

Please sign in to comment.