ipc/chromium/src/base/string_piece.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/string_piece.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,215 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +// Copied from strings/stringpiece.cc with modifications
     1.8 +
     1.9 +#include <algorithm>
    1.10 +#include <ostream>
    1.11 +
    1.12 +#include "base/string_piece.h"
    1.13 +
    1.14 +typedef StringPiece::size_type size_type;
    1.15 +
    1.16 +std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
    1.17 +  o.write(piece.data(), static_cast<std::streamsize>(piece.size()));
    1.18 +  return o;
    1.19 +}
    1.20 +
    1.21 +bool operator==(const StringPiece& x, const StringPiece& y) {
    1.22 +  if (x.size() != y.size())
    1.23 +    return false;
    1.24 +
    1.25 +  return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
    1.26 +}
    1.27 +
    1.28 +void StringPiece::CopyToString(std::string* target) const {
    1.29 +  target->assign(!empty() ? data() : "", size());
    1.30 +}
    1.31 +
    1.32 +void StringPiece::AppendToString(std::string* target) const {
    1.33 +  if (!empty())
    1.34 +    target->append(data(), size());
    1.35 +}
    1.36 +
    1.37 +size_type StringPiece::copy(char* buf, size_type n, size_type pos) const {
    1.38 +  size_type ret = std::min(length_ - pos, n);
    1.39 +  memcpy(buf, ptr_ + pos, ret);
    1.40 +  return ret;
    1.41 +}
    1.42 +
    1.43 +size_type StringPiece::find(const StringPiece& s, size_type pos) const {
    1.44 +  if (pos > length_)
    1.45 +    return npos;
    1.46 +
    1.47 +  const char* result = std::search(ptr_ + pos, ptr_ + length_,
    1.48 +                                   s.ptr_, s.ptr_ + s.length_);
    1.49 +  const size_type xpos = result - ptr_;
    1.50 +  return xpos + s.length_ <= length_ ? xpos : npos;
    1.51 +}
    1.52 +
    1.53 +size_type StringPiece::find(char c, size_type pos) const {
    1.54 +  if (pos >= length_)
    1.55 +    return npos;
    1.56 +
    1.57 +  const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
    1.58 +  return result != ptr_ + length_ ? result - ptr_ : npos;
    1.59 +}
    1.60 +
    1.61 +size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
    1.62 +  if (length_ < s.length_)
    1.63 +    return npos;
    1.64 +
    1.65 +  if (s.empty())
    1.66 +    return std::min(length_, pos);
    1.67 +
    1.68 +  const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
    1.69 +  const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
    1.70 +  return result != last ? result - ptr_ : npos;
    1.71 +}
    1.72 +
    1.73 +size_type StringPiece::rfind(char c, size_type pos) const {
    1.74 +  if (length_ == 0)
    1.75 +    return npos;
    1.76 +
    1.77 +  for (size_type i = std::min(pos, length_ - 1); ; --i) {
    1.78 +    if (ptr_[i] == c)
    1.79 +      return i;
    1.80 +    if (i == 0)
    1.81 +      break;
    1.82 +  }
    1.83 +  return npos;
    1.84 +}
    1.85 +
    1.86 +// For each character in characters_wanted, sets the index corresponding
    1.87 +// to the ASCII code of that character to 1 in table.  This is used by
    1.88 +// the find_.*_of methods below to tell whether or not a character is in
    1.89 +// the lookup table in constant time.
    1.90 +// The argument `table' must be an array that is large enough to hold all
    1.91 +// the possible values of an unsigned char.  Thus it should be be declared
    1.92 +// as follows:
    1.93 +//   bool table[UCHAR_MAX + 1]
    1.94 +static inline void BuildLookupTable(const StringPiece& characters_wanted,
    1.95 +                                    bool* table) {
    1.96 +  const size_type length = characters_wanted.length();
    1.97 +  const char* const data = characters_wanted.data();
    1.98 +  for (size_type i = 0; i < length; ++i) {
    1.99 +    table[static_cast<unsigned char>(data[i])] = true;
   1.100 +  }
   1.101 +}
   1.102 +
   1.103 +size_type StringPiece::find_first_of(const StringPiece& s,
   1.104 +                                     size_type pos) const {
   1.105 +  if (length_ == 0 || s.length_ == 0)
   1.106 +    return npos;
   1.107 +
   1.108 +  // Avoid the cost of BuildLookupTable() for a single-character search.
   1.109 +  if (s.length_ == 1)
   1.110 +    return find_first_of(s.ptr_[0], pos);
   1.111 +
   1.112 +  bool lookup[UCHAR_MAX + 1] = { false };
   1.113 +  BuildLookupTable(s, lookup);
   1.114 +  for (size_type i = pos; i < length_; ++i) {
   1.115 +    if (lookup[static_cast<unsigned char>(ptr_[i])]) {
   1.116 +      return i;
   1.117 +    }
   1.118 +  }
   1.119 +  return npos;
   1.120 +}
   1.121 +
   1.122 +size_type StringPiece::find_first_not_of(const StringPiece& s,
   1.123 +                                         size_type pos) const {
   1.124 +  if (length_ == 0)
   1.125 +    return npos;
   1.126 +
   1.127 +  if (s.length_ == 0)
   1.128 +    return 0;
   1.129 +
   1.130 +  // Avoid the cost of BuildLookupTable() for a single-character search.
   1.131 +  if (s.length_ == 1)
   1.132 +    return find_first_not_of(s.ptr_[0], pos);
   1.133 +
   1.134 +  bool lookup[UCHAR_MAX + 1] = { false };
   1.135 +  BuildLookupTable(s, lookup);
   1.136 +  for (size_type i = pos; i < length_; ++i) {
   1.137 +    if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
   1.138 +      return i;
   1.139 +    }
   1.140 +  }
   1.141 +  return npos;
   1.142 +}
   1.143 +
   1.144 +size_type StringPiece::find_first_not_of(char c, size_type pos) const {
   1.145 +  if (length_ == 0)
   1.146 +    return npos;
   1.147 +
   1.148 +  for (; pos < length_; ++pos) {
   1.149 +    if (ptr_[pos] != c) {
   1.150 +      return pos;
   1.151 +    }
   1.152 +  }
   1.153 +  return npos;
   1.154 +}
   1.155 +
   1.156 +size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const {
   1.157 +  if (length_ == 0 || s.length_ == 0)
   1.158 +    return npos;
   1.159 +
   1.160 +  // Avoid the cost of BuildLookupTable() for a single-character search.
   1.161 +  if (s.length_ == 1)
   1.162 +    return find_last_of(s.ptr_[0], pos);
   1.163 +
   1.164 +  bool lookup[UCHAR_MAX + 1] = { false };
   1.165 +  BuildLookupTable(s, lookup);
   1.166 +  for (size_type i = std::min(pos, length_ - 1); ; --i) {
   1.167 +    if (lookup[static_cast<unsigned char>(ptr_[i])])
   1.168 +      return i;
   1.169 +    if (i == 0)
   1.170 +      break;
   1.171 +  }
   1.172 +  return npos;
   1.173 +}
   1.174 +
   1.175 +size_type StringPiece::find_last_not_of(const StringPiece& s,
   1.176 +                                        size_type pos) const {
   1.177 +  if (length_ == 0)
   1.178 +    return npos;
   1.179 +
   1.180 +  size_type i = std::min(pos, length_ - 1);
   1.181 +  if (s.length_ == 0)
   1.182 +    return i;
   1.183 +
   1.184 +  // Avoid the cost of BuildLookupTable() for a single-character search.
   1.185 +  if (s.length_ == 1)
   1.186 +    return find_last_not_of(s.ptr_[0], pos);
   1.187 +
   1.188 +  bool lookup[UCHAR_MAX + 1] = { false };
   1.189 +  BuildLookupTable(s, lookup);
   1.190 +  for (; ; --i) {
   1.191 +    if (!lookup[static_cast<unsigned char>(ptr_[i])])
   1.192 +      return i;
   1.193 +    if (i == 0)
   1.194 +      break;
   1.195 +  }
   1.196 +  return npos;
   1.197 +}
   1.198 +
   1.199 +size_type StringPiece::find_last_not_of(char c, size_type pos) const {
   1.200 +  if (length_ == 0)
   1.201 +    return npos;
   1.202 +
   1.203 +  for (size_type i = std::min(pos, length_ - 1); ; --i) {
   1.204 +    if (ptr_[i] != c)
   1.205 +      return i;
   1.206 +    if (i == 0)
   1.207 +      break;
   1.208 +  }
   1.209 +  return npos;
   1.210 +}
   1.211 +
   1.212 +StringPiece StringPiece::substr(size_type pos, size_type n) const {
   1.213 +  if (pos > length_) pos = length_;
   1.214 +  if (n > length_ - pos) n = length_ - pos;
   1.215 +  return StringPiece(ptr_ + pos, n);
   1.216 +}
   1.217 +
   1.218 +const StringPiece::size_type StringPiece::npos = size_type(-1);

mercurial