From 9f4af937808fbbf2fbc479a61f13278ed1a1b12f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 8 Dec 2014 13:46:07 +0000 Subject: [PATCH] cleanup in string.hh --- src/interned_string.hh | 3 --- src/string.cc | 11 ----------- src/string.hh | 38 +++++++++++--------------------------- 3 files changed, 11 insertions(+), 41 deletions(-) diff --git a/src/interned_string.hh b/src/interned_string.hh index d875829d..fd79de2c 100644 --- a/src/interned_string.hh +++ b/src/interned_string.hh @@ -77,9 +77,6 @@ public: release_ifn(); } - using StringView::operator==; - using StringView::operator!=; - InternedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const { if (m_slot == -1) diff --git a/src/string.cc b/src/string.cc index 1313448c..14c680c9 100644 --- a/src/string.cc +++ b/src/string.cc @@ -102,17 +102,6 @@ String to_string(int val) return buf; } -bool prefix_match(StringView str, StringView prefix) -{ - auto it = str.begin(); - for (auto& c : prefix) - { - if (it ==str.end() or *it++ != c) - return false; - } - return true; -} - bool subsequence_match(StringView str, StringView subseq) { auto it = str.begin(); diff --git a/src/string.hh b/src/string.hh index 0911948e..fa55c2a2 100644 --- a/src/string.hh +++ b/src/string.hh @@ -68,8 +68,7 @@ public: StringView(const std::string& str) : m_data{str.data()}, m_length{(int)str.length()} {} StringView(const char& c) : m_data(&c), m_length(1) {} - bool operator==(StringView other) const; - bool operator!=(StringView other) const; + friend bool operator==(StringView lhs, StringView rhs); [[gnu::always_inline]] const char* data() const { return m_data; } @@ -130,42 +129,23 @@ private: ByteCount m_length; }; -inline bool StringView::operator==(StringView other) const +inline bool operator==(StringView lhs, StringView rhs) { - return m_length == other.m_length and memcmp(m_data, other.m_data, (int)m_length) == 0; + return lhs.m_length == rhs.m_length and memcmp(lhs.m_data, rhs.m_data, (int)lhs.m_length) == 0; } -inline bool StringView::operator!=(StringView other) const +inline bool operator!=(StringView lhs, StringView rhs) { - return !this->operator==(other); + return not (lhs == rhs); } bool operator<(StringView lhs, StringView rhs); -inline bool operator==(const char* lhs, StringView rhs) -{ - return StringView{lhs} == rhs; -} - -inline bool operator!=(const char* lhs, StringView rhs) -{ - return StringView{lhs} != rhs; -} - -inline bool operator==(const std::string& lhs, StringView rhs) -{ - return StringView{lhs} == rhs; -} - -inline bool operator!=(const std::string& lhs, StringView rhs) -{ - return StringView{lhs} != rhs; -} - inline ByteCount StringView::byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); } + inline CharCount StringView::char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); @@ -290,7 +270,11 @@ String to_string(const StronglyTypedNumber& val) return to_string((ValueType)val); } -bool prefix_match(StringView str, StringView prefix); +inline bool prefix_match(StringView str, StringView prefix) +{ + return str.substr(0_byte, prefix.length()) == prefix; +} + bool subsequence_match(StringView str, StringView subseq); String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);