summaryrefslogtreecommitdiff
path: root/src/string.cc
blob: 2c180e50d3a6e879329bb7b1aa855e58cb5883f5 (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
#include "string.hh"

#include "exception.hh"

namespace Kakoune
{

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;
}

int str_to_int(const String& str)
{
    try
    {
        return stoi(str);
    }
    catch (std::logic_error&)
    {
        throw runtime_error(str + "is not a number");
    }
}

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());
    }
}

}