ソースを参照

StringView: Add starts_with method

MinusGix 5 年 前
コミット
05f641a5a9
2 ファイル変更15 行追加0 行削除
  1. 13 0
      AK/StringView.cpp
  2. 2 0
      AK/StringView.h

+ 13 - 0
AK/StringView.cpp

@@ -40,6 +40,19 @@ Vector<StringView> StringView::split_view(const char separator) const
     return v;
 }
 
+bool StringView::starts_with(const StringView& str) const
+{
+    if (str.is_empty())
+        return true;
+    if (is_empty())
+        return false;
+    if (str.length() > length())
+        return false;
+    if (characters_without_null_termination() == str.characters_without_null_termination())
+        return true;
+    return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
+}
+
 StringView StringView::substring_view(int start, int length) const
 {
     if (!length)

+ 2 - 0
AK/StringView.h

@@ -41,6 +41,8 @@ public:
 
     unsigned hash() const;
 
+    bool starts_with(const StringView&) const;
+
     StringView substring_view(int start, int length) const;
     Vector<StringView> split_view(char) const;