home/src/string.cc
Maxime Coste ecb3a737a9 Tweak String class, and cleanup usages
remove String::clear, add operator=(String&&) add str_to_int(const String&)
to replace atoi.
2012-06-27 14:26:29 +02:00

52 lines
896 B
C++

#include "string.hh"
namespace Kakoune
{
String int_to_str(int value)
{
const bool negative = value < 0;
if (negative)
value = -value;
char buffer[16];
size_t pos = sizeof(buffer);
buffer[--pos] = 0;
do
{
buffer[--pos] = '0' + (value % 10);
value /= 10;
}
while (value);
if (negative)
buffer[--pos] = '-';
return String(buffer + pos);
}
int str_to_int(const String& str)
{
return atoi(str.c_str());
}
std::vector<String> split(const String& str, Character separator)
{
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;
}
}