-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_utils.cc
140 lines (120 loc) · 3.61 KB
/
string_utils.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Program
// Handy function to manipulate strings.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o string_utils string_utils.cc
//
// Execution
// ./string_utils
//
#include <iostream>
#include <vector>
#include <algorithm>
bool starts_with(std::string_view input, std::string_view target) {
return input.find(target) == 0;
}
bool ends_with(std::string_view input, std::string_view target) {
auto pos = input.length() - target.length();
return (input.find(target, pos) == pos);
}
void ltrim(std::string& input) {
auto space_end = std::find_if(std::begin(input), std::end(input),
[](const auto& ch) {
return (!std::isspace(ch));
}
);
input.erase(std::begin(input), space_end);
}
void rtrim(std::string& input) {
auto rit = std::find_if(input.rbegin(), input.rend(),
[](const auto& ch) {
return !(std::isspace(ch));
}
);
input.erase(rit.base(), input.end());
}
void trim(std::string& input) {
ltrim(input);
rtrim(input);
}
void tokenize(std::string_view input, const char target, std::vector<std::string>& token_list) {
using size_type = std::string::size_type;
size_type last_pos = 0;
size_type pos = input.find(target, last_pos);
while (pos != std::string::npos) {
token_list.emplace_back(input.substr(last_pos, pos - last_pos));
// Skip the character
last_pos = pos + 1;
pos = input.find(target, last_pos);
}
// Copy the left over part
auto rem = input.length() - last_pos;
if (rem > 0) {
token_list.emplace_back(input.substr(last_pos));
}
}
//
// Entry function
//
int main() {
// Test starts_with
{
std::cout << "Test # starts_with" << '\n';
std::cout << std::boolalpha << starts_with("testtesting", "te") << '\n';
std::cout << std::boolalpha << starts_with("testtesting", "ta") << '\n';
std::cout << std::boolalpha << starts_with("testtesting", "xy") << '\n';
}
{
std::cout << "Test # ends_with" << '\n';
std::cout << std::boolalpha << ends_with("test", "t") << '\n';
std::cout << std::boolalpha << ends_with("test", "est") << '\n';
std::cout << std::boolalpha << ends_with("test", "g") << '\n';
}
{
std::cout << "Test # ltrim" << '\n';
std::vector<std::string> strings {" test", "test ", " test ", " test test "};
for (auto str: strings) {
std::cout << "[" << str << "]";
ltrim(str);
std::cout << "[" << str << "]" << '\n';
}
}
{
std::cout << "Test # rtrim" << '\n';
std::vector<std::string> strings {" test", "test ", " test ", " test test "};
for (auto str: strings) {
std::cout << "[" << str << "]";
rtrim(str);
std::cout << "[" << str << "]" << '\n';
}
}
{
std::cout << "Test # tokenize" << '\n';
{
std::vector<std::string> token_list;
std::string input("first,second,third");
tokenize(input, ',', token_list);
std::cout << '\n' << "Show tokens of: " << input << '\n';
for (const auto& tok: token_list)
std::cout << "[" << tok << "]" << '\n';
}
{
std::vector<std::string> token_list;
std::string input("first,second,third,");
tokenize(input, ',', token_list);
std::cout << '\n' << "Show tokens of: " << input << '\n';
for (const auto& tok: token_list)
std::cout << "[" << tok << "]" << '\n';
}
{
std::vector<std::string> token_list;
std::string input("first,second,third,fourth,, ,");
tokenize(input, ',', token_list);
std::cout << '\n' << "Show tokens of: " << input << '\n';
for (const auto& tok: token_list)
std::cout << "[" << tok << "]" << '\n';
}
}
return 0;
}