mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-26 20:55:35 +03:00
AK: Add trim() method to String, StringView and StringUtils
The methods added make it possible to use the trim mechanism with specified characters, unlike trim_whitespace(), which uses predefined characters.
This commit is contained in:
parent
14506e8f5e
commit
0e4f7aa8e8
Notes:
sideshowbarker
2024-07-18 17:04:49 +09:00
Author: https://github.com/MaxWipfli Commit: https://github.com/SerenityOS/serenity/commit/0e4f7aa8e84 Pull-request: https://github.com/SerenityOS/serenity/pull/7478 Reviewed-by: https://github.com/awesomekling
@ -119,9 +119,14 @@ public:
|
||||
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
||||
|
||||
#ifndef KERNEL
|
||||
[[nodiscard]] String trim(const StringView& characters, TrimMode mode = TrimMode::Both) const
|
||||
{
|
||||
return StringUtils::trim(view(), characters, mode);
|
||||
}
|
||||
|
||||
[[nodiscard]] String trim_whitespace(TrimMode mode = TrimMode::Both) const
|
||||
{
|
||||
return StringUtils::trim_whitespace(StringView { characters(), length() }, mode);
|
||||
return StringUtils::trim_whitespace(view(), mode);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -292,7 +292,7 @@ bool is_whitespace(const StringView& str)
|
||||
return true;
|
||||
}
|
||||
|
||||
StringView trim_whitespace(const StringView& str, TrimMode mode)
|
||||
StringView trim(const StringView& str, const StringView& characters, TrimMode mode)
|
||||
{
|
||||
size_t substring_start = 0;
|
||||
size_t substring_length = str.length();
|
||||
@ -301,7 +301,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
|
||||
for (size_t i = 0; i < str.length(); ++i) {
|
||||
if (substring_length == 0)
|
||||
return "";
|
||||
if (!isspace(str[i]))
|
||||
if (!characters.contains(str[i]))
|
||||
break;
|
||||
++substring_start;
|
||||
--substring_length;
|
||||
@ -312,7 +312,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
|
||||
for (size_t i = str.length() - 1; i > 0; --i) {
|
||||
if (substring_length == 0)
|
||||
return "";
|
||||
if (!isspace(str[i]))
|
||||
if (!characters.contains(str[i]))
|
||||
break;
|
||||
--substring_length;
|
||||
}
|
||||
@ -321,6 +321,11 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
|
||||
return str.substring_view(substring_start, substring_length);
|
||||
}
|
||||
|
||||
StringView trim_whitespace(const StringView& str, TrimMode mode)
|
||||
{
|
||||
return trim(str, " \n\t\v\f\r", mode);
|
||||
}
|
||||
|
||||
Optional<size_t> find(const StringView& haystack, const StringView& needle)
|
||||
{
|
||||
return AK::memmem_optional(
|
||||
|
@ -50,7 +50,8 @@ bool ends_with(const StringView& a, const StringView& b, CaseSensitivity);
|
||||
bool starts_with(const StringView&, const StringView&, CaseSensitivity);
|
||||
bool contains(const StringView&, const StringView&, CaseSensitivity);
|
||||
bool is_whitespace(const StringView&);
|
||||
StringView trim_whitespace(const StringView&, TrimMode mode);
|
||||
StringView trim(const StringView& string, const StringView& characters, TrimMode mode);
|
||||
StringView trim_whitespace(const StringView& string, TrimMode mode);
|
||||
Optional<size_t> find(const StringView& haystack, const StringView& needle);
|
||||
String to_snakecase(const StringView&);
|
||||
|
||||
|
@ -79,6 +79,7 @@ public:
|
||||
[[nodiscard]] bool contains(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||
[[nodiscard]] bool equals_ignoring_case(const StringView& other) const;
|
||||
|
||||
[[nodiscard]] StringView trim(const StringView& characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
|
||||
[[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
|
||||
|
||||
Optional<size_t> find_first_of(char) const;
|
||||
|
Loading…
Reference in New Issue
Block a user