Replace some <cstring> function usage with c++ algorithms

This commit is contained in:
Maxime Coste 2015-02-10 23:09:30 +00:00
parent 8714c41403
commit 790e671f6c
4 changed files with 12 additions and 10 deletions

View File

@ -29,7 +29,7 @@ using std::max;
struct NCursesWin : WINDOW {}; struct NCursesWin : WINDOW {};
static const StringView assistant_cat[] = static constexpr StringView assistant_cat[] =
{ R"( ___ )", { R"( ___ )",
R"( / __) )", R"( / __) )",
R"( \ \ ╭)", R"( \ \ ╭)",
@ -42,7 +42,7 @@ static const StringView assistant_cat[] =
R"( /_/ /_/ )", R"( /_/ /_/ )",
R"( )"}; R"( )"};
static const StringView assistant_clippy[] = static constexpr StringView assistant_clippy[] =
{ " ╭──╮ ", { " ╭──╮ ",
" │ │ ", " │ │ ",
" @ @ ╭", " @ @ ╭",

View File

@ -26,7 +26,7 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
const int len = (int)str.length() + (back != 0 ? 1 : 0); const int len = (int)str.length() + (back != 0 ? 1 : 0);
void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1);
StringStorage* res = reinterpret_cast<StringStorage*>(ptr); 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->refcount = 0;
res->length = len; res->length = len;
if (back != 0) if (back != 0)

View File

@ -11,10 +11,7 @@ namespace Kakoune
bool operator<(StringView lhs, StringView rhs) bool operator<(StringView lhs, StringView rhs)
{ {
int cmp = strncmp(lhs.data(), rhs.data(), (int)std::min(lhs.length(), rhs.length())); return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
if (cmp == 0)
return lhs.length() < rhs.length();
return cmp < 0;
} }
Vector<String> split(StringView str, char separator, char escape) Vector<String> split(StringView str, char separator, char escape)

View File

@ -8,7 +8,6 @@
#include <string> #include <string>
#include <climits> #include <climits>
#include <cstring>
namespace Kakoune namespace Kakoune
{ {
@ -18,6 +17,11 @@ class StringView;
using StringBase = std::basic_string<char, std::char_traits<char>, using StringBase = std::basic_string<char, std::char_traits<char>,
Allocator<char, MemoryDomain::String>>; Allocator<char, MemoryDomain::String>>;
constexpr ByteCount strlen(const char* s)
{
return *s == 0 ? 0 : strlen(s+1) + 1;
}
class String : public StringBase class String : public StringBase
{ {
public: public:
@ -65,7 +69,7 @@ public:
constexpr StringView() : m_data{nullptr}, m_length{0} {} constexpr StringView() : m_data{nullptr}, m_length{0} {}
constexpr StringView(const char* data, ByteCount length) constexpr StringView(const char* data, ByteCount length)
: m_data{data}, m_length{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)} {} constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {}
template<typename Char, typename Traits, typename Alloc> 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()} {} 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) 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) inline bool operator!=(StringView lhs, StringView rhs)