From 97df59ddb7d6d3a0716bc872fdd5a172ed53b269 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 4 Nov 2014 13:31:15 +0000 Subject: [PATCH] Support single char StringView remove single char escape function overload, add unescape function --- src/string.cc | 25 +++++++++++++------------ src/string.hh | 3 ++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/string.cc b/src/string.cc index 79da055b..474b15b4 100644 --- a/src/string.cc +++ b/src/string.cc @@ -62,18 +62,6 @@ std::vector split(StringView str, char separator) return res; } -String escape(StringView str, char character, char escape) -{ - String res; - for (auto& c : str) - { - if (c == character) - res += escape; - res += c; - } - return res; -} - String escape(StringView str, StringView characters, char escape) { String res; @@ -86,6 +74,19 @@ String escape(StringView str, StringView characters, char escape) return res; } +String unescape(StringView str, StringView characters, char escape) +{ + String res; + for (auto& c : str) + { + if (contains(characters, c) and not res.empty() and res.back() == escape) + res.back() = c; + else + res += c; + } + return res; +} + int str_to_int(StringView str) { int res = 0; diff --git a/src/string.hh b/src/string.hh index afb3f64e..f7311d9d 100644 --- a/src/string.hh +++ b/src/string.hh @@ -66,6 +66,7 @@ public: StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {} constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {} 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; @@ -274,8 +275,8 @@ inline String operator+(StringView lhs, StringView rhs) std::vector split(StringView str, char separator, char escape); std::vector split(StringView str, char separator); -String escape(StringView str, char character, char escape); String escape(StringView str, StringView characters, char escape); +String unescape(StringView str, StringView characters, char escape); inline String operator"" _str(const char* str, size_t) {