ipc/chromium/src/base/string_piece.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/string_piece.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     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.h with modifications
     1.8 +//
     1.9 +// A string-like object that points to a sized piece of memory.
    1.10 +//
    1.11 +// Functions or methods may use const StringPiece& parameters to accept either
    1.12 +// a "const char*" or a "string" value that will be implicitly converted to
    1.13 +// a StringPiece.  The implicit conversion means that it is often appropriate
    1.14 +// to include this .h file in other files rather than forward-declaring
    1.15 +// StringPiece as would be appropriate for most other Google classes.
    1.16 +//
    1.17 +// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
    1.18 +// conversions from "const char*" to "string" and back again.
    1.19 +//
    1.20 +
    1.21 +#ifndef BASE_STRING_PIECE_H_
    1.22 +#define BASE_STRING_PIECE_H_
    1.23 +
    1.24 +#include <algorithm>
    1.25 +#include <iosfwd>
    1.26 +#include <string>
    1.27 +
    1.28 +#include "base/basictypes.h"
    1.29 +
    1.30 +class StringPiece {
    1.31 + public:
    1.32 +  typedef size_t size_type;
    1.33 +
    1.34 + private:
    1.35 +  const char*   ptr_;
    1.36 +  size_type     length_;
    1.37 +
    1.38 + public:
    1.39 +  // We provide non-explicit singleton constructors so users can pass
    1.40 +  // in a "const char*" or a "string" wherever a "StringPiece" is
    1.41 +  // expected.
    1.42 +  StringPiece() : ptr_(NULL), length_(0) { }
    1.43 +  StringPiece(const char* str)
    1.44 +    : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { }
    1.45 +  StringPiece(const std::string& str)
    1.46 +    : ptr_(str.data()), length_(str.size()) { }
    1.47 +  StringPiece(const char* offset, size_type len)
    1.48 +    : ptr_(offset), length_(len) { }
    1.49 +
    1.50 +  // data() may return a pointer to a buffer with embedded NULs, and the
    1.51 +  // returned buffer may or may not be null terminated.  Therefore it is
    1.52 +  // typically a mistake to pass data() to a routine that expects a NUL
    1.53 +  // terminated string.
    1.54 +  const char* data() const { return ptr_; }
    1.55 +  size_type size() const { return length_; }
    1.56 +  size_type length() const { return length_; }
    1.57 +  bool empty() const { return length_ == 0; }
    1.58 +
    1.59 +  void clear() { ptr_ = NULL; length_ = 0; }
    1.60 +  void set(const char* data, size_type len) { ptr_ = data; length_ = len; }
    1.61 +  void set(const char* str) {
    1.62 +    ptr_ = str;
    1.63 +    length_ = str ? strlen(str) : 0;
    1.64 +  }
    1.65 +  void set(const void* data, size_type len) {
    1.66 +    ptr_ = reinterpret_cast<const char*>(data);
    1.67 +    length_ = len;
    1.68 +  }
    1.69 +
    1.70 +  char operator[](size_type i) const { return ptr_[i]; }
    1.71 +
    1.72 +  void remove_prefix(size_type n) {
    1.73 +    ptr_ += n;
    1.74 +    length_ -= n;
    1.75 +  }
    1.76 +
    1.77 +  void remove_suffix(size_type n) {
    1.78 +    length_ -= n;
    1.79 +  }
    1.80 +
    1.81 +  int compare(const StringPiece& x) const {
    1.82 +    int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_));
    1.83 +    if (r == 0) {
    1.84 +      if (length_ < x.length_) r = -1;
    1.85 +      else if (length_ > x.length_) r = +1;
    1.86 +    }
    1.87 +    return r;
    1.88 +  }
    1.89 +
    1.90 +  std::string as_string() const {
    1.91 +    // std::string doesn't like to take a NULL pointer even with a 0 size.
    1.92 +    return std::string(!empty() ? data() : "", size());
    1.93 +  }
    1.94 +
    1.95 +  void CopyToString(std::string* target) const;
    1.96 +  void AppendToString(std::string* target) const;
    1.97 +
    1.98 +  // Does "this" start with "x"
    1.99 +  bool starts_with(const StringPiece& x) const {
   1.100 +    return ((length_ >= x.length_) &&
   1.101 +            (wordmemcmp(ptr_, x.ptr_, x.length_) == 0));
   1.102 +  }
   1.103 +
   1.104 +  // Does "this" end with "x"
   1.105 +  bool ends_with(const StringPiece& x) const {
   1.106 +    return ((length_ >= x.length_) &&
   1.107 +            (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
   1.108 +  }
   1.109 +
   1.110 +  // standard STL container boilerplate
   1.111 +  typedef char value_type;
   1.112 +  typedef const char* pointer;
   1.113 +  typedef const char& reference;
   1.114 +  typedef const char& const_reference;
   1.115 +  typedef ptrdiff_t difference_type;
   1.116 +  static const size_type npos;
   1.117 +  typedef const char* const_iterator;
   1.118 +  typedef const char* iterator;
   1.119 +  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
   1.120 +  typedef std::reverse_iterator<iterator> reverse_iterator;
   1.121 +  iterator begin() const { return ptr_; }
   1.122 +  iterator end() const { return ptr_ + length_; }
   1.123 +  const_reverse_iterator rbegin() const {
   1.124 +    return const_reverse_iterator(ptr_ + length_);
   1.125 +  }
   1.126 +  const_reverse_iterator rend() const {
   1.127 +    return const_reverse_iterator(ptr_);
   1.128 +  }
   1.129 +
   1.130 +  size_type max_size() const { return length_; }
   1.131 +  size_type capacity() const { return length_; }
   1.132 +
   1.133 +  size_type copy(char* buf, size_type n, size_type pos = 0) const;
   1.134 +
   1.135 +  size_type find(const StringPiece& s, size_type pos = 0) const;
   1.136 +  size_type find(char c, size_type pos = 0) const;
   1.137 +  size_type rfind(const StringPiece& s, size_type pos = npos) const;
   1.138 +  size_type rfind(char c, size_type pos = npos) const;
   1.139 +
   1.140 +  size_type find_first_of(const StringPiece& s, size_type pos = 0) const;
   1.141 +  size_type find_first_of(char c, size_type pos = 0) const {
   1.142 +    return find(c, pos);
   1.143 +  }
   1.144 +  size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const;
   1.145 +  size_type find_first_not_of(char c, size_type pos = 0) const;
   1.146 +  size_type find_last_of(const StringPiece& s, size_type pos = npos) const;
   1.147 +  size_type find_last_of(char c, size_type pos = npos) const {
   1.148 +    return rfind(c, pos);
   1.149 +  }
   1.150 +  size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const;
   1.151 +  size_type find_last_not_of(char c, size_type pos = npos) const;
   1.152 +
   1.153 +  StringPiece substr(size_type pos, size_type n = npos) const;
   1.154 +
   1.155 +  static int wordmemcmp(const char* p, const char* p2, size_type N) {
   1.156 +    return memcmp(p, p2, N);
   1.157 +  }
   1.158 +};
   1.159 +
   1.160 +bool operator==(const ::StringPiece& x, const ::StringPiece& y);
   1.161 +
   1.162 +inline bool operator!=(const ::StringPiece& x, const ::StringPiece& y) {
   1.163 +  return !(x == y);
   1.164 +}
   1.165 +
   1.166 +inline bool operator<(const ::StringPiece& x, const ::StringPiece& y) {
   1.167 +  const int r = ::StringPiece::wordmemcmp(x.data(), y.data(),
   1.168 +                                        std::min(x.size(), y.size()));
   1.169 +  return ((r < 0) || ((r == 0) && (x.size() < y.size())));
   1.170 +}
   1.171 +
   1.172 +inline bool operator>(const ::StringPiece& x, const ::StringPiece& y) {
   1.173 +  return y < x;
   1.174 +}
   1.175 +
   1.176 +inline bool operator<=(const ::StringPiece& x, const ::StringPiece& y) {
   1.177 +  return !(x > y);
   1.178 +}
   1.179 +
   1.180 +inline bool operator>=(const ::StringPiece& x, const ::StringPiece& y) {
   1.181 +  return !(x < y);
   1.182 +}
   1.183 +
   1.184 +// allow StringPiece to be logged (needed for unit testing).
   1.185 +extern std::ostream& operator<<(std::ostream& o, const ::StringPiece& piece);
   1.186 +
   1.187 +#endif  // BASE_STRING_PIECE_H_

mercurial