string_view.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/string_view.h"
  15. #ifndef ABSL_USES_STD_STRING_VIEW
  16. #include <algorithm>
  17. #include <climits>
  18. #include <cstring>
  19. #include <ostream>
  20. #include "absl/strings/internal/memutil.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace {
  24. void WritePadding(std::ostream& o, size_t pad) {
  25. char fill_buf[32];
  26. memset(fill_buf, o.fill(), sizeof(fill_buf));
  27. while (pad) {
  28. size_t n = std::min(pad, sizeof(fill_buf));
  29. o.write(fill_buf, n);
  30. pad -= n;
  31. }
  32. }
  33. class LookupTable {
  34. public:
  35. // For each character in wanted, sets the index corresponding
  36. // to the ASCII code of that character. This is used by
  37. // the find_.*_of methods below to tell whether or not a character is in
  38. // the lookup table in constant time.
  39. explicit LookupTable(string_view wanted) {
  40. for (char c : wanted) {
  41. table_[Index(c)] = true;
  42. }
  43. }
  44. bool operator[](char c) const { return table_[Index(c)]; }
  45. private:
  46. static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
  47. bool table_[UCHAR_MAX + 1] = {};
  48. };
  49. } // namespace
  50. std::ostream& operator<<(std::ostream& o, string_view piece) {
  51. std::ostream::sentry sentry(o);
  52. if (sentry) {
  53. size_t lpad = 0;
  54. size_t rpad = 0;
  55. if (static_cast<size_t>(o.width()) > piece.size()) {
  56. size_t pad = o.width() - piece.size();
  57. if ((o.flags() & o.adjustfield) == o.left) {
  58. rpad = pad;
  59. } else {
  60. lpad = pad;
  61. }
  62. }
  63. if (lpad) WritePadding(o, lpad);
  64. o.write(piece.data(), piece.size());
  65. if (rpad) WritePadding(o, rpad);
  66. o.width(0);
  67. }
  68. return o;
  69. }
  70. string_view::size_type string_view::find(string_view s,
  71. size_type pos) const noexcept {
  72. if (empty() || pos > length_) {
  73. if (empty() && pos == 0 && s.empty()) return 0;
  74. return npos;
  75. }
  76. const char* result =
  77. strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
  78. return result ? result - ptr_ : npos;
  79. }
  80. string_view::size_type string_view::find(char c, size_type pos) const noexcept {
  81. if (empty() || pos >= length_) {
  82. return npos;
  83. }
  84. const char* result =
  85. static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
  86. return result != nullptr ? result - ptr_ : npos;
  87. }
  88. string_view::size_type string_view::rfind(string_view s,
  89. size_type pos) const noexcept {
  90. if (length_ < s.length_) return npos;
  91. if (s.empty()) return std::min(length_, pos);
  92. const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
  93. const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
  94. return result != last ? result - ptr_ : npos;
  95. }
  96. // Search range is [0..pos] inclusive. If pos == npos, search everything.
  97. string_view::size_type string_view::rfind(char c,
  98. size_type pos) const noexcept {
  99. // Note: memrchr() is not available on Windows.
  100. if (empty()) return npos;
  101. for (size_type i = std::min(pos, length_ - 1);; --i) {
  102. if (ptr_[i] == c) {
  103. return i;
  104. }
  105. if (i == 0) break;
  106. }
  107. return npos;
  108. }
  109. string_view::size_type string_view::find_first_of(
  110. string_view s, size_type pos) const noexcept {
  111. if (empty() || s.empty()) {
  112. return npos;
  113. }
  114. // Avoid the cost of LookupTable() for a single-character search.
  115. if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
  116. LookupTable tbl(s);
  117. for (size_type i = pos; i < length_; ++i) {
  118. if (tbl[ptr_[i]]) {
  119. return i;
  120. }
  121. }
  122. return npos;
  123. }
  124. string_view::size_type string_view::find_first_not_of(
  125. string_view s, size_type pos) const noexcept {
  126. if (empty()) return npos;
  127. // Avoid the cost of LookupTable() for a single-character search.
  128. if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
  129. LookupTable tbl(s);
  130. for (size_type i = pos; i < length_; ++i) {
  131. if (!tbl[ptr_[i]]) {
  132. return i;
  133. }
  134. }
  135. return npos;
  136. }
  137. string_view::size_type string_view::find_first_not_of(
  138. char c, size_type pos) const noexcept {
  139. if (empty()) return npos;
  140. for (; pos < length_; ++pos) {
  141. if (ptr_[pos] != c) {
  142. return pos;
  143. }
  144. }
  145. return npos;
  146. }
  147. string_view::size_type string_view::find_last_of(string_view s,
  148. size_type pos) const noexcept {
  149. if (empty() || s.empty()) return npos;
  150. // Avoid the cost of LookupTable() for a single-character search.
  151. if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
  152. LookupTable tbl(s);
  153. for (size_type i = std::min(pos, length_ - 1);; --i) {
  154. if (tbl[ptr_[i]]) {
  155. return i;
  156. }
  157. if (i == 0) break;
  158. }
  159. return npos;
  160. }
  161. string_view::size_type string_view::find_last_not_of(
  162. string_view s, size_type pos) const noexcept {
  163. if (empty()) return npos;
  164. size_type i = std::min(pos, length_ - 1);
  165. if (s.empty()) return i;
  166. // Avoid the cost of LookupTable() for a single-character search.
  167. if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
  168. LookupTable tbl(s);
  169. for (;; --i) {
  170. if (!tbl[ptr_[i]]) {
  171. return i;
  172. }
  173. if (i == 0) break;
  174. }
  175. return npos;
  176. }
  177. string_view::size_type string_view::find_last_not_of(
  178. char c, size_type pos) const noexcept {
  179. if (empty()) return npos;
  180. size_type i = std::min(pos, length_ - 1);
  181. for (;; --i) {
  182. if (ptr_[i] != c) {
  183. return i;
  184. }
  185. if (i == 0) break;
  186. }
  187. return npos;
  188. }
  189. // MSVC has non-standard behavior that implicitly creates definitions for static
  190. // const members. These implicit definitions conflict with explicit out-of-class
  191. // member definitions that are required by the C++ standard, resulting in
  192. // LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
  193. // MSVC to choose only one definition for the symbol it decorates. See details
  194. // at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
  195. #ifdef _MSC_VER
  196. #define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
  197. #else
  198. #define ABSL_STRING_VIEW_SELECTANY
  199. #endif
  200. ABSL_STRING_VIEW_SELECTANY
  201. constexpr string_view::size_type string_view::npos;
  202. ABSL_STRING_VIEW_SELECTANY
  203. constexpr string_view::size_type string_view::kMaxSize;
  204. ABSL_NAMESPACE_END
  205. } // namespace absl
  206. #endif // ABSL_USES_STD_STRING_VIEW