Unravel Engine C++ Reference
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include "utils.h"
2#include <algorithm>
3
4namespace string_utils
5{
6
7auto ltrim(const std::string& str) -> std::string
8{
9 auto str_modified = str;
10 alterable::ltrim(str_modified);
11 return str_modified;
12}
13
14auto rtrim(const std::string& str) -> std::string
15{
16 auto str_modified = str;
17 alterable::rtrim(str_modified);
18 return str_modified;
19}
20
21auto trim(const std::string& str) -> std::string
22{
23 auto str_modified = str;
24 alterable::trim(str_modified);
25 return str_modified;
26}
27
28auto replace(const std::string& str, const std::string& search, const std::string& replace) -> std::string
29{
30 auto str_modified = str;
31 alterable::replace(str_modified, search, replace);
32 return str_modified;
33}
34
35auto to_upper(const std::string& str) -> std::string
36{
37 auto str_modified = str;
38 alterable::to_upper(str_modified);
39 return str_modified;
40}
41
42auto to_lower(const std::string& str) -> std::string
43{
44 auto str_modified = str;
45 alterable::to_lower(str_modified);
46 return str_modified;
47}
48
49auto capitalize(const std::string& word) -> std::string
50{
51 auto str_modified = word;
52 alterable::capitalize(str_modified);
53 return str_modified;
54}
55
56auto extract_substring(const std::string& str, const std::string& from, const std::string& to) -> std::string
57{
58 auto it_from = str.find(from);
59 if(it_from != std::string::npos)
60 {
61 auto result = str.substr(it_from + from.size(), std::string::npos);
62 auto it_to = result.find_first_of(to);
63 if(it_to != std::string::npos)
64 {
65 result = result.substr(0, it_to);
66 alterable::trim(result);
67 return result;
68 }
69 }
70
71 return {};
72}
73
74namespace alterable
75{
76void ltrim(std::string& str)
77{
78 str.erase(str.begin(),
79 std::find_if(str.begin(),
80 str.end(),
81 [](unsigned char ch)
82 {
83 return !std::isspace(ch);
84 }));
85}
86
87void rtrim(std::string& str)
88{
89 str.erase(std::find_if(str.rbegin(),
90 str.rend(),
91 [](unsigned char ch)
92 {
93 return !std::isspace(ch);
94 })
95 .base(),
96 str.end());
97}
98
99void trim(std::string& str)
100{
101 alterable::ltrim(str);
102 alterable::rtrim(str);
103}
104
105void replace(std::string& str, const std::string& search, const std::string& replace)
106{
107 if(search.empty())
108 {
109 return;
110 }
111
112 size_t pos = 0;
113 while((pos = str.find(search, pos)) != std::string::npos)
114 {
115 str.replace(pos, search.length(), replace);
116 pos += replace.length();
117 }
118}
119
120void to_upper(std::string& str)
121{
122 std::transform(std::begin(str),
123 std::end(str),
124 std::begin(str),
125 [](unsigned char c)
126 {
127 return std::toupper(c);
128 });
129}
130
131void to_lower(std::string& str)
132{
133 std::transform(std::begin(str),
134 std::end(str),
135 std::begin(str),
136 [](unsigned char c)
137 {
138 return std::tolower(c);
139 });
140}
141
142void capitalize(std::string& str)
143{
144 if (str.empty())
145 {
146 return;
147 }
148
149 str[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(str[0])));
150}
151}
152
153auto tokenize(const std::string& str, const std::string& delimiters) -> string_tokens_t
154{
155 string_tokens_t tokens;
156 // Skip delimiters at beginning.
157 auto last_pos = str.find_first_not_of(delimiters, 0);
158 // Find first "non-delimiter".
159 auto pos = str.find_first_of(delimiters, last_pos);
160
161 while(std::string::npos != pos || std::string::npos != last_pos)
162 {
163 // Found a token, add it to the vector.
164 tokens.emplace_back(str.substr(last_pos, pos - last_pos));
165 // Skip delimiters. Note the "not_of"
166 last_pos = str.find_first_not_of(delimiters, pos);
167 // Find next "non-delimiter"
168 pos = str.find_first_of(delimiters, last_pos);
169 }
170
171 return tokens;
172}
173
174// namespace alterable
175
176// namespace alterable
177
178} // namespace string_utils
void rtrim(std::string &str)
Definition utils.cpp:87
void replace(std::string &str, const std::string &search, const std::string &replace)
Definition utils.cpp:105
void to_lower(std::string &str)
Definition utils.cpp:131
void capitalize(std::string &str)
Definition utils.cpp:142
void trim(std::string &str)
Definition utils.cpp:99
void to_upper(std::string &str)
Definition utils.cpp:120
void ltrim(std::string &str)
Definition utils.cpp:76
auto capitalize(const std::string &word) -> std::string
Definition utils.cpp:49
auto tokenize(const std::string &str, const std::string &delimiters) -> string_tokens_t
Definition utils.cpp:153
auto replace(const std::string &str, const std::string &search, const std::string &replace) -> std::string
Definition utils.cpp:28
auto extract_substring(const std::string &str, const std::string &from, const std::string &to) -> std::string
Definition utils.cpp:56
std::vector< std::string > string_tokens_t
Definition utils.h:9
auto to_lower(const std::string &str) -> std::string
Definition utils.cpp:42
auto trim(const std::string &str) -> std::string
Definition utils.cpp:21
auto ltrim(const std::string &str) -> std::string
Definition utils.cpp:7
auto to_upper(const std::string &str) -> std::string
Definition utils.cpp:35
auto rtrim(const std::string &str) -> std::string
Definition utils.cpp:14