mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Generalize AK::String::to_int() for more types
This commit is contained in:
parent
39364bdda4
commit
37df4bbd90
Notes:
sideshowbarker
2024-07-19 00:43:10 +09:00
Author: https://github.com/ccapitalK Commit: https://github.com/SerenityOS/serenity/commit/37df4bbd906 Pull-request: https://github.com/SerenityOS/serenity/pull/4459 Issue: https://github.com/SerenityOS/serenity/issues/4377
8 changed files with 93 additions and 24 deletions
|
@ -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
|
||||
{
|
||||
return StringUtils::equals_ignoring_case(view(), other);
|
||||
|
|
|
@ -82,7 +82,10 @@ public:
|
|||
|
||||
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 starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||
|
|
|
@ -217,16 +217,28 @@ ByteBuffer String::to_byte_buffer() const
|
|||
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>
|
||||
String String::number(T value) { return formatted("{}", value); }
|
||||
|
||||
|
|
|
@ -114,8 +114,10 @@ public:
|
|||
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
||||
bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
||||
|
||||
Optional<int> to_int() const;
|
||||
Optional<unsigned> to_uint() const;
|
||||
template<typename T = int>
|
||||
Optional<T> to_int() const;
|
||||
template<typename T = unsigned>
|
||||
Optional<T> to_uint() const;
|
||||
|
||||
String to_lowercase() const;
|
||||
String to_uppercase() const;
|
||||
|
|
|
@ -96,7 +96,8 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
|
|||
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();
|
||||
if (str_trimmed.is_empty())
|
||||
|
@ -113,7 +114,7 @@ Optional<int> 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<int> convert_to_int(const StringView& str)
|
|||
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();
|
||||
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<unsigned> convert_to_uint(const StringView& str)
|
|||
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();
|
||||
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<unsigned> convert_to_uint_from_hex(const StringView& str)
|
|||
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)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
|
|
|
@ -59,9 +59,12 @@ struct MaskSpan {
|
|||
namespace StringUtils {
|
||||
|
||||
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector<MaskSpan>* match_spans = nullptr);
|
||||
Optional<int> convert_to_int(const StringView&);
|
||||
Optional<unsigned> convert_to_uint(const StringView&);
|
||||
Optional<unsigned> convert_to_uint_from_hex(const StringView&);
|
||||
template<typename T = int>
|
||||
Optional<T> convert_to_int(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 ends_with(const StringView& a, const StringView& b, CaseSensitivity);
|
||||
bool starts_with(const StringView&, const StringView&, CaseSensitivity);
|
||||
|
|
|
@ -222,16 +222,28 @@ StringView StringView::substring_view_starting_after_substring(const StringView&
|
|||
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
|
||||
{
|
||||
if (is_empty())
|
||||
|
|
|
@ -111,8 +111,10 @@ public:
|
|||
// following newline.".
|
||||
Vector<StringView> lines(bool consider_cr = true) const;
|
||||
|
||||
Optional<int> to_int() const;
|
||||
Optional<unsigned> to_uint() const;
|
||||
template<typename T = int>
|
||||
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
|
||||
// the given substring view, or after its end, and continuing until the end of this string
|
||||
|
|
Loading…
Reference in a new issue