1 #ifndef BTLLIB_UTIL_HPP
2 #define BTLLIB_UTIL_HPP
10 inline std::vector<std::string>
11 split(
const std::string& s,
const std::string& delim);
13 ltrim(std::string& s);
15 rtrim(std::string& s);
19 starts_with(std::string s, std::string prefix);
21 ends_with(std::string s, std::string suffix);
23 inline std::vector<std::string>
24 split(
const std::string& s,
const std::string& delim)
26 std::vector<std::string> tokens;
27 size_t pos1 = 0, pos2 = 0;
28 while ((pos2 = s.find(delim, pos2)) != std::string::npos) {
29 tokens.push_back(s.substr(pos1, pos2 - pos1));
33 tokens.push_back(s.substr(pos1));
40 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int ch) {
41 return !bool(std::isspace(ch));
48 s.erase(std::find_if(s.rbegin(),
50 [](
int ch) { return !bool(std::isspace(ch)); })
63 starts_with(std::string s, std::string prefix)
65 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
66 std::transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower);
67 return s.find(prefix) == 0;
71 ends_with(std::string s, std::string suffix)
73 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
74 std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
75 auto pos = s.rfind(suffix);
76 return (pos != std::string::npos) && (pos == s.size() - suffix.size());