30 #include "mongo/bson/util/builder.h"
48 stream& operator<<(
const T& v) {
52 operator std::string()
const {
57 inline bool startsWith(
const char* str,
const char* prefix) {
59 const char* p = prefix;
68 inline bool startsWith(
const std::string& s,
const std::string& p) {
69 return startsWith(s.c_str(), p.c_str());
73 inline bool startsWith(
const char* p,
char ch) {
76 inline bool startsWith(
const std::string& s,
char ch) {
77 return startsWith(s.c_str(), ch);
80 inline bool endsWith(
const std::string& s,
const std::string& p) {
85 return strncmp(s.c_str() + x - l, p.c_str(), l) == 0;
87 inline bool endsWith(
const char* s,
char p) {
88 size_t len = strlen(s);
89 return len && s[len - 1] == p;
91 inline bool endsWith(
const char* p,
const char* suffix) {
93 size_t b = strlen(suffix);
96 return strcmp(p + a - b, suffix) == 0;
99 inline bool equals(
const char* a,
const char* b) {
100 return strcmp(a, b) == 0;
104 inline const char* after(
const char* s,
char x) {
105 const char* p = strchr(s, x);
106 return (p != 0) ? p + 1 :
"";
108 inline 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) :
"";
114 inline const char* after(
const char* s,
const char* x) {
115 const char* p = strstr(s, x);
116 return (p != 0) ? p + strlen(x) :
"";
118 inline 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()) :
"";
126 inline bool contains(
const std::string& s,
const std::string& x) {
127 return strstr(s.c_str(), x.c_str()) != 0;
129 inline bool contains(
const std::string& s,
char x) {
131 return strchr(s.c_str(), x) != 0;
135 inline 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;
141 inline 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;
148 inline int shareCommonPrefix(
const char* p,
const char* q) {
151 if (*p == 0 || *q == 0)
161 inline int shareCommonPrefix(
const std::string& a,
const std::string& b) {
162 return shareCommonPrefix(a.c_str(), b.c_str());
166 inline unsigned toUnsigned(
const std::string& a) {
168 const char* p = a.c_str();
172 x = x * 10 + (*p -
'0');
183 inline 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);
191 L = std::string(start, p - start);
192 R = std::string(p + 1);
196 inline 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);
204 L = std::string(start, p - start);
205 R = std::string(p + 1);
210 inline unsigned count(
const std::string& s,
char c) {
212 for (
unsigned i = 0; i < s.size(); i++)
219 inline std::string ltrim(
const std::string& s) {
220 const char* p = s.c_str();
227 inline 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++) {
233 if (!contains(chars, *i)) {
235 s.replace(to, to + 1, 1, *i);
239 s.erase(to, s.end());
250 inline int strcasecmp(
const char* s1,
const char* s2) {
251 return _stricmp(s1, s2);
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:53
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
the idea here is to make one liners easy.
Definition: str.h:44
String utilities.
Definition: str.h:32