From 790e671f6cd34fc955dfc458b0a26a20a3fd8089 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 10 Feb 2015 23:09:30 +0000 Subject: [PATCH] Replace some function usage with c++ algorithms --- src/ncurses_ui.cc | 4 ++-- src/shared_string.hh | 2 +- src/string.cc | 5 +---- src/string.hh | 11 ++++++++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index d49efa8c..e5b58a4a 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -29,7 +29,7 @@ using std::max; struct NCursesWin : WINDOW {}; -static const StringView assistant_cat[] = +static constexpr StringView assistant_cat[] = { R"( ___ )", R"( / __) )", R"( \ \ ╭)", @@ -42,7 +42,7 @@ static const StringView assistant_cat[] = R"( /_/ /_/ )", R"( )"}; -static const StringView assistant_clippy[] = +static constexpr StringView assistant_clippy[] = { " ╭──╮ ", " │ │ ", " @ @ ╭", diff --git a/src/shared_string.hh b/src/shared_string.hh index 0725c613..f9adf737 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -26,7 +26,7 @@ struct StringStorage : UseMemoryDomain const int len = (int)str.length() + (back != 0 ? 1 : 0); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1); StringStorage* res = reinterpret_cast(ptr); - memcpy(res->data(), str.data(), (int)str.length()); + std::copy(str.begin(), str.end(), res->data()); res->refcount = 0; res->length = len; if (back != 0) diff --git a/src/string.cc b/src/string.cc index dab1d2fe..db2a7e21 100644 --- a/src/string.cc +++ b/src/string.cc @@ -11,10 +11,7 @@ namespace Kakoune bool operator<(StringView lhs, StringView rhs) { - int cmp = strncmp(lhs.data(), rhs.data(), (int)std::min(lhs.length(), rhs.length())); - if (cmp == 0) - return lhs.length() < rhs.length(); - return cmp < 0; + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } Vector split(StringView str, char separator, char escape) diff --git a/src/string.hh b/src/string.hh index 62cbe5cc..7166c8b3 100644 --- a/src/string.hh +++ b/src/string.hh @@ -8,7 +8,6 @@ #include #include -#include namespace Kakoune { @@ -18,6 +17,11 @@ class StringView; using StringBase = std::basic_string, Allocator>; +constexpr ByteCount strlen(const char* s) +{ + return *s == 0 ? 0 : strlen(s+1) + 1; +} + class String : public StringBase { public: @@ -65,7 +69,7 @@ public: constexpr StringView() : m_data{nullptr}, m_length{0} {} constexpr StringView(const char* data, ByteCount length) : m_data{data}, m_length{length} {} - StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {} + constexpr StringView(const char* data) : m_data{data}, m_length{strlen(data)} {} constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {} template StringView(const std::basic_string& str) : m_data{str.data()}, m_length{(int)str.length()} {} @@ -136,7 +140,8 @@ private: inline bool operator==(StringView lhs, StringView rhs) { - return lhs.m_length == rhs.m_length and memcmp(lhs.m_data, rhs.m_data, (int)lhs.m_length) == 0; + return lhs.m_length == rhs.m_length and + std::equal(lhs.begin(), lhs.end(), rhs.begin()); } inline bool operator!=(StringView lhs, StringView rhs)