-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.hh
118 lines (89 loc) · 3.05 KB
/
string.hh
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// -*- mode: c++; c-basic-offset: 2; -*-
#pragma once
/**
* @file string.hh
* @date Sep 23, 2021
* @brief Usefult string functions. Mostly resemble python's
*/
#include "nvsl/error.hh"
#include <regex>
#include <string>
#include <vector>
namespace nvsl {
/** @brief Split string using a delimeter into a vector of strings */
inline std::vector<std::string> split(const std::string &str,
const std::string &delim,
size_t assert_length = UINT64_MAX) {
std::vector<std::string> result;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty()) result.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
if (assert_length != UINT64_MAX) {
NVSL_ASSERT(result.size() == assert_length, "Not enough tokens");
}
return result;
}
/** @brief Concat all the elements of a string vector into a stingle string */
inline auto zip(const std::vector<std::string> arr,
const std::string join_str) {
std::string result = "";
size_t iter = 0;
for (const auto &tok : arr) {
result += tok;
if (iter++ != arr.size() - 1) result += join_str;
}
return result;
}
/** @brief Checks if a string is suffix of another string */
inline auto is_suffix(const std::string &suffix, const std::string &str) {
auto mismatch =
std::mismatch(suffix.rbegin(), suffix.rend(), str.rbegin()).first;
return mismatch == suffix.rend();
}
/** @brief Checks if a string is prefix of another string */
inline bool is_prefix(const std::string &prefix, const std::string &str) {
auto mismatch =
std::mismatch(prefix.begin(), prefix.end(), str.begin()).first;
return mismatch == prefix.end();
}
template <typename T>
inline const std::string S(T val) {
return std::to_string(val);
}
template <>
inline const std::string S(void *val) {
std::stringstream ss;
ss << val;
return ss.str();
}
template <>
inline const std::string S(const char *c) {
return std::string(c);
}
template <>
inline const std::string S(char *c) {
return std::string(c);
}
inline std::string ltrim(const std::string &str) {
const auto start = str.find_first_not_of(" \t\n");
if (start == std::string::npos) return str;
return str.substr(start, str.size() - start);
}
inline std::string rtrim(const std::string &str) {
const auto end = str.find_last_not_of(" \t\n");
if (end == std::string::npos) return str;
return str.substr(0, end + 1);
}
inline std::string replace(const std::string &str, const std::string &substr,
const std::string &replacement) {
return std::regex_replace(str, std::regex(substr), replacement);
}
inline std::string trim(const std::string &str) {
return ltrim(rtrim(str));
}
} // namespace nvsl