diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 5ad1725d3ab..aa6a3e79cac 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -74,26 +74,26 @@ FlyString::FlyString(StringView const& string) } template -Optional FlyString::to_int() const +Optional FlyString::to_int(TrimWhitespace trim_whitespace) const { - return StringUtils::convert_to_int(view()); + return StringUtils::convert_to_int(view(), trim_whitespace); } -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_int(TrimWhitespace) const; +template Optional FlyString::to_int(TrimWhitespace) const; +template Optional FlyString::to_int(TrimWhitespace) const; +template Optional FlyString::to_int(TrimWhitespace) const; template -Optional FlyString::to_uint() const +Optional FlyString::to_uint(TrimWhitespace trim_whitespace) const { - return StringUtils::convert_to_uint(view()); + return StringUtils::convert_to_uint(view(), trim_whitespace); } -template Optional FlyString::to_uint() const; -template Optional FlyString::to_uint() const; -template Optional FlyString::to_uint() const; -template Optional FlyString::to_uint() const; +template Optional FlyString::to_uint(TrimWhitespace) const; +template Optional FlyString::to_uint(TrimWhitespace) const; +template Optional FlyString::to_uint(TrimWhitespace) const; +template Optional FlyString::to_uint(TrimWhitespace) const; bool FlyString::equals_ignoring_case(const StringView& other) const { diff --git a/AK/FlyString.h b/AK/FlyString.h index 0e107053e0a..d91e94c68b5 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -6,6 +6,7 @@ #pragma once +#include "AK/StringUtils.h" #include namespace AK { @@ -73,9 +74,9 @@ public: FlyString to_lowercase() const; template - Optional to_int() const; + Optional to_int(TrimWhitespace = TrimWhitespace::Yes) const; template - Optional to_uint() const; + Optional to_uint(TrimWhitespace = TrimWhitespace::Yes) 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 17eb0ac745c..79d849496f0 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -180,26 +180,26 @@ ByteBuffer String::to_byte_buffer() const } template -Optional String::to_int() const +Optional String::to_int(TrimWhitespace trim_whitespace) const { - return StringUtils::convert_to_int(view()); + return StringUtils::convert_to_int(view(), trim_whitespace); } -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_int(TrimWhitespace) const; +template Optional String::to_int(TrimWhitespace) const; +template Optional String::to_int(TrimWhitespace) const; +template Optional String::to_int(TrimWhitespace) const; template -Optional String::to_uint() const +Optional String::to_uint(TrimWhitespace trim_whitespace) const { - return StringUtils::convert_to_uint(view()); + return StringUtils::convert_to_uint(view(), trim_whitespace); } -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 Optional String::to_uint(TrimWhitespace) const; +template Optional String::to_uint(TrimWhitespace) const; +template Optional String::to_uint(TrimWhitespace) const; +template Optional String::to_uint(TrimWhitespace) const; bool String::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const { diff --git a/AK/String.h b/AK/String.h index 6f9b98fdde7..a3e70ba8034 100644 --- a/AK/String.h +++ b/AK/String.h @@ -112,9 +112,9 @@ public: [[nodiscard]] bool matches(const StringView& mask, Vector&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; template - [[nodiscard]] Optional to_int() const; + [[nodiscard]] Optional to_int(TrimWhitespace = TrimWhitespace::Yes) const; template - [[nodiscard]] Optional to_uint() const; + [[nodiscard]] Optional to_uint(TrimWhitespace = TrimWhitespace::Yes) const; [[nodiscard]] String to_lowercase() const; [[nodiscard]] String to_uppercase() const; diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 18743b1521e..5c5d5db10c9 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -86,18 +86,20 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case } template -Optional convert_to_int(const StringView& str) +Optional convert_to_int(const StringView& str, TrimWhitespace trim_whitespace) { - auto str_trimmed = str.trim_whitespace(); - if (str_trimmed.is_empty()) + auto string = trim_whitespace == TrimWhitespace::Yes + ? str.trim_whitespace() + : str; + if (string.is_empty()) return {}; T sign = 1; size_t i = 0; - const auto characters = str_trimmed.characters_without_null_termination(); + const auto characters = string.characters_without_null_termination(); if (characters[0] == '-' || characters[0] == '+') { - if (str_trimmed.length() == 1) + if (string.length() == 1) return {}; i++; if (characters[0] == '-') @@ -105,7 +107,7 @@ Optional convert_to_int(const StringView& str) } T value = 0; - for (; i < str_trimmed.length(); i++) { + for (; i < string.length(); i++) { if (characters[i] < '0' || characters[i] > '9') return {}; @@ -118,22 +120,24 @@ Optional convert_to_int(const StringView& str) return value; } -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_int(const StringView& str, TrimWhitespace); +template Optional convert_to_int(const StringView& str, TrimWhitespace); +template Optional convert_to_int(const StringView& str, TrimWhitespace); +template Optional convert_to_int(const StringView& str, TrimWhitespace); template -Optional convert_to_uint(const StringView& str) +Optional convert_to_uint(const StringView& str, TrimWhitespace trim_whitespace) { - auto str_trimmed = str.trim_whitespace(); - if (str_trimmed.is_empty()) + auto string = trim_whitespace == TrimWhitespace::Yes + ? str.trim_whitespace() + : str; + if (string.is_empty()) return {}; T value = 0; - const auto characters = str_trimmed.characters_without_null_termination(); + const auto characters = string.characters_without_null_termination(); - for (size_t i = 0; i < str_trimmed.length(); i++) { + for (size_t i = 0; i < string.length(); i++) { if (characters[i] < '0' || characters[i] > '9') return {}; @@ -146,26 +150,28 @@ Optional convert_to_uint(const StringView& str) return value; } -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(const StringView& str); -template Optional convert_to_uint(const StringView& str); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); +template Optional convert_to_uint(const StringView& str, TrimWhitespace); template -Optional convert_to_uint_from_hex(const StringView& str) +Optional convert_to_uint_from_hex(const StringView& str, TrimWhitespace trim_whitespace) { - auto str_trimmed = str.trim_whitespace(); - if (str_trimmed.is_empty()) + auto string = trim_whitespace == TrimWhitespace::Yes + ? str.trim_whitespace() + : str; + if (string.is_empty()) return {}; T value = 0; - const auto count = str_trimmed.length(); + const auto count = string.length(); const T upper_bound = NumericLimits::max(); for (size_t i = 0; i < count; i++) { - char digit = str_trimmed[i]; + char digit = string[i]; u8 digit_val; if (value > (upper_bound >> 4)) return {}; @@ -185,10 +191,10 @@ 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); +template Optional convert_to_uint_from_hex(const StringView& str, TrimWhitespace); +template Optional convert_to_uint_from_hex(const StringView& str, TrimWhitespace); +template Optional convert_to_uint_from_hex(const StringView& str, TrimWhitespace); +template Optional convert_to_uint_from_hex(const StringView& str, TrimWhitespace); static inline char to_lowercase(char c) { diff --git a/AK/StringUtils.h b/AK/StringUtils.h index a7f7ac2881e..fa7ae1902d6 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -22,6 +22,11 @@ enum class TrimMode { Both }; +enum class TrimWhitespace { + Yes, + No, +}; + struct MaskSpan { size_t start; size_t length; @@ -40,11 +45,11 @@ namespace StringUtils { bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector* match_spans = nullptr); template -Optional convert_to_int(const StringView&); +Optional convert_to_int(const StringView&, TrimWhitespace = TrimWhitespace::Yes); template -Optional convert_to_uint(const StringView&); +Optional convert_to_uint(const StringView&, TrimWhitespace = TrimWhitespace::Yes); template -Optional convert_to_uint_from_hex(const StringView&); +Optional convert_to_uint_from_hex(const StringView&, TrimWhitespace = TrimWhitespace::Yes); 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); @@ -61,3 +66,4 @@ String to_snakecase(const StringView&); using AK::CaseSensitivity; using AK::TrimMode; +using AK::TrimWhitespace;