diff options
author | Andreas Kling <kling@serenityos.org> | 2022-02-19 00:36:30 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-19 14:45:59 +0100 |
commit | 4b900bc10038e0b08cdb1eec5adc12275e40e066 (patch) | |
tree | 2f8ec51289a28ddae71cb250ef896da0f6353d18 | |
parent | 25504f6a1b8e37c3da36845a27441cc5aba67f0c (diff) | |
download | serenity-4b900bc10038e0b08cdb1eec5adc12275e40e066.zip |
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.
-rw-r--r-- | AK/String.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/AK/String.h b/AK/String.h index 9f7988dcdc..27f7799fa7 100644 --- a/AK/String.h +++ b/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 |