诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

939 Zeilen
38 KiB

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