PageRenderTime 31ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/AI_Patch/src/thirdparty/protobuf-2.3.0/src/google/protobuf/stubs/strutil.h

#
C++ Header | 459 lines | 164 code | 51 blank | 244 comment | 29 complexity | 4d4d0c928c40094b3c85e71053786213 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. namespace google {
  37. namespace protobuf {
  38. #ifdef _MSC_VER
  39. #define strtoll _strtoi64
  40. #define strtoull _strtoui64
  41. #elif defined(__DECCXX) && defined(__osf__)
  42. // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
  43. #define strtoll strtol
  44. #define strtoull strtoul
  45. #endif
  46. // ----------------------------------------------------------------------
  47. // ascii_isalnum()
  48. // Check if an ASCII character is alphanumeric. We can't use ctype's
  49. // isalnum() because it is affected by locale. This function is applied
  50. // to identifiers in the protocol buffer language, not to natural-language
  51. // strings, so locale should not be taken into account.
  52. // ascii_isdigit()
  53. // Like above, but only accepts digits.
  54. // ----------------------------------------------------------------------
  55. inline bool ascii_isalnum(char c) {
  56. return ('a' <= c && c <= 'z') ||
  57. ('A' <= c && c <= 'Z') ||
  58. ('0' <= c && c <= '9');
  59. }
  60. inline bool ascii_isdigit(char c) {
  61. return ('0' <= c && c <= '9');
  62. }
  63. // ----------------------------------------------------------------------
  64. // HasPrefixString()
  65. // Check if a string begins with a given prefix.
  66. // StripPrefixString()
  67. // Given a string and a putative prefix, returns the string minus the
  68. // prefix string if the prefix matches, otherwise the original
  69. // string.
  70. // ----------------------------------------------------------------------
  71. inline bool HasPrefixString(const string& str,
  72. const string& prefix) {
  73. return str.size() >= prefix.size() &&
  74. str.compare(0, prefix.size(), prefix) == 0;
  75. }
  76. inline string StripPrefixString(const string& str, const string& prefix) {
  77. if (HasPrefixString(str, prefix)) {
  78. return str.substr(prefix.size());
  79. } else {
  80. return str;
  81. }
  82. }
  83. // ----------------------------------------------------------------------
  84. // HasSuffixString()
  85. // Return true if str ends in suffix.
  86. // StripSuffixString()
  87. // Given a string and a putative suffix, returns the string minus the
  88. // suffix string if the suffix matches, otherwise the original
  89. // string.
  90. // ----------------------------------------------------------------------
  91. inline bool HasSuffixString(const string& str,
  92. const string& suffix) {
  93. return str.size() >= suffix.size() &&
  94. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  95. }
  96. inline string StripSuffixString(const string& str, const string& suffix) {
  97. if (HasSuffixString(str, suffix)) {
  98. return str.substr(0, str.size() - suffix.size());
  99. } else {
  100. return str;
  101. }
  102. }
  103. // ----------------------------------------------------------------------
  104. // StripString
  105. // Replaces any occurrence of the character 'remove' (or the characters
  106. // in 'remove') with the character 'replacewith'.
  107. // Good for keeping html characters or protocol characters (\t) out
  108. // of places where they might cause a problem.
  109. // ----------------------------------------------------------------------
  110. LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
  111. char replacewith);
  112. // ----------------------------------------------------------------------
  113. // LowerString()
  114. // UpperString()
  115. // Convert the characters in "s" to lowercase or uppercase. ASCII-only:
  116. // these functions intentionally ignore locale because they are applied to
  117. // identifiers used in the Protocol Buffer language, not to natural-language
  118. // strings.
  119. // ----------------------------------------------------------------------
  120. inline void LowerString(string * s) {
  121. string::iterator end = s->end();
  122. for (string::iterator i = s->begin(); i != end; ++i) {
  123. // tolower() changes based on locale. We don't want this!
  124. if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
  125. }
  126. }
  127. inline void UpperString(string * s) {
  128. string::iterator end = s->end();
  129. for (string::iterator i = s->begin(); i != end; ++i) {
  130. // toupper() changes based on locale. We don't want this!
  131. if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
  132. }
  133. }
  134. // ----------------------------------------------------------------------
  135. // StringReplace()
  136. // Give me a string and two patterns "old" and "new", and I replace
  137. // the first instance of "old" in the string with "new", if it
  138. // exists. RETURN a new string, regardless of whether the replacement
  139. // happened or not.
  140. // ----------------------------------------------------------------------
  141. LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
  142. const string& newsub, bool replace_all);
  143. // ----------------------------------------------------------------------
  144. // SplitStringUsing()
  145. // Split a string using a character delimiter. Append the components
  146. // to 'result'. If there are consecutive delimiters, this function skips
  147. // over all of them.
  148. // ----------------------------------------------------------------------
  149. LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
  150. vector<string>* res);
  151. // ----------------------------------------------------------------------
  152. // JoinStrings()
  153. // These methods concatenate a vector of strings into a C++ string, using
  154. // the C-string "delim" as a separator between components. There are two
  155. // flavors of the function, one flavor returns the concatenated string,
  156. // another takes a pointer to the target string. In the latter case the
  157. // target string is cleared and overwritten.
  158. // ----------------------------------------------------------------------
  159. LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
  160. const char* delim, string* result);
  161. inline string JoinStrings(const vector<string>& components,
  162. const char* delim) {
  163. string result;
  164. JoinStrings(components, delim, &result);
  165. return result;
  166. }
  167. // ----------------------------------------------------------------------
  168. // UnescapeCEscapeSequences()
  169. // Copies "source" to "dest", rewriting C-style escape sequences
  170. // -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
  171. // equivalents. "dest" must be sufficiently large to hold all
  172. // the characters in the rewritten string (i.e. at least as large
  173. // as strlen(source) + 1 should be safe, since the replacements
  174. // are always shorter than the original escaped sequences). It's
  175. // safe for source and dest to be the same. RETURNS the length
  176. // of dest.
  177. //
  178. // It allows hex sequences \xhh, or generally \xhhhhh with an
  179. // arbitrary number of hex digits, but all of them together must
  180. // specify a value of a single byte (e.g. \x0045 is equivalent
  181. // to \x45, and \x1234 is erroneous).
  182. //
  183. // It also allows escape sequences of the form \uhhhh (exactly four
  184. // hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
  185. // hex digits, upper or lower case) to specify a Unicode code
  186. // point. The dest array will contain the UTF8-encoded version of
  187. // that code-point (e.g., if source contains \u2019, then dest will
  188. // contain the three bytes 0xE2, 0x80, and 0x99). For the inverse
  189. // transformation, use UniLib::UTF8EscapeString
  190. // (util/utf8/unilib.h), not CEscapeString.
  191. //
  192. // Errors: In the first form of the call, errors are reported with
  193. // LOG(ERROR). The same is true for the second form of the call if
  194. // the pointer to the string vector is NULL; otherwise, error
  195. // messages are stored in the vector. In either case, the effect on
  196. // the dest array is not defined, but rest of the source will be
  197. // processed.
  198. // ----------------------------------------------------------------------
  199. LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
  200. LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
  201. vector<string> *errors);
  202. // ----------------------------------------------------------------------
  203. // UnescapeCEscapeString()
  204. // This does the same thing as UnescapeCEscapeSequences, but creates
  205. // a new string. The caller does not need to worry about allocating
  206. // a dest buffer. This should be used for non performance critical
  207. // tasks such as printing debug messages. It is safe for src and dest
  208. // to be the same.
  209. //
  210. // The second call stores its errors in a supplied string vector.
  211. // If the string vector pointer is NULL, it reports the errors with LOG().
  212. //
  213. // In the first and second calls, the length of dest is returned. In the
  214. // the third call, the new string is returned.
  215. // ----------------------------------------------------------------------
  216. LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
  217. LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
  218. vector<string> *errors);
  219. LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
  220. // ----------------------------------------------------------------------
  221. // CEscapeString()
  222. // Copies 'src' to 'dest', escaping dangerous characters using
  223. // C-style escape sequences. This is very useful for preparing query
  224. // flags. 'src' and 'dest' should not overlap.
  225. // Returns the number of bytes written to 'dest' (not including the \0)
  226. // or -1 if there was insufficient space.
  227. //
  228. // Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped.
  229. // ----------------------------------------------------------------------
  230. LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
  231. char* dest, int dest_len);
  232. // ----------------------------------------------------------------------
  233. // CEscape()
  234. // More convenient form of CEscapeString: returns result as a "string".
  235. // This version is slower than CEscapeString() because it does more
  236. // allocation. However, it is much more convenient to use in
  237. // non-speed-critical code like logging messages etc.
  238. // ----------------------------------------------------------------------
  239. LIBPROTOBUF_EXPORT string CEscape(const string& src);
  240. namespace strings {
  241. // Like CEscape() but does not escape bytes with the upper bit set.
  242. LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
  243. // Like CEscape() but uses hex (\x) escapes instead of octals.
  244. LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
  245. } // namespace strings
  246. // ----------------------------------------------------------------------
  247. // strto32()
  248. // strtou32()
  249. // strto64()
  250. // strtou64()
  251. // Architecture-neutral plug compatible replacements for strtol() and
  252. // strtoul(). Long's have different lengths on ILP-32 and LP-64
  253. // platforms, so using these is safer, from the point of view of
  254. // overflow behavior, than using the standard libc functions.
  255. // ----------------------------------------------------------------------
  256. LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr,
  257. int base);
  258. LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr,
  259. int base);
  260. inline int32 strto32(const char *nptr, char **endptr, int base) {
  261. if (sizeof(int32) == sizeof(long))
  262. return strtol(nptr, endptr, base);
  263. else
  264. return strto32_adaptor(nptr, endptr, base);
  265. }
  266. inline uint32 strtou32(const char *nptr, char **endptr, int base) {
  267. if (sizeof(uint32) == sizeof(unsigned long))
  268. return strtoul(nptr, endptr, base);
  269. else
  270. return strtou32_adaptor(nptr, endptr, base);
  271. }
  272. // For now, long long is 64-bit on all the platforms we care about, so these
  273. // functions can simply pass the call to strto[u]ll.
  274. inline int64 strto64(const char *nptr, char **endptr, int base) {
  275. GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
  276. sizeof_int64_is_not_sizeof_long_long);
  277. return strtoll(nptr, endptr, base);
  278. }
  279. inline uint64 strtou64(const char *nptr, char **endptr, int base) {
  280. GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
  281. sizeof_uint64_is_not_sizeof_long_long);
  282. return strtoull(nptr, endptr, base);
  283. }
  284. // ----------------------------------------------------------------------
  285. // FastIntToBuffer()
  286. // FastHexToBuffer()
  287. // FastHex64ToBuffer()
  288. // FastHex32ToBuffer()
  289. // FastTimeToBuffer()
  290. // These are intended for speed. FastIntToBuffer() assumes the
  291. // integer is non-negative. FastHexToBuffer() puts output in
  292. // hex rather than decimal. FastTimeToBuffer() puts the output
  293. // into RFC822 format.
  294. //
  295. // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
  296. // padded to exactly 16 bytes (plus one byte for '\0')
  297. //
  298. // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
  299. // padded to exactly 8 bytes (plus one byte for '\0')
  300. //
  301. // All functions take the output buffer as an arg.
  302. // They all return a pointer to the beginning of the output,
  303. // which may not be the beginning of the input buffer.
  304. // ----------------------------------------------------------------------
  305. // Suggested buffer size for FastToBuffer functions. Also works with
  306. // DoubleToBuffer() and FloatToBuffer().
  307. static const int kFastToBufferSize = 32;
  308. LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
  309. LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
  310. char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
  311. char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
  312. LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
  313. LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
  314. LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
  315. // at least 22 bytes long
  316. inline char* FastIntToBuffer(int i, char* buffer) {
  317. return (sizeof(i) == 4 ?
  318. FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
  319. }
  320. inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
  321. return (sizeof(i) == 4 ?
  322. FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
  323. }
  324. inline char* FastLongToBuffer(long i, char* buffer) {
  325. return (sizeof(i) == 4 ?
  326. FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
  327. }
  328. inline char* FastULongToBuffer(unsigned long i, char* buffer) {
  329. return (sizeof(i) == 4 ?
  330. FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
  331. }
  332. // ----------------------------------------------------------------------
  333. // FastInt32ToBufferLeft()
  334. // FastUInt32ToBufferLeft()
  335. // FastInt64ToBufferLeft()
  336. // FastUInt64ToBufferLeft()
  337. //
  338. // Like the Fast*ToBuffer() functions above, these are intended for speed.
  339. // Unlike the Fast*ToBuffer() functions, however, these functions write
  340. // their output to the beginning of the buffer (hence the name, as the
  341. // output is left-aligned). The caller is responsible for ensuring that
  342. // the buffer has enough space to hold the output.
  343. //
  344. // Returns a pointer to the end of the string (i.e. the null character
  345. // terminating the string).
  346. // ----------------------------------------------------------------------
  347. LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
  348. LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
  349. LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
  350. LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
  351. // Just define these in terms of the above.
  352. inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
  353. FastUInt32ToBufferLeft(i, buffer);
  354. return buffer;
  355. }
  356. inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
  357. FastUInt64ToBufferLeft(i, buffer);
  358. return buffer;
  359. }
  360. // ----------------------------------------------------------------------
  361. // SimpleItoa()
  362. // Description: converts an integer to a string.
  363. //
  364. // Return value: string
  365. // ----------------------------------------------------------------------
  366. LIBPROTOBUF_EXPORT string SimpleItoa(int i);
  367. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
  368. LIBPROTOBUF_EXPORT string SimpleItoa(long i);
  369. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
  370. LIBPROTOBUF_EXPORT string SimpleItoa(long long i);
  371. LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
  372. // ----------------------------------------------------------------------
  373. // SimpleDtoa()
  374. // SimpleFtoa()
  375. // DoubleToBuffer()
  376. // FloatToBuffer()
  377. // Description: converts a double or float to a string which, if
  378. // passed to NoLocaleStrtod(), will produce the exact same original double
  379. // (except in case of NaN; all NaNs are considered the same value).
  380. // We try to keep the string short but it's not guaranteed to be as
  381. // short as possible.
  382. //
  383. // DoubleToBuffer() and FloatToBuffer() write the text to the given
  384. // buffer and return it. The buffer must be at least
  385. // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
  386. // bytes for floats. kFastToBufferSize is also guaranteed to be large
  387. // enough to hold either.
  388. //
  389. // Return value: string
  390. // ----------------------------------------------------------------------
  391. LIBPROTOBUF_EXPORT string SimpleDtoa(double value);
  392. LIBPROTOBUF_EXPORT string SimpleFtoa(float value);
  393. LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
  394. LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
  395. // In practice, doubles should never need more than 24 bytes and floats
  396. // should never need more than 14 (including null terminators), but we
  397. // overestimate to be safe.
  398. static const int kDoubleToBufferSize = 32;
  399. static const int kFloatToBufferSize = 24;
  400. // ----------------------------------------------------------------------
  401. // NoLocaleStrtod()
  402. // Exactly like strtod(), except it always behaves as if in the "C"
  403. // locale (i.e. decimal points must be '.'s).
  404. // ----------------------------------------------------------------------
  405. LIBPROTOBUF_EXPORT double NoLocaleStrtod(const char* text, char** endptr);
  406. } // namespace protobuf
  407. } // namespace google
  408. #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__