mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add fast path in String::trim() and String::trim_whitespace()
If the trimmed string would be the entire string, just return *this instead of creating a new StringImpl.
This commit is contained in:
parent
25504f6a1b
commit
4b900bc100
Notes:
sideshowbarker
2024-07-17 18:32:01 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/4b900bc100
1 changed files with 8 additions and 2 deletions
10
AK/String.h
10
AK/String.h
|
@ -127,12 +127,18 @@ public:
|
|||
#ifndef KERNEL
|
||||
[[nodiscard]] String trim(StringView characters, TrimMode mode = TrimMode::Both) const
|
||||
{
|
||||
return StringUtils::trim(view(), characters, mode);
|
||||
auto trimmed_view = StringUtils::trim(view(), characters, mode);
|
||||
if (view() == trimmed_view)
|
||||
return *this;
|
||||
return trimmed_view;
|
||||
}
|
||||
|
||||
[[nodiscard]] String trim_whitespace(TrimMode mode = TrimMode::Both) const
|
||||
{
|
||||
return StringUtils::trim_whitespace(view(), mode);
|
||||
auto trimmed_view = StringUtils::trim_whitespace(view(), mode);
|
||||
if (view() == trimmed_view)
|
||||
return *this;
|
||||
return trimmed_view;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in a new issue