诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

271 lines
9.2 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. #include <google/protobuf/stubs/stringpiece.h>
  31. #include <string.h>
  32. #include <algorithm>
  33. #include <climits>
  34. #include <string>
  35. #include <ostream>
  36. #include <google/protobuf/stubs/logging.h>
  37. namespace google {
  38. namespace protobuf {
  39. std::ostream& operator<<(std::ostream& o, StringPiece piece) {
  40. o.write(piece.data(), piece.size());
  41. return o;
  42. }
  43. // Out-of-line error path.
  44. void StringPiece::LogFatalSizeTooBig(size_t size, const char* details) {
  45. GOOGLE_LOG(FATAL) << "size too big: " << size << " details: " << details;
  46. }
  47. StringPiece::StringPiece(StringPiece x, stringpiece_ssize_type pos)
  48. : ptr_(x.ptr_ + pos), length_(x.length_ - pos) {
  49. GOOGLE_DCHECK_LE(0, pos);
  50. GOOGLE_DCHECK_LE(pos, x.length_);
  51. }
  52. StringPiece::StringPiece(StringPiece x,
  53. stringpiece_ssize_type pos,
  54. stringpiece_ssize_type len)
  55. : ptr_(x.ptr_ + pos), length_(std::min(len, x.length_ - pos)) {
  56. GOOGLE_DCHECK_LE(0, pos);
  57. GOOGLE_DCHECK_LE(pos, x.length_);
  58. GOOGLE_DCHECK_GE(len, 0);
  59. }
  60. void StringPiece::CopyToString(string* target) const {
  61. target->assign(ptr_, length_);
  62. }
  63. void StringPiece::AppendToString(string* target) const {
  64. target->append(ptr_, length_);
  65. }
  66. bool StringPiece::Consume(StringPiece x) {
  67. if (starts_with(x)) {
  68. ptr_ += x.length_;
  69. length_ -= x.length_;
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool StringPiece::ConsumeFromEnd(StringPiece x) {
  75. if (ends_with(x)) {
  76. length_ -= x.length_;
  77. return true;
  78. }
  79. return false;
  80. }
  81. stringpiece_ssize_type StringPiece::copy(char* buf,
  82. size_type n,
  83. size_type pos) const {
  84. stringpiece_ssize_type ret = std::min(length_ - pos, n);
  85. memcpy(buf, ptr_ + pos, ret);
  86. return ret;
  87. }
  88. bool StringPiece::contains(StringPiece s) const {
  89. return find(s, 0) != npos;
  90. }
  91. stringpiece_ssize_type StringPiece::find(StringPiece s, size_type pos) const {
  92. if (length_ <= 0 || pos > static_cast<size_type>(length_)) {
  93. if (length_ == 0 && pos == 0 && s.length_ == 0) return 0;
  94. return npos;
  95. }
  96. const char *result = std::search(ptr_ + pos, ptr_ + length_,
  97. s.ptr_, s.ptr_ + s.length_);
  98. return result == ptr_ + length_ ? npos : result - ptr_;
  99. }
  100. stringpiece_ssize_type StringPiece::find(char c, size_type pos) const {
  101. if (length_ <= 0 || pos >= static_cast<size_type>(length_)) {
  102. return npos;
  103. }
  104. const char* result = static_cast<const char*>(
  105. memchr(ptr_ + pos, c, length_ - pos));
  106. return result != nullptr ? result - ptr_ : npos;
  107. }
  108. stringpiece_ssize_type StringPiece::rfind(StringPiece s, size_type pos) const {
  109. if (length_ < s.length_) return npos;
  110. const size_t ulen = length_;
  111. if (s.length_ == 0) return std::min(ulen, pos);
  112. const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_;
  113. const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
  114. return result != last ? result - ptr_ : npos;
  115. }
  116. // Search range is [0..pos] inclusive. If pos == npos, search everything.
  117. stringpiece_ssize_type StringPiece::rfind(char c, size_type pos) const {
  118. // Note: memrchr() is not available on Windows.
  119. if (length_ <= 0) return npos;
  120. for (stringpiece_ssize_type i =
  121. std::min(pos, static_cast<size_type>(length_ - 1));
  122. i >= 0; --i) {
  123. if (ptr_[i] == c) {
  124. return i;
  125. }
  126. }
  127. return npos;
  128. }
  129. // For each character in characters_wanted, sets the index corresponding
  130. // to the ASCII code of that character to 1 in table. This is used by
  131. // the find_.*_of methods below to tell whether or not a character is in
  132. // the lookup table in constant time.
  133. // The argument `table' must be an array that is large enough to hold all
  134. // the possible values of an unsigned char. Thus it should be be declared
  135. // as follows:
  136. // bool table[UCHAR_MAX + 1]
  137. static inline void BuildLookupTable(StringPiece characters_wanted,
  138. bool* table) {
  139. const stringpiece_ssize_type length = characters_wanted.length();
  140. const char* const data = characters_wanted.data();
  141. for (stringpiece_ssize_type i = 0; i < length; ++i) {
  142. table[static_cast<unsigned char>(data[i])] = true;
  143. }
  144. }
  145. stringpiece_ssize_type StringPiece::find_first_of(StringPiece s,
  146. size_type pos) const {
  147. if (length_ <= 0 || s.length_ <= 0) {
  148. return npos;
  149. }
  150. // Avoid the cost of BuildLookupTable() for a single-character search.
  151. if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
  152. bool lookup[UCHAR_MAX + 1] = { false };
  153. BuildLookupTable(s, lookup);
  154. for (stringpiece_ssize_type i = pos; i < length_; ++i) {
  155. if (lookup[static_cast<unsigned char>(ptr_[i])]) {
  156. return i;
  157. }
  158. }
  159. return npos;
  160. }
  161. stringpiece_ssize_type StringPiece::find_first_not_of(StringPiece s,
  162. size_type pos) const {
  163. if (length_ <= 0) return npos;
  164. if (s.length_ <= 0) return 0;
  165. // Avoid the cost of BuildLookupTable() for a single-character search.
  166. if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
  167. bool lookup[UCHAR_MAX + 1] = { false };
  168. BuildLookupTable(s, lookup);
  169. for (stringpiece_ssize_type i = pos; i < length_; ++i) {
  170. if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
  171. return i;
  172. }
  173. }
  174. return npos;
  175. }
  176. stringpiece_ssize_type StringPiece::find_first_not_of(char c,
  177. size_type pos) const {
  178. if (length_ <= 0) return npos;
  179. for (; pos < static_cast<size_type>(length_); ++pos) {
  180. if (ptr_[pos] != c) {
  181. return pos;
  182. }
  183. }
  184. return npos;
  185. }
  186. stringpiece_ssize_type StringPiece::find_last_of(StringPiece s,
  187. size_type pos) const {
  188. if (length_ <= 0 || s.length_ <= 0) return npos;
  189. // Avoid the cost of BuildLookupTable() for a single-character search.
  190. if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
  191. bool lookup[UCHAR_MAX + 1] = { false };
  192. BuildLookupTable(s, lookup);
  193. for (stringpiece_ssize_type i =
  194. std::min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {
  195. if (lookup[static_cast<unsigned char>(ptr_[i])]) {
  196. return i;
  197. }
  198. }
  199. return npos;
  200. }
  201. stringpiece_ssize_type StringPiece::find_last_not_of(StringPiece s,
  202. size_type pos) const {
  203. if (length_ <= 0) return npos;
  204. stringpiece_ssize_type i = std::min(pos, static_cast<size_type>(length_ - 1));
  205. if (s.length_ <= 0) return i;
  206. // Avoid the cost of BuildLookupTable() for a single-character search.
  207. if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
  208. bool lookup[UCHAR_MAX + 1] = { false };
  209. BuildLookupTable(s, lookup);
  210. for (; i >= 0; --i) {
  211. if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
  212. return i;
  213. }
  214. }
  215. return npos;
  216. }
  217. stringpiece_ssize_type StringPiece::find_last_not_of(char c,
  218. size_type pos) const {
  219. if (length_ <= 0) return npos;
  220. for (stringpiece_ssize_type i =
  221. std::min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {
  222. if (ptr_[i] != c) {
  223. return i;
  224. }
  225. }
  226. return npos;
  227. }
  228. StringPiece StringPiece::substr(size_type pos, size_type n) const {
  229. if (pos > length_) pos = length_;
  230. if (n > length_ - pos) n = length_ - pos;
  231. return StringPiece(ptr_ + pos, n);
  232. }
  233. const StringPiece::size_type StringPiece::npos = size_type(-1);
  234. } // namespace protobuf
  235. } // namespace google