MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
str.h
1// @file str.h
2
3/* Copyright 2010 10gen Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
27#include <string>
28#include <sstream>
29
30#include "mongo/bson/util/builder.h"
31
32namespace mongoutils {
33
34namespace str {
35
44class stream {
45public:
47 template <class T>
48 stream& operator<<(const T& v) {
49 ss << v;
50 return *this;
51 }
52 operator std::string() const {
53 return ss.str();
54 }
55};
56
57inline bool startsWith(const char* str, const char* prefix) {
58 const char* s = str;
59 const char* p = prefix;
60 while (*p) {
61 if (*p != *s)
62 return false;
63 p++;
64 s++;
65 }
66 return true;
67}
68inline bool startsWith(const std::string& s, const std::string& p) {
69 return startsWith(s.c_str(), p.c_str());
70}
71
72// while these are trivial today use in case we do different wide char things later
73inline bool startsWith(const char* p, char ch) {
74 return *p == ch;
75}
76inline bool startsWith(const std::string& s, char ch) {
77 return startsWith(s.c_str(), ch);
78}
79
80inline bool endsWith(const std::string& s, const std::string& p) {
81 int l = p.size();
82 int x = s.size();
83 if (x < l)
84 return false;
85 return strncmp(s.c_str() + x - l, p.c_str(), l) == 0;
86}
87inline bool endsWith(const char* s, char p) {
88 size_t len = strlen(s);
89 return len && s[len - 1] == p;
90}
91inline bool endsWith(const char* p, const char* suffix) {
92 size_t a = strlen(p);
93 size_t b = strlen(suffix);
94 if (b > a)
95 return false;
96 return strcmp(p + a - b, suffix) == 0;
97}
98
99inline bool equals(const char* a, const char* b) {
100 return strcmp(a, b) == 0;
101}
102
104inline const char* after(const char* s, char x) {
105 const char* p = strchr(s, x);
106 return (p != 0) ? p + 1 : "";
107}
108inline std::string after(const std::string& s, char x) {
109 const char* p = strchr(s.c_str(), x);
110 return (p != 0) ? std::string(p + 1) : "";
111}
112
114inline const char* after(const char* s, const char* x) {
115 const char* p = strstr(s, x);
116 return (p != 0) ? p + strlen(x) : "";
117}
118inline std::string after(const std::string& s, const std::string& x) {
119 const char* p = strstr(s.c_str(), x.c_str());
120 return (p != 0) ? std::string(p + x.size()) : "";
121}
122
126inline bool contains(const std::string& s, const std::string& x) {
127 return strstr(s.c_str(), x.c_str()) != 0;
128}
129inline bool contains(const std::string& s, char x) {
130 verify(x != '\0'); // this expects c-strings so don't use when looking for NUL bytes
131 return strchr(s.c_str(), x) != 0;
132}
133
135inline std::string before(const std::string& s, char x) {
136 const char* p = strchr(s.c_str(), x);
137 return (p != 0) ? s.substr(0, p - s.c_str()) : s;
138}
139
141inline std::string before(const std::string& s, const std::string& x) {
142 const char* p = strstr(s.c_str(), x.c_str());
143 return (p != 0) ? s.substr(0, p - s.c_str()) : s;
144}
145
148inline int shareCommonPrefix(const char* p, const char* q) {
149 int ofs = 0;
150 while (1) {
151 if (*p == 0 || *q == 0)
152 break;
153 if (*p != *q)
154 break;
155 p++;
156 q++;
157 ofs++;
158 }
159 return ofs;
160}
161inline int shareCommonPrefix(const std::string& a, const std::string& b) {
162 return shareCommonPrefix(a.c_str(), b.c_str());
163}
164
166inline unsigned toUnsigned(const std::string& a) {
167 unsigned x = 0;
168 const char* p = a.c_str();
169 while (1) {
170 if (!isdigit(*p))
171 break;
172 x = x * 10 + (*p - '0');
173 p++;
174 }
175 return x;
176}
177
183inline bool splitOn(const std::string& s, char c, std::string& L, std::string& R) {
184 const char* start = s.c_str();
185 const char* p = strchr(start, c);
186 if (p == 0) {
187 L = s;
188 R.clear();
189 return false;
190 }
191 L = std::string(start, p - start);
192 R = std::string(p + 1);
193 return true;
194}
196inline bool rSplitOn(const std::string& s, char c, std::string& L, std::string& R) {
197 const char* start = s.c_str();
198 const char* p = strrchr(start, c);
199 if (p == 0) {
200 L = s;
201 R.clear();
202 return false;
203 }
204 L = std::string(start, p - start);
205 R = std::string(p + 1);
206 return true;
207}
208
210inline unsigned count(const std::string& s, char c) {
211 unsigned n = 0;
212 for (unsigned i = 0; i < s.size(); i++)
213 if (s[i] == c)
214 n++;
215 return n;
216}
217
219inline std::string ltrim(const std::string& s) {
220 const char* p = s.c_str();
221 while (*p == ' ')
222 p++;
223 return p;
224}
225
227inline void stripTrailing(std::string& s, const char* chars) {
228 std::string::iterator to = s.begin();
229 for (std::string::iterator i = s.begin(); i != s.end(); i++) {
230 // During each iteration if i finds a non-"chars" character it writes it to the
231 // position of t. So the part of the string left from the "to" iterator is already
232 // "cleared" string.
233 if (!contains(chars, *i)) {
234 if (i != to)
235 s.replace(to, to + 1, 1, *i);
236 to++;
237 }
238 }
239 s.erase(to, s.end());
240}
241
242} // namespace str
243
244} // namespace mongoutils
245
246namespace mongo {
247using namespace mongoutils;
248
249#if defined(_WIN32)
250inline int strcasecmp(const char* s1, const char* s2) {
251 return _stricmp(s1, s2);
252}
253#endif
254
255} // namespace mongo
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8
Definition builder.h:325
the idea here is to make one liners easy.
Definition str.h:44
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:32
String utilities.
Definition str.h:32