1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:57:44 +00:00

AK: Move trim_whitespace() into StringUtils and add it to StringView

No behaviour change; also patches use of `String::TrimMode` in LibJS.
This commit is contained in:
AnotherTest 2020-09-20 18:05:04 +04:30 committed by Andreas Kling
parent 50e9000b40
commit 5fbec2b003
6 changed files with 56 additions and 47 deletions

View file

@ -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;