2012-05-29 07:19:50 +02:00
|
|
|
#include "string.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2013-03-29 19:31:06 +01:00
|
|
|
#include "exception.hh"
|
2012-05-29 07:19:50 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-10-01 20:20:08 +02:00
|
|
|
std::vector<String> split(const String& str, char separator)
|
2012-05-29 07:19:50 +02:00
|
|
|
{
|
|
|
|
auto begin = str.begin();
|
|
|
|
auto end = str.begin();
|
|
|
|
|
|
|
|
std::vector<String> res;
|
|
|
|
while (end != str.end())
|
|
|
|
{
|
|
|
|
while (end != str.end() and *end != separator)
|
|
|
|
++end;
|
|
|
|
res.push_back(String(begin, end));
|
|
|
|
if (end == str.end())
|
|
|
|
break;
|
|
|
|
begin = ++end;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:09:42 +02:00
|
|
|
int str_to_int(const String& str)
|
|
|
|
{
|
2013-06-18 22:11:44 +02:00
|
|
|
int res = 0;
|
|
|
|
if (sscanf(str.c_str(), "%i", &res) != 1)
|
2013-05-17 14:09:42 +02:00
|
|
|
throw runtime_error(str + "is not a number");
|
2013-06-18 22:11:44 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String to_string(int val)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%i", val);
|
|
|
|
return buf;
|
2013-05-17 14:09:42 +02:00
|
|
|
}
|
|
|
|
|
2013-03-29 19:31:06 +01: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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|