strutil.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. // from google3/strings/strutil.h
  31. #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
  32. #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
  33. #include <stdlib.h>
  34. #include <vector>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/stubs/stringpiece.h>
  37. namespace google {
  38. namespace protobuf {
  39. #ifdef _MSC_VER
  40. #define strtoll _strtoi64
  41. #define strtoull _strtoui64
  42. #elif defined(__DECCXX) && defined(__osf__)
  43. // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
  44. #define strtoll strtol
  45. #define strtoull strtoul
  46. #endif
  47. // ----------------------------------------------------------------------
  48. // ascii_isalnum()
  49. // Check if an ASCII character is alphanumeric. We can't use ctype's
  50. // isalnum() because it is affected by locale. This function is applied
  51. // to identifiers in the protocol buffer language, not to natural-language
  52. // strings, so locale should not be taken into account.
  53. // ascii_isdigit()
  54. // Like above, but only accepts digits.
  55. // ascii_isspace()
  56. // Check if the character is a space character.
  57. // ----------------------------------------------------------------------
  58. inline bool ascii_isalnum(char c) {
  59. return ('a' <= c && c <= 'z') ||
  60. ('A' <= c && c <= 'Z') ||
  61. ('0' <= c && c <= '9');
  62. }
  63. inline bool ascii_isdigit(char c) {
  64. return ('0' <= c && c <= '9');
  65. }
  66. inline bool ascii_isspace(char c) {
  67. return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
  68. c == '\r';
  69. }
  70. inline bool ascii_isupper(char c) {
  71. return c >= 'A' && c <= 'Z';
  72. }
  73. inline bool ascii_islower(char c) {
  74. return c >= 'a' && c <= 'z';
  75. }
  76. inline char ascii_toupper(char c) {
  77. return ascii_islower(c) ? c - ('a' - 'A') : c;
  78. }
  79. inline char ascii_tolower(char c) {
  80. return ascii_isupper(c) ? c + ('a' - 'A') : c;
  81. }
  82. inline int hex_digit_to_int(char c) {
  83. /* Assume ASCII. */
  84. int x = static_cast<unsigned char>(c);
  85. if (x > '9') {
  86. x += 9;
  87. }
  88. return x & 0xf;
  89. }
  90. // ----------------------------------------------------------------------
  91. // HasPrefixString()
  92. // Check if a string begins with a given prefix.
  93. // StripPrefixString()
  94. // Given a string and a putative prefix, returns the string minus the
  95. // prefix string if the prefix matches, otherwise the original
  96. // string.
  97. // ----------------------------------------------------------------------
  98. inline bool HasPrefixString(const string& str,
  99. const string& prefix) {
  100. return str.size() >= prefix.size() &&
  101. str.compare(0, prefix.size(), prefix) == 0;
  102. }
  103. inline string StripPrefixString(const string& str, const string& prefix) {
  104. if (HasPrefixString(str, prefix)) {
  105. return str.substr(prefix.size());
  106. } else {
  107. return str;
  108. }
  109. }
  110. // ----------------------------------------------------------------------
  111. // HasSuffixString()
  112. // Return true if str ends in suffix.
  113. // StripSuffixString()
  114. // Given a string and a putative suffix, returns the string minus the
  115. // suffix string if the suffix matches, otherwise the original
  116. // string.
  117. // ----------------------------------------------------------------------
  118. inline bool HasSuffixString(const string& str,
  119. const string& suffix) {
  120. return str.size() >= suffix.size() &&
  121. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  122. }
  123. inline string StripSuffixString(const string& str, const string& suffix) {
  124. if (HasSuffixString(str, suffix)) {
  125. return str.substr(0, str.size() - suffix.size());
  126. } else {
  127. return str;
  128. }
  129. }
  130. // ----------------------------------------------------------------------
  131. // StripString
  132. // Replaces any occurrence of the character 'remove' (or the characters
  133. // in 'remove') with the character 'replacewith'.
  134. // Good for keeping html characters or protocol characters (\t) out
  135. // of places where they might cause a problem.
  136. // StripWhitespace
  137. // Removes whitespaces from both ends of the given string.
  138. // ----------------------------------------------------------------------
  139. LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
  140. char replacewith);
  141. LIBPROTOBUF_EXPORT void StripWhitespace(string* s);
  142. // ----------------------------------------------------------------------
  143. // LowerString()
  144. // UpperString()
  145. // ToUpper()
  146. // Convert the characters in "s" to lowercase or uppercase. ASCII-only:
  147. // these functions intentionally ignore locale because they are applied to
  148. // identifiers used in the Protocol Buffer language, not to natural-language
  149. // strings.
  150. // ----------------------------------------------------------------------
  151. inline void LowerString(string * s) {
  152. string::iterator end = s->end();
  153. for (string::iterator i = s->begin(); i != end; ++i) {
  154. // tolower() changes based on locale. We don't want this!
  155. if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
  156. }
  157. }
  158. inline void UpperString(string * s) {
  159. string::iterator end = s->end();
  160. for (string::iterator i = s->begin(); i != end; ++i) {
  161. // toupper() changes based on locale. We don't want this!
  162. if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
  163. }
  164. }
  165. inline string ToUpper(const string& s) {
  166. string out = s;
  167. UpperString(&out);
  168. return out;
  169. }
  170. // ----------------------------------------------------------------------
  171. // StringReplace()
  172. // Give me a string and two patterns "old" and "new", and I replace
  173. // the first instance of "old" in the string with "new", if it
  174. // exists. RETURN a new string, regardless of whether the replacement
  175. // happened or not.
  176. // ----------------------------------------------------------------------
  177. LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
  178. const string& newsub, bool replace_all);
  179. // ----------------------------------------------------------------------
  180. // SplitStringUsing()
  181. // Split a string using a character delimiter. Append the components
  182. // to 'result'. If there are consecutive delimiters, this function skips
  183. // over all of them.
  184. // ----------------------------------------------------------------------
  185. LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
  186. vector<string>* res);
  187. // Split a string using one or more byte delimiters, presented
  188. // as a nul-terminated c string. Append the components to 'result'.
  189. // If there are consecutive delimiters, this function will return
  190. // corresponding empty strings. If you want to drop the empty
  191. // strings, try SplitStringUsing().
  192. //
  193. // If "full" is the empty string, yields an empty string as the only value.
  194. // ----------------------------------------------------------------------
  195. LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full,
  196. const char* delim,
  197. vector<string>* result);
  198. // ----------------------------------------------------------------------
  199. // Split()
  200. // Split a string using a character delimiter.
  201. // ----------------------------------------------------------------------
  202. inline vector<string> Split(
  203. const string& full, const char* delim, bool skip_empty = true) {
  204. vector<string> result;
  205. if (skip_empty) {
  206. SplitStringUsing(full, delim, &result);
  207. } else {
  208. SplitStringAllowEmpty(full, delim, &result);
  209. }
  210. return result;
  211. }
  212. // ----------------------------------------------------------------------
  213. // JoinStrings()
  214. // These methods concatenate a vector of strings into a C++ string, using
  215. // the C-string "delim" as a separator between components. There are two
  216. // flavors of the function, one flavor returns the concatenated string,
  217. // another takes a pointer to the target string. In the latter case the
  218. // target string is cleared and overwritten.
  219. // ----------------------------------------------------------------------
  220. LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
  221. const char* delim, string* result);
  222. inline string JoinStrings(const vector<string>& components,
  223. const char* delim) {
  224. string result;
  225. JoinStrings(components, delim, &result);
  226. return result;
  227. }
  228. // ----------------------------------------------------------------------
  229. // UnescapeCEscapeSequences()
  230. // Copies "source" to "dest", rewriting C-style escape sequences
  231. // -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
  232. // equivalents. "dest" must be sufficiently large to hold all
  233. // the characters in the rewritten string (i.e. at least as large
  234. // as strlen(source) + 1 should be safe, since the replacements
  235. // are always shorter than the original escaped sequences). It's
  236. // safe for source and dest to be the same. RETURNS the length
  237. // of dest.
  238. //
  239. // It allows hex sequences \xhh, or generally \xhhhhh with an
  240. // arbitrary number of hex digits, but all of them together must
  241. // specify a value of a single byte (e.g. \x0045 is equivalent
  242. // to \x45, and \x1234 is erroneous).
  243. //
  244. // It also allows escape sequences of the form \uhhhh (exactly four
  245. // hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
  246. // hex digits, upper or lower case) to specify a Unicode code
  247. // point. The dest array will contain the UTF8-encoded version of
  248. // that code-point (e.g., if source contains \u2019, then dest will
  249. // contain the three bytes 0xE2, 0x80, and 0x99).
  250. //
  251. // Errors: In the first form of the call, errors are reported with
  252. // LOG(ERROR). The same is true for the second form of the call if
  253. // the pointer to the string vector is NULL; otherwise, error
  254. // messages are stored in the vector. In either case, the effect on
  255. // the dest array is not defined, but rest of the source will be
  256. // processed.
  257. // ----------------------------------------------------------------------
  258. LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
  259. LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
  260. vector<string> *errors);
  261. // ----------------------------------------------------------------------
  262. // UnescapeCEscapeString()
  263. // This does the same thing as UnescapeCEscapeSequences, but creates
  264. // a new string. The caller does not need to worry about allocating
  265. // a dest buffer. This should be used for non performance critical
  266. // tasks such as printing debug messages. It is safe for src and dest
  267. // to be the same.
  268. //
  269. // The second call stores its errors in a supplied string vector.
  270. // If the string vector pointer is NULL, it reports the errors with LOG().
  271. //
  272. // In the first and second calls, the length of dest is returned. In the
  273. // the third call, the new string is returned.
  274. // ----------------------------------------------------------------------
  275. LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
  276. LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
  277. vector<string> *errors);
  278. LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
  279. // ----------------------------------------------------------------------
  280. // CEscapeString()
  281. // Copies 'src' to 'dest', escaping dangerous characters using
  282. // C-style escape sequences. This is very useful for preparing query
  283. // flags. 'src' and 'dest' should not overlap.
  284. // Returns the number of bytes written to 'dest' (not including the \0)
  285. // or -1 if there was insufficient space.
  286. //
  287. // Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped.
  288. // ----------------------------------------------------------------------
  289. LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
  290. char* dest, int dest_len);
  291. // ----------------------------------------------------------------------
  292. // CEscape()
  293. // More convenient form of CEscapeString: returns result as a "string".
  294. // This version is slower than CEscapeString() because it does more
  295. // allocation. However, it is much more convenient to use in
  296. // non-speed-critical code like logging messages etc.
  297. // ----------------------------------------------------------------------
  298. LIBPROTOBUF_EXPORT string CEscape(const string& src);
  299. namespace strings {
  300. // Like CEscape() but does not escape bytes with the upper bit set.
  301. LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
  302. // Like CEscape() but uses hex (\x) escapes instead of octals.
  303. LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
  304. } // namespace strings
  305. // ----------------------------------------------------------------------
  306. // strto32()
  307. // strtou32()
  308. // strto64()
  309. // strtou64()
  310. // Architecture-neutral plug compatible replacements for strtol() and
  311. // strtoul(). Long's have different lengths on ILP-32 and LP-64
  312. // platforms, so using these is safer, from the point of view of
  313. // overflow behavior, than using the standard libc functions.
  314. // ----------------------------------------------------------------------
  315. LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr,
  316. int base);
  317. LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr,
  318. int base);
  319. inline int32 strto32(const char *nptr, char **endptr, int base) {
  320. if (sizeof(int32) == sizeof(long))
  321. return strtol(nptr, endptr, base);
  322. else
  323. return strto32_adaptor(nptr, endptr, base);
  324. }
  325. inline uint32 strtou32(const char *nptr, char **endptr, int base) {
  326. if (sizeof(uint32) == sizeof(unsigned long))
  327. return strtoul(nptr, endptr, base);
  328. else
  329. return strtou32_adaptor(nptr, endptr, base);
  330. }
  331. // For now, long long is 64-bit on all the platforms we care about, so these
  332. // functions can simply pass the call to strto[u]ll.
  333. inline int64 strto64(const char *nptr, char **endptr, int base) {
  334. GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
  335. sizeof_int64_is_not_sizeof_long_long);
  336. return strtoll(nptr, endptr, base);
  337. }
  338. inline uint64 strtou64(const char *nptr, char **endptr, int base) {
  339. GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
  340. sizeof_uint64_is_not_sizeof_long_long);
  341. return strtoull(nptr, endptr, base);
  342. }
  343. // ----------------------------------------------------------------------
  344. // safe_strtob()
  345. // safe_strto32()
  346. // safe_strtou32()
  347. // safe_strto64()
  348. // safe_strtou64()
  349. // safe_strtof()
  350. // safe_strtod()
  351. // ----------------------------------------------------------------------
  352. LIBPROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
  353. LIBPROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value);
  354. LIBPROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value);
  355. inline bool safe_strto32(const char* str, int32* value) {
  356. return safe_strto32(string(str), value);
  357. }
  358. inline bool safe_strto32(StringPiece str, int32* value) {
  359. return safe_strto32(str.ToString(), value);
  360. }
  361. inline bool safe_strtou32(const char* str, uint32* value) {
  362. return safe_strtou32(string(str), value);
  363. }
  364. inline bool safe_strtou32(StringPiece str, uint32* value) {
  365. return safe_strtou32(str.ToString(), value);
  366. }
  367. LIBPROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value);
  368. LIBPROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value);
  369. inline bool safe_strto64(const char* str, int64* value) {
  370. return safe_strto64(string(str), value);
  371. }
  372. inline bool safe_strto64(StringPiece str, int64* value) {
  373. return safe_strto64(str.ToString(), value);
  374. }
  375. inline bool safe_strtou64(const char* str, uint64* value) {
  376. return safe_strtou64(string(str), value);
  377. }
  378. inline bool safe_strtou64(StringPiece str, uint64* value) {
  379. return safe_strtou64(str.ToString(), value);
  380. }
  381. LIBPROTOBUF_EXPORT bool safe_strtof(const char* str, float* value);
  382. LIBPROTOBUF_EXPORT bool safe_strtod(const char* str, double* value);
  383. inline bool safe_strtof(const string& str, float* value) {
  384. return safe_strtof(str.c_str(), value);
  385. }
  386. inline bool safe_strtod(const string& str, double* value) {
  387. return safe_strtod(str.c_str(), value);
  388. }
  389. inline bool safe_strtof(StringPiece str, float* value) {
  390. return safe_strtof(str.ToString(), value);
  391. }
  392. inline bool safe_strtod(StringPiece str, double* value) {
  393. return safe_strtod(str.ToString(), value);
  394. }
  395. // ----------------------------------------------------------------------
  396. // FastIntToBuffer()
  397. // FastHexToBuffer()
  398. // FastHex64ToBuffer()
  399. // FastHex32ToBuffer()
  400. // FastTimeToBuffer()
  401. // These are intended for speed. FastIntToBuffer() assumes the
  402. // integer is non-negative. FastHexToBuffer() puts output in
  403. // hex rather than decimal. FastTimeToBuffer() puts the output
  404. // into RFC822 format.
  405. //
  406. // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
  407. // padded to exactly 16 bytes (plus one byte for '\0')
  408. //
  409. // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
  410. // padded to exactly 8 bytes (plus one byte for '\0')
  411. //
  412. // All functions take the output buffer as an arg.
  413. // They all return a pointer to the beginning of the output,
  414. // which may not be the beginning of the input buffer.
  415. // ----------------------------------------------------------------------
  416. // Suggested buffer size for FastToBuffer functions. Also works with
  417. // DoubleToBuffer() and FloatToBuffer().
  418. static const int kFastToBufferSize = 32;
  419. LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
  420. LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
  421. char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
  422. char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
  423. LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
  424. LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
  425. LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
  426. // at least 22 bytes long
  427. inline char* FastIntToBuffer(int i, char* buffer) {
  428. return (sizeof(i) == 4 ?
  429. FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
  430. }
  431. inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
  432. return (sizeof(i) == 4 ?
  433. FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
  434. }
  435. inline char* FastLongToBuffer(long i, char* buffer) {
  436. return (sizeof(i) == 4 ?
  437. FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
  438. }
  439. inline char* FastULongToBuffer(unsigned long i, char* buffer) {
  440. return (sizeof(i) == 4 ?
  441. FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
  442. }
  443. // ----------------------------------------------------------------------
  444. // FastInt32ToBufferLeft()
  445. // FastUInt32ToBufferLeft()
  446. // FastInt64ToBufferLeft()
  447. // FastUInt64ToBufferLeft()
  448. //
  449. // Like the Fast*ToBuffer() functions above, these are intended for speed.
  450. // Unlike the Fast*ToBuffer() functions, however, these functions write
  451. // their output to the beginning of the buffer (hence the name, as the
  452. // output is left-aligned). The caller is responsible for ensuring that
  453. // the buffer has enough space to hold the output.
  454. //
  455. // Returns a pointer to the end of the string (i.e. the null character
  456. // terminating the string).
  457. // ----------------------------------------------------------------------
  458. LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
  459. LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
  460. LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
  461. LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
  462. // Just define these in terms of the above.
  463. inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
  464. FastUInt32ToBufferLeft(i, buffer);
  465. return buffer;
  466. }
  467. inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
  468. FastUInt64ToBufferLeft(i, buffer);
  469. return buffer;
  470. }
  471. inline string SimpleBtoa(bool value) {
  472. return value ? "true" : "false";
  473. }
  474. // ----------------------------------------------------------------------
  475. // SimpleItoa()
  476. // Description: converts an integer to a string.
  477. //
  478. // Return value: string
  479. // ----------------------------------------------------------------------
  480. LIBPROTOBUF_EXPORT string SimpleItoa(int i);
  481. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
  482. LIBPROTOBUF_EXPORT string SimpleItoa(long i);
  483. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
  484. LIBPROTOBUF_EXPORT string SimpleItoa(long long i);
  485. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
  486. // ----------------------------------------------------------------------
  487. // SimpleDtoa()
  488. // SimpleFtoa()
  489. // DoubleToBuffer()
  490. // FloatToBuffer()
  491. // Description: converts a double or float to a string which, if
  492. // passed to NoLocaleStrtod(), will produce the exact same original double
  493. // (except in case of NaN; all NaNs are considered the same value).
  494. // We try to keep the string short but it's not guaranteed to be as
  495. // short as possible.
  496. //
  497. // DoubleToBuffer() and FloatToBuffer() write the text to the given
  498. // buffer and return it. The buffer must be at least
  499. // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
  500. // bytes for floats. kFastToBufferSize is also guaranteed to be large
  501. // enough to hold either.
  502. //
  503. // Return value: string
  504. // ----------------------------------------------------------------------
  505. LIBPROTOBUF_EXPORT string SimpleDtoa(double value);
  506. LIBPROTOBUF_EXPORT string SimpleFtoa(float value);
  507. LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
  508. LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
  509. // In practice, doubles should never need more than 24 bytes and floats
  510. // should never need more than 14 (including null terminators), but we
  511. // overestimate to be safe.
  512. static const int kDoubleToBufferSize = 32;
  513. static const int kFloatToBufferSize = 24;
  514. namespace strings {
  515. enum PadSpec {
  516. NO_PAD = 1,
  517. ZERO_PAD_2,
  518. ZERO_PAD_3,
  519. ZERO_PAD_4,
  520. ZERO_PAD_5,
  521. ZERO_PAD_6,
  522. ZERO_PAD_7,
  523. ZERO_PAD_8,
  524. ZERO_PAD_9,
  525. ZERO_PAD_10,
  526. ZERO_PAD_11,
  527. ZERO_PAD_12,
  528. ZERO_PAD_13,
  529. ZERO_PAD_14,
  530. ZERO_PAD_15,
  531. ZERO_PAD_16,
  532. };
  533. struct Hex {
  534. uint64 value;
  535. enum PadSpec spec;
  536. template <class Int>
  537. explicit Hex(Int v, PadSpec s = NO_PAD)
  538. : spec(s) {
  539. // Prevent sign-extension by casting integers to
  540. // their unsigned counterparts.
  541. #ifdef LANG_CXX11
  542. static_assert(
  543. sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
  544. "Unknown integer type");
  545. #endif
  546. value = sizeof(v) == 1 ? static_cast<uint8>(v)
  547. : sizeof(v) == 2 ? static_cast<uint16>(v)
  548. : sizeof(v) == 4 ? static_cast<uint32>(v)
  549. : static_cast<uint64>(v);
  550. }
  551. };
  552. struct LIBPROTOBUF_EXPORT AlphaNum {
  553. const char *piece_data_; // move these to string_ref eventually
  554. size_t piece_size_; // move these to string_ref eventually
  555. char digits[kFastToBufferSize];
  556. // No bool ctor -- bools convert to an integral type.
  557. // A bool ctor would also convert incoming pointers (bletch).
  558. AlphaNum(int32 i32)
  559. : piece_data_(digits),
  560. piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {}
  561. AlphaNum(uint32 u32)
  562. : piece_data_(digits),
  563. piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {}
  564. AlphaNum(int64 i64)
  565. : piece_data_(digits),
  566. piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
  567. AlphaNum(uint64 u64)
  568. : piece_data_(digits),
  569. piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
  570. AlphaNum(float f)
  571. : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {}
  572. AlphaNum(double f)
  573. : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {}
  574. AlphaNum(Hex hex);
  575. AlphaNum(const char* c_str)
  576. : piece_data_(c_str), piece_size_(strlen(c_str)) {}
  577. // TODO: Add a string_ref constructor, eventually
  578. // AlphaNum(const StringPiece &pc) : piece(pc) {}
  579. AlphaNum(const string& str)
  580. : piece_data_(str.data()), piece_size_(str.size()) {}
  581. AlphaNum(StringPiece str)
  582. : piece_data_(str.data()), piece_size_(str.size()) {}
  583. size_t size() const { return piece_size_; }
  584. const char *data() const { return piece_data_; }
  585. private:
  586. // Use ":" not ':'
  587. AlphaNum(char c); // NOLINT(runtime/explicit)
  588. // Disallow copy and assign.
  589. AlphaNum(const AlphaNum&);
  590. void operator=(const AlphaNum&);
  591. };
  592. } // namespace strings
  593. using strings::AlphaNum;
  594. // ----------------------------------------------------------------------
  595. // StrCat()
  596. // This merges the given strings or numbers, with no delimiter. This
  597. // is designed to be the fastest possible way to construct a string out
  598. // of a mix of raw C strings, strings, bool values,
  599. // and numeric values.
  600. //
  601. // Don't use this for user-visible strings. The localization process
  602. // works poorly on strings built up out of fragments.
  603. //
  604. // For clarity and performance, don't use StrCat when appending to a
  605. // string. In particular, avoid using any of these (anti-)patterns:
  606. // str.append(StrCat(...)
  607. // str += StrCat(...)
  608. // str = StrCat(str, ...)
  609. // where the last is the worse, with the potential to change a loop
  610. // from a linear time operation with O(1) dynamic allocations into a
  611. // quadratic time operation with O(n) dynamic allocations. StrAppend
  612. // is a better choice than any of the above, subject to the restriction
  613. // of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may
  614. // be a reference into str.
  615. // ----------------------------------------------------------------------
  616. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b);
  617. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  618. const AlphaNum& c);
  619. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  620. const AlphaNum& c, const AlphaNum& d);
  621. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  622. const AlphaNum& c, const AlphaNum& d,
  623. const AlphaNum& e);
  624. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  625. const AlphaNum& c, const AlphaNum& d,
  626. const AlphaNum& e, const AlphaNum& f);
  627. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  628. const AlphaNum& c, const AlphaNum& d,
  629. const AlphaNum& e, const AlphaNum& f,
  630. const AlphaNum& g);
  631. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  632. const AlphaNum& c, const AlphaNum& d,
  633. const AlphaNum& e, const AlphaNum& f,
  634. const AlphaNum& g, const AlphaNum& h);
  635. LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
  636. const AlphaNum& c, const AlphaNum& d,
  637. const AlphaNum& e, const AlphaNum& f,
  638. const AlphaNum& g, const AlphaNum& h,
  639. const AlphaNum& i);
  640. inline string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); }
  641. // ----------------------------------------------------------------------
  642. // StrAppend()
  643. // Same as above, but adds the output to the given string.
  644. // WARNING: For speed, StrAppend does not try to check each of its input
  645. // arguments to be sure that they are not a subset of the string being
  646. // appended to. That is, while this will work:
  647. //
  648. // string s = "foo";
  649. // s += s;
  650. //
  651. // This will not (necessarily) work:
  652. //
  653. // string s = "foo";
  654. // StrAppend(&s, s);
  655. //
  656. // Note: while StrCat supports appending up to 9 arguments, StrAppend
  657. // is currently limited to 4. That's rarely an issue except when
  658. // automatically transforming StrCat to StrAppend, and can easily be
  659. // worked around as consecutive calls to StrAppend are quite efficient.
  660. // ----------------------------------------------------------------------
  661. LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a);
  662. LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
  663. const AlphaNum& b);
  664. LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
  665. const AlphaNum& b, const AlphaNum& c);
  666. LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
  667. const AlphaNum& b, const AlphaNum& c,
  668. const AlphaNum& d);
  669. // ----------------------------------------------------------------------
  670. // Join()
  671. // These methods concatenate a range of components into a C++ string, using
  672. // the C-string "delim" as a separator between components.
  673. // ----------------------------------------------------------------------
  674. template <typename Iterator>
  675. void Join(Iterator start, Iterator end,
  676. const char* delim, string* result) {
  677. for (Iterator it = start; it != end; ++it) {
  678. if (it != start) {
  679. result->append(delim);
  680. }
  681. StrAppend(result, *it);
  682. }
  683. }
  684. template <typename Range>
  685. string Join(const Range& components,
  686. const char* delim) {
  687. string result;
  688. Join(components.begin(), components.end(), delim, &result);
  689. return result;
  690. }
  691. // ----------------------------------------------------------------------
  692. // ToHex()
  693. // Return a lower-case hex string representation of the given integer.
  694. // ----------------------------------------------------------------------
  695. LIBPROTOBUF_EXPORT string ToHex(uint64 num);
  696. // ----------------------------------------------------------------------
  697. // GlobalReplaceSubstring()
  698. // Replaces all instances of a substring in a string. Does nothing
  699. // if 'substring' is empty. Returns the number of replacements.
  700. //
  701. // NOTE: The string pieces must not overlap s.
  702. // ----------------------------------------------------------------------
  703. LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring,
  704. const string& replacement,
  705. string* s);
  706. // ----------------------------------------------------------------------
  707. // Base64Unescape()
  708. // Converts "src" which is encoded in Base64 to its binary equivalent and
  709. // writes it to "dest". If src contains invalid characters, dest is cleared
  710. // and the function returns false. Returns true on success.
  711. // ----------------------------------------------------------------------
  712. LIBPROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest);
  713. // ----------------------------------------------------------------------
  714. // WebSafeBase64Unescape()
  715. // This is a variation of Base64Unescape which uses '-' instead of '+', and
  716. // '_' instead of '/'. src is not null terminated, instead specify len. I
  717. // recommend that slen<szdest, but we honor szdest anyway.
  718. // RETURNS the length of dest, or -1 if src contains invalid chars.
  719. // The variation that stores into a string clears the string first, and
  720. // returns false (with dest empty) if src contains invalid chars; for
  721. // this version src and dest must be different strings.
  722. // ----------------------------------------------------------------------
  723. LIBPROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen,
  724. char* dest, int szdest);
  725. LIBPROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest);
  726. // Return the length to use for the output buffer given to the base64 escape
  727. // routines. Make sure to use the same value for do_padding in both.
  728. // This function may return incorrect results if given input_len values that
  729. // are extremely high, which should happen rarely.
  730. LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len,
  731. bool do_padding);
  732. // Use this version when calling Base64Escape without a do_padding arg.
  733. LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len);
  734. // ----------------------------------------------------------------------
  735. // Base64Escape()
  736. // WebSafeBase64Escape()
  737. // Encode "src" to "dest" using base64 encoding.
  738. // src is not null terminated, instead specify len.
  739. // 'dest' should have at least CalculateBase64EscapedLen() length.
  740. // RETURNS the length of dest.
  741. // The WebSafe variation use '-' instead of '+' and '_' instead of '/'
  742. // so that we can place the out in the URL or cookies without having
  743. // to escape them. It also has an extra parameter "do_padding",
  744. // which when set to false will prevent padding with "=".
  745. // ----------------------------------------------------------------------
  746. LIBPROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen,
  747. char* dest, int szdest);
  748. LIBPROTOBUF_EXPORT int WebSafeBase64Escape(
  749. const unsigned char* src, int slen, char* dest,
  750. int szdest, bool do_padding);
  751. // Encode src into dest with padding.
  752. LIBPROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest);
  753. // Encode src into dest web-safely without padding.
  754. LIBPROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest);
  755. // Encode src into dest web-safely with padding.
  756. LIBPROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src,
  757. string* dest);
  758. LIBPROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
  759. string* dest, bool do_padding);
  760. LIBPROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
  761. string* dest, bool do_padding);
  762. static const int UTFmax = 4;
  763. // ----------------------------------------------------------------------
  764. // EncodeAsUTF8Char()
  765. // Helper to append a Unicode code point to a string as UTF8, without bringing
  766. // in any external dependencies. The output buffer must be as least 4 bytes
  767. // large.
  768. // ----------------------------------------------------------------------
  769. LIBPROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
  770. // ----------------------------------------------------------------------
  771. // UTF8FirstLetterNumBytes()
  772. // Length of the first UTF-8 character.
  773. // ----------------------------------------------------------------------
  774. LIBPROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len);
  775. } // namespace protobuf
  776. } // namespace google
  777. #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__