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:
Andreas Kling 2022-02-19 00:36:30 +01:00
parent 25504f6a1b
commit 4b900bc100

View file

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