mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:07:45 +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
1 changed files with 8 additions and 2 deletions
10
AK/String.h
10
AK/String.h
|
@ -127,12 +127,18 @@ public:
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
[[nodiscard]] String trim(StringView characters, TrimMode mode = TrimMode::Both) const
|
[[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
|
[[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
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue