From 7bce096afdf182216c9da4c94765e12662188c98 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Sun, 2 Jun 2019 12:26:28 +0200 Subject: Take StringView in more places We should work towards a pattern where we take StringView as function arguments, and store String as member, to push the String construction to the last possible moment. --- AK/AKString.h | 6 +++--- AK/BufferStream.h | 10 +--------- AK/FileSystemPath.cpp | 2 +- AK/FileSystemPath.h | 2 +- AK/String.cpp | 10 +++++----- AK/StringBuilder.cpp | 2 +- AK/StringBuilder.h | 2 +- 7 files changed, 13 insertions(+), 21 deletions(-) (limited to 'AK') diff --git a/AK/AKString.h b/AK/AKString.h index a2a972ebf9..e75172a870 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -87,7 +87,7 @@ public: }; static String repeated(char, int count); - bool matches(const String& pattern, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; + bool matches(const StringView& pattern, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; int to_int(bool& ok) const; unsigned to_uint(bool& ok) const; @@ -122,7 +122,7 @@ public: return (*m_impl)[i]; } - bool ends_with(const String&) const; + bool ends_with(const StringView&) const; bool operator==(const String&) const; bool operator!=(const String& other) const { return !(*this == other); } @@ -166,7 +166,7 @@ public: StringView view() const { return { characters(), length() }; } private: - bool match_helper(const String& mask) const; + bool match_helper(const StringView& mask) const; RetainPtr m_impl; }; diff --git a/AK/BufferStream.h b/AK/BufferStream.h index db2907963f..7edc9f6e65 100644 --- a/AK/BufferStream.h +++ b/AK/BufferStream.h @@ -36,15 +36,7 @@ public: m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu; } - void operator<<(const char* str) - { - ssize_t len = strlen(str); - ASSERT(len >= 0); - for (ssize_t i = 0; i < len; ++i) - m_buffer[m_offset++] = str[i]; - } - - void operator<<(const String& value) + void operator<<(const StringView& value) { for (ssize_t i = 0; i < value.length(); ++i) m_buffer[m_offset++] = value[i]; diff --git a/AK/FileSystemPath.cpp b/AK/FileSystemPath.cpp index 07d8d5ea82..fbef784160 100644 --- a/AK/FileSystemPath.cpp +++ b/AK/FileSystemPath.cpp @@ -5,7 +5,7 @@ namespace AK { -FileSystemPath::FileSystemPath(const String& s) +FileSystemPath::FileSystemPath(const StringView& s) : m_string(s) { m_is_valid = canonicalize(); diff --git a/AK/FileSystemPath.h b/AK/FileSystemPath.h index b62160a526..d985baded1 100644 --- a/AK/FileSystemPath.h +++ b/AK/FileSystemPath.h @@ -7,7 +7,7 @@ namespace AK { class FileSystemPath { public: FileSystemPath() {} - explicit FileSystemPath(const String&); + explicit FileSystemPath(const StringView&); bool is_valid() const { return m_is_valid; } String string() const { return m_string; } diff --git a/AK/String.cpp b/AK/String.cpp index ed3943318c..8bfc787686 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -175,7 +175,7 @@ String String::format(const char* fmt, ...) return builder.to_string(); } -bool String::ends_with(const String& str) const +bool String::ends_with(const StringView& str) const { if (str.is_empty()) return true; @@ -196,20 +196,20 @@ String String::repeated(char ch, int count) return *impl; } -bool String::matches(const String& mask, CaseSensitivity case_sensitivity) const +bool String::matches(const StringView& mask, CaseSensitivity case_sensitivity) const { if (case_sensitivity == CaseSensitivity::CaseInsensitive) { String this_lower = this->to_lowercase(); - String mask_lower = mask.to_lowercase(); + String mask_lower = String(mask).to_lowercase(); return this_lower.match_helper(mask_lower); } return match_helper(mask); } -bool String::match_helper(const String& mask) const +bool String::match_helper(const StringView& mask) const { - if (is_null() || mask.is_null()) + if (is_null()) return false; const char* string_ptr = characters(); diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 0fe550242e..6f6a86de26 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -16,7 +16,7 @@ StringBuilder::StringBuilder(ssize_t initial_capacity) m_buffer.grow(initial_capacity); } -void StringBuilder::append(const String& str) +void StringBuilder::append(const StringView& str) { if (str.is_empty()) return; diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 700540942a..65bc8e2c07 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -11,7 +11,7 @@ public: explicit StringBuilder(ssize_t initial_capacity = 16); ~StringBuilder() {} - void append(const String&); + void append(const StringView&); void append(char); void append(const char*, ssize_t); void appendf(const char*, ...); -- cgit v1.2.3