Support single char StringView

remove single char escape function overload, add unescape function
This commit is contained in:
Maxime Coste 2014-11-04 13:31:15 +00:00
parent b167c11671
commit 97df59ddb7
2 changed files with 15 additions and 13 deletions

View File

@ -62,18 +62,6 @@ std::vector<StringView> split(StringView str, char separator)
return res; 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 escape(StringView str, StringView characters, char escape)
{ {
String res; String res;
@ -86,6 +74,19 @@ String escape(StringView str, StringView characters, char escape)
return res; 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 str_to_int(StringView str)
{ {
int res = 0; int res = 0;

View File

@ -66,6 +66,7 @@ public:
StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {} 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)} {} 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 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;
bool operator!=(StringView other) const; bool operator!=(StringView other) const;
@ -274,8 +275,8 @@ inline String operator+(StringView lhs, StringView rhs)
std::vector<String> split(StringView str, char separator, char escape); std::vector<String> split(StringView str, char separator, char escape);
std::vector<StringView> split(StringView str, char separator); std::vector<StringView> split(StringView str, char separator);
String escape(StringView str, char character, char escape);
String escape(StringView str, StringView characters, 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) inline String operator"" _str(const char* str, size_t)
{ {