tgbotxx 1.2.9.2
Telegram Bot C++ Library
Loading...
Searching...
No Matches
StringUtils.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <random>
4#include <span>
5#include <sstream>
6#include <string>
7#include <vector>
8
9namespace tgbotxx {
11 namespace StringUtils {
17 template<typename T, typename D>
18 [[nodiscard]] static std::string join(std::span<T> con, const D& delim) {
19 std::ostringstream oss{};
20 auto it = con.begin();
21 if (it == con.end()) {
22 return {};
23 }
24 oss << *it;
25 ++it;
26 for (; it != con.end(); ++it) {
27 oss << delim << *it;
28 }
29 return oss.str();
30 }
31 template<typename T, typename D>
32 [[nodiscard]] static std::string join(const std::vector<T>& con, const D& delim) {
33 return join(std::span{con}, delim);
34 }
35
40 [[nodiscard]] static std::vector<std::string> split(const std::string& str, char delim) {
41 std::vector<std::string> res;
42 std::stringstream ss{str};
43 std::string chunk;
44 while (std::getline(ss, chunk, delim))
45 res.push_back(chunk);
46 return res;
47 }
48
52 [[nodiscard]] static std::string toLowerCopy(std::string str) {
53 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
54 return str;
55 }
56
59 static void toLower(std::string& str) {
60 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
61 }
62
66 [[nodiscard]] static std::string toUpperCopy(std::string str) {
67 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
68 return str;
69 }
70
73 static void toUpper(std::string& str) {
74 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
75 }
76
79 static void ltrim(std::string& str) {
80 str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) {
81 return !std::isspace(ch);
82 }));
83 }
84
87 static void rtrim(std::string& str) {
88 str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
89 return !std::isspace(ch);
90 }).base(),
91 str.end());
92 }
93
94
97 static void trim(std::string& str) {
98 rtrim(str);
99 ltrim(str);
100 }
101
105 [[nodiscard]] static std::string ltrimCopy(std::string s) {
106 ltrim(s);
107 return s;
108 }
109
113 [[nodiscard]] static std::string rtrimCopy(std::string s) {
114 rtrim(s);
115 return s;
116 }
117
121 [[nodiscard]] static std::string trimCopy(std::string s) {
122 trim(s);
123 return s;
124 }
125
131 [[nodiscard]] static bool startsWith(const std::string& str, const std::string& prefix, bool ignoreCase = false) {
132 if (str.size() < prefix.size()) return false;
133 if (ignoreCase)
134 return std::equal(prefix.begin(), prefix.end(), str.begin(),
135 [](char a, char b) { return std::tolower(a) == std::tolower(b); });
136 return str.compare(0, prefix.size(), prefix) == 0;
137 }
138
144 [[nodiscard]] static bool endsWith(const std::string& str, const std::string& suffix, bool ignoreCase = false) {
145 if (str.size() < suffix.size()) return false;
146 if (ignoreCase)
147 return std::equal(suffix.begin(), suffix.end(), str.begin() + str.size() - suffix.size(),
148 [](char a, char b) { return std::tolower(a) == std::tolower(b); });
149 return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
150 }
151
156 static void replace(std::string& str, const std::string& search, const std::string& replace) {
157 if (search.empty()) return; // Avoid infinite loops
158 std::size_t pos{0};
159 while ((pos = str.find(search, pos)) != std::string::npos) {
160 str.replace(pos, search.length(), replace);
161 pos += replace.length();
162 }
163 }
164
165
171 [[nodiscard]] static std::string replaceCopy(std::string str, const std::string& search, const std::string& replace) {
172 if (search.empty()) return str; // Avoid infinite loops
173 std::size_t pos{0};
174 while ((pos = str.find(search, pos)) != std::string::npos) {
175 str.replace(pos, search.length(), replace);
176 pos += replace.length();
177 }
178 return str;
179 }
180
185 template<typename T>
186 [[nodiscard]] static T to(const std::string& str) {
187 T v{};
188 std::istringstream oss{str};
189 oss >> v;
190 return v;
191 }
192
194 [[nodiscard]] static std::string random(std::size_t length) {
195 static std::random_device seed{};
196 static std::default_random_engine engine{seed()};
197 static std::uniform_int_distribution<short> choice(0, 2);
198 static std::uniform_int_distribution<int> lowercaseAlpha('a', 'z');
199 static std::uniform_int_distribution<int> uppercaseAlpha('A', 'Z');
200 static std::uniform_int_distribution<int> digits('0', '9');
201
202 std::string str(length, '\000');
203 for (char& c: str) {
204 switch (choice(engine)) {
205 case 0: // a-z
206 c = static_cast<char>(lowercaseAlpha(engine));
207 break;
208 case 1: // A-Z
209 c = static_cast<char>(uppercaseAlpha(engine));
210 break;
211 case 2: // 0-9
212 c = static_cast<char>(digits(engine));
213 break;
214 default:
215 break;
216 }
217 }
218 return str;
219 }
220 };
221}
static bool startsWith(const std::string &str, const std::string &prefix, bool ignoreCase=false)
Returns true if str starts with prefix.
static void toLower(std::string &str)
Lowercase original string str.
static std::string toUpperCopy(std::string str)
Uppercase a string.
static void rtrim(std::string &str)
Right trim a string from end (in place)
static std::string replaceCopy(std::string str, const std::string &search, const std::string &replace)
static std::string trimCopy(std::string s)
Left and Right trim a string from start and end (copy)
static std::string join(std::span< T > con, const D &delim)
Form a string from a container with a delimiter.
static void trim(std::string &str)
Left and right trim a string (from beginning and end) (in place)
static std::vector< std::string > split(const std::string &str, char delim)
Split a string by delimiter.
static void replace(std::string &str, const std::string &search, const std::string &replace)
static std::string random(std::size_t length)
Returns a random string of length characters.
static std::string ltrimCopy(std::string s)
Left trim a string from start (copy)
static void toUpper(std::string &str)
Uppercase original string str.
static void ltrim(std::string &str)
Left trim a string from start (in place)
static std::string rtrimCopy(std::string s)
Right trim a string from end (copy)
static bool endsWith(const std::string &str, const std::string &suffix, bool ignoreCase=false)
Returns true if str ends with suffix.
static std::string toLowerCopy(std::string str)
Lowercase a string.
static T to(const std::string &str)
std::shared_ptr< T > Ptr
Definition Ptr.hpp:6