1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

AK: Add a way to disable the trimming of whitespace in to_*int

This behavior might not always be desirable, and so this patch adds a
way to disable it.
This commit is contained in:
sin-ack 2021-06-18 16:21:27 +00:00 committed by Linus Groh
parent 8be7bdaac3
commit 3abcfcc178
6 changed files with 74 additions and 61 deletions

View file

@ -74,26 +74,26 @@ FlyString::FlyString(StringView const& string)
} }
template<typename T> template<typename T>
Optional<T> FlyString::to_int() const Optional<T> FlyString::to_int(TrimWhitespace trim_whitespace) const
{ {
return StringUtils::convert_to_int<T>(view()); return StringUtils::convert_to_int<T>(view(), trim_whitespace);
} }
template Optional<i8> FlyString::to_int() const; template Optional<i8> FlyString::to_int(TrimWhitespace) const;
template Optional<i16> FlyString::to_int() const; template Optional<i16> FlyString::to_int(TrimWhitespace) const;
template Optional<i32> FlyString::to_int() const; template Optional<i32> FlyString::to_int(TrimWhitespace) const;
template Optional<i64> FlyString::to_int() const; template Optional<i64> FlyString::to_int(TrimWhitespace) const;
template<typename T> template<typename T>
Optional<T> FlyString::to_uint() const Optional<T> FlyString::to_uint(TrimWhitespace trim_whitespace) const
{ {
return StringUtils::convert_to_uint<T>(view()); return StringUtils::convert_to_uint<T>(view(), trim_whitespace);
} }
template Optional<u8> FlyString::to_uint() const; template Optional<u8> FlyString::to_uint(TrimWhitespace) const;
template Optional<u16> FlyString::to_uint() const; template Optional<u16> FlyString::to_uint(TrimWhitespace) const;
template Optional<u32> FlyString::to_uint() const; template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
template Optional<u64> FlyString::to_uint() const; template Optional<u64> FlyString::to_uint(TrimWhitespace) const;
bool FlyString::equals_ignoring_case(const StringView& other) const bool FlyString::equals_ignoring_case(const StringView& other) const
{ {

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "AK/StringUtils.h"
#include <AK/String.h> #include <AK/String.h>
namespace AK { namespace AK {
@ -73,9 +74,9 @@ public:
FlyString to_lowercase() const; FlyString to_lowercase() const;
template<typename T = int> template<typename T = int>
Optional<T> to_int() const; Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
template<typename T = unsigned> template<typename T = unsigned>
Optional<T> to_uint() const; Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) 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

@ -180,26 +180,26 @@ ByteBuffer String::to_byte_buffer() const
} }
template<typename T> template<typename T>
Optional<T> String::to_int() const Optional<T> String::to_int(TrimWhitespace trim_whitespace) const
{ {
return StringUtils::convert_to_int<T>(view()); return StringUtils::convert_to_int<T>(view(), trim_whitespace);
} }
template Optional<i8> String::to_int() const; template Optional<i8> String::to_int(TrimWhitespace) const;
template Optional<i16> String::to_int() const; template Optional<i16> String::to_int(TrimWhitespace) const;
template Optional<i32> String::to_int() const; template Optional<i32> String::to_int(TrimWhitespace) const;
template Optional<i64> String::to_int() const; template Optional<i64> String::to_int(TrimWhitespace) const;
template<typename T> template<typename T>
Optional<T> String::to_uint() const Optional<T> String::to_uint(TrimWhitespace trim_whitespace) const
{ {
return StringUtils::convert_to_uint<T>(view()); return StringUtils::convert_to_uint<T>(view(), trim_whitespace);
} }
template Optional<u8> String::to_uint() const; template Optional<u8> String::to_uint(TrimWhitespace) const;
template Optional<u16> String::to_uint() const; template Optional<u16> String::to_uint(TrimWhitespace) const;
template Optional<u32> String::to_uint() const; template Optional<u32> String::to_uint(TrimWhitespace) const;
template Optional<u64> String::to_uint() const; template Optional<u64> String::to_uint(TrimWhitespace) const;
bool String::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const bool String::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const
{ {

View file

@ -112,9 +112,9 @@ public:
[[nodiscard]] bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; [[nodiscard]] bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
template<typename T = int> template<typename T = int>
[[nodiscard]] Optional<T> to_int() const; [[nodiscard]] Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
template<typename T = unsigned> template<typename T = unsigned>
[[nodiscard]] Optional<T> to_uint() const; [[nodiscard]] Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
[[nodiscard]] String to_lowercase() const; [[nodiscard]] String to_lowercase() const;
[[nodiscard]] String to_uppercase() const; [[nodiscard]] String to_uppercase() const;

View file

@ -86,18 +86,20 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
} }
template<typename T> template<typename T>
Optional<T> convert_to_int(const StringView& str) Optional<T> convert_to_int(const StringView& str, TrimWhitespace trim_whitespace)
{ {
auto str_trimmed = str.trim_whitespace(); auto string = trim_whitespace == TrimWhitespace::Yes
if (str_trimmed.is_empty()) ? str.trim_whitespace()
: str;
if (string.is_empty())
return {}; return {};
T sign = 1; T sign = 1;
size_t i = 0; 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 (characters[0] == '-' || characters[0] == '+') {
if (str_trimmed.length() == 1) if (string.length() == 1)
return {}; return {};
i++; i++;
if (characters[0] == '-') if (characters[0] == '-')
@ -105,7 +107,7 @@ Optional<T> convert_to_int(const StringView& str)
} }
T value = 0; T value = 0;
for (; i < str_trimmed.length(); i++) { for (; i < string.length(); i++) {
if (characters[i] < '0' || characters[i] > '9') if (characters[i] < '0' || characters[i] > '9')
return {}; return {};
@ -118,22 +120,24 @@ Optional<T> convert_to_int(const StringView& str)
return value; return value;
} }
template Optional<i8> convert_to_int(const StringView& str); template Optional<i8> convert_to_int(const StringView& str, TrimWhitespace);
template Optional<i16> convert_to_int(const StringView& str); template Optional<i16> convert_to_int(const StringView& str, TrimWhitespace);
template Optional<i32> convert_to_int(const StringView& str); template Optional<i32> convert_to_int(const StringView& str, TrimWhitespace);
template Optional<i64> convert_to_int(const StringView& str); template Optional<i64> convert_to_int(const StringView& str, TrimWhitespace);
template<typename T> template<typename T>
Optional<T> convert_to_uint(const StringView& str) Optional<T> convert_to_uint(const StringView& str, TrimWhitespace trim_whitespace)
{ {
auto str_trimmed = str.trim_whitespace(); auto string = trim_whitespace == TrimWhitespace::Yes
if (str_trimmed.is_empty()) ? str.trim_whitespace()
: str;
if (string.is_empty())
return {}; return {};
T value = 0; 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') if (characters[i] < '0' || characters[i] > '9')
return {}; return {};
@ -146,26 +150,28 @@ Optional<T> convert_to_uint(const StringView& str)
return value; return value;
} }
template Optional<u8> convert_to_uint(const StringView& str); template Optional<u8> convert_to_uint(const StringView& str, TrimWhitespace);
template Optional<u16> convert_to_uint(const StringView& str); template Optional<u16> convert_to_uint(const StringView& str, TrimWhitespace);
template Optional<u32> convert_to_uint(const StringView& str); template Optional<u32> convert_to_uint(const StringView& str, TrimWhitespace);
template Optional<u64> convert_to_uint(const StringView& str); template Optional<u64> convert_to_uint(const StringView& str, TrimWhitespace);
template Optional<long> convert_to_uint(const StringView& str); template Optional<long> convert_to_uint(const StringView& str, TrimWhitespace);
template Optional<long long> convert_to_uint(const StringView& str); template Optional<long long> convert_to_uint(const StringView& str, TrimWhitespace);
template<typename T> template<typename T>
Optional<T> convert_to_uint_from_hex(const StringView& str) Optional<T> convert_to_uint_from_hex(const StringView& str, TrimWhitespace trim_whitespace)
{ {
auto str_trimmed = str.trim_whitespace(); auto string = trim_whitespace == TrimWhitespace::Yes
if (str_trimmed.is_empty()) ? str.trim_whitespace()
: str;
if (string.is_empty())
return {}; return {};
T value = 0; T value = 0;
const auto count = str_trimmed.length(); const auto count = string.length();
const T upper_bound = NumericLimits<T>::max(); const T upper_bound = NumericLimits<T>::max();
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
char digit = str_trimmed[i]; char digit = string[i];
u8 digit_val; u8 digit_val;
if (value > (upper_bound >> 4)) if (value > (upper_bound >> 4))
return {}; return {};
@ -185,10 +191,10 @@ Optional<T> 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<u8> convert_to_uint_from_hex(const StringView& str, TrimWhitespace);
template Optional<u16> convert_to_uint_from_hex(const StringView& str); template Optional<u16> convert_to_uint_from_hex(const StringView& str, TrimWhitespace);
template Optional<u32> convert_to_uint_from_hex(const StringView& str); template Optional<u32> convert_to_uint_from_hex(const StringView& str, TrimWhitespace);
template Optional<u64> convert_to_uint_from_hex(const StringView& str); template Optional<u64> convert_to_uint_from_hex(const StringView& str, TrimWhitespace);
static inline char to_lowercase(char c) static inline char to_lowercase(char c)
{ {

View file

@ -22,6 +22,11 @@ enum class TrimMode {
Both Both
}; };
enum class TrimWhitespace {
Yes,
No,
};
struct MaskSpan { struct MaskSpan {
size_t start; size_t start;
size_t length; size_t length;
@ -40,11 +45,11 @@ 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);
template<typename T = int> template<typename T = int>
Optional<T> convert_to_int(const StringView&); Optional<T> convert_to_int(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
template<typename T = unsigned> template<typename T = unsigned>
Optional<T> convert_to_uint(const StringView&); Optional<T> convert_to_uint(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
template<typename T = unsigned> template<typename T = unsigned>
Optional<T> convert_to_uint_from_hex(const StringView&); Optional<T> convert_to_uint_from_hex(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
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);
@ -61,3 +66,4 @@ String to_snakecase(const StringView&);
using AK::CaseSensitivity; using AK::CaseSensitivity;
using AK::TrimMode; using AK::TrimMode;
using AK::TrimWhitespace;