1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // from google3/strings/strutil.h
32
33 #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
34 #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
35
36 #include <stdlib.h>
37 #include <vector>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/stubs/stringpiece.h>
40
41 #include <google/protobuf/port_def.inc>
42
43 namespace google {
44 namespace protobuf {
45
46 #if defined(_MSC_VER) && _MSC_VER < 1800
47 #define strtoll _strtoi64
48 #define strtoull _strtoui64
49 #elif defined(__DECCXX) && defined(__osf__)
50 // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
51 #define strtoll strtol
52 #define strtoull strtoul
53 #endif
54
55 // ----------------------------------------------------------------------
56 // ascii_isalnum()
57 // Check if an ASCII character is alphanumeric. We can't use ctype's
58 // isalnum() because it is affected by locale. This function is applied
59 // to identifiers in the protocol buffer language, not to natural-language
60 // strings, so locale should not be taken into account.
61 // ascii_isdigit()
62 // Like above, but only accepts digits.
63 // ascii_isspace()
64 // Check if the character is a space character.
65 // ----------------------------------------------------------------------
66
ascii_isalnum(char c)67 inline bool ascii_isalnum(char c) {
68 return ('a' <= c && c <= 'z') ||
69 ('A' <= c && c <= 'Z') ||
70 ('0' <= c && c <= '9');
71 }
72
ascii_isdigit(char c)73 inline bool ascii_isdigit(char c) {
74 return ('0' <= c && c <= '9');
75 }
76
ascii_isspace(char c)77 inline bool ascii_isspace(char c) {
78 return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
79 c == '\r';
80 }
81
ascii_isupper(char c)82 inline bool ascii_isupper(char c) {
83 return c >= 'A' && c <= 'Z';
84 }
85
ascii_islower(char c)86 inline bool ascii_islower(char c) {
87 return c >= 'a' && c <= 'z';
88 }
89
ascii_toupper(char c)90 inline char ascii_toupper(char c) {
91 return ascii_islower(c) ? c - ('a' - 'A') : c;
92 }
93
ascii_tolower(char c)94 inline char ascii_tolower(char c) {
95 return ascii_isupper(c) ? c + ('a' - 'A') : c;
96 }
97
hex_digit_to_int(char c)98 inline int hex_digit_to_int(char c) {
99 /* Assume ASCII. */
100 int x = static_cast<unsigned char>(c);
101 if (x > '9') {
102 x += 9;
103 }
104 return x & 0xf;
105 }
106
107 // ----------------------------------------------------------------------
108 // HasPrefixString()
109 // Check if a string begins with a given prefix.
110 // StripPrefixString()
111 // Given a string and a putative prefix, returns the string minus the
112 // prefix string if the prefix matches, otherwise the original
113 // string.
114 // ----------------------------------------------------------------------
HasPrefixString(const string & str,const string & prefix)115 inline bool HasPrefixString(const string& str,
116 const string& prefix) {
117 return str.size() >= prefix.size() &&
118 str.compare(0, prefix.size(), prefix) == 0;
119 }
120
StripPrefixString(const string & str,const string & prefix)121 inline string StripPrefixString(const string& str, const string& prefix) {
122 if (HasPrefixString(str, prefix)) {
123 return str.substr(prefix.size());
124 } else {
125 return str;
126 }
127 }
128
129 // ----------------------------------------------------------------------
130 // HasSuffixString()
131 // Return true if str ends in suffix.
132 // StripSuffixString()
133 // Given a string and a putative suffix, returns the string minus the
134 // suffix string if the suffix matches, otherwise the original
135 // string.
136 // ----------------------------------------------------------------------
HasSuffixString(const string & str,const string & suffix)137 inline bool HasSuffixString(const string& str,
138 const string& suffix) {
139 return str.size() >= suffix.size() &&
140 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
141 }
142
StripSuffixString(const string & str,const string & suffix)143 inline string StripSuffixString(const string& str, const string& suffix) {
144 if (HasSuffixString(str, suffix)) {
145 return str.substr(0, str.size() - suffix.size());
146 } else {
147 return str;
148 }
149 }
150
151 // ----------------------------------------------------------------------
152 // ReplaceCharacters
153 // Replaces any occurrence of the character 'remove' (or the characters
154 // in 'remove') with the character 'replacewith'.
155 // Good for keeping html characters or protocol characters (\t) out
156 // of places where they might cause a problem.
157 // StripWhitespace
158 // Removes whitespaces from both ends of the given string.
159 // ----------------------------------------------------------------------
160 PROTOBUF_EXPORT void ReplaceCharacters(string* s, const char* remove,
161 char replacewith);
162
163 PROTOBUF_EXPORT void StripWhitespace(string* s);
164
165 // ----------------------------------------------------------------------
166 // LowerString()
167 // UpperString()
168 // ToUpper()
169 // Convert the characters in "s" to lowercase or uppercase. ASCII-only:
170 // these functions intentionally ignore locale because they are applied to
171 // identifiers used in the Protocol Buffer language, not to natural-language
172 // strings.
173 // ----------------------------------------------------------------------
174
LowerString(string * s)175 inline void LowerString(string * s) {
176 string::iterator end = s->end();
177 for (string::iterator i = s->begin(); i != end; ++i) {
178 // tolower() changes based on locale. We don't want this!
179 if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
180 }
181 }
182
UpperString(string * s)183 inline void UpperString(string * s) {
184 string::iterator end = s->end();
185 for (string::iterator i = s->begin(); i != end; ++i) {
186 // toupper() changes based on locale. We don't want this!
187 if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
188 }
189 }
190
ToUpper(const string & s)191 inline string ToUpper(const string& s) {
192 string out = s;
193 UpperString(&out);
194 return out;
195 }
196
197 // ----------------------------------------------------------------------
198 // StringReplace()
199 // Give me a string and two patterns "old" and "new", and I replace
200 // the first instance of "old" in the string with "new", if it
201 // exists. RETURN a new string, regardless of whether the replacement
202 // happened or not.
203 // ----------------------------------------------------------------------
204
205 PROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
206 const string& newsub, bool replace_all);
207
208 // ----------------------------------------------------------------------
209 // SplitStringUsing()
210 // Split a string using a character delimiter. Append the components
211 // to 'result'. If there are consecutive delimiters, this function skips
212 // over all of them.
213 // ----------------------------------------------------------------------
214 PROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
215 std::vector<string>* res);
216
217 // Split a string using one or more byte delimiters, presented
218 // as a nul-terminated c string. Append the components to 'result'.
219 // If there are consecutive delimiters, this function will return
220 // corresponding empty strings. If you want to drop the empty
221 // strings, try SplitStringUsing().
222 //
223 // If "full" is the empty string, yields an empty string as the only value.
224 // ----------------------------------------------------------------------
225 PROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full,
226 const char* delim,
227 std::vector<string>* result);
228
229 // ----------------------------------------------------------------------
230 // Split()
231 // Split a string using a character delimiter.
232 // ----------------------------------------------------------------------
233 inline std::vector<string> Split(
234 const string& full, const char* delim, bool skip_empty = true) {
235 std::vector<string> result;
236 if (skip_empty) {
237 SplitStringUsing(full, delim, &result);
238 } else {
239 SplitStringAllowEmpty(full, delim, &result);
240 }
241 return result;
242 }
243
244 // ----------------------------------------------------------------------
245 // JoinStrings()
246 // These methods concatenate a vector of strings into a C++ string, using
247 // the C-string "delim" as a separator between components. There are two
248 // flavors of the function, one flavor returns the concatenated string,
249 // another takes a pointer to the target string. In the latter case the
250 // target string is cleared and overwritten.
251 // ----------------------------------------------------------------------
252 PROTOBUF_EXPORT void JoinStrings(const std::vector<string>& components,
253 const char* delim, string* result);
254
JoinStrings(const std::vector<string> & components,const char * delim)255 inline string JoinStrings(const std::vector<string>& components,
256 const char* delim) {
257 string result;
258 JoinStrings(components, delim, &result);
259 return result;
260 }
261
262 // ----------------------------------------------------------------------
263 // UnescapeCEscapeSequences()
264 // Copies "source" to "dest", rewriting C-style escape sequences
265 // -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
266 // equivalents. "dest" must be sufficiently large to hold all
267 // the characters in the rewritten string (i.e. at least as large
268 // as strlen(source) + 1 should be safe, since the replacements
269 // are always shorter than the original escaped sequences). It's
270 // safe for source and dest to be the same. RETURNS the length
271 // of dest.
272 //
273 // It allows hex sequences \xhh, or generally \xhhhhh with an
274 // arbitrary number of hex digits, but all of them together must
275 // specify a value of a single byte (e.g. \x0045 is equivalent
276 // to \x45, and \x1234 is erroneous).
277 //
278 // It also allows escape sequences of the form \uhhhh (exactly four
279 // hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
280 // hex digits, upper or lower case) to specify a Unicode code
281 // point. The dest array will contain the UTF8-encoded version of
282 // that code-point (e.g., if source contains \u2019, then dest will
283 // contain the three bytes 0xE2, 0x80, and 0x99).
284 //
285 // Errors: In the first form of the call, errors are reported with
286 // LOG(ERROR). The same is true for the second form of the call if
287 // the pointer to the string std::vector is nullptr; otherwise, error
288 // messages are stored in the std::vector. In either case, the effect on
289 // the dest array is not defined, but rest of the source will be
290 // processed.
291 // ----------------------------------------------------------------------
292
293 PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
294 PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
295 std::vector<string>* errors);
296
297 // ----------------------------------------------------------------------
298 // UnescapeCEscapeString()
299 // This does the same thing as UnescapeCEscapeSequences, but creates
300 // a new string. The caller does not need to worry about allocating
301 // a dest buffer. This should be used for non performance critical
302 // tasks such as printing debug messages. It is safe for src and dest
303 // to be the same.
304 //
305 // The second call stores its errors in a supplied string vector.
306 // If the string vector pointer is nullptr, it reports the errors with LOG().
307 //
308 // In the first and second calls, the length of dest is returned. In the
309 // the third call, the new string is returned.
310 // ----------------------------------------------------------------------
311
312 PROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
313 PROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
314 std::vector<string>* errors);
315 PROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
316
317 // ----------------------------------------------------------------------
318 // CEscape()
319 // Escapes 'src' using C-style escape sequences and returns the resulting
320 // string.
321 //
322 // Escaped chars: \n, \r, \t, ", ', \, and !isprint().
323 // ----------------------------------------------------------------------
324 PROTOBUF_EXPORT string CEscape(const string& src);
325
326 // ----------------------------------------------------------------------
327 // CEscapeAndAppend()
328 // Escapes 'src' using C-style escape sequences, and appends the escaped
329 // string to 'dest'.
330 // ----------------------------------------------------------------------
331 PROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, string* dest);
332
333 namespace strings {
334 // Like CEscape() but does not escape bytes with the upper bit set.
335 PROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
336
337 // Like CEscape() but uses hex (\x) escapes instead of octals.
338 PROTOBUF_EXPORT string CHexEscape(const string& src);
339 } // namespace strings
340
341 // ----------------------------------------------------------------------
342 // strto32()
343 // strtou32()
344 // strto64()
345 // strtou64()
346 // Architecture-neutral plug compatible replacements for strtol() and
347 // strtoul(). Long's have different lengths on ILP-32 and LP-64
348 // platforms, so using these is safer, from the point of view of
349 // overflow behavior, than using the standard libc functions.
350 // ----------------------------------------------------------------------
351 PROTOBUF_EXPORT int32 strto32_adaptor(const char* nptr, char** endptr,
352 int base);
353 PROTOBUF_EXPORT uint32 strtou32_adaptor(const char* nptr, char** endptr,
354 int base);
355
strto32(const char * nptr,char ** endptr,int base)356 inline int32 strto32(const char *nptr, char **endptr, int base) {
357 if (sizeof(int32) == sizeof(long))
358 return strtol(nptr, endptr, base);
359 else
360 return strto32_adaptor(nptr, endptr, base);
361 }
362
strtou32(const char * nptr,char ** endptr,int base)363 inline uint32 strtou32(const char *nptr, char **endptr, int base) {
364 if (sizeof(uint32) == sizeof(unsigned long))
365 return strtoul(nptr, endptr, base);
366 else
367 return strtou32_adaptor(nptr, endptr, base);
368 }
369
370 // For now, long long is 64-bit on all the platforms we care about, so these
371 // functions can simply pass the call to strto[u]ll.
strto64(const char * nptr,char ** endptr,int base)372 inline int64 strto64(const char *nptr, char **endptr, int base) {
373 GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
374 sizeof_int64_is_not_sizeof_long_long);
375 return strtoll(nptr, endptr, base);
376 }
377
strtou64(const char * nptr,char ** endptr,int base)378 inline uint64 strtou64(const char *nptr, char **endptr, int base) {
379 GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
380 sizeof_uint64_is_not_sizeof_long_long);
381 return strtoull(nptr, endptr, base);
382 }
383
384 // ----------------------------------------------------------------------
385 // safe_strtob()
386 // safe_strto32()
387 // safe_strtou32()
388 // safe_strto64()
389 // safe_strtou64()
390 // safe_strtof()
391 // safe_strtod()
392 // ----------------------------------------------------------------------
393 PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
394
395 PROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value);
396 PROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value);
safe_strto32(const char * str,int32 * value)397 inline bool safe_strto32(const char* str, int32* value) {
398 return safe_strto32(string(str), value);
399 }
safe_strto32(StringPiece str,int32 * value)400 inline bool safe_strto32(StringPiece str, int32* value) {
401 return safe_strto32(str.ToString(), value);
402 }
safe_strtou32(const char * str,uint32 * value)403 inline bool safe_strtou32(const char* str, uint32* value) {
404 return safe_strtou32(string(str), value);
405 }
safe_strtou32(StringPiece str,uint32 * value)406 inline bool safe_strtou32(StringPiece str, uint32* value) {
407 return safe_strtou32(str.ToString(), value);
408 }
409
410 PROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value);
411 PROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value);
safe_strto64(const char * str,int64 * value)412 inline bool safe_strto64(const char* str, int64* value) {
413 return safe_strto64(string(str), value);
414 }
safe_strto64(StringPiece str,int64 * value)415 inline bool safe_strto64(StringPiece str, int64* value) {
416 return safe_strto64(str.ToString(), value);
417 }
safe_strtou64(const char * str,uint64 * value)418 inline bool safe_strtou64(const char* str, uint64* value) {
419 return safe_strtou64(string(str), value);
420 }
safe_strtou64(StringPiece str,uint64 * value)421 inline bool safe_strtou64(StringPiece str, uint64* value) {
422 return safe_strtou64(str.ToString(), value);
423 }
424
425 PROTOBUF_EXPORT bool safe_strtof(const char* str, float* value);
426 PROTOBUF_EXPORT bool safe_strtod(const char* str, double* value);
safe_strtof(const string & str,float * value)427 inline bool safe_strtof(const string& str, float* value) {
428 return safe_strtof(str.c_str(), value);
429 }
safe_strtod(const string & str,double * value)430 inline bool safe_strtod(const string& str, double* value) {
431 return safe_strtod(str.c_str(), value);
432 }
safe_strtof(StringPiece str,float * value)433 inline bool safe_strtof(StringPiece str, float* value) {
434 return safe_strtof(str.ToString(), value);
435 }
safe_strtod(StringPiece str,double * value)436 inline bool safe_strtod(StringPiece str, double* value) {
437 return safe_strtod(str.ToString(), value);
438 }
439
440 // ----------------------------------------------------------------------
441 // FastIntToBuffer()
442 // FastHexToBuffer()
443 // FastHex64ToBuffer()
444 // FastHex32ToBuffer()
445 // FastTimeToBuffer()
446 // These are intended for speed. FastIntToBuffer() assumes the
447 // integer is non-negative. FastHexToBuffer() puts output in
448 // hex rather than decimal. FastTimeToBuffer() puts the output
449 // into RFC822 format.
450 //
451 // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
452 // padded to exactly 16 bytes (plus one byte for '\0')
453 //
454 // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
455 // padded to exactly 8 bytes (plus one byte for '\0')
456 //
457 // All functions take the output buffer as an arg.
458 // They all return a pointer to the beginning of the output,
459 // which may not be the beginning of the input buffer.
460 // ----------------------------------------------------------------------
461
462 // Suggested buffer size for FastToBuffer functions. Also works with
463 // DoubleToBuffer() and FloatToBuffer().
464 static const int kFastToBufferSize = 32;
465
466 PROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
467 PROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
468 char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
469 char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
470 PROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
471 PROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
472 PROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
473
474 // at least 22 bytes long
FastIntToBuffer(int i,char * buffer)475 inline char* FastIntToBuffer(int i, char* buffer) {
476 return (sizeof(i) == 4 ?
477 FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
478 }
FastUIntToBuffer(unsigned int i,char * buffer)479 inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
480 return (sizeof(i) == 4 ?
481 FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
482 }
FastLongToBuffer(long i,char * buffer)483 inline char* FastLongToBuffer(long i, char* buffer) {
484 return (sizeof(i) == 4 ?
485 FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
486 }
FastULongToBuffer(unsigned long i,char * buffer)487 inline char* FastULongToBuffer(unsigned long i, char* buffer) {
488 return (sizeof(i) == 4 ?
489 FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
490 }
491
492 // ----------------------------------------------------------------------
493 // FastInt32ToBufferLeft()
494 // FastUInt32ToBufferLeft()
495 // FastInt64ToBufferLeft()
496 // FastUInt64ToBufferLeft()
497 //
498 // Like the Fast*ToBuffer() functions above, these are intended for speed.
499 // Unlike the Fast*ToBuffer() functions, however, these functions write
500 // their output to the beginning of the buffer (hence the name, as the
501 // output is left-aligned). The caller is responsible for ensuring that
502 // the buffer has enough space to hold the output.
503 //
504 // Returns a pointer to the end of the string (i.e. the null character
505 // terminating the string).
506 // ----------------------------------------------------------------------
507
508 PROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
509 PROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
510 PROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
511 PROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
512
513 // Just define these in terms of the above.
FastUInt32ToBuffer(uint32 i,char * buffer)514 inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
515 FastUInt32ToBufferLeft(i, buffer);
516 return buffer;
517 }
FastUInt64ToBuffer(uint64 i,char * buffer)518 inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
519 FastUInt64ToBufferLeft(i, buffer);
520 return buffer;
521 }
522
SimpleBtoa(bool value)523 inline string SimpleBtoa(bool value) {
524 return value ? "true" : "false";
525 }
526
527 // ----------------------------------------------------------------------
528 // SimpleItoa()
529 // Description: converts an integer to a string.
530 //
531 // Return value: string
532 // ----------------------------------------------------------------------
533 PROTOBUF_EXPORT string SimpleItoa(int i);
534 PROTOBUF_EXPORT string SimpleItoa(unsigned int i);
535 PROTOBUF_EXPORT string SimpleItoa(long i);
536 PROTOBUF_EXPORT string SimpleItoa(unsigned long i);
537 PROTOBUF_EXPORT string SimpleItoa(long long i);
538 PROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
539
540 // ----------------------------------------------------------------------
541 // SimpleDtoa()
542 // SimpleFtoa()
543 // DoubleToBuffer()
544 // FloatToBuffer()
545 // Description: converts a double or float to a string which, if
546 // passed to NoLocaleStrtod(), will produce the exact same original double
547 // (except in case of NaN; all NaNs are considered the same value).
548 // We try to keep the string short but it's not guaranteed to be as
549 // short as possible.
550 //
551 // DoubleToBuffer() and FloatToBuffer() write the text to the given
552 // buffer and return it. The buffer must be at least
553 // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
554 // bytes for floats. kFastToBufferSize is also guaranteed to be large
555 // enough to hold either.
556 //
557 // Return value: string
558 // ----------------------------------------------------------------------
559 PROTOBUF_EXPORT string SimpleDtoa(double value);
560 PROTOBUF_EXPORT string SimpleFtoa(float value);
561
562 PROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
563 PROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
564
565 // In practice, doubles should never need more than 24 bytes and floats
566 // should never need more than 14 (including null terminators), but we
567 // overestimate to be safe.
568 static const int kDoubleToBufferSize = 32;
569 static const int kFloatToBufferSize = 24;
570
571 namespace strings {
572
573 enum PadSpec {
574 NO_PAD = 1,
575 ZERO_PAD_2,
576 ZERO_PAD_3,
577 ZERO_PAD_4,
578 ZERO_PAD_5,
579 ZERO_PAD_6,
580 ZERO_PAD_7,
581 ZERO_PAD_8,
582 ZERO_PAD_9,
583 ZERO_PAD_10,
584 ZERO_PAD_11,
585 ZERO_PAD_12,
586 ZERO_PAD_13,
587 ZERO_PAD_14,
588 ZERO_PAD_15,
589 ZERO_PAD_16,
590 };
591
592 struct Hex {
593 uint64 value;
594 enum PadSpec spec;
595 template <class Int>
596 explicit Hex(Int v, PadSpec s = NO_PAD)
specHex597 : spec(s) {
598 // Prevent sign-extension by casting integers to
599 // their unsigned counterparts.
600 #ifdef LANG_CXX11
601 static_assert(
602 sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
603 "Unknown integer type");
604 #endif
605 value = sizeof(v) == 1 ? static_cast<uint8>(v)
606 : sizeof(v) == 2 ? static_cast<uint16>(v)
607 : sizeof(v) == 4 ? static_cast<uint32>(v)
608 : static_cast<uint64>(v);
609 }
610 };
611
612 struct PROTOBUF_EXPORT AlphaNum {
613 const char *piece_data_; // move these to string_ref eventually
614 size_t piece_size_; // move these to string_ref eventually
615
616 char digits[kFastToBufferSize];
617
618 // No bool ctor -- bools convert to an integral type.
619 // A bool ctor would also convert incoming pointers (bletch).
620
AlphaNumAlphaNum621 AlphaNum(int i32)
622 : piece_data_(digits),
623 piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {}
AlphaNumAlphaNum624 AlphaNum(unsigned int u32)
625 : piece_data_(digits),
626 piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {}
AlphaNumAlphaNum627 AlphaNum(long long i64)
628 : piece_data_(digits),
629 piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
AlphaNumAlphaNum630 AlphaNum(unsigned long long u64)
631 : piece_data_(digits),
632 piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
633
634 // Note: on some architectures, "long" is only 32 bits, not 64, but the
635 // performance hit of using FastInt64ToBufferLeft to handle 32-bit values
636 // is quite minor.
AlphaNumAlphaNum637 AlphaNum(long i64)
638 : piece_data_(digits),
639 piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
AlphaNumAlphaNum640 AlphaNum(unsigned long u64)
641 : piece_data_(digits),
642 piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
643
AlphaNumAlphaNum644 AlphaNum(float f)
645 : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {}
AlphaNumAlphaNum646 AlphaNum(double f)
647 : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {}
648
649 AlphaNum(Hex hex);
650
AlphaNumAlphaNum651 AlphaNum(const char* c_str)
652 : piece_data_(c_str), piece_size_(strlen(c_str)) {}
653 // TODO: Add a string_ref constructor, eventually
654 // AlphaNum(const StringPiece &pc) : piece(pc) {}
655
AlphaNumAlphaNum656 AlphaNum(const string& str)
657 : piece_data_(str.data()), piece_size_(str.size()) {}
658
AlphaNumAlphaNum659 AlphaNum(StringPiece str)
660 : piece_data_(str.data()), piece_size_(str.size()) {}
661
AlphaNumAlphaNum662 AlphaNum(internal::StringPiecePod str)
663 : piece_data_(str.data()), piece_size_(str.size()) {}
664
sizeAlphaNum665 size_t size() const { return piece_size_; }
dataAlphaNum666 const char *data() const { return piece_data_; }
667
668 private:
669 // Use ":" not ':'
670 AlphaNum(char c); // NOLINT(runtime/explicit)
671
672 // Disallow copy and assign.
673 AlphaNum(const AlphaNum&);
674 void operator=(const AlphaNum&);
675 };
676
677 } // namespace strings
678
679 using strings::AlphaNum;
680
681 // ----------------------------------------------------------------------
682 // StrCat()
683 // This merges the given strings or numbers, with no delimiter. This
684 // is designed to be the fastest possible way to construct a string out
685 // of a mix of raw C strings, strings, bool values,
686 // and numeric values.
687 //
688 // Don't use this for user-visible strings. The localization process
689 // works poorly on strings built up out of fragments.
690 //
691 // For clarity and performance, don't use StrCat when appending to a
692 // string. In particular, avoid using any of these (anti-)patterns:
693 // str.append(StrCat(...)
694 // str += StrCat(...)
695 // str = StrCat(str, ...)
696 // where the last is the worse, with the potential to change a loop
697 // from a linear time operation with O(1) dynamic allocations into a
698 // quadratic time operation with O(n) dynamic allocations. StrAppend
699 // is a better choice than any of the above, subject to the restriction
700 // of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may
701 // be a reference into str.
702 // ----------------------------------------------------------------------
703
704 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b);
705 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
706 const AlphaNum& c);
707 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
708 const AlphaNum& c, const AlphaNum& d);
709 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
710 const AlphaNum& c, const AlphaNum& d,
711 const AlphaNum& e);
712 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
713 const AlphaNum& c, const AlphaNum& d,
714 const AlphaNum& e, const AlphaNum& f);
715 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
716 const AlphaNum& c, const AlphaNum& d,
717 const AlphaNum& e, const AlphaNum& f,
718 const AlphaNum& g);
719 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
720 const AlphaNum& c, const AlphaNum& d,
721 const AlphaNum& e, const AlphaNum& f,
722 const AlphaNum& g, const AlphaNum& h);
723 PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
724 const AlphaNum& c, const AlphaNum& d,
725 const AlphaNum& e, const AlphaNum& f,
726 const AlphaNum& g, const AlphaNum& h,
727 const AlphaNum& i);
728
StrCat(const AlphaNum & a)729 inline string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); }
730
731 // ----------------------------------------------------------------------
732 // StrAppend()
733 // Same as above, but adds the output to the given string.
734 // WARNING: For speed, StrAppend does not try to check each of its input
735 // arguments to be sure that they are not a subset of the string being
736 // appended to. That is, while this will work:
737 //
738 // string s = "foo";
739 // s += s;
740 //
741 // This will not (necessarily) work:
742 //
743 // string s = "foo";
744 // StrAppend(&s, s);
745 //
746 // Note: while StrCat supports appending up to 9 arguments, StrAppend
747 // is currently limited to 4. That's rarely an issue except when
748 // automatically transforming StrCat to StrAppend, and can easily be
749 // worked around as consecutive calls to StrAppend are quite efficient.
750 // ----------------------------------------------------------------------
751
752 PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a);
753 PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
754 const AlphaNum& b);
755 PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
756 const AlphaNum& b, const AlphaNum& c);
757 PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
758 const AlphaNum& b, const AlphaNum& c,
759 const AlphaNum& d);
760
761 // ----------------------------------------------------------------------
762 // Join()
763 // These methods concatenate a range of components into a C++ string, using
764 // the C-string "delim" as a separator between components.
765 // ----------------------------------------------------------------------
766 template <typename Iterator>
Join(Iterator start,Iterator end,const char * delim,string * result)767 void Join(Iterator start, Iterator end,
768 const char* delim, string* result) {
769 for (Iterator it = start; it != end; ++it) {
770 if (it != start) {
771 result->append(delim);
772 }
773 StrAppend(result, *it);
774 }
775 }
776
777 template <typename Range>
Join(const Range & components,const char * delim)778 string Join(const Range& components,
779 const char* delim) {
780 string result;
781 Join(components.begin(), components.end(), delim, &result);
782 return result;
783 }
784
785 // ----------------------------------------------------------------------
786 // ToHex()
787 // Return a lower-case hex string representation of the given integer.
788 // ----------------------------------------------------------------------
789 PROTOBUF_EXPORT string ToHex(uint64 num);
790
791 // ----------------------------------------------------------------------
792 // GlobalReplaceSubstring()
793 // Replaces all instances of a substring in a string. Does nothing
794 // if 'substring' is empty. Returns the number of replacements.
795 //
796 // NOTE: The string pieces must not overlap s.
797 // ----------------------------------------------------------------------
798 PROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring,
799 const string& replacement,
800 string* s);
801
802 // ----------------------------------------------------------------------
803 // Base64Unescape()
804 // Converts "src" which is encoded in Base64 to its binary equivalent and
805 // writes it to "dest". If src contains invalid characters, dest is cleared
806 // and the function returns false. Returns true on success.
807 // ----------------------------------------------------------------------
808 PROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest);
809
810 // ----------------------------------------------------------------------
811 // WebSafeBase64Unescape()
812 // This is a variation of Base64Unescape which uses '-' instead of '+', and
813 // '_' instead of '/'. src is not null terminated, instead specify len. I
814 // recommend that slen<szdest, but we honor szdest anyway.
815 // RETURNS the length of dest, or -1 if src contains invalid chars.
816
817 // The variation that stores into a string clears the string first, and
818 // returns false (with dest empty) if src contains invalid chars; for
819 // this version src and dest must be different strings.
820 // ----------------------------------------------------------------------
821 PROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen, char* dest,
822 int szdest);
823 PROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest);
824
825 // Return the length to use for the output buffer given to the base64 escape
826 // routines. Make sure to use the same value for do_padding in both.
827 // This function may return incorrect results if given input_len values that
828 // are extremely high, which should happen rarely.
829 PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len, bool do_padding);
830 // Use this version when calling Base64Escape without a do_padding arg.
831 PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len);
832
833 // ----------------------------------------------------------------------
834 // Base64Escape()
835 // WebSafeBase64Escape()
836 // Encode "src" to "dest" using base64 encoding.
837 // src is not null terminated, instead specify len.
838 // 'dest' should have at least CalculateBase64EscapedLen() length.
839 // RETURNS the length of dest.
840 // The WebSafe variation use '-' instead of '+' and '_' instead of '/'
841 // so that we can place the out in the URL or cookies without having
842 // to escape them. It also has an extra parameter "do_padding",
843 // which when set to false will prevent padding with "=".
844 // ----------------------------------------------------------------------
845 PROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen, char* dest,
846 int szdest);
847 PROTOBUF_EXPORT int WebSafeBase64Escape(const unsigned char* src, int slen,
848 char* dest, int szdest,
849 bool do_padding);
850 // Encode src into dest with padding.
851 PROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest);
852 // Encode src into dest web-safely without padding.
853 PROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest);
854 // Encode src into dest web-safely with padding.
855 PROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src,
856 string* dest);
857
858 PROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
859 string* dest, bool do_padding);
860 PROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
861 string* dest, bool do_padding);
862
IsValidCodePoint(uint32 code_point)863 inline bool IsValidCodePoint(uint32 code_point) {
864 return code_point < 0xD800 ||
865 (code_point >= 0xE000 && code_point <= 0x10FFFF);
866 }
867
868 static const int UTFmax = 4;
869 // ----------------------------------------------------------------------
870 // EncodeAsUTF8Char()
871 // Helper to append a Unicode code point to a string as UTF8, without bringing
872 // in any external dependencies. The output buffer must be as least 4 bytes
873 // large.
874 // ----------------------------------------------------------------------
875 PROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
876
877 // ----------------------------------------------------------------------
878 // UTF8FirstLetterNumBytes()
879 // Length of the first UTF-8 character.
880 // ----------------------------------------------------------------------
881 PROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len);
882
883 // From google3/third_party/absl/strings/escaping.h
884
885 // ----------------------------------------------------------------------
886 // CleanStringLineEndings()
887 // Clean up a multi-line string to conform to Unix line endings.
888 // Reads from src and appends to dst, so usually dst should be empty.
889 //
890 // If there is no line ending at the end of a non-empty string, it can
891 // be added automatically.
892 //
893 // Four different types of input are correctly handled:
894 //
895 // - Unix/Linux files: line ending is LF: pass through unchanged
896 //
897 // - DOS/Windows files: line ending is CRLF: convert to LF
898 //
899 // - Legacy Mac files: line ending is CR: convert to LF
900 //
901 // - Garbled files: random line endings: convert gracefully
902 // lonely CR, lonely LF, CRLF: convert to LF
903 //
904 // @param src The multi-line string to convert
905 // @param dst The converted string is appended to this string
906 // @param auto_end_last_line Automatically terminate the last line
907 //
908 // Limitations:
909 //
910 // This does not do the right thing for CRCRLF files created by
911 // broken programs that do another Unix->DOS conversion on files
912 // that are already in CRLF format. For this, a two-pass approach
913 // brute-force would be needed that
914 //
915 // (1) determines the presence of LF (first one is ok)
916 // (2) if yes, removes any CR, else convert every CR to LF
917 PROTOBUF_EXPORT void CleanStringLineEndings(const string& src, string* dst,
918 bool auto_end_last_line);
919
920 // Same as above, but transforms the argument in place.
921 PROTOBUF_EXPORT void CleanStringLineEndings(string* str,
922 bool auto_end_last_line);
923
924 namespace strings {
EndsWith(StringPiece text,StringPiece suffix)925 inline bool EndsWith(StringPiece text, StringPiece suffix) {
926 return suffix.empty() ||
927 (text.size() >= suffix.size() &&
928 memcmp(text.data() + (text.size() - suffix.size()), suffix.data(),
929 suffix.size()) == 0);
930 }
931 } // namespace strings
932
933 namespace internal {
934
935 // A locale-independent version of the standard strtod(), which always
936 // uses a dot as the decimal separator.
937 double NoLocaleStrtod(const char* str, char** endptr);
938
939 } // namespace internal
940
941 } // namespace protobuf
942 } // namespace google
943
944 #include <google/protobuf/port_undef.inc>
945
946 #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
947