string_view.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/string_view.h"
00016 
00017 #ifndef ABSL_HAVE_STD_STRING_VIEW
00018 
00019 #include <algorithm>
00020 #include <climits>
00021 #include <cstring>
00022 #include <ostream>
00023 
00024 #include "absl/strings/internal/memutil.h"
00025 
00026 namespace absl {
00027 
00028 namespace {
00029 void WritePadding(std::ostream& o, size_t pad) {
00030   char fill_buf[32];
00031   memset(fill_buf, o.fill(), sizeof(fill_buf));
00032   while (pad) {
00033     size_t n = std::min(pad, sizeof(fill_buf));
00034     o.write(fill_buf, n);
00035     pad -= n;
00036   }
00037 }
00038 
00039 class LookupTable {
00040  public:
00041   // For each character in wanted, sets the index corresponding
00042   // to the ASCII code of that character. This is used by
00043   // the find_.*_of methods below to tell whether or not a character is in
00044   // the lookup table in constant time.
00045   explicit LookupTable(string_view wanted) {
00046     for (char c : wanted) {
00047       table_[Index(c)] = true;
00048     }
00049   }
00050   bool operator[](char c) const { return table_[Index(c)]; }
00051 
00052  private:
00053   static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
00054   bool table_[UCHAR_MAX + 1] = {};
00055 };
00056 
00057 }  // namespace
00058 
00059 std::ostream& operator<<(std::ostream& o, string_view piece) {
00060   std::ostream::sentry sentry(o);
00061   if (sentry) {
00062     size_t lpad = 0;
00063     size_t rpad = 0;
00064     if (static_cast<size_t>(o.width()) > piece.size()) {
00065       size_t pad = o.width() - piece.size();
00066       if ((o.flags() & o.adjustfield) == o.left) {
00067         rpad = pad;
00068       } else {
00069         lpad = pad;
00070       }
00071     }
00072     if (lpad) WritePadding(o, lpad);
00073     o.write(piece.data(), piece.size());
00074     if (rpad) WritePadding(o, rpad);
00075     o.width(0);
00076   }
00077   return o;
00078 }
00079 
00080 string_view::size_type string_view::copy(char* buf, size_type n,
00081                                          size_type pos) const {
00082   size_type ulen = length_;
00083   assert(pos <= ulen);
00084   size_type rlen = std::min(ulen - pos, n);
00085   if (rlen > 0) {
00086     const char* start = ptr_ + pos;
00087     std::copy(start, start + rlen, buf);
00088   }
00089   return rlen;
00090 }
00091 
00092 string_view::size_type string_view::find(string_view s, size_type pos) const
00093     noexcept {
00094   if (empty() || pos > length_) {
00095     if (empty() && pos == 0 && s.empty()) return 0;
00096     return npos;
00097   }
00098   const char* result =
00099       strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
00100   return result ? result - ptr_ : npos;
00101 }
00102 
00103 string_view::size_type string_view::find(char c, size_type pos) const noexcept {
00104   if (empty() || pos >= length_) {
00105     return npos;
00106   }
00107   const char* result =
00108       static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
00109   return result != nullptr ? result - ptr_ : npos;
00110 }
00111 
00112 string_view::size_type string_view::rfind(string_view s, size_type pos) const
00113     noexcept {
00114   if (length_ < s.length_) return npos;
00115   if (s.empty()) return std::min(length_, pos);
00116   const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
00117   const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
00118   return result != last ? result - ptr_ : npos;
00119 }
00120 
00121 // Search range is [0..pos] inclusive.  If pos == npos, search everything.
00122 string_view::size_type string_view::rfind(char c, size_type pos) const
00123     noexcept {
00124   // Note: memrchr() is not available on Windows.
00125   if (empty()) return npos;
00126   for (size_type i = std::min(pos, length_ - 1);; --i) {
00127     if (ptr_[i] == c) {
00128       return i;
00129     }
00130     if (i == 0) break;
00131   }
00132   return npos;
00133 }
00134 
00135 string_view::size_type string_view::find_first_of(string_view s,
00136                                                   size_type pos) const
00137     noexcept {
00138   if (empty() || s.empty()) {
00139     return npos;
00140   }
00141   // Avoid the cost of LookupTable() for a single-character search.
00142   if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
00143   LookupTable tbl(s);
00144   for (size_type i = pos; i < length_; ++i) {
00145     if (tbl[ptr_[i]]) {
00146       return i;
00147     }
00148   }
00149   return npos;
00150 }
00151 
00152 string_view::size_type string_view::find_first_not_of(string_view s,
00153                                                       size_type pos) const
00154     noexcept {
00155   if (empty()) return npos;
00156   // Avoid the cost of LookupTable() for a single-character search.
00157   if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
00158   LookupTable tbl(s);
00159   for (size_type i = pos; i < length_; ++i) {
00160     if (!tbl[ptr_[i]]) {
00161       return i;
00162     }
00163   }
00164   return npos;
00165 }
00166 
00167 string_view::size_type string_view::find_first_not_of(char c,
00168                                                       size_type pos) const
00169     noexcept {
00170   if (empty()) return npos;
00171   for (; pos < length_; ++pos) {
00172     if (ptr_[pos] != c) {
00173       return pos;
00174     }
00175   }
00176   return npos;
00177 }
00178 
00179 string_view::size_type string_view::find_last_of(string_view s,
00180                                                  size_type pos) const noexcept {
00181   if (empty() || s.empty()) return npos;
00182   // Avoid the cost of LookupTable() for a single-character search.
00183   if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
00184   LookupTable tbl(s);
00185   for (size_type i = std::min(pos, length_ - 1);; --i) {
00186     if (tbl[ptr_[i]]) {
00187       return i;
00188     }
00189     if (i == 0) break;
00190   }
00191   return npos;
00192 }
00193 
00194 string_view::size_type string_view::find_last_not_of(string_view s,
00195                                                      size_type pos) const
00196     noexcept {
00197   if (empty()) return npos;
00198   size_type i = std::min(pos, length_ - 1);
00199   if (s.empty()) return i;
00200   // Avoid the cost of LookupTable() for a single-character search.
00201   if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
00202   LookupTable tbl(s);
00203   for (;; --i) {
00204     if (!tbl[ptr_[i]]) {
00205       return i;
00206     }
00207     if (i == 0) break;
00208   }
00209   return npos;
00210 }
00211 
00212 string_view::size_type string_view::find_last_not_of(char c,
00213                                                      size_type pos) const
00214     noexcept {
00215   if (empty()) return npos;
00216   size_type i = std::min(pos, length_ - 1);
00217   for (;; --i) {
00218     if (ptr_[i] != c) {
00219       return i;
00220     }
00221     if (i == 0) break;
00222   }
00223   return npos;
00224 }
00225 
00226 // MSVC has non-standard behavior that implicitly creates definitions for static
00227 // const members. These implicit definitions conflict with explicit out-of-class
00228 // member definitions that are required by the C++ standard, resulting in
00229 // LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
00230 // MSVC to choose only one definition for the symbol it decorates. See details
00231 // at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
00232 #ifdef _MSC_VER
00233 #define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
00234 #else
00235 #define ABSL_STRING_VIEW_SELECTANY
00236 #endif
00237 
00238 ABSL_STRING_VIEW_SELECTANY
00239 constexpr string_view::size_type string_view::npos;
00240 ABSL_STRING_VIEW_SELECTANY
00241 constexpr string_view::size_type string_view::kMaxSize;
00242 
00243 }  // namespace absl
00244 
00245 #endif  // ABSL_HAVE_STD_STRING_VIEW


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15