mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 14:27:35 +00:00
AK: Replace String::trim_spaces() with String::trim_whitespace()
As suggested by @awesomekling in a code review and (initially) ignored by me :^) Implementation is roughly based on LibJS's trim_string(), but with a fix for trimming all-whitespace strings.
This commit is contained in:
parent
0c14ee035c
commit
1febee768d
3 changed files with 41 additions and 12 deletions
|
@ -369,18 +369,42 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
||||||
return positions.size();
|
return positions.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::trim_spaces() const
|
String String::trim_whitespace(TrimMode mode) const
|
||||||
{
|
{
|
||||||
size_t start = 0;
|
auto is_whitespace_character = [](char ch) -> bool {
|
||||||
size_t end = length();
|
return ch == '\t'
|
||||||
while (characters()[start] == ' ')
|
|| ch == '\n'
|
||||||
++start;
|
|| ch == '\v'
|
||||||
while (characters()[end] == ' ') {
|
|| ch == '\f'
|
||||||
if (end <= start)
|
|| ch == '\r'
|
||||||
return "";
|
|| ch == ' ';
|
||||||
--end;
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return substring(start, end - start);
|
|
||||||
|
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)
|
String escape_html_entities(const StringView& html)
|
||||||
|
|
|
@ -114,7 +114,12 @@ public:
|
||||||
String to_lowercase() const;
|
String to_lowercase() const;
|
||||||
String to_uppercase() const;
|
String to_uppercase() const;
|
||||||
|
|
||||||
String trim_spaces() const;
|
enum class TrimMode {
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
Both
|
||||||
|
};
|
||||||
|
String trim_whitespace(TrimMode mode = TrimMode::Both) const;
|
||||||
|
|
||||||
bool equals_ignoring_case(const StringView&) const;
|
bool equals_ignoring_case(const StringView&) const;
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ void StyleProperties::load_font() const
|
||||||
|
|
||||||
// FIXME: Do this properly, with quote handling etc.
|
// FIXME: Do this properly, with quote handling etc.
|
||||||
for (auto& font_name : font_family.split(',')) {
|
for (auto& font_name : font_family.split(',')) {
|
||||||
font_name = font_name.trim_spaces();
|
font_name = font_name.trim_whitespace();
|
||||||
if (font_name == "monospace")
|
if (font_name == "monospace")
|
||||||
font_name = "Csilla";
|
font_name = "Csilla";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue