AK: Generalize AK::String::to_int() for more types

This commit is contained in:
Sahan Fernando 2020-12-11 00:17:30 +11:00 committed by Andreas Kling
parent 39364bdda4
commit 37df4bbd90
Notes: sideshowbarker 2024-07-19 00:43:10 +09:00
8 changed files with 93 additions and 24 deletions

View file

@ -85,11 +85,28 @@ FlyString::FlyString(const char* string)
{ {
} }
Optional<int> FlyString::to_int() const template<typename T>
Optional<T> FlyString::to_int() const
{ {
return StringUtils::convert_to_int(view()); return StringUtils::convert_to_int<T>(view());
} }
template Optional<i8> FlyString::to_int() const;
template Optional<i16> FlyString::to_int() const;
template Optional<i32> FlyString::to_int() const;
template Optional<i64> FlyString::to_int() const;
template<typename T>
Optional<T> FlyString::to_uint() const
{
return StringUtils::convert_to_uint<T>(view());
}
template Optional<u8> FlyString::to_uint() const;
template Optional<u16> FlyString::to_uint() const;
template Optional<u32> FlyString::to_uint() const;
template Optional<u64> FlyString::to_uint() const;
bool FlyString::equals_ignoring_case(const StringView& other) const bool FlyString::equals_ignoring_case(const StringView& other) const
{ {
return StringUtils::equals_ignoring_case(view(), other); return StringUtils::equals_ignoring_case(view(), other);

View file

@ -82,7 +82,10 @@ public:
FlyString to_lowercase() const; FlyString to_lowercase() const;
Optional<int> to_int() const; template<typename T = int>
Optional<T> to_int() const;
template<typename T = unsigned>
Optional<T> to_uint() const;
bool equals_ignoring_case(const StringView&) const; bool equals_ignoring_case(const StringView&) const;
bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const; bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;

View file

@ -217,16 +217,28 @@ ByteBuffer String::to_byte_buffer() const
return ByteBuffer::copy(reinterpret_cast<const u8*>(characters()), length()); return ByteBuffer::copy(reinterpret_cast<const u8*>(characters()), length());
} }
Optional<int> String::to_int() const template<typename T>
Optional<T> String::to_int() const
{ {
return StringUtils::convert_to_int(view()); return StringUtils::convert_to_int<T>(view());
} }
Optional<unsigned> String::to_uint() const template Optional<i8> String::to_int() const;
template Optional<i16> String::to_int() const;
template Optional<i32> String::to_int() const;
template Optional<i64> String::to_int() const;
template<typename T>
Optional<T> String::to_uint() const
{ {
return StringUtils::convert_to_uint(view()); return StringUtils::convert_to_uint<T>(view());
} }
template Optional<u8> String::to_uint() const;
template Optional<u16> String::to_uint() const;
template Optional<u32> String::to_uint() const;
template Optional<u64> String::to_uint() const;
template<typename T> template<typename T>
String String::number(T value) { return formatted("{}", value); } String String::number(T value) { return formatted("{}", value); }

View file

@ -114,8 +114,10 @@ public:
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
Optional<int> to_int() const; template<typename T = int>
Optional<unsigned> to_uint() const; Optional<T> to_int() const;
template<typename T = unsigned>
Optional<T> to_uint() const;
String to_lowercase() const; String to_lowercase() const;
String to_uppercase() const; String to_uppercase() const;

View file

@ -96,7 +96,8 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
return string_ptr == string_end && mask_ptr == mask_end; return string_ptr == string_end && mask_ptr == mask_end;
} }
Optional<int> convert_to_int(const StringView& str) template<typename T>
Optional<T> convert_to_int(const StringView& str)
{ {
auto str_trimmed = str.trim_whitespace(); auto str_trimmed = str.trim_whitespace();
if (str_trimmed.is_empty()) if (str_trimmed.is_empty())
@ -113,7 +114,7 @@ Optional<int> convert_to_int(const StringView& str)
negative = (characters[0] == '-'); negative = (characters[0] == '-');
} }
int value = 0; T value = 0;
for (; i < str_trimmed.length(); i++) { for (; i < str_trimmed.length(); i++) {
if (characters[i] < '0' || characters[i] > '9') if (characters[i] < '0' || characters[i] > '9')
return {}; return {};
@ -123,13 +124,19 @@ Optional<int> convert_to_int(const StringView& str)
return negative ? -value : value; return negative ? -value : value;
} }
Optional<unsigned> convert_to_uint(const StringView& str) template Optional<i8> convert_to_int(const StringView& str);
template Optional<i16> convert_to_int(const StringView& str);
template Optional<i32> convert_to_int(const StringView& str);
template Optional<i64> convert_to_int(const StringView& str);
template<typename T>
Optional<T> convert_to_uint(const StringView& str)
{ {
auto str_trimmed = str.trim_whitespace(); auto str_trimmed = str.trim_whitespace();
if (str_trimmed.is_empty()) if (str_trimmed.is_empty())
return {}; return {};
unsigned value = 0; T value = 0;
const auto characters = str_trimmed.characters_without_null_termination(); const auto characters = str_trimmed.characters_without_null_termination();
for (size_t i = 0; i < str_trimmed.length(); i++) { for (size_t i = 0; i < str_trimmed.length(); i++) {
@ -142,13 +149,19 @@ Optional<unsigned> convert_to_uint(const StringView& str)
return value; return value;
} }
Optional<unsigned> convert_to_uint_from_hex(const StringView& str) template Optional<u8> convert_to_uint(const StringView& str);
template Optional<u16> convert_to_uint(const StringView& str);
template Optional<u32> convert_to_uint(const StringView& str);
template Optional<u64> convert_to_uint(const StringView& str);
template<typename T>
Optional<T> convert_to_uint_from_hex(const StringView& str)
{ {
auto str_trimmed = str.trim_whitespace(); auto str_trimmed = str.trim_whitespace();
if (str_trimmed.is_empty()) if (str_trimmed.is_empty())
return {}; return {};
unsigned value = 0; T value = 0;
const auto count = str_trimmed.length(); const auto count = str_trimmed.length();
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
@ -170,6 +183,11 @@ Optional<unsigned> convert_to_uint_from_hex(const StringView& str)
return value; return value;
} }
template Optional<u8> convert_to_uint_from_hex(const StringView& str);
template Optional<u16> convert_to_uint_from_hex(const StringView& str);
template Optional<u32> convert_to_uint_from_hex(const StringView& str);
template Optional<u64> convert_to_uint_from_hex(const StringView& str);
static inline char to_lowercase(char c) static inline char to_lowercase(char c)
{ {
if (c >= 'A' && c <= 'Z') if (c >= 'A' && c <= 'Z')

View file

@ -59,9 +59,12 @@ struct MaskSpan {
namespace StringUtils { namespace StringUtils {
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector<MaskSpan>* match_spans = nullptr); bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector<MaskSpan>* match_spans = nullptr);
Optional<int> convert_to_int(const StringView&); template<typename T = int>
Optional<unsigned> convert_to_uint(const StringView&); Optional<T> convert_to_int(const StringView&);
Optional<unsigned> convert_to_uint_from_hex(const StringView&); template<typename T = unsigned>
Optional<T> convert_to_uint(const StringView&);
template<typename T = unsigned>
Optional<T> convert_to_uint_from_hex(const StringView&);
bool equals_ignoring_case(const StringView&, const StringView&); bool equals_ignoring_case(const StringView&, const StringView&);
bool ends_with(const StringView& a, const StringView& b, CaseSensitivity); bool ends_with(const StringView& a, const StringView& b, CaseSensitivity);
bool starts_with(const StringView&, const StringView&, CaseSensitivity); bool starts_with(const StringView&, const StringView&, CaseSensitivity);

View file

@ -222,16 +222,28 @@ StringView StringView::substring_view_starting_after_substring(const StringView&
return { remaining_characters, remaining_length }; return { remaining_characters, remaining_length };
} }
Optional<int> StringView::to_int() const template<typename T>
Optional<T> StringView::to_int() const
{ {
return StringUtils::convert_to_int(*this); return StringUtils::convert_to_int<T>(*this);
} }
Optional<unsigned> StringView::to_uint() const template Optional<i8> StringView::to_int() const;
template Optional<i16> StringView::to_int() const;
template Optional<i32> StringView::to_int() const;
template Optional<i64> StringView::to_int() const;
template<typename T>
Optional<T> StringView::to_uint() const
{ {
return StringUtils::convert_to_uint(*this); return StringUtils::convert_to_uint<T>(*this);
} }
template Optional<u8> StringView::to_uint() const;
template Optional<u16> StringView::to_uint() const;
template Optional<u32> StringView::to_uint() const;
template Optional<u64> StringView::to_uint() const;
unsigned StringView::hash() const unsigned StringView::hash() const
{ {
if (is_empty()) if (is_empty())

View file

@ -111,8 +111,10 @@ public:
// following newline.". // following newline.".
Vector<StringView> lines(bool consider_cr = true) const; Vector<StringView> lines(bool consider_cr = true) const;
Optional<int> to_int() const; template<typename T = int>
Optional<unsigned> to_uint() const; Optional<T> to_int() const;
template<typename T = unsigned>
Optional<T> to_uint() const;
// Create a new substring view of this string view, starting either at the beginning of // 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 // the given substring view, or after its end, and continuing until the end of this string