stig
/
src
/
ob
/
string.cc
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
#include "ob/string.hh"
#include <cstddef>
#include <string>
#include <vector>
#include <regex>
#include <optional>
#include <limits>
namespace OB::String
{
std::vector<std::string> split(std::string const& str, std::string const& delim, std::size_t times)
{
std::vector<std::string> vtok;
std::size_t start {0};
auto end = str.find(delim);
while ((times-- > 0) && (end != std::string::npos))
{
vtok.emplace_back(str.substr(start, end - start));
start = end + delim.length();
end = str.find(delim, start);
}
vtok.emplace_back(str.substr(start, end));
return vtok;
}
bool assert_rx(std::string str, std::regex rx)
{
std::smatch m;
if (std::regex_match(str, m, rx, std::regex_constants::match_not_null))
{
return true;
}
return false;
}
std::optional<std::vector<std::string>> match(std::string const& str, std::regex rx)
{
std::smatch m;
if (std::regex_match(str, m, rx, std::regex_constants::match_not_null))
{
std::vector<std::string> v;
for (auto const& e : m)
{
v.emplace_back(std::string(e));
}
return v;
}
return {};
}
} // namespace OB::String