mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:34:57 +00: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
4 changed files with 17 additions and 5 deletions
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue