summaryrefslogtreecommitdiff
path: root/src/string.cc
blob: 2152cb15abb1ecf7a48a4a29844d1636a624433b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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, char 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;
}

String String::replace(const String& expression,
                       const String& replacement) const
{
   boost::regex re(expression.m_content);
   return String(boost::regex_replace(m_content, re, replacement.m_content));
}

}