tgbotxx  1.1.6.9
Telegram Bot C++ Library
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 
9 namespace tgbotxx {
11  namespace StringUtils {
17  template<typename T, typename D>
18  [[nodiscard]] static std::string join(const std::span<T>& con, const D& delim) {
19  std::ostringstream oss{};
20  for (std::size_t i = 0; i < con.size(); ++i) {
21  oss << con[i];
22  if (i != con.size() - 1) {
23  oss << delim;
24  }
25  }
26  return oss.str();
27  }
28  template<typename T, typename D>
29  [[nodiscard]] static std::string join(const std::vector<T>& con, const D& delim) {
30  return join(std::span{con}, delim);
31  }
32 
37  [[nodiscard]] static std::vector<std::string> split(const std::string& str, char delim) {
38  std::vector<std::string> res;
39  std::stringstream ss{str};
40  std::string chunk;
41  while (std::getline(ss, chunk, delim))
42  res.push_back(chunk);
43  return res;
44  }
45 
49  [[nodiscard]] static std::string toLowerCopy(std::string str) {
50  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
51  return str;
52  }
53 
56  static void toLower(std::string& str) {
57  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
58  }
59 
63  [[nodiscard]] static std::string toUpperCopy(std::string str) {
64  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
65  return str;
66  }
67 
70  static void toUpper(std::string& str) {
71  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
72  }
73 
76  static void ltrim(std::string& str) {
77  str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) {
78  return !std::isspace(ch);
79  }));
80  }
81 
84  static void rtrim(std::string& str) {
85  str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
86  return !std::isspace(ch);
87  }).base(),
88  str.end());
89  }
90 
91 
94  static void trim(std::string& str) {
95  rtrim(str);
96  ltrim(str);
97  }
98 
102  [[nodiscard]] static std::string ltrimCopy(std::string s) {
103  ltrim(s);
104  return s;
105  }
106 
110  [[nodiscard]] static std::string rtrimCopy(std::string s) {
111  rtrim(s);
112  return s;
113  }
114 
118  [[nodiscard]] static std::string trimCopy(std::string s) {
119  trim(s);
120  return s;
121  }
122 
128  [[nodiscard]] static bool startsWith(const std::string& str, const std::string& prefix, bool ignoreCase = false) {
129  if (str.size() < prefix.size()) return false;
130  if (ignoreCase)
131  return std::equal(prefix.begin(), prefix.end(), str.begin(),
132  [](char a, char b) { return std::tolower(a) == std::tolower(b); });
133  return str.compare(0, prefix.size(), prefix) == 0;
134  }
135 
141  [[nodiscard]] static bool endsWith(const std::string& str, const std::string& suffix, bool ignoreCase = false) {
142  if (str.size() < suffix.size()) return false;
143  if (ignoreCase)
144  return std::equal(suffix.begin(), suffix.end(), str.begin() + str.size() - suffix.size(),
145  [](char a, char b) { return std::tolower(a) == std::tolower(b); });
146  return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
147  }
148 
153  static void replace(std::string& str, const std::string& search, const std::string& replace) {
154  if (search.empty()) return; // Avoid infinite loops
155  std::size_t pos{0};
156  while ((pos = str.find(search, pos)) != std::string::npos) {
157  str.replace(pos, search.length(), replace);
158  pos += replace.length();
159  }
160  }
161 
162 
168  [[nodiscard]] static std::string replaceCopy(std::string str, const std::string& search, const std::string& replace) {
169  if (search.empty()) return str; // Avoid infinite loops
170  std::size_t pos{0};
171  while ((pos = str.find(search, pos)) != std::string::npos) {
172  str.replace(pos, search.length(), replace);
173  pos += replace.length();
174  }
175  return str;
176  }
177 
182  template<typename T>
183  [[nodiscard]] static T to(const std::string& str) {
184  T v{};
185  std::istringstream oss{str};
186  oss >> v;
187  return v;
188  }
189 
191  [[nodiscard]] static std::string random(std::size_t length) {
192  static std::random_device seed{};
193  static std::default_random_engine engine{seed()};
194  static std::uniform_int_distribution<short> choice(0, 2);
195  static std::uniform_int_distribution<int> lowercaseAlpha('a', 'z');
196  static std::uniform_int_distribution<int> uppercaseAlpha('A', 'Z');
197  static std::uniform_int_distribution<int> digits('0', '9');
198 
199  std::string str(length, '\000');
200  for (char& c: str) {
201  switch (choice(engine)) {
202  case 0: // a-z
203  c = lowercaseAlpha(engine);
204  break;
205  case 1: // A-Z
206  c = uppercaseAlpha(engine);
207  break;
208  case 2: // 0-9
209  c = digits(engine);
210  break;
211  default:
212  break;
213  }
214  }
215  return str;
216  }
217  };
218 }
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.
Definition: StringUtils.hpp:56
static std::string toUpperCopy(std::string str)
Uppercase a string.
Definition: StringUtils.hpp:63
static std::string join(const std::span< T > &con, const D &delim)
Form a string from a container with a delimiter.
Definition: StringUtils.hpp:18
static void rtrim(std::string &str)
Right trim a string from end (in place)
Definition: StringUtils.hpp:84
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 void trim(std::string &str)
Left and right trim a string (from beginning and end) (in place)
Definition: StringUtils.hpp:94
static std::vector< std::string > split(const std::string &str, char delim)
Split a string by delimiter.
Definition: StringUtils.hpp:37
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.
Definition: StringUtils.hpp:70
static void ltrim(std::string &str)
Left trim a string from start (in place)
Definition: StringUtils.hpp:76
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.
Definition: StringUtils.hpp:49
static T to(const std::string &str)
Definition: Api.hpp:14