2012-05-29 09:19:50 +04:00
|
|
|
#include "string.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
#include "exception.hh"
|
2012-05-29 09:19:50 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-07-25 00:37:17 +04:00
|
|
|
std::vector<String> split(const String& str, char separator, char escape)
|
2012-05-29 09:19:50 +04:00
|
|
|
{
|
|
|
|
auto begin = str.begin();
|
|
|
|
auto end = str.begin();
|
|
|
|
|
|
|
|
std::vector<String> res;
|
|
|
|
while (end != str.end())
|
|
|
|
{
|
2013-07-25 00:37:17 +04:00
|
|
|
res.emplace_back();
|
|
|
|
String& element = res.back();
|
|
|
|
while (end != str.end())
|
|
|
|
{
|
|
|
|
auto c = *end;
|
|
|
|
if (c == escape and end + 1 != end and *(end+1) == separator)
|
|
|
|
{
|
|
|
|
element += separator;
|
|
|
|
end += 2;
|
|
|
|
}
|
|
|
|
else if (c == separator)
|
|
|
|
{
|
|
|
|
++end;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
element += c;
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
begin = end;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String escape(const String& str, char character, char escape)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (auto& c : str)
|
|
|
|
{
|
|
|
|
if (c == character)
|
|
|
|
res += escape;
|
|
|
|
res += c;
|
2012-05-29 09:19:50 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-17 16:09:42 +04:00
|
|
|
int str_to_int(const String& str)
|
|
|
|
{
|
2013-06-19 00:11:44 +04:00
|
|
|
int res = 0;
|
|
|
|
if (sscanf(str.c_str(), "%i", &res) != 1)
|
2013-05-17 16:09:42 +04:00
|
|
|
throw runtime_error(str + "is not a number");
|
2013-06-19 00:11:44 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String to_string(int val)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%i", val);
|
|
|
|
return buf;
|
2013-05-17 16:09:42 +04:00
|
|
|
}
|
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
String option_to_string(const Regex& re)
|
|
|
|
{
|
|
|
|
return String{re.str()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void option_from_string(const String& str, Regex& re)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
re = Regex{str.begin(), str.end()};
|
|
|
|
}
|
|
|
|
catch (boost::regex_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error("unable to create regex: "_str + err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
bool prefix_match(StringView str, StringView prefix)
|
2013-09-23 23:16:25 +04:00
|
|
|
{
|
|
|
|
auto it = str.begin();
|
|
|
|
for (auto& c : prefix)
|
|
|
|
{
|
|
|
|
if (it ==str.end() or *it++ != c)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
bool subsequence_match(StringView str, StringView subseq)
|
2013-09-23 23:16:57 +04:00
|
|
|
{
|
|
|
|
auto it = str.begin();
|
|
|
|
for (auto& c : subseq)
|
|
|
|
{
|
|
|
|
if (it == str.end())
|
|
|
|
return false;
|
|
|
|
while (*it != c)
|
|
|
|
{
|
|
|
|
if (++it == str.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-29 09:19:50 +04:00
|
|
|
}
|