PDFxTMDLib  1.0.0
StringUtils.h
Go to the documentation of this file.
1 #pragma once
2 // https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
3 #include <algorithm>
4 #include <cctype>
5 #include <locale>
6 
7 namespace PDFxTMD
8 {
9 inline void ltrim(std::string &s)
10 {
11  s.erase(s.begin(),
12  std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
13 }
14 
15 inline void rtrim(std::string &s)
16 {
17  s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); })
18  .base(),
19  s.end());
20 }
21 inline void trim(std::string &s)
22 {
23  rtrim(s);
24  ltrim(s);
25 }
26 
27 inline std::string ltrim_copy(std::string s)
28 {
29  ltrim(s);
30  return s;
31 }
32 
33 inline std::string rtrim_copy(std::string s)
34 {
35  rtrim(s);
36  return s;
37 }
38 
39 inline std::string trim_copy(std::string s)
40 {
41  trim(s);
42  return s;
43 }
44 inline std::string ToLower(const std::string &str)
45 {
46  std::string lowerStr = str;
47  std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
48  return lowerStr;
49 }
50 
51 // Function to convert a string to uppercase
52 inline std::string ToUpper(const std::string &str)
53 {
54  std::string upperStr = str;
55  std::transform(upperStr.begin(), upperStr.end(), upperStr.begin(), ::toupper);
56  return upperStr;
57 }
58 inline bool StartsWith(const std::string &str, const std::string &prefix)
59 {
60  if (str.length() < prefix.length())
61  {
62  return false;
63  }
64  return str.compare(0, prefix.length(), prefix) == 0;
65 }
66 } // namespace PDFxTMD
Definition: AllFlavorsShape.h:14
std::string ToUpper(const std::string &str)
Definition: StringUtils.h:52
void rtrim(std::string &s)
Definition: StringUtils.h:15
std::string trim_copy(std::string s)
Definition: StringUtils.h:39
std::string rtrim_copy(std::string s)
Definition: StringUtils.h:33
@ s
Definition: PartonUtils.h:68
std::string ltrim_copy(std::string s)
Definition: StringUtils.h:27
std::string ToLower(const std::string &str)
Definition: StringUtils.h:44
void ltrim(std::string &s)
Definition: StringUtils.h:9
bool StartsWith(const std::string &str, const std::string &prefix)
Definition: StringUtils.h:58
void trim(std::string &s)
Definition: StringUtils.h:21