From d20e26c69029e9ee80362e7eb08d848541ecb02a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 11 May 2020 01:43:33 +0100 Subject: AK: Add String::trim_spaces() --- AK/String.cpp | 14 ++++++++++++++ AK/String.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/AK/String.cpp b/AK/String.cpp index 78176780cb..6965731b3a 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -369,6 +369,20 @@ int String::replace(const String& needle, const String& replacement, bool all_oc return positions.size(); } +String String::trim_spaces() const +{ + size_t start = 0; + size_t end = length(); + while (characters()[start] == ' ') + ++start; + while (characters()[end] == ' ') { + if (end <= start) + return ""; + --end; + } + return substring(start, end - start); +} + String escape_html_entities(const StringView& html) { StringBuilder builder; diff --git a/AK/String.h b/AK/String.h index bf5ac828ce..943a65bebd 100644 --- a/AK/String.h +++ b/AK/String.h @@ -114,6 +114,8 @@ public: String to_lowercase() const; String to_uppercase() const; + String trim_spaces() const; + bool equals_ignoring_case(const StringView&) const; bool contains(const String&) const; -- cgit v1.2.3