diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-02-10 23:09:30 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-02-10 23:09:30 +0000 |
| commit | 790e671f6cd34fc955dfc458b0a26a20a3fd8089 (patch) | |
| tree | 2674a97bfc583fc39a4b19e8ed8da928bd8d27ce /src | |
| parent | 8714c414033cee64c6993305b0862ae9f3e88d31 (diff) | |
Replace some <cstring> function usage with c++ algorithms
Diffstat (limited to 'src')
| -rw-r--r-- | src/ncurses_ui.cc | 4 | ||||
| -rw-r--r-- | src/shared_string.hh | 2 | ||||
| -rw-r--r-- | src/string.cc | 5 | ||||
| -rw-r--r-- | 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<MemoryDomain::SharedString> const int len = (int)str.length() + (back != 0 ? 1 : 0); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1); StringStorage* res = reinterpret_cast<StringStorage*>(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<String> 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 <string> #include <climits> -#include <cstring> namespace Kakoune { @@ -18,6 +17,11 @@ class StringView; using StringBase = std::basic_string<char, std::char_traits<char>, Allocator<char, MemoryDomain::String>>; +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<typename Char, typename Traits, typename Alloc> StringView(const std::basic_string<Char, Traits, Alloc>& 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) |
