浏览代码

AK: Exclude StringView String APIs from the Kernel

These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
Idan Horowitz 3 年之前
父节点
当前提交
4c6a1f4db2
共有 2 个文件被更改,包括 39 次插入6 次删除
  1. 11 2
      AK/StringView.cpp
  2. 28 4
      AK/StringView.h

+ 11 - 2
AK/StringView.cpp

@@ -7,15 +7,19 @@
 #include <AK/AnyOf.h>
 #include <AK/AnyOf.h>
 #include <AK/ByteBuffer.h>
 #include <AK/ByteBuffer.h>
 #include <AK/Find.h>
 #include <AK/Find.h>
-#include <AK/FlyString.h>
 #include <AK/Function.h>
 #include <AK/Function.h>
 #include <AK/Memory.h>
 #include <AK/Memory.h>
-#include <AK/String.h>
 #include <AK/StringView.h>
 #include <AK/StringView.h>
 #include <AK/Vector.h>
 #include <AK/Vector.h>
 
 
+#ifndef KERNEL
+#    include <AK/FlyString.h>
+#    include <AK/String.h>
+#endif
+
 namespace AK {
 namespace AK {
 
 
+#ifndef KERNEL
 StringView::StringView(const String& string)
 StringView::StringView(const String& string)
     : m_characters(string.characters())
     : m_characters(string.characters())
     , m_length(string.length())
     , m_length(string.length())
@@ -27,6 +31,7 @@ StringView::StringView(const FlyString& string)
     , m_length(string.length())
     , m_length(string.length())
 {
 {
 }
 }
+#endif
 
 
 StringView::StringView(const ByteBuffer& buffer)
 StringView::StringView(const ByteBuffer& buffer)
     : m_characters((const char*)buffer.data())
     : m_characters((const char*)buffer.data())
@@ -142,6 +147,7 @@ bool StringView::equals_ignoring_case(StringView other) const
     return StringUtils::equals_ignoring_case(*this, other);
     return StringUtils::equals_ignoring_case(*this, other);
 }
 }
 
 
+#ifndef KERNEL
 String StringView::to_lowercase_string() const
 String StringView::to_lowercase_string() const
 {
 {
     return StringImpl::create_lowercased(characters_without_null_termination(), length());
     return StringImpl::create_lowercased(characters_without_null_termination(), length());
@@ -156,6 +162,7 @@ String StringView::to_titlecase_string() const
 {
 {
     return StringUtils::to_titlecase(*this);
     return StringUtils::to_titlecase(*this);
 }
 }
+#endif
 
 
 StringView StringView::substring_view_starting_from_substring(StringView substring) const
 StringView StringView::substring_view_starting_from_substring(StringView substring) const
 {
 {
@@ -201,6 +208,7 @@ template Optional<unsigned long long> StringView::to_uint() const;
 template Optional<long> StringView::to_uint() const;
 template Optional<long> StringView::to_uint() const;
 template Optional<long long> StringView::to_uint() const;
 template Optional<long long> StringView::to_uint() const;
 
 
+#ifndef KERNEL
 bool StringView::operator==(const String& string) const
 bool StringView::operator==(const String& string) const
 {
 {
     return *this == string.view();
     return *this == string.view();
@@ -212,6 +220,7 @@ String StringView::replace(StringView needle, StringView replacement, bool all_o
 {
 {
     return StringUtils::replace(*this, needle, replacement, all_occurrences);
     return StringUtils::replace(*this, needle, replacement, all_occurrences);
 }
 }
+#endif
 
 
 Vector<size_t> StringView::find_all(StringView needle) const
 Vector<size_t> StringView::find_all(StringView needle) const
 {
 {

+ 28 - 4
AK/StringView.h

@@ -45,14 +45,21 @@ public:
     }
     }
 
 
     StringView(const ByteBuffer&);
     StringView(const ByteBuffer&);
+#ifndef KERNEL
     StringView(const String&);
     StringView(const String&);
     StringView(const FlyString&);
     StringView(const FlyString&);
+#endif
 
 
     explicit StringView(ByteBuffer&&) = delete;
     explicit StringView(ByteBuffer&&) = delete;
+#ifndef KERNEL
     explicit StringView(String&&) = delete;
     explicit StringView(String&&) = delete;
     explicit StringView(FlyString&&) = delete;
     explicit StringView(FlyString&&) = delete;
+#endif
 
 
-    [[nodiscard]] constexpr bool is_null() const { return m_characters == nullptr; }
+    [[nodiscard]] constexpr bool is_null() const
+    {
+        return m_characters == nullptr;
+    }
     [[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
     [[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
 
 
     [[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
     [[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
@@ -87,11 +94,16 @@ public:
     [[nodiscard]] StringView trim(StringView characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
     [[nodiscard]] StringView trim(StringView characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
     [[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
     [[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
 
 
+#ifndef KERNEL
     [[nodiscard]] String to_lowercase_string() const;
     [[nodiscard]] String to_lowercase_string() const;
     [[nodiscard]] String to_uppercase_string() const;
     [[nodiscard]] String to_uppercase_string() const;
     [[nodiscard]] String to_titlecase_string() const;
     [[nodiscard]] String to_titlecase_string() const;
+#endif
 
 
-    [[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
+    [[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const
+    {
+        return StringUtils::find(*this, needle, start);
+    }
     [[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
     [[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
     [[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
     [[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
     // FIXME: Implement find_last(StringView) for API symmetry.
     // FIXME: Implement find_last(StringView) for API symmetry.
@@ -202,7 +214,9 @@ public:
         return !(*this == cstring);
         return !(*this == cstring);
     }
     }
 
 
+#ifndef KERNEL
     bool operator==(const String&) const;
     bool operator==(const String&) const;
+#endif
 
 
     [[nodiscard]] constexpr int compare(StringView other) const
     [[nodiscard]] constexpr int compare(StringView other) const
     {
     {
@@ -242,12 +256,22 @@ public:
 
 
     constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
     constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
 
 
+#ifndef KERNEL
     [[nodiscard]] String to_string() const;
     [[nodiscard]] String to_string() const;
+#endif
 
 
-    [[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
+    [[nodiscard]] bool is_whitespace() const
+    {
+        return StringUtils::is_whitespace(*this);
+    }
 
 
+#ifndef KERNEL
     [[nodiscard]] String replace(StringView needle, StringView replacement, bool all_occurrences = false) const;
     [[nodiscard]] String replace(StringView needle, StringView replacement, bool all_occurrences = false) const;
-    [[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle); }
+#endif
+    [[nodiscard]] size_t count(StringView needle) const
+    {
+        return StringUtils::count(*this, needle);
+    }
 
 
     template<typename... Ts>
     template<typename... Ts>
     [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
     [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const