stringpiece.cc 8.9 KB

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