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 extract_substring(const std::string& str, const std::string& from, const std::string& to) -> std::string
50{
51 auto it_from = str.find(from);
52 if(it_from != std::string::npos)
53 {
54 auto result = str.substr(it_from + from.size(), std::string::npos);
55 auto it_to = result.find_first_of(to);
56 if(it_to != std::string::npos)
57 {
58 result = result.substr(0, it_to);
59 alterable::trim(result);
60 return result;
61 }
62 }
63
64 return {};
65}
66
67namespace alterable
68{
69void ltrim(std::string& str)
70{
71 str.erase(str.begin(),
72 std::find_if(str.begin(),
73 str.end(),
74 [](unsigned char ch)
75 {
76 return !std::isspace(ch);
77 }));
78}
79
80void rtrim(std::string& str)
81{
82 str.erase(std::find_if(str.rbegin(),
83 str.rend(),
84 [](unsigned char ch)
85 {
86 return !std::isspace(ch);
87 })
88 .base(),
89 str.end());
90}
91
92void trim(std::string& str)
93{
96}
97
98void replace(std::string& str, const std::string& search, const std::string& replace)
99{
100 if(search.empty())
101 {
102 return;
103 }
104
105 size_t pos = 0;
106 while((pos = str.find(search, pos)) != std::string::npos)
107 {
108 str.replace(pos, search.length(), replace);
109 pos += replace.length();
110 }
111}
112
113void to_upper(std::string& str)
114{
115 std::transform(std::begin(str),
116 std::end(str),
117 std::begin(str),
118 [](unsigned char c)
119 {
120 return std::toupper(c);
121 });
122}
123
124void to_lower(std::string& str)
125{
126 std::transform(std::begin(str),
127 std::end(str),
128 std::begin(str),
129 [](unsigned char c)
130 {
131 return std::tolower(c);
132 });
133}
134}
135
136auto tokenize(const std::string& str, const std::string& delimiters) -> string_tokens_t
137{
138 string_tokens_t tokens;
139 // Skip delimiters at beginning.
140 auto last_pos = str.find_first_not_of(delimiters, 0);
141 // Find first "non-delimiter".
142 auto pos = str.find_first_of(delimiters, last_pos);
143
144 while(std::string::npos != pos || std::string::npos != last_pos)
145 {
146 // Found a token, add it to the vector.
147 tokens.emplace_back(str.substr(last_pos, pos - last_pos));
148 // Skip delimiters. Note the "not_of"
149 last_pos = str.find_first_not_of(delimiters, pos);
150 // Find next "non-delimiter"
151 pos = str.find_first_of(delimiters, last_pos);
152 }
153
154 return tokens;
155}
156
157// namespace alterable
158
159// namespace alterable
160
161} // namespace string_utils
void rtrim(std::string &str)
Definition utils.cpp:80
void replace(std::string &str, const std::string &search, const std::string &replace)
Definition utils.cpp:98
void to_lower(std::string &str)
Definition utils.cpp:124
void trim(std::string &str)
Definition utils.cpp:92
void to_upper(std::string &str)
Definition utils.cpp:113
void ltrim(std::string &str)
Definition utils.cpp:69
auto tokenize(const std::string &str, const std::string &delimiters) -> string_tokens_t
Definition utils.cpp:136
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:49
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