Use more StringView in string helper functions

This commit is contained in:
Maxime Coste 2014-08-03 10:02:17 +01:00
parent de402c4703
commit 8ee2aa9ba7
2 changed files with 32 additions and 21 deletions

View File

@ -1,46 +1,44 @@
#include "string.hh" #include "string.hh"
#include "exception.hh" #include "exception.hh"
#include "utils.hh"
#include "utf8_iterator.hh" #include "utf8_iterator.hh"
namespace Kakoune namespace Kakoune
{ {
std::vector<String> split(const String& str, char separator, char escape) std::vector<String> split(StringView str, char separator, char escape)
{ {
auto begin = str.begin();
auto end = str.begin();
std::vector<String> res; std::vector<String> res;
while (end != str.end()) auto it = str.begin();
while (it != str.end())
{ {
res.emplace_back(); res.emplace_back();
String& element = res.back(); String& element = res.back();
while (end != str.end()) while (it != str.end())
{ {
auto c = *end; auto c = *it;
if (c == escape and end + 1 != end and *(end+1) == separator) if (c == escape and it + 1 != str.end() and *(it+1) == separator)
{ {
element += separator; element += separator;
end += 2; it += 2;
} }
else if (c == separator) else if (c == separator)
{ {
++end; ++it;
break; break;
} }
else else
{ {
element += c; element += c;
++end; ++it;
} }
} }
begin = end;
} }
return res; return res;
} }
String escape(const String& str, char character, char escape) String escape(StringView str, char character, char escape)
{ {
String res; String res;
for (auto& c : str) for (auto& c : str)
@ -52,10 +50,22 @@ String escape(const String& str, char character, char escape)
return res; return res;
} }
int str_to_int(const String& str) String escape(StringView str, StringView characters, char escape)
{
String res;
for (auto& c : str)
{
if (contains(characters, c))
res += escape;
res += c;
}
return res;
}
int str_to_int(StringView str)
{ {
int res = 0; int res = 0;
if (sscanf(str.c_str(), "%i", &res) != 1) if (sscanf(str.zstr(), "%i", &res) != 1)
throw runtime_error(str + "is not a number"); throw runtime_error(str + "is not a number");
return res; return res;
} }
@ -72,7 +82,7 @@ String option_to_string(const Regex& re)
return String{re.str()}; return String{re.str()};
} }
void option_from_string(const String& str, Regex& re) void option_from_string(StringView str, Regex& re)
{ {
try try
{ {

View File

@ -61,7 +61,7 @@ public:
: m_data{data}, m_length{length} {} : m_data{data}, m_length{length} {}
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 String& str) : m_data{str.data()}, m_length{str.length()} {} StringView(const std::string& str) : m_data{str.data()}, m_length{(int)str.length()} {}
bool operator==(StringView other) const; bool operator==(StringView other) const;
bool operator!=(StringView other) const; bool operator!=(StringView other) const;
@ -259,8 +259,9 @@ inline String operator+(StringView lhs, StringView rhs)
return res; return res;
} }
std::vector<String> split(const String& str, char separator, char escape = 0); std::vector<String> split(StringView str, char separator, char escape = 0);
String escape(const String& str, char character, char escape); String escape(StringView str, char character, char escape);
String escape(StringView str, StringView characters, char escape);
inline String operator"" _str(const char* str, size_t) inline String operator"" _str(const char* str, size_t)
{ {
@ -275,9 +276,9 @@ inline String codepoint_to_str(Codepoint cp)
} }
String option_to_string(const Regex& re); String option_to_string(const Regex& re);
void option_from_string(const String& str, Regex& re); void option_from_string(StringView str, Regex& re);
int str_to_int(const String& str); int str_to_int(StringView str);
String to_string(int val); String to_string(int val);