1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

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.
This commit is contained in:
Robin Burchell 2019-06-02 12:26:28 +02:00 committed by Andreas Kling
parent b55b6cd7fc
commit 7bce096afd
14 changed files with 28 additions and 36 deletions

View file

@ -87,7 +87,7 @@ public:
}; };
static String repeated(char, int count); 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; int to_int(bool& ok) const;
unsigned to_uint(bool& ok) const; unsigned to_uint(bool& ok) const;
@ -122,7 +122,7 @@ public:
return (*m_impl)[i]; 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&) const;
bool operator!=(const String& other) const { return !(*this == other); } bool operator!=(const String& other) const { return !(*this == other); }
@ -166,7 +166,7 @@ public:
StringView view() const { return { characters(), length() }; } StringView view() const { return { characters(), length() }; }
private: private:
bool match_helper(const String& mask) const; bool match_helper(const StringView& mask) const;
RetainPtr<StringImpl> m_impl; RetainPtr<StringImpl> m_impl;
}; };

View file

@ -36,15 +36,7 @@ public:
m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu; m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu;
} }
void operator<<(const char* str) void operator<<(const StringView& value)
{
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)
{ {
for (ssize_t i = 0; i < value.length(); ++i) for (ssize_t i = 0; i < value.length(); ++i)
m_buffer[m_offset++] = value[i]; m_buffer[m_offset++] = value[i];

View file

@ -5,7 +5,7 @@
namespace AK { namespace AK {
FileSystemPath::FileSystemPath(const String& s) FileSystemPath::FileSystemPath(const StringView& s)
: m_string(s) : m_string(s)
{ {
m_is_valid = canonicalize(); m_is_valid = canonicalize();

View file

@ -7,7 +7,7 @@ namespace AK {
class FileSystemPath { class FileSystemPath {
public: public:
FileSystemPath() {} FileSystemPath() {}
explicit FileSystemPath(const String&); explicit FileSystemPath(const StringView&);
bool is_valid() const { return m_is_valid; } bool is_valid() const { return m_is_valid; }
String string() const { return m_string; } String string() const { return m_string; }

View file

@ -175,7 +175,7 @@ String String::format(const char* fmt, ...)
return builder.to_string(); return builder.to_string();
} }
bool String::ends_with(const String& str) const bool String::ends_with(const StringView& str) const
{ {
if (str.is_empty()) if (str.is_empty())
return true; return true;
@ -196,20 +196,20 @@ String String::repeated(char ch, int count)
return *impl; 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) { if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
String this_lower = this->to_lowercase(); 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 this_lower.match_helper(mask_lower);
} }
return match_helper(mask); 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; return false;
const char* string_ptr = characters(); const char* string_ptr = characters();

View file

@ -16,7 +16,7 @@ StringBuilder::StringBuilder(ssize_t initial_capacity)
m_buffer.grow(initial_capacity); m_buffer.grow(initial_capacity);
} }
void StringBuilder::append(const String& str) void StringBuilder::append(const StringView& str)
{ {
if (str.is_empty()) if (str.is_empty())
return; return;

View file

@ -11,7 +11,7 @@ public:
explicit StringBuilder(ssize_t initial_capacity = 16); explicit StringBuilder(ssize_t initial_capacity = 16);
~StringBuilder() {} ~StringBuilder() {}
void append(const String&); void append(const StringView&);
void append(char); void append(char);
void append(const char*, ssize_t); void append(const char*, ssize_t);
void appendf(const char*, ...); void appendf(const char*, ...);

View file

@ -108,7 +108,7 @@ void DirectoryView::set_view_mode(ViewMode mode)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
void DirectoryView::add_path_to_history(const String& path) void DirectoryView::add_path_to_history(const StringView& path)
{ {
if (m_path_history_position < m_path_history.size()) if (m_path_history_position < m_path_history.size())
m_path_history.resize(m_path_history_position + 1); m_path_history.resize(m_path_history_position + 1);
@ -117,13 +117,13 @@ void DirectoryView::add_path_to_history(const String& path)
m_path_history_position = m_path_history.size() - 1; m_path_history_position = m_path_history.size() - 1;
} }
void DirectoryView::open(const String& path) void DirectoryView::open(const StringView& path)
{ {
add_path_to_history(path); add_path_to_history(path);
model().open(path); model().open(path);
} }
void DirectoryView::set_status_message(const String& message) void DirectoryView::set_status_message(const StringView& message)
{ {
if (on_status_message) if (on_status_message)
on_status_message(message); on_status_message(message);

View file

@ -12,7 +12,7 @@ public:
explicit DirectoryView(GWidget* parent); explicit DirectoryView(GWidget* parent);
virtual ~DirectoryView() override; virtual ~DirectoryView() override;
void open(const String& path); void open(const StringView& path);
String path() const { return model().path(); } String path() const { return model().path(); }
void open_parent_directory(); void open_parent_directory();
void open_previous_directory(); void open_previous_directory();
@ -22,8 +22,8 @@ public:
void refresh(); void refresh();
Function<void(const String&)> on_path_change; Function<void(const StringView&)> on_path_change;
Function<void(String)> on_status_message; Function<void(const StringView&)> on_status_message;
Function<void(int done, int total)> on_thumbnail_progress; Function<void(int done, int total)> on_thumbnail_progress;
enum ViewMode enum ViewMode
@ -41,14 +41,14 @@ private:
void handle_activation(const GModelIndex&); void handle_activation(const GModelIndex&);
void set_status_message(const String&); void set_status_message(const StringView&);
ViewMode m_view_mode { Invalid }; ViewMode m_view_mode { Invalid };
Retained<GDirectoryModel> m_model; Retained<GDirectoryModel> m_model;
int m_path_history_position { 0 }; int m_path_history_position { 0 };
Vector<String> m_path_history; Vector<String> m_path_history;
void add_path_to_history(const String& path); void add_path_to_history(const StringView& path);
GTableView* m_table_view { nullptr }; GTableView* m_table_view { nullptr };
GItemView* m_item_view { nullptr }; GItemView* m_item_view { nullptr };

View file

@ -199,8 +199,8 @@ int main(int argc, char** argv)
go_back_action->set_enabled(directory_view->path_history_position() > 0); go_back_action->set_enabled(directory_view->path_history_position() > 0);
}; };
directory_view->on_status_message = [statusbar] (String message) { directory_view->on_status_message = [statusbar] (const StringView& message) {
statusbar->set_text(move(message)); statusbar->set_text(message);
}; };
directory_view->on_thumbnail_progress = [&] (int done, int total) { directory_view->on_thumbnail_progress = [&] (int done, int total) {

View file

@ -1,7 +1,7 @@
#include "CDirIterator.h" #include "CDirIterator.h"
#include <cerrno> #include <cerrno>
CDirIterator::CDirIterator(const String& path, Flags flags) CDirIterator::CDirIterator(const StringView& path, Flags flags)
: m_flags(flags) : m_flags(flags)
{ {
m_dir = opendir(path.characters()); m_dir = opendir(path.characters());

View file

@ -11,7 +11,7 @@ public:
SkipDots = 0x1, SkipDots = 0x1,
}; };
CDirIterator(const String& path, Flags = Flags::NoFlags); CDirIterator(const StringView& path, Flags = Flags::NoFlags);
~CDirIterator(); ~CDirIterator();
bool has_error() const { return m_error != 0; } bool has_error() const { return m_error != 0; }

View file

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
CFile::CFile(const String& filename) CFile::CFile(const StringView& filename)
: m_filename(filename) : m_filename(filename)
{ {
} }

View file

@ -6,11 +6,11 @@
class CFile final : public CIODevice { class CFile final : public CIODevice {
public: public:
CFile() {} CFile() {}
explicit CFile(const String&); explicit CFile(const StringView&);
virtual ~CFile() override; virtual ~CFile() override;
String filename() const { return m_filename; } String filename() const { return m_filename; }
void set_filename(const String& filename) { m_filename = filename; } void set_filename(const StringView& filename) { m_filename = filename; }
virtual bool open(CIODevice::OpenMode) override; virtual bool open(CIODevice::OpenMode) override;