Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/protobuf/src/google/protobuf/stubs/strutil.h

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pull whole protobuf Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
(...skipping 15 matching lines...) Expand all
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // from google3/strings/strutil.h 31 // from google3/strings/strutil.h
32 32
33 #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ 33 #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
34 #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ 34 #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
35 35
36 #include <stdlib.h> 36 #include <stdlib.h>
37 #include <vector> 37 #include <vector>
38 #include <google/protobuf/stubs/common.h> 38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/stubs/stringpiece.h>
39 40
40 namespace google { 41 namespace google {
41 namespace protobuf { 42 namespace protobuf {
42 43
43 #ifdef _MSC_VER 44 #ifdef _MSC_VER
44 #define strtoll _strtoi64 45 #define strtoll _strtoi64
45 #define strtoull _strtoui64 46 #define strtoull _strtoui64
46 #elif defined(__DECCXX) && defined(__osf__) 47 #elif defined(__DECCXX) && defined(__osf__)
47 // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit. 48 // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
48 #define strtoll strtol 49 #define strtoll strtol
49 #define strtoull strtoul 50 #define strtoull strtoul
50 #endif 51 #endif
51 52
52 // ---------------------------------------------------------------------- 53 // ----------------------------------------------------------------------
53 // ascii_isalnum() 54 // ascii_isalnum()
54 // Check if an ASCII character is alphanumeric. We can't use ctype's 55 // Check if an ASCII character is alphanumeric. We can't use ctype's
55 // isalnum() because it is affected by locale. This function is applied 56 // isalnum() because it is affected by locale. This function is applied
56 // to identifiers in the protocol buffer language, not to natural-language 57 // to identifiers in the protocol buffer language, not to natural-language
57 // strings, so locale should not be taken into account. 58 // strings, so locale should not be taken into account.
58 // ascii_isdigit() 59 // ascii_isdigit()
59 // Like above, but only accepts digits. 60 // Like above, but only accepts digits.
61 // ascii_isspace()
62 // Check if the character is a space character.
60 // ---------------------------------------------------------------------- 63 // ----------------------------------------------------------------------
61 64
62 inline bool ascii_isalnum(char c) { 65 inline bool ascii_isalnum(char c) {
63 return ('a' <= c && c <= 'z') || 66 return ('a' <= c && c <= 'z') ||
64 ('A' <= c && c <= 'Z') || 67 ('A' <= c && c <= 'Z') ||
65 ('0' <= c && c <= '9'); 68 ('0' <= c && c <= '9');
66 } 69 }
67 70
68 inline bool ascii_isdigit(char c) { 71 inline bool ascii_isdigit(char c) {
69 return ('0' <= c && c <= '9'); 72 return ('0' <= c && c <= '9');
70 } 73 }
71 74
75 inline bool ascii_isspace(char c) {
76 return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
77 c == '\r';
78 }
79
80 inline bool ascii_isupper(char c) {
81 return c >= 'A' && c <= 'Z';
82 }
83
84 inline bool ascii_islower(char c) {
85 return c >= 'a' && c <= 'z';
86 }
87
88 inline char ascii_toupper(char c) {
89 return ascii_islower(c) ? c - ('a' - 'A') : c;
90 }
91
92 inline char ascii_tolower(char c) {
93 return ascii_isupper(c) ? c + ('a' - 'A') : c;
94 }
95
96 inline int hex_digit_to_int(char c) {
97 /* Assume ASCII. */
98 int x = static_cast<unsigned char>(c);
99 if (x > '9') {
100 x += 9;
101 }
102 return x & 0xf;
103 }
104
72 // ---------------------------------------------------------------------- 105 // ----------------------------------------------------------------------
73 // HasPrefixString() 106 // HasPrefixString()
74 // Check if a string begins with a given prefix. 107 // Check if a string begins with a given prefix.
75 // StripPrefixString() 108 // StripPrefixString()
76 // Given a string and a putative prefix, returns the string minus the 109 // Given a string and a putative prefix, returns the string minus the
77 // prefix string if the prefix matches, otherwise the original 110 // prefix string if the prefix matches, otherwise the original
78 // string. 111 // string.
79 // ---------------------------------------------------------------------- 112 // ----------------------------------------------------------------------
80 inline bool HasPrefixString(const string& str, 113 inline bool HasPrefixString(const string& str,
81 const string& prefix) { 114 const string& prefix) {
(...skipping 30 matching lines...) Expand all
112 return str; 145 return str;
113 } 146 }
114 } 147 }
115 148
116 // ---------------------------------------------------------------------- 149 // ----------------------------------------------------------------------
117 // StripString 150 // StripString
118 // Replaces any occurrence of the character 'remove' (or the characters 151 // Replaces any occurrence of the character 'remove' (or the characters
119 // in 'remove') with the character 'replacewith'. 152 // in 'remove') with the character 'replacewith'.
120 // Good for keeping html characters or protocol characters (\t) out 153 // Good for keeping html characters or protocol characters (\t) out
121 // of places where they might cause a problem. 154 // of places where they might cause a problem.
155 // StripWhitespace
156 // Removes whitespaces from both ends of the given string.
122 // ---------------------------------------------------------------------- 157 // ----------------------------------------------------------------------
123 LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove, 158 LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
124 char replacewith); 159 char replacewith);
125 160
161 LIBPROTOBUF_EXPORT void StripWhitespace(string* s);
162
163
126 // ---------------------------------------------------------------------- 164 // ----------------------------------------------------------------------
127 // LowerString() 165 // LowerString()
128 // UpperString() 166 // UpperString()
167 // ToUpper()
129 // Convert the characters in "s" to lowercase or uppercase. ASCII-only: 168 // Convert the characters in "s" to lowercase or uppercase. ASCII-only:
130 // these functions intentionally ignore locale because they are applied to 169 // these functions intentionally ignore locale because they are applied to
131 // identifiers used in the Protocol Buffer language, not to natural-language 170 // identifiers used in the Protocol Buffer language, not to natural-language
132 // strings. 171 // strings.
133 // ---------------------------------------------------------------------- 172 // ----------------------------------------------------------------------
134 173
135 inline void LowerString(string * s) { 174 inline void LowerString(string * s) {
136 string::iterator end = s->end(); 175 string::iterator end = s->end();
137 for (string::iterator i = s->begin(); i != end; ++i) { 176 for (string::iterator i = s->begin(); i != end; ++i) {
138 // tolower() changes based on locale. We don't want this! 177 // tolower() changes based on locale. We don't want this!
139 if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A'; 178 if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
140 } 179 }
141 } 180 }
142 181
143 inline void UpperString(string * s) { 182 inline void UpperString(string * s) {
144 string::iterator end = s->end(); 183 string::iterator end = s->end();
145 for (string::iterator i = s->begin(); i != end; ++i) { 184 for (string::iterator i = s->begin(); i != end; ++i) {
146 // toupper() changes based on locale. We don't want this! 185 // toupper() changes based on locale. We don't want this!
147 if ('a' <= *i && *i <= 'z') *i += 'A' - 'a'; 186 if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
148 } 187 }
149 } 188 }
150 189
190 inline string ToUpper(const string& s) {
191 string out = s;
192 UpperString(&out);
193 return out;
194 }
195
151 // ---------------------------------------------------------------------- 196 // ----------------------------------------------------------------------
152 // StringReplace() 197 // StringReplace()
153 // Give me a string and two patterns "old" and "new", and I replace 198 // Give me a string and two patterns "old" and "new", and I replace
154 // the first instance of "old" in the string with "new", if it 199 // the first instance of "old" in the string with "new", if it
155 // exists. RETURN a new string, regardless of whether the replacement 200 // exists. RETURN a new string, regardless of whether the replacement
156 // happened or not. 201 // happened or not.
157 // ---------------------------------------------------------------------- 202 // ----------------------------------------------------------------------
158 203
159 LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub, 204 LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
160 const string& newsub, bool replace_all); 205 const string& newsub, bool replace_all);
(...skipping 13 matching lines...) Expand all
174 // corresponding empty strings. If you want to drop the empty 219 // corresponding empty strings. If you want to drop the empty
175 // strings, try SplitStringUsing(). 220 // strings, try SplitStringUsing().
176 // 221 //
177 // If "full" is the empty string, yields an empty string as the only value. 222 // If "full" is the empty string, yields an empty string as the only value.
178 // ---------------------------------------------------------------------- 223 // ----------------------------------------------------------------------
179 LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full, 224 LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full,
180 const char* delim, 225 const char* delim,
181 vector<string>* result); 226 vector<string>* result);
182 227
183 // ---------------------------------------------------------------------- 228 // ----------------------------------------------------------------------
229 // Split()
230 // Split a string using a character delimiter.
231 // ----------------------------------------------------------------------
232 inline vector<string> Split(
233 const string& full, const char* delim, bool skip_empty = true) {
234 vector<string> result;
235 if (skip_empty) {
236 SplitStringUsing(full, delim, &result);
237 } else {
238 SplitStringAllowEmpty(full, delim, &result);
239 }
240 return result;
241 }
242
243 // ----------------------------------------------------------------------
184 // JoinStrings() 244 // JoinStrings()
185 // These methods concatenate a vector of strings into a C++ string, using 245 // These methods concatenate a vector of strings into a C++ string, using
186 // the C-string "delim" as a separator between components. There are two 246 // the C-string "delim" as a separator between components. There are two
187 // flavors of the function, one flavor returns the concatenated string, 247 // flavors of the function, one flavor returns the concatenated string,
188 // another takes a pointer to the target string. In the latter case the 248 // another takes a pointer to the target string. In the latter case the
189 // target string is cleared and overwritten. 249 // target string is cleared and overwritten.
190 // ---------------------------------------------------------------------- 250 // ----------------------------------------------------------------------
191 LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components, 251 LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
192 const char* delim, string* result); 252 const char* delim, string* result);
193 253
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // In the first and second calls, the length of dest is returned. In the 307 // In the first and second calls, the length of dest is returned. In the
248 // the third call, the new string is returned. 308 // the third call, the new string is returned.
249 // ---------------------------------------------------------------------- 309 // ----------------------------------------------------------------------
250 310
251 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest); 311 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
252 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest, 312 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
253 vector<string> *errors); 313 vector<string> *errors);
254 LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src); 314 LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
255 315
256 // ---------------------------------------------------------------------- 316 // ----------------------------------------------------------------------
257 // CEscapeString() 317 // CEscape()
258 // Copies 'src' to 'dest', escaping dangerous characters using 318 // Escapes 'src' using C-style escape sequences and returns the resulting
259 // C-style escape sequences. This is very useful for preparing query 319 // string.
260 // flags. 'src' and 'dest' should not overlap.
261 // Returns the number of bytes written to 'dest' (not including the \0)
262 // or -1 if there was insufficient space.
263 // 320 //
264 // Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped. 321 // Escaped chars: \n, \r, \t, ", ', \, and !isprint().
265 // ----------------------------------------------------------------------
266 LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
267 char* dest, int dest_len);
268
269 // ----------------------------------------------------------------------
270 // CEscape()
271 // More convenient form of CEscapeString: returns result as a "string".
272 // This version is slower than CEscapeString() because it does more
273 // allocation. However, it is much more convenient to use in
274 // non-speed-critical code like logging messages etc.
275 // ---------------------------------------------------------------------- 322 // ----------------------------------------------------------------------
276 LIBPROTOBUF_EXPORT string CEscape(const string& src); 323 LIBPROTOBUF_EXPORT string CEscape(const string& src);
277 324
325 // ----------------------------------------------------------------------
326 // CEscapeAndAppend()
327 // Escapes 'src' using C-style escape sequences, and appends the escaped
328 // string to 'dest'.
329 // ----------------------------------------------------------------------
330 LIBPROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, string* dest);
331
278 namespace strings { 332 namespace strings {
279 // Like CEscape() but does not escape bytes with the upper bit set. 333 // Like CEscape() but does not escape bytes with the upper bit set.
280 LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src); 334 LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
281 335
282 // Like CEscape() but uses hex (\x) escapes instead of octals. 336 // Like CEscape() but uses hex (\x) escapes instead of octals.
283 LIBPROTOBUF_EXPORT string CHexEscape(const string& src); 337 LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
284 } // namespace strings 338 } // namespace strings
285 339
286 // ---------------------------------------------------------------------- 340 // ----------------------------------------------------------------------
287 // strto32() 341 // strto32()
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 return strtoll(nptr, endptr, base); 374 return strtoll(nptr, endptr, base);
321 } 375 }
322 376
323 inline uint64 strtou64(const char *nptr, char **endptr, int base) { 377 inline uint64 strtou64(const char *nptr, char **endptr, int base) {
324 GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long), 378 GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
325 sizeof_uint64_is_not_sizeof_long_long); 379 sizeof_uint64_is_not_sizeof_long_long);
326 return strtoull(nptr, endptr, base); 380 return strtoull(nptr, endptr, base);
327 } 381 }
328 382
329 // ---------------------------------------------------------------------- 383 // ----------------------------------------------------------------------
384 // safe_strtob()
385 // safe_strto32()
386 // safe_strtou32()
387 // safe_strto64()
388 // safe_strtou64()
389 // safe_strtof()
390 // safe_strtod()
391 // ----------------------------------------------------------------------
392 LIBPROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
393
394 LIBPROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value);
395 LIBPROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value);
396 inline bool safe_strto32(const char* str, int32* value) {
397 return safe_strto32(string(str), value);
398 }
399 inline bool safe_strto32(StringPiece str, int32* value) {
400 return safe_strto32(str.ToString(), value);
401 }
402 inline bool safe_strtou32(const char* str, uint32* value) {
403 return safe_strtou32(string(str), value);
404 }
405 inline bool safe_strtou32(StringPiece str, uint32* value) {
406 return safe_strtou32(str.ToString(), value);
407 }
408
409 LIBPROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value);
410 LIBPROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value);
411 inline bool safe_strto64(const char* str, int64* value) {
412 return safe_strto64(string(str), value);
413 }
414 inline bool safe_strto64(StringPiece str, int64* value) {
415 return safe_strto64(str.ToString(), value);
416 }
417 inline bool safe_strtou64(const char* str, uint64* value) {
418 return safe_strtou64(string(str), value);
419 }
420 inline bool safe_strtou64(StringPiece str, uint64* value) {
421 return safe_strtou64(str.ToString(), value);
422 }
423
424 LIBPROTOBUF_EXPORT bool safe_strtof(const char* str, float* value);
425 LIBPROTOBUF_EXPORT bool safe_strtod(const char* str, double* value);
426 inline bool safe_strtof(const string& str, float* value) {
427 return safe_strtof(str.c_str(), value);
428 }
429 inline bool safe_strtod(const string& str, double* value) {
430 return safe_strtod(str.c_str(), value);
431 }
432 inline bool safe_strtof(StringPiece str, float* value) {
433 return safe_strtof(str.ToString(), value);
434 }
435 inline bool safe_strtod(StringPiece str, double* value) {
436 return safe_strtod(str.ToString(), value);
437 }
438
439 // ----------------------------------------------------------------------
330 // FastIntToBuffer() 440 // FastIntToBuffer()
331 // FastHexToBuffer() 441 // FastHexToBuffer()
332 // FastHex64ToBuffer() 442 // FastHex64ToBuffer()
333 // FastHex32ToBuffer() 443 // FastHex32ToBuffer()
334 // FastTimeToBuffer() 444 // FastTimeToBuffer()
335 // These are intended for speed. FastIntToBuffer() assumes the 445 // These are intended for speed. FastIntToBuffer() assumes the
336 // integer is non-negative. FastHexToBuffer() puts output in 446 // integer is non-negative. FastHexToBuffer() puts output in
337 // hex rather than decimal. FastTimeToBuffer() puts the output 447 // hex rather than decimal. FastTimeToBuffer() puts the output
338 // into RFC822 format. 448 // into RFC822 format.
339 // 449 //
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 // Just define these in terms of the above. 512 // Just define these in terms of the above.
403 inline char* FastUInt32ToBuffer(uint32 i, char* buffer) { 513 inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
404 FastUInt32ToBufferLeft(i, buffer); 514 FastUInt32ToBufferLeft(i, buffer);
405 return buffer; 515 return buffer;
406 } 516 }
407 inline char* FastUInt64ToBuffer(uint64 i, char* buffer) { 517 inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
408 FastUInt64ToBufferLeft(i, buffer); 518 FastUInt64ToBufferLeft(i, buffer);
409 return buffer; 519 return buffer;
410 } 520 }
411 521
522 inline string SimpleBtoa(bool value) {
523 return value ? "true" : "false";
524 }
525
412 // ---------------------------------------------------------------------- 526 // ----------------------------------------------------------------------
413 // SimpleItoa() 527 // SimpleItoa()
414 // Description: converts an integer to a string. 528 // Description: converts an integer to a string.
415 // 529 //
416 // Return value: string 530 // Return value: string
417 // ---------------------------------------------------------------------- 531 // ----------------------------------------------------------------------
418 LIBPROTOBUF_EXPORT string SimpleItoa(int i); 532 LIBPROTOBUF_EXPORT string SimpleItoa(int i);
419 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i); 533 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
420 LIBPROTOBUF_EXPORT string SimpleItoa(long i); 534 LIBPROTOBUF_EXPORT string SimpleItoa(long i);
421 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i); 535 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
(...skipping 24 matching lines...) Expand all
446 560
447 LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer); 561 LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
448 LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer); 562 LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
449 563
450 // In practice, doubles should never need more than 24 bytes and floats 564 // In practice, doubles should never need more than 24 bytes and floats
451 // should never need more than 14 (including null terminators), but we 565 // should never need more than 14 (including null terminators), but we
452 // overestimate to be safe. 566 // overestimate to be safe.
453 static const int kDoubleToBufferSize = 32; 567 static const int kDoubleToBufferSize = 32;
454 static const int kFloatToBufferSize = 24; 568 static const int kFloatToBufferSize = 24;
455 569
456 // ---------------------------------------------------------------------- 570 namespace strings {
457 // NoLocaleStrtod() 571
458 // Exactly like strtod(), except it always behaves as if in the "C" 572 enum PadSpec {
459 // locale (i.e. decimal points must be '.'s). 573 NO_PAD = 1,
460 // ---------------------------------------------------------------------- 574 ZERO_PAD_2,
461 575 ZERO_PAD_3,
462 LIBPROTOBUF_EXPORT double NoLocaleStrtod(const char* text, char** endptr); 576 ZERO_PAD_4,
577 ZERO_PAD_5,
578 ZERO_PAD_6,
579 ZERO_PAD_7,
580 ZERO_PAD_8,
581 ZERO_PAD_9,
582 ZERO_PAD_10,
583 ZERO_PAD_11,
584 ZERO_PAD_12,
585 ZERO_PAD_13,
586 ZERO_PAD_14,
587 ZERO_PAD_15,
588 ZERO_PAD_16,
589 };
590
591 struct Hex {
592 uint64 value;
593 enum PadSpec spec;
594 template <class Int>
595 explicit Hex(Int v, PadSpec s = NO_PAD)
596 : spec(s) {
597 // Prevent sign-extension by casting integers to
598 // their unsigned counterparts.
599 #ifdef LANG_CXX11
600 static_assert(
601 sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
602 "Unknown integer type");
603 #endif
604 value = sizeof(v) == 1 ? static_cast<uint8>(v)
605 : sizeof(v) == 2 ? static_cast<uint16>(v)
606 : sizeof(v) == 4 ? static_cast<uint32>(v)
607 : static_cast<uint64>(v);
608 }
609 };
610
611 struct LIBPROTOBUF_EXPORT AlphaNum {
612 const char *piece_data_; // move these to string_ref eventually
613 size_t piece_size_; // move these to string_ref eventually
614
615 char digits[kFastToBufferSize];
616
617 // No bool ctor -- bools convert to an integral type.
618 // A bool ctor would also convert incoming pointers (bletch).
619
620 AlphaNum(int32 i32)
621 : piece_data_(digits),
622 piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {}
623 AlphaNum(uint32 u32)
624 : piece_data_(digits),
625 piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {}
626 AlphaNum(int64 i64)
627 : piece_data_(digits),
628 piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
629 AlphaNum(uint64 u64)
630 : piece_data_(digits),
631 piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
632
633 AlphaNum(float f)
634 : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {}
635 AlphaNum(double f)
636 : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {}
637
638 AlphaNum(Hex hex);
639
640 AlphaNum(const char* c_str)
641 : piece_data_(c_str), piece_size_(strlen(c_str)) {}
642 // TODO: Add a string_ref constructor, eventually
643 // AlphaNum(const StringPiece &pc) : piece(pc) {}
644
645 AlphaNum(const string& str)
646 : piece_data_(str.data()), piece_size_(str.size()) {}
647
648 AlphaNum(StringPiece str)
649 : piece_data_(str.data()), piece_size_(str.size()) {}
650
651 size_t size() const { return piece_size_; }
652 const char *data() const { return piece_data_; }
653
654 private:
655 // Use ":" not ':'
656 AlphaNum(char c); // NOLINT(runtime/explicit)
657
658 // Disallow copy and assign.
659 AlphaNum(const AlphaNum&);
660 void operator=(const AlphaNum&);
661 };
662
663 } // namespace strings
664
665 using strings::AlphaNum;
666
667 // ----------------------------------------------------------------------
668 // StrCat()
669 // This merges the given strings or numbers, with no delimiter. This
670 // is designed to be the fastest possible way to construct a string out
671 // of a mix of raw C strings, strings, bool values,
672 // and numeric values.
673 //
674 // Don't use this for user-visible strings. The localization process
675 // works poorly on strings built up out of fragments.
676 //
677 // For clarity and performance, don't use StrCat when appending to a
678 // string. In particular, avoid using any of these (anti-)patterns:
679 // str.append(StrCat(...)
680 // str += StrCat(...)
681 // str = StrCat(str, ...)
682 // where the last is the worse, with the potential to change a loop
683 // from a linear time operation with O(1) dynamic allocations into a
684 // quadratic time operation with O(n) dynamic allocations. StrAppend
685 // is a better choice than any of the above, subject to the restriction
686 // of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may
687 // be a reference into str.
688 // ----------------------------------------------------------------------
689
690 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b);
691 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
692 const AlphaNum& c);
693 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
694 const AlphaNum& c, const AlphaNum& d);
695 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
696 const AlphaNum& c, const AlphaNum& d,
697 const AlphaNum& e);
698 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
699 const AlphaNum& c, const AlphaNum& d,
700 const AlphaNum& e, const AlphaNum& f);
701 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
702 const AlphaNum& c, const AlphaNum& d,
703 const AlphaNum& e, const AlphaNum& f,
704 const AlphaNum& g);
705 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
706 const AlphaNum& c, const AlphaNum& d,
707 const AlphaNum& e, const AlphaNum& f,
708 const AlphaNum& g, const AlphaNum& h);
709 LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
710 const AlphaNum& c, const AlphaNum& d,
711 const AlphaNum& e, const AlphaNum& f,
712 const AlphaNum& g, const AlphaNum& h,
713 const AlphaNum& i);
714
715 inline string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); }
716
717 // ----------------------------------------------------------------------
718 // StrAppend()
719 // Same as above, but adds the output to the given string.
720 // WARNING: For speed, StrAppend does not try to check each of its input
721 // arguments to be sure that they are not a subset of the string being
722 // appended to. That is, while this will work:
723 //
724 // string s = "foo";
725 // s += s;
726 //
727 // This will not (necessarily) work:
728 //
729 // string s = "foo";
730 // StrAppend(&s, s);
731 //
732 // Note: while StrCat supports appending up to 9 arguments, StrAppend
733 // is currently limited to 4. That's rarely an issue except when
734 // automatically transforming StrCat to StrAppend, and can easily be
735 // worked around as consecutive calls to StrAppend are quite efficient.
736 // ----------------------------------------------------------------------
737
738 LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a);
739 LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
740 const AlphaNum& b);
741 LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
742 const AlphaNum& b, const AlphaNum& c);
743 LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
744 const AlphaNum& b, const AlphaNum& c,
745 const AlphaNum& d);
746
747 // ----------------------------------------------------------------------
748 // Join()
749 // These methods concatenate a range of components into a C++ string, using
750 // the C-string "delim" as a separator between components.
751 // ----------------------------------------------------------------------
752 template <typename Iterator>
753 void Join(Iterator start, Iterator end,
754 const char* delim, string* result) {
755 for (Iterator it = start; it != end; ++it) {
756 if (it != start) {
757 result->append(delim);
758 }
759 StrAppend(result, *it);
760 }
761 }
762
763 template <typename Range>
764 string Join(const Range& components,
765 const char* delim) {
766 string result;
767 Join(components.begin(), components.end(), delim, &result);
768 return result;
769 }
770
771 // ----------------------------------------------------------------------
772 // ToHex()
773 // Return a lower-case hex string representation of the given integer.
774 // ----------------------------------------------------------------------
775 LIBPROTOBUF_EXPORT string ToHex(uint64 num);
776
777 // ----------------------------------------------------------------------
778 // GlobalReplaceSubstring()
779 // Replaces all instances of a substring in a string. Does nothing
780 // if 'substring' is empty. Returns the number of replacements.
781 //
782 // NOTE: The string pieces must not overlap s.
783 // ----------------------------------------------------------------------
784 LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring,
785 const string& replacement,
786 string* s);
787
788 // ----------------------------------------------------------------------
789 // Base64Unescape()
790 // Converts "src" which is encoded in Base64 to its binary equivalent and
791 // writes it to "dest". If src contains invalid characters, dest is cleared
792 // and the function returns false. Returns true on success.
793 // ----------------------------------------------------------------------
794 LIBPROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest);
795
796 // ----------------------------------------------------------------------
797 // WebSafeBase64Unescape()
798 // This is a variation of Base64Unescape which uses '-' instead of '+', and
799 // '_' instead of '/'. src is not null terminated, instead specify len. I
800 // recommend that slen<szdest, but we honor szdest anyway.
801 // RETURNS the length of dest, or -1 if src contains invalid chars.
802
803 // The variation that stores into a string clears the string first, and
804 // returns false (with dest empty) if src contains invalid chars; for
805 // this version src and dest must be different strings.
806 // ----------------------------------------------------------------------
807 LIBPROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen,
808 char* dest, int szdest);
809 LIBPROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest);
810
811 // Return the length to use for the output buffer given to the base64 escape
812 // routines. Make sure to use the same value for do_padding in both.
813 // This function may return incorrect results if given input_len values that
814 // are extremely high, which should happen rarely.
815 LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len,
816 bool do_padding);
817 // Use this version when calling Base64Escape without a do_padding arg.
818 LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len);
819
820 // ----------------------------------------------------------------------
821 // Base64Escape()
822 // WebSafeBase64Escape()
823 // Encode "src" to "dest" using base64 encoding.
824 // src is not null terminated, instead specify len.
825 // 'dest' should have at least CalculateBase64EscapedLen() length.
826 // RETURNS the length of dest.
827 // The WebSafe variation use '-' instead of '+' and '_' instead of '/'
828 // so that we can place the out in the URL or cookies without having
829 // to escape them. It also has an extra parameter "do_padding",
830 // which when set to false will prevent padding with "=".
831 // ----------------------------------------------------------------------
832 LIBPROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen,
833 char* dest, int szdest);
834 LIBPROTOBUF_EXPORT int WebSafeBase64Escape(
835 const unsigned char* src, int slen, char* dest,
836 int szdest, bool do_padding);
837 // Encode src into dest with padding.
838 LIBPROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest);
839 // Encode src into dest web-safely without padding.
840 LIBPROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest);
841 // Encode src into dest web-safely with padding.
842 LIBPROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src,
843 string* dest);
844
845 LIBPROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
846 string* dest, bool do_padding);
847 LIBPROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
848 string* dest, bool do_padding);
849
850 static const int UTFmax = 4;
851 // ----------------------------------------------------------------------
852 // EncodeAsUTF8Char()
853 // Helper to append a Unicode code point to a string as UTF8, without bringing
854 // in any external dependencies. The output buffer must be as least 4 bytes
855 // large.
856 // ----------------------------------------------------------------------
857 LIBPROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
858
859 // ----------------------------------------------------------------------
860 // UTF8FirstLetterNumBytes()
861 // Length of the first UTF-8 character.
862 // ----------------------------------------------------------------------
863 LIBPROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len);
463 864
464 } // namespace protobuf 865 } // namespace protobuf
465 } // namespace google 866 } // namespace google
466 867
467 #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ 868 #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698