Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string_util.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_STRING_UTIL_H_
20 #define PAGESPEED_KERNEL_BASE_STRING_UTIL_H_
21 
22 #include <cctype>
23 #include <cstddef>
24 #include <map>
25 #include <set>
26 #include <vector>
27 
28 #include "base/logging.h"
31 
32 
33 #include <cstdlib>
34 #include <string>
35 #include "base/strings/string_number_conversions.h"
36 #include "base/strings/string_piece.h"
37 #include "base/strings/string_util.h"
38 #include "base/strings/stringprintf.h"
39 
40 using base::StringAppendF;
41 using base::StringAppendV;
42 using base::SStringPrintf;
43 using base::StringPiece;
44 using base::StringPrintf;
45 
46 typedef StringPiece::size_type stringpiece_ssize_type;
47 
48 namespace strings {
49 inline bool StartsWith(StringPiece a, StringPiece b) {
50  return a.starts_with(b);
51 }
52 inline bool EndsWith(StringPiece a, StringPiece b) {
53  return a.ends_with(b);
54 }
55 }
56 
57 
60 #define STATIC_STRLEN(static_string) (arraysize(static_string) - 1)
61 
62 namespace net_instaweb {
63 
64 struct StringCompareInsensitive;
65 
66 typedef std::map<GoogleString, GoogleString> StringStringMap;
67 typedef std::map<GoogleString, int> StringIntMap;
68 typedef std::set<GoogleString> StringSet;
69 typedef std::set<GoogleString, StringCompareInsensitive> StringSetInsensitive;
70 typedef std::vector<GoogleString> StringVector;
71 typedef std::vector<StringPiece> StringPieceVector;
72 typedef std::vector<const GoogleString*> ConstStringStarVector;
73 typedef std::vector<GoogleString*> StringStarVector;
74 typedef std::vector<const char*> CharStarVector;
75 
76 inline GoogleString IntegerToString(int i) {
77  return base::IntToString(i);
78 }
79 
80 inline GoogleString UintToString(unsigned int i) {
81  return base::UintToString(i);
82 }
83 
84 inline GoogleString Integer64ToString(int64 i) {
85  return base::Int64ToString(i);
86 }
87 
88 inline GoogleString PointerToString(void* pointer) {
89  return StringPrintf("%p", pointer);
90 }
91 
94 inline bool StringToInt(const char* in, int* out) {
95  return base::StringToInt(in, out);
96 }
97 
98 inline bool StringToInt64(const char* in, int64* out) {
99  return base::StringToInt64(in, out);
100 }
101 
102 inline bool StringToInt(const GoogleString& in, int* out) {
103  return base::StringToInt(in, out);
104 }
105 
106 inline bool StringToInt64(const GoogleString& in, int64* out) {
107  return base::StringToInt64(in, out);
108 }
109 
110 
114 bool StringToDouble(const char* in, double* out);
115 
116 inline bool StringToDouble(GoogleString in, double* out) {
117  const char* in_c_str = in.c_str();
118  if (strlen(in_c_str) != in.size()) {
120  return false;
121  }
122  return StringToDouble(in_c_str, out);
123 }
124 
125 inline bool StringToDouble(StringPiece in, double* out) {
126  return StringToDouble(in.as_string(), out);
127 }
128 
132 StringPiece PieceAfterEquals(StringPiece piece);
133 
134 
135 GoogleString StrCat(StringPiece a, StringPiece b);
136 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c);
137 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d);
138 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
139  StringPiece e);
140 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
141  StringPiece e, StringPiece f);
142 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
143  StringPiece e, StringPiece f, StringPiece g);
144 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
145  StringPiece e, StringPiece f, StringPiece g, StringPiece h);
146 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
147  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
148  StringPiece i);
149 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
150  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
151  StringPiece i, StringPiece j);
152 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
153  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
154  StringPiece i, StringPiece j);
155 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
156  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
157  StringPiece i, StringPiece j, StringPiece k);
158 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
159  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
160  StringPiece i, StringPiece j, StringPiece k, StringPiece l);
161 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
162  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
163  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
164  StringPiece m);
165 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
166  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
167  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
168  StringPiece m, StringPiece n);
169 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
170  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
171  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
172  StringPiece m, StringPiece n, StringPiece o);
173 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
174  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
175  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
176  StringPiece m, StringPiece n, StringPiece o, StringPiece p);
177 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
178  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
179  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
180  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
181  StringPiece q);
182 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
183  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
184  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
185  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
186  StringPiece q, StringPiece r);
187 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
188  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
189  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
190  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
191  StringPiece q, StringPiece r, StringPiece s);
192 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
193  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
194  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
195  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
196  StringPiece q, StringPiece r, StringPiece s, StringPiece t);
197 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
198  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
199  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
200  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
201  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
202  StringPiece u);
203 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
204  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
205  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
206  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
207  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
208  StringPiece u, StringPiece v);
209 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
210  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
211  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
212  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
213  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
214  StringPiece u, StringPiece v, StringPiece w);
215 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
216  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
217  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
218  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
219  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
220  StringPiece u, StringPiece v, StringPiece w, StringPiece x);
221 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
222  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
223  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
224  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
225  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
226  StringPiece u, StringPiece v, StringPiece w, StringPiece x,
227  StringPiece y);
228 GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c, StringPiece d,
229  StringPiece e, StringPiece f, StringPiece g, StringPiece h,
230  StringPiece i, StringPiece j, StringPiece k, StringPiece l,
231  StringPiece m, StringPiece n, StringPiece o, StringPiece p,
232  StringPiece q, StringPiece r, StringPiece s, StringPiece t,
233  StringPiece u, StringPiece v, StringPiece w, StringPiece x,
234  StringPiece y, StringPiece z);
235 
236 namespace internal {
237 
239 GoogleString StrCatNineOrMore(const StringPiece* a1, ...);
240 
241 }
242 
244 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
245  StringPiece d, StringPiece e, StringPiece f,
246  StringPiece g, StringPiece h, StringPiece i) {
247  const StringPiece* null_stringpiece = NULL;
248  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i,
249  null_stringpiece);
250 }
251 
252 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
253  StringPiece d, StringPiece e, StringPiece f,
254  StringPiece g, StringPiece h, StringPiece i,
255  StringPiece j) {
256  const StringPiece* null_stringpiece = NULL;
257  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j,
258  null_stringpiece);
259 }
260 
261 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
262  StringPiece d, StringPiece e, StringPiece f,
263  StringPiece g, StringPiece h, StringPiece i,
264  StringPiece j, StringPiece k) {
265  const StringPiece* null_stringpiece = NULL;
266  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
267  null_stringpiece);
268 }
269 
270 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
271  StringPiece d, StringPiece e, StringPiece f,
272  StringPiece g, StringPiece h, StringPiece i,
273  StringPiece j, StringPiece k, StringPiece l) {
274  const StringPiece* null_stringpiece = NULL;
275  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
276  &l, null_stringpiece);
277 }
278 
279 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
280  StringPiece d, StringPiece e, StringPiece f,
281  StringPiece g, StringPiece h, StringPiece i,
282  StringPiece j, StringPiece k, StringPiece l,
283  StringPiece m) {
284  const StringPiece* null_stringpiece = NULL;
285  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
286  &l, &m, null_stringpiece);
287 }
288 
289 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
290  StringPiece d, StringPiece e, StringPiece f,
291  StringPiece g, StringPiece h, StringPiece i,
292  StringPiece j, StringPiece k, StringPiece l,
293  StringPiece m, StringPiece n) {
294  const StringPiece* null_stringpiece = NULL;
295  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
296  &l, &m, &n, null_stringpiece);
297 }
298 
299 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
300  StringPiece d, StringPiece e, StringPiece f,
301  StringPiece g, StringPiece h, StringPiece i,
302  StringPiece j, StringPiece k, StringPiece l,
303  StringPiece m, StringPiece n, StringPiece o) {
304  const StringPiece* null_stringpiece = NULL;
305  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
306  &l, &m, &n, &o, null_stringpiece);
307 }
308 
309 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
310  StringPiece d, StringPiece e, StringPiece f,
311  StringPiece g, StringPiece h, StringPiece i,
312  StringPiece j, StringPiece k, StringPiece l,
313  StringPiece m, StringPiece n, StringPiece o,
314  StringPiece p) {
315  const StringPiece* null_stringpiece = NULL;
316  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
317  &l, &m, &n, &o, &p, null_stringpiece);
318 }
319 
320 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
321  StringPiece d, StringPiece e, StringPiece f,
322  StringPiece g, StringPiece h, StringPiece i,
323  StringPiece j, StringPiece k, StringPiece l,
324  StringPiece m, StringPiece n, StringPiece o,
325  StringPiece p, StringPiece q) {
326  const StringPiece* null_stringpiece = NULL;
327  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
328  &l, &m, &n, &o, &p, &q, null_stringpiece);
329 }
330 
331 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
332  StringPiece d, StringPiece e, StringPiece f,
333  StringPiece g, StringPiece h, StringPiece i,
334  StringPiece j, StringPiece k, StringPiece l,
335  StringPiece m, StringPiece n, StringPiece o,
336  StringPiece p, StringPiece q, StringPiece r) {
337  const StringPiece* null_stringpiece = NULL;
338  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
339  &l, &m, &n, &o, &p, &q, &r,
340  null_stringpiece);
341 }
342 
343 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
344  StringPiece d, StringPiece e, StringPiece f,
345  StringPiece g, StringPiece h, StringPiece i,
346  StringPiece j, StringPiece k, StringPiece l,
347  StringPiece m, StringPiece n, StringPiece o,
348  StringPiece p, StringPiece q, StringPiece r,
349  StringPiece s) {
350  const StringPiece* null_stringpiece = NULL;
351  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
352  &l, &m, &n, &o, &p, &q, &r, &s,
353  null_stringpiece);
354 }
355 
356 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
357  StringPiece d, StringPiece e, StringPiece f,
358  StringPiece g, StringPiece h, StringPiece i,
359  StringPiece j, StringPiece k, StringPiece l,
360  StringPiece m, StringPiece n, StringPiece o,
361  StringPiece p, StringPiece q, StringPiece r,
362  StringPiece s, StringPiece t) {
363  const StringPiece* null_stringpiece = NULL;
364  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
365  &l, &m, &n, &o, &p, &q, &r, &s, &t,
366  null_stringpiece);
367 }
368 
369 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
370  StringPiece d, StringPiece e, StringPiece f,
371  StringPiece g, StringPiece h, StringPiece i,
372  StringPiece j, StringPiece k, StringPiece l,
373  StringPiece m, StringPiece n, StringPiece o,
374  StringPiece p, StringPiece q, StringPiece r,
375  StringPiece s, StringPiece t, StringPiece u) {
376  const StringPiece* null_stringpiece = NULL;
377  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
378  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u,
379  null_stringpiece);
380 }
381 
382 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
383  StringPiece d, StringPiece e, StringPiece f,
384  StringPiece g, StringPiece h, StringPiece i,
385  StringPiece j, StringPiece k, StringPiece l,
386  StringPiece m, StringPiece n, StringPiece o,
387  StringPiece p, StringPiece q, StringPiece r,
388  StringPiece s, StringPiece t, StringPiece u,
389  StringPiece v) {
390  const StringPiece* null_stringpiece = NULL;
391  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
392  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u, &v,
393  null_stringpiece);
394 }
395 
396 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
397  StringPiece d, StringPiece e, StringPiece f,
398  StringPiece g, StringPiece h, StringPiece i,
399  StringPiece j, StringPiece k, StringPiece l,
400  StringPiece m, StringPiece n, StringPiece o,
401  StringPiece p, StringPiece q, StringPiece r,
402  StringPiece s, StringPiece t, StringPiece u,
403  StringPiece v, StringPiece w) {
404  const StringPiece* null_stringpiece = NULL;
405  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
406  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u, &v,
407  &w, null_stringpiece);
408 }
409 
410 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
411  StringPiece d, StringPiece e, StringPiece f,
412  StringPiece g, StringPiece h, StringPiece i,
413  StringPiece j, StringPiece k, StringPiece l,
414  StringPiece m, StringPiece n, StringPiece o,
415  StringPiece p, StringPiece q, StringPiece r,
416  StringPiece s, StringPiece t, StringPiece u,
417  StringPiece v, StringPiece w, StringPiece x) {
418  const StringPiece* null_stringpiece = NULL;
419  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
420  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u, &v,
421  &w, &x, null_stringpiece);
422 }
423 
424 inline GoogleString StrCat(
425  StringPiece a, StringPiece b, StringPiece c, StringPiece d, StringPiece e,
426  StringPiece f, StringPiece g, StringPiece h, StringPiece i, StringPiece j,
427  StringPiece k, StringPiece l, StringPiece m, StringPiece n, StringPiece o,
428  StringPiece p, StringPiece q, StringPiece r, StringPiece s, StringPiece t,
429  StringPiece u, StringPiece v, StringPiece w, StringPiece x, StringPiece y) {
430  const StringPiece* null_stringpiece = NULL;
431  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
432  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u, &v,
433  &w, &x, &y, null_stringpiece);
434 }
435 
436 inline GoogleString StrCat(StringPiece a, StringPiece b, StringPiece c,
437  StringPiece d, StringPiece e, StringPiece f,
438  StringPiece g, StringPiece h, StringPiece i,
439  StringPiece j, StringPiece k, StringPiece l,
440  StringPiece m, StringPiece n, StringPiece o,
441  StringPiece p, StringPiece q, StringPiece r,
442  StringPiece s, StringPiece t, StringPiece u,
443  StringPiece v, StringPiece w, StringPiece x,
444  StringPiece y, StringPiece z) {
445  const StringPiece* null_stringpiece = NULL;
446  return internal::StrCatNineOrMore(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k,
447  &l, &m, &n, &o, &p, &q, &r, &s, &t, &u, &v,
448  &w, &x, &y, &z, null_stringpiece);
449 }
450 
451 inline void StrAppend(GoogleString* target, StringPiece a) {
452  a.AppendToString(target);
453 }
454 void StrAppend(GoogleString* target, StringPiece a, StringPiece b);
455 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
456  StringPiece c);
457 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
458  StringPiece c, StringPiece d);
459 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
460  StringPiece c, StringPiece d, StringPiece e);
461 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
462  StringPiece c, StringPiece d, StringPiece e, StringPiece f);
463 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
464  StringPiece c, StringPiece d, StringPiece e, StringPiece f,
465  StringPiece g);
466 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
467  StringPiece c, StringPiece d, StringPiece e, StringPiece f,
468  StringPiece g, StringPiece h);
469 void StrAppend(GoogleString* target, StringPiece a, StringPiece b,
470  StringPiece c, StringPiece d, StringPiece e, StringPiece f,
471  StringPiece g, StringPiece h, StringPiece i);
472 
475 void SplitStringPieceToVector(StringPiece sp, StringPiece separators,
476  StringPieceVector* components,
477  bool omit_empty_strings);
478 
481 void SplitStringUsingSubstr(StringPiece full, StringPiece substr,
482  StringPieceVector* result);
483 
484 void BackslashEscape(StringPiece src, StringPiece to_escape,
485  GoogleString* dest);
486 
487 GoogleString CEscape(StringPiece src);
488 
492 
493 bool HasPrefixString(StringPiece str, StringPiece prefix);
494 
495 void UpperString(GoogleString* str);
496 
497 void LowerString(GoogleString* str);
498 
499 inline bool OnlyWhitespace(const GoogleString& str) {
500  return ContainsOnlyWhitespaceASCII(str);
501 }
502 
508 int GlobalReplaceSubstring(StringPiece substring, StringPiece replacement,
509  GoogleString* s);
510 
513 stringpiece_ssize_type FindIgnoreCase(StringPiece haystack, StringPiece needle);
514 
515 
520 int GlobalEraseBracketedSubstring(StringPiece left, StringPiece right,
521  GoogleString* string);
522 
526 GoogleString JoinStringStar(const ConstStringStarVector& vector,
527  StringPiece delim);
528 
532 
535 inline char UpperChar(char c) {
536  if ((c >= 'a') && (c <= 'z')) {
537  c += 'A' - 'a';
538  }
539  return c;
540 }
541 
544 inline char LowerChar(char c) {
545  if ((c >= 'A') && (c <= 'Z')) {
546  c += 'a' - 'A';
547  }
548  return c;
549 }
550 
556 inline char IsHtmlSpace(char c) {
557  return (c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') || (c == '\f');
558 }
559 
560 inline char* strdup(const char* str) {
561  return base::strdup(str);
562 }
563 
565 int StringCaseCompare(StringPiece s1, StringPiece s2);
566 
570 inline bool IsAsciiAlphaNumeric(char ch) {
571  return (((ch >= 'a') && (ch <= 'z')) ||
572  ((ch >= 'A') && (ch <= 'Z')) ||
573  ((ch >= '0') && (ch <= '9')));
574 }
575 
577 inline bool IsHexDigit(char c) {
578  return ('0' <= c && c <= '9') ||
579  ('A' <= c && c <= 'F') ||
580  ('a' <= c && c <= 'f');
581 }
582 
583 inline bool IsDecimalDigit(char c) {
584  return (c >= '0' && c <= '9');
585 }
586 
589 bool TrimWhitespace(StringPiece* str);
590 
592 void TrimQuote(StringPiece* str);
593 
597 void TrimUrlQuotes(StringPiece* str);
598 
600 bool TrimLeadingWhitespace(StringPiece* str);
601 
603 bool TrimTrailingWhitespace(StringPiece* str);
604 
607 inline void TrimWhitespace(StringPiece in, GoogleString* output) {
608  DCHECK((in.data() < output->data()) ||
609  (in.data() >= (output->data() + output->length())))
610  << "Illegal argument aliasing in TrimWhitespace";
611  StringPiece temp(in);
612  TrimWhitespace(&temp);
613  temp.CopyToString(output);
614 }
615 
618 bool AccumulateDecimalValue(char c, uint32* value);
619 
622 bool AccumulateHexValue(char c, uint32* value);
623 
625 bool MemCaseEqual(const char* s1, size_t size1, const char* s2, size_t size2);
626 inline bool StringCaseEqual(StringPiece s1, StringPiece s2) {
627  return MemCaseEqual(s1.data(), s1.size(), s2.data(), s2.size());
628 }
629 
631 bool StringCaseStartsWith(StringPiece str, StringPiece prefix);
633 bool StringCaseEndsWith(StringPiece str, StringPiece suffix);
634 
637 bool StringEqualConcat(StringPiece str, StringPiece first, StringPiece second);
638 
642 int CountCharacterMismatches(StringPiece s1, StringPiece s2);
643 
645  bool operator()(const char* s1, const char* s2) const {
646  return (StringCaseCompare(s1, s2) < 0);
647  }
648 };
649 
651  bool operator()(const char* s1, const char* s2) const {
652  return (strcmp(s1, s2) < 0);
653  }
654 };
655 
657  bool operator()(StringPiece s1, StringPiece s2) const { return s1 < s2; }
658 };
659 
661  bool operator()(StringPiece s1, StringPiece s2) const {
662  return (StringCaseCompare(s1, s2) < 0);
663  }
664 };
665 
668 bool SplitStringPieceToIntegerVector(StringPiece src, StringPiece separators,
669  std::vector<int>* ints);
670 
672 inline bool EndsInSlash(StringPiece path) {
673  return strings::EndsWith(path, "/");
674 }
675 
677 inline void EnsureEndsInSlash(GoogleString* dir) {
678  if (!EndsInSlash(*dir)) {
679  dir->append("/");
680  }
681 }
682 
687 void ParseShellLikeString(StringPiece input, std::vector<GoogleString>* output);
688 
693 int CountSubstring(StringPiece text, StringPiece substring);
694 
696 inline GoogleString* StringVectorAdd(StringVector* v) {
697  v->push_back(GoogleString());
698  return &v->back();
699 }
700 
702 template<typename I>
704  GoogleString* dest, I start, I end, StringPiece sep) {
705  if (start == end) {
707  return;
708  }
709  size_t size = dest->size();
710  size_t sep_size = 0;
711  for (I str = start; str != end; ++str) {
712  size += str->size() + sep_size;
713  sep_size = sep.size();
714  }
715  dest->reserve(size);
716  StringPiece to_prepend("");
717  for (I str = start; str != end; ++str) {
718  StrAppend(dest, to_prepend, *str);
719  to_prepend = sep;
720  }
721 }
722 
727 template<typename C>
729  GoogleString* dest, const C& collection, StringPiece sep) {
730  AppendJoinIterator(dest, collection.begin(), collection.end(), sep);
731 }
732 
733 template<typename C>
734 GoogleString JoinCollection(const C& collection, StringPiece sep) {
735  GoogleString result;
736  AppendJoinCollection(&result, collection, sep);
737  return result;
738 }
739 
741 inline const char* BoolToString(bool b) {
742  return (b ? "true" : "false");
743 }
744 
746 inline bool IsAscii(char c) {
747  return isascii(static_cast<unsigned char>(c));
748 }
749 
752 inline bool IsNonControlAscii(char c) {
753  return ('\x20' <= c) && (c <= '\x7E');
754 }
755 
756 
757 }
758 
759 #endif
bool IsAscii(char c)
Using isascii with signed chars is unfortunately undefined.
Definition: string_util.h:746
int GlobalReplaceSubstring(StringPiece substring, StringPiece replacement, GoogleString *s)
void ParseShellLikeString(StringPiece input, std::vector< GoogleString > *output)
bool TrimWhitespace(StringPiece *str)
const char * BoolToString(bool b)
Converts a boolean to string.
Definition: string_util.h:741
bool EndsInSlash(StringPiece path)
Does a path end in slash?
Definition: string_util.h:672
int GlobalEraseBracketedSubstring(StringPiece left, StringPiece right, GoogleString *string)
int CountCharacterMismatches(StringPiece s1, StringPiece s2)
bool HasPrefixString(StringPiece str, StringPiece prefix)
StringPiece PieceAfterEquals(StringPiece piece)
bool IsHexDigit(char c)
Convenience functions.
Definition: string_util.h:577
void SplitStringUsingSubstr(StringPiece full, StringPiece substr, StringPieceVector *result)
void AppendJoinCollection(GoogleString *dest, const C &collection, StringPiece sep)
Definition: string_util.h:728
char LowerChar(char c)
Definition: string_util.h:544
bool StringToInt(const char *in, int *out)
Definition: string_util.h:94
bool TrimTrailingWhitespace(StringPiece *str)
Trims trailing HTML whitespace. Returns true if any whitespace was trimmed.
stringpiece_ssize_type FindIgnoreCase(StringPiece haystack, StringPiece needle)
bool TrimLeadingWhitespace(StringPiece *str)
Trims leading HTML whitespace. Returns true if any whitespace was trimmed.
Definition: string_util.h:644
char IsHtmlSpace(char c)
Definition: string_util.h:556
bool MemCaseEqual(const char *s1, size_t size1, const char *s2, size_t size2)
Return true iff the two strings are equal, ignoring case.
Definition: string_util.h:650
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool AccumulateDecimalValue(char c, uint32 *value)
void TrimQuote(StringPiece *str)
In-place removal of leading and trailing quote. Removes whitespace as well.
Definition: string_util.h:660
bool IsAsciiAlphaNumeric(char ch)
Definition: string_util.h:570
bool StringCaseEndsWith(StringPiece str, StringPiece suffix)
Return true iff str ends with suffix, ignoring case.
void EnsureEndsInSlash(GoogleString *dir)
Make sure directory's path ends in '/'.
Definition: string_util.h:677
bool StringEqualConcat(StringPiece str, StringPiece first, StringPiece second)
GoogleString JoinStringStar(const ConstStringStarVector &vector, StringPiece delim)
GoogleString * StringVectorAdd(StringVector *v)
Appends new empty string to a StringVector and returns a pointer to it.
Definition: string_util.h:696
int StringCaseCompare(StringPiece s1, StringPiece s2)
Case-insensitive string comparison that is locale-independent.
bool AccumulateHexValue(char c, uint32 *value)
int CountSubstring(StringPiece text, StringPiece substring)
bool SplitStringPieceToIntegerVector(StringPiece src, StringPiece separators, std::vector< int > *ints)
void AppendJoinIterator(GoogleString *dest, I start, I end, StringPiece sep)
Append string-like objects accessed through an iterator.
Definition: string_util.h:703
bool StringToDouble(const char *in, double *out)
Definition: string_util.h:656
char UpperChar(char c)
Definition: string_util.h:535
void TrimUrlQuotes(StringPiece *str)
bool IsNonControlAscii(char c)
Definition: string_util.h:752
bool StringCaseStartsWith(StringPiece str, StringPiece prefix)
Return true iff str starts with prefix, ignoring case.
void SplitStringPieceToVector(StringPiece sp, StringPiece separators, StringPieceVector *components, bool omit_empty_strings)