诸暨麻将添加redis
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

495 linhas
18 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. // A StringPiece points to part or all of a string, Cord, double-quoted string
  31. // literal, or other string-like object. A StringPiece does *not* own the
  32. // string to which it points. A StringPiece is not null-terminated.
  33. //
  34. // You can use StringPiece as a function or method parameter. A StringPiece
  35. // parameter can receive a double-quoted string literal argument, a "const
  36. // char*" argument, a string argument, or a StringPiece argument with no data
  37. // copying. Systematic use of StringPiece for arguments reduces data
  38. // copies and strlen() calls.
  39. //
  40. // Prefer passing StringPieces by value:
  41. // void MyFunction(StringPiece arg);
  42. // If circumstances require, you may also pass by const reference:
  43. // void MyFunction(const StringPiece& arg); // not preferred
  44. // Both of these have the same lifetime semantics. Passing by value
  45. // generates slightly smaller code. For more discussion, see the thread
  46. // go/stringpiecebyvalue on c-users.
  47. //
  48. // StringPiece is also suitable for local variables if you know that
  49. // the lifetime of the underlying object is longer than the lifetime
  50. // of your StringPiece variable.
  51. //
  52. // Beware of binding a StringPiece to a temporary:
  53. // StringPiece sp = obj.MethodReturningString(); // BAD: lifetime problem
  54. //
  55. // This code is okay:
  56. // string str = obj.MethodReturningString(); // str owns its contents
  57. // StringPiece sp(str); // GOOD, because str outlives sp
  58. //
  59. // StringPiece is sometimes a poor choice for a return value and usually a poor
  60. // choice for a data member. If you do use a StringPiece this way, it is your
  61. // responsibility to ensure that the object pointed to by the StringPiece
  62. // outlives the StringPiece.
  63. //
  64. // A StringPiece may represent just part of a string; thus the name "Piece".
  65. // For example, when splitting a string, vector<StringPiece> is a natural data
  66. // type for the output. For another example, a Cord is a non-contiguous,
  67. // potentially very long string-like object. The Cord class has an interface
  68. // that iteratively provides StringPiece objects that point to the
  69. // successive pieces of a Cord object.
  70. //
  71. // A StringPiece is not null-terminated. If you write code that scans a
  72. // StringPiece, you must check its length before reading any characters.
  73. // Common idioms that work on null-terminated strings do not work on
  74. // StringPiece objects.
  75. //
  76. // There are several ways to create a null StringPiece:
  77. // StringPiece()
  78. // StringPiece(nullptr)
  79. // StringPiece(nullptr, 0)
  80. // For all of the above, sp.data() == nullptr, sp.length() == 0,
  81. // and sp.empty() == true. Also, if you create a StringPiece with
  82. // a non-null pointer then sp.data() != nullptr. Once created,
  83. // sp.data() will stay either nullptr or not-nullptr, except if you call
  84. // sp.clear() or sp.set().
  85. //
  86. // Thus, you can use StringPiece(nullptr) to signal an out-of-band value
  87. // that is different from other StringPiece values. This is similar
  88. // to the way that const char* p1 = nullptr; is different from
  89. // const char* p2 = "";.
  90. //
  91. // There are many ways to create an empty StringPiece:
  92. // StringPiece()
  93. // StringPiece(nullptr)
  94. // StringPiece(nullptr, 0)
  95. // StringPiece("")
  96. // StringPiece("", 0)
  97. // StringPiece("abcdef", 0)
  98. // StringPiece("abcdef"+6, 0)
  99. // For all of the above, sp.length() will be 0 and sp.empty() will be true.
  100. // For some empty StringPiece values, sp.data() will be nullptr.
  101. // For some empty StringPiece values, sp.data() will not be nullptr.
  102. //
  103. // Be careful not to confuse: null StringPiece and empty StringPiece.
  104. // The set of empty StringPieces properly includes the set of null StringPieces.
  105. // That is, every null StringPiece is an empty StringPiece,
  106. // but some non-null StringPieces are empty Stringpieces too.
  107. //
  108. // All empty StringPiece values compare equal to each other.
  109. // Even a null StringPieces compares equal to a non-null empty StringPiece:
  110. // StringPiece() == StringPiece("", 0)
  111. // StringPiece(nullptr) == StringPiece("abc", 0)
  112. // StringPiece(nullptr, 0) == StringPiece("abcdef"+6, 0)
  113. //
  114. // Look carefully at this example:
  115. // StringPiece("") == nullptr
  116. // True or false? TRUE, because StringPiece::operator== converts
  117. // the right-hand side from nullptr to StringPiece(nullptr),
  118. // and then compares two zero-length spans of characters.
  119. // However, we are working to make this example produce a compile error.
  120. //
  121. // Suppose you want to write:
  122. // bool TestWhat?(StringPiece sp) { return sp == nullptr; } // BAD
  123. // Do not do that. Write one of these instead:
  124. // bool TestNull(StringPiece sp) { return sp.data() == nullptr; }
  125. // bool TestEmpty(StringPiece sp) { return sp.empty(); }
  126. // The intent of TestWhat? is unclear. Did you mean TestNull or TestEmpty?
  127. // Right now, TestWhat? behaves likes TestEmpty.
  128. // We are working to make TestWhat? produce a compile error.
  129. // TestNull is good to test for an out-of-band signal.
  130. // TestEmpty is good to test for an empty StringPiece.
  131. //
  132. // Caveats (again):
  133. // (1) The lifetime of the pointed-to string (or piece of a string)
  134. // must be longer than the lifetime of the StringPiece.
  135. // (2) There may or may not be a '\0' character after the end of
  136. // StringPiece data.
  137. // (3) A null StringPiece is empty.
  138. // An empty StringPiece may or may not be a null StringPiece.
  139. #ifndef GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
  140. #define GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
  141. #include <assert.h>
  142. #include <stddef.h>
  143. #include <string.h>
  144. #include <iosfwd>
  145. #include <limits>
  146. #include <string>
  147. #include <google/protobuf/stubs/common.h>
  148. #include <google/protobuf/stubs/hash.h>
  149. #include <google/protobuf/port_def.inc>
  150. namespace google {
  151. namespace protobuf {
  152. // StringPiece has *two* size types.
  153. // StringPiece::size_type
  154. // is unsigned
  155. // is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64
  156. // no future changes intended
  157. // stringpiece_ssize_type
  158. // is signed
  159. // is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64
  160. // future changes intended: http://go/64BitStringPiece
  161. //
  162. typedef string::difference_type stringpiece_ssize_type;
  163. // STRINGPIECE_CHECK_SIZE protects us from 32-bit overflows.
  164. // TODO(mec): delete this after stringpiece_ssize_type goes 64 bit.
  165. #if !defined(NDEBUG)
  166. #define STRINGPIECE_CHECK_SIZE 1
  167. #elif defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
  168. #define STRINGPIECE_CHECK_SIZE 1
  169. #else
  170. #define STRINGPIECE_CHECK_SIZE 0
  171. #endif
  172. class PROTOBUF_EXPORT StringPiece {
  173. private:
  174. const char* ptr_;
  175. stringpiece_ssize_type length_;
  176. // Prevent overflow in debug mode or fortified mode.
  177. // sizeof(stringpiece_ssize_type) may be smaller than sizeof(size_t).
  178. static stringpiece_ssize_type CheckedSsizeTFromSizeT(size_t size) {
  179. #if STRINGPIECE_CHECK_SIZE > 0
  180. #ifdef max
  181. #undef max
  182. #endif
  183. if (size > static_cast<size_t>(
  184. std::numeric_limits<stringpiece_ssize_type>::max())) {
  185. // Some people grep for this message in logs
  186. // so take care if you ever change it.
  187. LogFatalSizeTooBig(size, "size_t to int conversion");
  188. }
  189. #endif
  190. return static_cast<stringpiece_ssize_type>(size);
  191. }
  192. // Out-of-line error path.
  193. static void LogFatalSizeTooBig(size_t size, const char* details);
  194. public:
  195. // We provide non-explicit singleton constructors so users can pass
  196. // in a "const char*" or a "string" wherever a "StringPiece" is
  197. // expected.
  198. //
  199. // Style guide exception granted:
  200. // http://goto/style-guide-exception-20978288
  201. StringPiece() : ptr_(nullptr), length_(0) {}
  202. StringPiece(const char* str) // NOLINT(runtime/explicit)
  203. : ptr_(str), length_(0) {
  204. if (str != nullptr) {
  205. length_ = CheckedSsizeTFromSizeT(strlen(str));
  206. }
  207. }
  208. template <class Allocator>
  209. StringPiece( // NOLINT(runtime/explicit)
  210. const std::basic_string<char, std::char_traits<char>, Allocator>& str)
  211. : ptr_(str.data()), length_(0) {
  212. length_ = CheckedSsizeTFromSizeT(str.size());
  213. }
  214. StringPiece(const char* offset, stringpiece_ssize_type len)
  215. : ptr_(offset), length_(len) {
  216. assert(len >= 0);
  217. }
  218. // Substring of another StringPiece.
  219. // pos must be non-negative and <= x.length().
  220. StringPiece(StringPiece x, stringpiece_ssize_type pos);
  221. // Substring of another StringPiece.
  222. // pos must be non-negative and <= x.length().
  223. // len must be non-negative and will be pinned to at most x.length() - pos.
  224. StringPiece(StringPiece x,
  225. stringpiece_ssize_type pos,
  226. stringpiece_ssize_type len);
  227. // data() may return a pointer to a buffer with embedded NULs, and the
  228. // returned buffer may or may not be null terminated. Therefore it is
  229. // typically a mistake to pass data() to a routine that expects a NUL
  230. // terminated string.
  231. const char* data() const { return ptr_; }
  232. stringpiece_ssize_type size() const { return length_; }
  233. stringpiece_ssize_type length() const { return length_; }
  234. bool empty() const { return length_ == 0; }
  235. void clear() {
  236. ptr_ = nullptr;
  237. length_ = 0;
  238. }
  239. void set(const char* data, stringpiece_ssize_type len) {
  240. assert(len >= 0);
  241. ptr_ = data;
  242. length_ = len;
  243. }
  244. void set(const char* str) {
  245. ptr_ = str;
  246. if (str != nullptr)
  247. length_ = CheckedSsizeTFromSizeT(strlen(str));
  248. else
  249. length_ = 0;
  250. }
  251. void set(const void* data, stringpiece_ssize_type len) {
  252. ptr_ = reinterpret_cast<const char*>(data);
  253. length_ = len;
  254. }
  255. char operator[](stringpiece_ssize_type i) const {
  256. assert(0 <= i);
  257. assert(i < length_);
  258. return ptr_[i];
  259. }
  260. void remove_prefix(stringpiece_ssize_type n) {
  261. assert(length_ >= n);
  262. ptr_ += n;
  263. length_ -= n;
  264. }
  265. void remove_suffix(stringpiece_ssize_type n) {
  266. assert(length_ >= n);
  267. length_ -= n;
  268. }
  269. // returns {-1, 0, 1}
  270. int compare(StringPiece x) const {
  271. const stringpiece_ssize_type min_size =
  272. length_ < x.length_ ? length_ : x.length_;
  273. int r = memcmp(ptr_, x.ptr_, static_cast<size_t>(min_size));
  274. if (r < 0) return -1;
  275. if (r > 0) return 1;
  276. if (length_ < x.length_) return -1;
  277. if (length_ > x.length_) return 1;
  278. return 0;
  279. }
  280. string as_string() const {
  281. return ToString();
  282. }
  283. // We also define ToString() here, since many other string-like
  284. // interfaces name the routine that converts to a C++ string
  285. // "ToString", and it's confusing to have the method that does that
  286. // for a StringPiece be called "as_string()". We also leave the
  287. // "as_string()" method defined here for existing code.
  288. string ToString() const {
  289. if (ptr_ == nullptr) return string();
  290. return string(data(), static_cast<size_type>(size()));
  291. }
  292. operator string() const {
  293. return ToString();
  294. }
  295. void CopyToString(string* target) const;
  296. void AppendToString(string* target) const;
  297. bool starts_with(StringPiece x) const {
  298. return (length_ >= x.length_) &&
  299. (memcmp(ptr_, x.ptr_, static_cast<size_t>(x.length_)) == 0);
  300. }
  301. bool ends_with(StringPiece x) const {
  302. return ((length_ >= x.length_) &&
  303. (memcmp(ptr_ + (length_-x.length_), x.ptr_,
  304. static_cast<size_t>(x.length_)) == 0));
  305. }
  306. // Checks whether StringPiece starts with x and if so advances the beginning
  307. // of it to past the match. It's basically a shortcut for starts_with
  308. // followed by remove_prefix.
  309. bool Consume(StringPiece x);
  310. // Like above but for the end of the string.
  311. bool ConsumeFromEnd(StringPiece x);
  312. // standard STL container boilerplate
  313. typedef char value_type;
  314. typedef const char* pointer;
  315. typedef const char& reference;
  316. typedef const char& const_reference;
  317. typedef size_t size_type;
  318. typedef ptrdiff_t difference_type;
  319. static const size_type npos;
  320. typedef const char* const_iterator;
  321. typedef const char* iterator;
  322. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  323. typedef std::reverse_iterator<iterator> reverse_iterator;
  324. iterator begin() const { return ptr_; }
  325. iterator end() const { return ptr_ + length_; }
  326. const_reverse_iterator rbegin() const {
  327. return const_reverse_iterator(ptr_ + length_);
  328. }
  329. const_reverse_iterator rend() const {
  330. return const_reverse_iterator(ptr_);
  331. }
  332. stringpiece_ssize_type max_size() const { return length_; }
  333. stringpiece_ssize_type capacity() const { return length_; }
  334. // cpplint.py emits a false positive [build/include_what_you_use]
  335. stringpiece_ssize_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT
  336. bool contains(StringPiece s) const;
  337. stringpiece_ssize_type find(StringPiece s, size_type pos = 0) const;
  338. stringpiece_ssize_type find(char c, size_type pos = 0) const;
  339. stringpiece_ssize_type rfind(StringPiece s, size_type pos = npos) const;
  340. stringpiece_ssize_type rfind(char c, size_type pos = npos) const;
  341. stringpiece_ssize_type find_first_of(StringPiece s, size_type pos = 0) const;
  342. stringpiece_ssize_type find_first_of(char c, size_type pos = 0) const {
  343. return find(c, pos);
  344. }
  345. stringpiece_ssize_type find_first_not_of(StringPiece s,
  346. size_type pos = 0) const;
  347. stringpiece_ssize_type find_first_not_of(char c, size_type pos = 0) const;
  348. stringpiece_ssize_type find_last_of(StringPiece s,
  349. size_type pos = npos) const;
  350. stringpiece_ssize_type find_last_of(char c, size_type pos = npos) const {
  351. return rfind(c, pos);
  352. }
  353. stringpiece_ssize_type find_last_not_of(StringPiece s,
  354. size_type pos = npos) const;
  355. stringpiece_ssize_type find_last_not_of(char c, size_type pos = npos) const;
  356. StringPiece substr(size_type pos, size_type n = npos) const;
  357. };
  358. // This large function is defined inline so that in a fairly common case where
  359. // one of the arguments is a literal, the compiler can elide a lot of the
  360. // following comparisons.
  361. inline bool operator==(StringPiece x, StringPiece y) {
  362. stringpiece_ssize_type len = x.size();
  363. if (len != y.size()) {
  364. return false;
  365. }
  366. return x.data() == y.data() || len <= 0 ||
  367. memcmp(x.data(), y.data(), static_cast<size_t>(len)) == 0;
  368. }
  369. inline bool operator!=(StringPiece x, StringPiece y) {
  370. return !(x == y);
  371. }
  372. inline bool operator<(StringPiece x, StringPiece y) {
  373. const stringpiece_ssize_type min_size =
  374. x.size() < y.size() ? x.size() : y.size();
  375. const int r = memcmp(x.data(), y.data(), static_cast<size_t>(min_size));
  376. return (r < 0) || (r == 0 && x.size() < y.size());
  377. }
  378. inline bool operator>(StringPiece x, StringPiece y) {
  379. return y < x;
  380. }
  381. inline bool operator<=(StringPiece x, StringPiece y) {
  382. return !(x > y);
  383. }
  384. inline bool operator>=(StringPiece x, StringPiece y) {
  385. return !(x < y);
  386. }
  387. // allow StringPiece to be logged
  388. extern std::ostream& operator<<(std::ostream& o, StringPiece piece);
  389. namespace internal {
  390. // StringPiece is not a POD and can not be used in an union (pre C++11). We
  391. // need a POD version of it.
  392. struct StringPiecePod {
  393. // Create from a StringPiece.
  394. static StringPiecePod CreateFromStringPiece(StringPiece str) {
  395. StringPiecePod pod;
  396. pod.data_ = str.data();
  397. pod.size_ = str.size();
  398. return pod;
  399. }
  400. // Cast to StringPiece.
  401. operator StringPiece() const { return StringPiece(data_, size_); }
  402. bool operator==(const char* value) const {
  403. return StringPiece(data_, size_) == StringPiece(value);
  404. }
  405. char operator[](stringpiece_ssize_type i) const {
  406. assert(0 <= i);
  407. assert(i < size_);
  408. return data_[i];
  409. }
  410. const char* data() const { return data_; }
  411. stringpiece_ssize_type size() const {
  412. return size_;
  413. }
  414. std::string ToString() const {
  415. return std::string(data_, static_cast<size_t>(size_));
  416. }
  417. operator string() const { return ToString(); }
  418. private:
  419. const char* data_;
  420. stringpiece_ssize_type size_;
  421. };
  422. } // namespace internal
  423. } // namespace protobuf
  424. } // namespace google
  425. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
  426. template<> struct hash<StringPiece> {
  427. size_t operator()(const StringPiece& s) const {
  428. size_t result = 0;
  429. for (const char *str = s.data(), *end = str + s.size(); str < end; str++) {
  430. result = 5 * result + static_cast<size_t>(*str);
  431. }
  432. return result;
  433. }
  434. };
  435. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END
  436. #include <google/protobuf/port_undef.inc>
  437. #endif // STRINGS_STRINGPIECE_H_