From 5fbec2b003081f0b444f88b18e3250a96dbdd13f Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sun, 20 Sep 2020 18:05:04 +0430 Subject: [PATCH] AK: Move trim_whitespace() into StringUtils and add it to StringView No behaviour change; also patches use of `String::TrimMode` in LibJS. --- AK/String.cpp | 38 --------------------- AK/String.h | 12 +++---- AK/StringUtils.cpp | 37 ++++++++++++++++++++ AK/StringUtils.h | 8 +++++ AK/StringView.h | 2 ++ Libraries/LibJS/Runtime/StringPrototype.cpp | 6 ++-- 6 files changed, 56 insertions(+), 47 deletions(-) diff --git a/AK/String.cpp b/AK/String.cpp index 57582ca22ef..ebb5c70dbcc 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -337,44 +337,6 @@ int String::replace(const String& needle, const String& replacement, bool all_oc return positions.size(); } -String String::trim_whitespace(TrimMode mode) const -{ - auto is_whitespace_character = [](char ch) -> bool { - return ch == '\t' - || ch == '\n' - || ch == '\v' - || ch == '\f' - || ch == '\r' - || ch == ' '; - }; - - size_t substring_start = 0; - size_t substring_length = length(); - - if (mode == TrimMode::Left || mode == TrimMode::Both) { - for (size_t i = 0; i < length(); ++i) { - if (substring_length == 0) - return ""; - if (!is_whitespace_character(characters()[i])) - break; - ++substring_start; - --substring_length; - } - } - - if (mode == TrimMode::Right || mode == TrimMode::Both) { - for (size_t i = length() - 1; i > 0; --i) { - if (substring_length == 0) - return ""; - if (!is_whitespace_character(characters()[i])) - break; - --substring_length; - } - } - - return substring(substring_start, substring_length); -} - String escape_html_entities(const StringView& html) { StringBuilder builder; diff --git a/AK/String.h b/AK/String.h index 3c36d84b174..e5b0d1ef0db 100644 --- a/AK/String.h +++ b/AK/String.h @@ -119,12 +119,12 @@ public: String to_lowercase() const; String to_uppercase() const; - enum class TrimMode { - Left, - Right, - Both - }; - String trim_whitespace(TrimMode mode = TrimMode::Both) const; +#ifndef KERNEL + String trim_whitespace(TrimMode mode = TrimMode::Both) const + { + return StringUtils::trim_whitespace(StringView { characters(), length() }, mode); + } +#endif bool equals_ignoring_case(const StringView&) const; diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 363cd07ab5c..c4af681388e 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -227,6 +227,43 @@ bool starts_with(const StringView& str, const StringView& start, CaseSensitivity return true; } +StringView trim_whitespace(const StringView& str, TrimMode mode) +{ + auto is_whitespace_character = [](char ch) -> bool { + return ch == '\t' + || ch == '\n' + || ch == '\v' + || ch == '\f' + || ch == '\r' + || ch == ' '; + }; + + size_t substring_start = 0; + size_t substring_length = str.length(); + + if (mode == TrimMode::Left || mode == TrimMode::Both) { + for (size_t i = 0; i < str.length(); ++i) { + if (substring_length == 0) + return ""; + if (!is_whitespace_character(str[i])) + break; + ++substring_start; + --substring_length; + } + } + + if (mode == TrimMode::Right || mode == TrimMode::Both) { + for (size_t i = str.length() - 1; i > 0; --i) { + if (substring_length == 0) + return ""; + if (!is_whitespace_character(str[i])) + break; + --substring_length; + } + } + + return str.substring_view(substring_start, substring_length); +} } } diff --git a/AK/StringUtils.h b/AK/StringUtils.h index b2b91851668..e30345e3b0d 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -36,6 +36,12 @@ enum class CaseSensitivity { CaseSensitive, }; +enum class TrimMode { + Left, + Right, + Both +}; + namespace StringUtils { bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive); @@ -45,8 +51,10 @@ 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); +StringView trim_whitespace(const StringView&, TrimMode mode); } } using AK::CaseSensitivity; +using AK::TrimMode; diff --git a/AK/StringView.h b/AK/StringView.h index 2f99cd48a4a..894b4865469 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -91,6 +91,8 @@ public: bool contains(const StringView&) const; bool equals_ignoring_case(const StringView& other) const; + StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); } + Optional find_first_of(char) const; Optional find_first_of(const StringView&) const; diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 2d8b7c76aa6..77c633688bf 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -282,7 +282,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim) auto string = ak_string_from(vm, global_object); if (string.is_null()) return {}; - return js_string(vm, string.trim_whitespace(String::TrimMode::Both)); + return js_string(vm, string.trim_whitespace(TrimMode::Both)); } JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_start) @@ -290,7 +290,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_start) auto string = ak_string_from(vm, global_object); if (string.is_null()) return {}; - return js_string(vm, string.trim_whitespace(String::TrimMode::Left)); + return js_string(vm, string.trim_whitespace(TrimMode::Left)); } JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_end) @@ -298,7 +298,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_end) auto string = ak_string_from(vm, global_object); if (string.is_null()) return {}; - return js_string(vm, string.trim_whitespace(String::TrimMode::Right)); + return js_string(vm, string.trim_whitespace(TrimMode::Right)); } JS_DEFINE_NATIVE_FUNCTION(StringPrototype::concat)