Browse Source

AK: Add String{,View}::is_whitespace()

+Tests!
AnotherTest 4 years ago
parent
commit
f3ecea1fb3
5 changed files with 28 additions and 11 deletions
  1. 2 0
      AK/String.h
  2. 12 11
      AK/StringUtils.cpp
  3. 1 0
      AK/StringUtils.h
  4. 2 0
      AK/StringView.h
  5. 11 0
      AK/Tests/TestStringUtils.cpp

+ 2 - 0
AK/String.h

@@ -122,6 +122,8 @@ public:
     String to_lowercase() const;
     String to_uppercase() const;
 
+    bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
+
 #ifndef KERNEL
     String trim_whitespace(TrimMode mode = TrimMode::Both) const
     {

+ 12 - 11
AK/StringUtils.cpp

@@ -31,6 +31,7 @@
 #include <AK/StringUtils.h>
 #include <AK/StringView.h>
 #include <AK/Vector.h>
+#include <ctype.h>
 
 namespace AK {
 
@@ -302,17 +303,17 @@ bool contains(const StringView& str, const StringView& needle, CaseSensitivity c
     return false;
 }
 
-StringView trim_whitespace(const StringView& str, TrimMode mode)
+bool is_whitespace(const StringView& str)
 {
-    auto is_whitespace_character = [](char ch) -> bool {
-        return ch == '\t'
-            || ch == '\n'
-            || ch == '\v'
-            || ch == '\f'
-            || ch == '\r'
-            || ch == ' ';
-    };
+    for (auto ch : str) {
+        if (!isspace(ch))
+            return false;
+    }
+    return true;
+}
 
+StringView trim_whitespace(const StringView& str, TrimMode mode)
+{
     size_t substring_start = 0;
     size_t substring_length = str.length();
 
@@ -320,7 +321,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
         for (size_t i = 0; i < str.length(); ++i) {
             if (substring_length == 0)
                 return "";
-            if (!is_whitespace_character(str[i]))
+            if (!isspace(str[i]))
                 break;
             ++substring_start;
             --substring_length;
@@ -331,7 +332,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
         for (size_t i = str.length() - 1; i > 0; --i) {
             if (substring_length == 0)
                 return "";
-            if (!is_whitespace_character(str[i]))
+            if (!isspace(str[i]))
                 break;
             --substring_length;
         }

+ 1 - 0
AK/StringUtils.h

@@ -69,6 +69,7 @@ bool equals_ignoring_case(const StringView&, const StringView&);
 bool ends_with(const StringView& a, const StringView& b, CaseSensitivity);
 bool starts_with(const StringView&, const StringView&, CaseSensitivity);
 bool contains(const StringView&, const StringView&, CaseSensitivity);
+bool is_whitespace(const StringView&);
 StringView trim_whitespace(const StringView&, TrimMode mode);
 }
 

+ 2 - 0
AK/StringView.h

@@ -180,6 +180,8 @@ public:
 
     String to_string() const;
 
+    bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
+
     template<typename T, typename... Rest>
     bool is_one_of(const T& string, Rest... rest) const
     {

+ 11 - 0
AK/Tests/TestStringUtils.cpp

@@ -287,4 +287,15 @@ TEST_CASE(contains)
     EXPECT(!AK::StringUtils::contains(test_string, "L", CaseSensitivity::CaseInsensitive));
 }
 
+TEST_CASE(is_whitespace)
+{
+    EXPECT(AK::StringUtils::is_whitespace(""));
+    EXPECT(AK::StringUtils::is_whitespace("   "));
+    EXPECT(AK::StringUtils::is_whitespace("  \t"));
+    EXPECT(AK::StringUtils::is_whitespace("  \t\n"));
+    EXPECT(AK::StringUtils::is_whitespace("  \t\n\r\v"));
+    EXPECT(!AK::StringUtils::is_whitespace("  a "));
+    EXPECT(!AK::StringUtils::is_whitespace("a\t"));
+}
+
 TEST_MAIN(StringUtils)