CppCommon  1.0.4.1
C++ Common Library
string_utils.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 inline bool StringUtils::IsBlankInternal(char ch)
12 {
13  return std::isspace(ch);
14 }
15 
16 inline bool StringUtils::IsBlank(char ch)
17 {
18  return IsBlankInternal(ch);
19 }
20 
21 inline char StringUtils::ToLowerInternal(char ch)
22 {
23  return (char)std::tolower(ch);
24 }
25 
26 inline char StringUtils::ToLower(char ch)
27 {
28  return ToLowerInternal(ch);
29 }
30 
31 inline char StringUtils::ToUpperInternal(char ch)
32 {
33  return (char)std::toupper(ch);
34 }
35 
36 inline char StringUtils::ToUpper(char ch)
37 {
38  return ToUpperInternal(ch);
39 }
40 
41 inline std::string StringUtils::ToLower(std::string_view str)
42 {
43  std::string result(str);
44  Lower(result);
45  return result;
46 }
47 
48 inline std::string StringUtils::ToUpper(std::string_view str)
49 {
50  std::string result(str);
51  Upper(result);
52  return result;
53 }
54 
55 inline std::string& StringUtils::Lower(std::string& str)
56 {
57  std::transform(str.begin(), str.end(), str.begin(), ToLowerInternal);
58  return str;
59 }
60 
61 inline std::string& StringUtils::Upper(std::string& str)
62 {
63  std::transform(str.begin(), str.end(), str.begin(), ToUpperInternal);
64  return str;
65 }
66 
67 inline std::string& StringUtils::Trim(std::string& str)
68 {
69  return LTrim(RTrim(str));
70 }
71 
72 inline std::string StringUtils::RemoveBlank(std::string_view str)
73 {
74  std::string result(str);
75  RemoveBlank(result);
76  return result;
77 }
78 
79 inline std::string& StringUtils::RemoveBlank(std::string& str)
80 {
81  str.erase(std::remove_if(str.begin(), str.end(), IsBlankInternal), str.end());
82  return str;
83 }
84 
85 inline bool StringUtils::Contains(std::string_view str, const char ch)
86 {
87  return (str.find(ch) != std::string::npos);
88 }
89 
90 inline bool StringUtils::Contains(std::string_view str, const char* substr)
91 {
92  return (str.find(substr) != std::string::npos);
93 }
94 
95 inline bool StringUtils::Contains(std::string_view str, std::string_view substr)
96 {
97  return (str.find(substr) != std::string::npos);
98 }
99 
100 inline bool StringUtils::StartsWith(std::string_view str, std::string_view prefix)
101 {
102  return (str.size() >= prefix.size()) && (str.compare(0, prefix.size(), prefix) == 0);
103 }
104 
105 inline bool StringUtils::EndsWith(std::string_view str, std::string_view suffix)
106 {
107  return (str.size() >= suffix.size()) && (str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0);
108 }
109 
110 template <typename T>
111 inline std::string StringUtils::ToString(const T& value)
112 {
113  std::ostringstream ss;
114  ss << value;
115  return ss.str();
116 }
117 
118 template <>
119 inline std::string StringUtils::ToString(const int8_t& value)
120 {
121  std::ostringstream ss;
122  ss << (int32_t)value;
123  return ss.str();
124 }
125 
126 template <>
127 inline std::string StringUtils::ToString(const uint8_t& value)
128 {
129  std::ostringstream ss;
130  ss << (uint32_t)value;
131  return ss.str();
132 }
133 
134 template <typename T>
135 inline T StringUtils::FromString(std::string_view str)
136 {
137  T result;
138  std::istringstream(std::string(str)) >> result;
139  return result;
140 }
141 
142 template <>
143 inline int8_t StringUtils::FromString(std::string_view str)
144 {
145  int32_t result;
146  std::istringstream(std::string(str)) >> result;
147  return (int8_t)result;
148 }
149 
150 template <>
151 inline uint8_t StringUtils::FromString(std::string_view str)
152 {
153  uint32_t result;
154  std::istringstream(std::string(str)) >> result;
155  return (uint8_t)result;
156 }
157 
158 template <>
159 inline std::string StringUtils::FromString(std::string_view str)
160 {
161  return std::string(str);
162 }
163 
164 template <>
165 inline std::string_view StringUtils::FromString(std::string_view str)
166 {
167  return str;
168 }
169 
170 template <>
171 bool StringUtils::FromString(std::string_view str);
172 
173 } // namespace CppCommon
static std::string & Trim(std::string &str)
Trims space characters from the both sides of the given string.
static std::string & Lower(std::string &str)
Convert the given string to lower case.
static bool StartsWith(std::string_view str, std::string_view prefix)
Checks the given string for specific prefix.
static std::string & LTrim(std::string &str)
Trims space characters from the start of the given string.
static std::string ToString(const T &value)
Converts arbitrary datatypes into string using std::ostringstream.
static T FromString(std::string_view str)
Converts strings to arbitrary datatypes using std::istringstream.
static char ToLower(char ch)
Convert the given character to lower case.
static std::string & RTrim(std::string &str)
Trims space characters from the end of the given string.
static char ToUpper(char ch)
Convert the given character to UPPER case.
static bool IsBlank(char ch)
Is the given character blank (empty or contains only space characters)?
static bool Contains(std::string_view str, const char ch)
Is the given string contains the given character?
static std::string & Upper(std::string &str)
Convert the given string to UPPER case.
static bool EndsWith(std::string_view str, std::string_view suffix)
Checks the given string for specific suffix.
static std::string RemoveBlank(std::string_view str)
Remove blank characters from the given string.
C++ Common project definitions.
Definition: token_bucket.h:15