diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 5736329fdb9..adbe1928989 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -85,11 +85,28 @@ FlyString::FlyString(const char* string) { } -Optional FlyString::to_int() const +template +Optional FlyString::to_int() const { - return StringUtils::convert_to_int(view()); + return StringUtils::convert_to_int(view()); } +template Optional FlyString::to_int() const; +template Optional FlyString::to_int() const; +template Optional FlyString::to_int() const; +template Optional FlyString::to_int() const; + +template +Optional FlyString::to_uint() const +{ + return StringUtils::convert_to_uint(view()); +} + +template Optional FlyString::to_uint() const; +template Optional FlyString::to_uint() const; +template Optional FlyString::to_uint() const; +template Optional FlyString::to_uint() const; + bool FlyString::equals_ignoring_case(const StringView& other) const { return StringUtils::equals_ignoring_case(view(), other); diff --git a/AK/FlyString.h b/AK/FlyString.h index f69196fd5dc..8752acaa016 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -82,7 +82,10 @@ public: FlyString to_lowercase() const; - Optional to_int() const; + template + Optional to_int() const; + template + Optional to_uint() const; bool equals_ignoring_case(const StringView&) const; bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const; diff --git a/AK/String.cpp b/AK/String.cpp index c7315f2b5b1..355371e4a9a 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -217,16 +217,28 @@ ByteBuffer String::to_byte_buffer() const return ByteBuffer::copy(reinterpret_cast(characters()), length()); } -Optional String::to_int() const +template +Optional String::to_int() const { - return StringUtils::convert_to_int(view()); + return StringUtils::convert_to_int(view()); } -Optional String::to_uint() const +template Optional String::to_int() const; +template Optional String::to_int() const; +template Optional String::to_int() const; +template Optional String::to_int() const; + +template +Optional String::to_uint() const { - return StringUtils::convert_to_uint(view()); + return StringUtils::convert_to_uint(view()); } +template Optional String::to_uint() const; +template Optional String::to_uint() const; +template Optional String::to_uint() const; +template Optional String::to_uint() const; + template String String::number(T value) { return formatted("{}", value); } diff --git a/AK/String.h b/AK/String.h index edeceeb87bd..c30f8bb715b 100644 --- a/AK/String.h +++ b/AK/String.h @@ -114,8 +114,10 @@ public: bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; bool matches(const StringView& mask, Vector&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; - Optional to_int() const; - Optional to_uint() const; + template + Optional to_int() const; + template + Optional to_uint() const; String to_lowercase() const; String to_uppercase() const; diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index adca8ca11fe..5310476d83a 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -96,7 +96,8 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case return string_ptr == string_end && mask_ptr == mask_end; } -Optional convert_to_int(const StringView& str) +template +Optional convert_to_int(const StringView& str) { auto str_trimmed = str.trim_whitespace(); if (str_trimmed.is_empty()) @@ -113,7 +114,7 @@ Optional convert_to_int(const StringView& str) negative = (characters[0] == '-'); } - int value = 0; + T value = 0; for (; i < str_trimmed.length(); i++) { if (characters[i] < '0' || characters[i] > '9') return {}; @@ -123,13 +124,19 @@ Optional convert_to_int(const StringView& str) return negative ? -value : value; } -Optional convert_to_uint(const StringView& str) +template Optional convert_to_int(const StringView& str); +template Optional convert_to_int(const StringView& str); +template Optional convert_to_int(const StringView& str); +template Optional convert_to_int(const StringView& str); + +template +Optional convert_to_uint(const StringView& str) { auto str_trimmed = str.trim_whitespace(); if (str_trimmed.is_empty()) return {}; - unsigned value = 0; + T value = 0; const auto characters = str_trimmed.characters_without_null_termination(); for (size_t i = 0; i < str_trimmed.length(); i++) { @@ -142,13 +149,19 @@ Optional convert_to_uint(const StringView& str) return value; } -Optional convert_to_uint_from_hex(const StringView& str) +template Optional convert_to_uint(const StringView& str); +template Optional convert_to_uint(const StringView& str); +template Optional convert_to_uint(const StringView& str); +template Optional convert_to_uint(const StringView& str); + +template +Optional convert_to_uint_from_hex(const StringView& str) { auto str_trimmed = str.trim_whitespace(); if (str_trimmed.is_empty()) return {}; - unsigned value = 0; + T value = 0; const auto count = str_trimmed.length(); for (size_t i = 0; i < count; i++) { @@ -170,6 +183,11 @@ Optional convert_to_uint_from_hex(const StringView& str) return value; } +template Optional convert_to_uint_from_hex(const StringView& str); +template Optional convert_to_uint_from_hex(const StringView& str); +template Optional convert_to_uint_from_hex(const StringView& str); +template Optional convert_to_uint_from_hex(const StringView& str); + static inline char to_lowercase(char c) { if (c >= 'A' && c <= 'Z') diff --git a/AK/StringUtils.h b/AK/StringUtils.h index 849be54d18b..e2b5e4430dd 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -59,9 +59,12 @@ struct MaskSpan { namespace StringUtils { bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector* match_spans = nullptr); -Optional convert_to_int(const StringView&); -Optional convert_to_uint(const StringView&); -Optional convert_to_uint_from_hex(const StringView&); +template +Optional convert_to_int(const StringView&); +template +Optional convert_to_uint(const StringView&); +template +Optional convert_to_uint_from_hex(const StringView&); bool equals_ignoring_case(const StringView&, const StringView&); bool ends_with(const StringView& a, const StringView& b, CaseSensitivity); bool starts_with(const StringView&, const StringView&, CaseSensitivity); diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 205ed239aff..f1ee2dbb869 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -222,16 +222,28 @@ StringView StringView::substring_view_starting_after_substring(const StringView& return { remaining_characters, remaining_length }; } -Optional StringView::to_int() const +template +Optional StringView::to_int() const { - return StringUtils::convert_to_int(*this); + return StringUtils::convert_to_int(*this); } -Optional StringView::to_uint() const +template Optional StringView::to_int() const; +template Optional StringView::to_int() const; +template Optional StringView::to_int() const; +template Optional StringView::to_int() const; + +template +Optional StringView::to_uint() const { - return StringUtils::convert_to_uint(*this); + return StringUtils::convert_to_uint(*this); } +template Optional StringView::to_uint() const; +template Optional StringView::to_uint() const; +template Optional StringView::to_uint() const; +template Optional StringView::to_uint() const; + unsigned StringView::hash() const { if (is_empty()) diff --git a/AK/StringView.h b/AK/StringView.h index 99f03beaa56..1b90b7ba815 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -111,8 +111,10 @@ public: // following newline.". Vector lines(bool consider_cr = true) const; - Optional to_int() const; - Optional to_uint() const; + template + Optional to_int() const; + template + Optional to_uint() 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