1// Copyright 2001-2010 The RE2 Authors.  All Rights Reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// A string-like object that points to a sized piece of memory.
6//
7// Functions or methods may use const StringPiece& parameters to accept either
8// a "const char*" or a "string" value that will be implicitly converted to
9// a StringPiece.  The implicit conversion means that it is often appropriate
10// to include this .h file in other files rather than forward-declaring
11// StringPiece as would be appropriate for most other Google classes.
12//
13// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
14// conversions from "const char*" to "string" and back again.
15//
16//
17// Arghh!  I wish C++ literals were "string".
18
19#ifndef STRINGS_STRINGPIECE_H__
20#define STRINGS_STRINGPIECE_H__
21
22#include <string.h>
23#include <cstddef>
24#include <iosfwd>
25#include <string>
26#ifdef WIN32
27#include <algorithm>
28#endif
29
30namespace re2 {
31
32class StringPiece {
33 private:
34  const char*   ptr_;
35  int           length_;
36
37 public:
38  // We provide non-explicit singleton constructors so users can pass
39  // in a "const char*" or a "string" wherever a "StringPiece" is
40  // expected.
41  StringPiece() : ptr_(NULL), length_(0) { }
42  StringPiece(const char* str)
43    : ptr_(str), length_((str == NULL) ? 0 : static_cast<int>(strlen(str))) { }
44  StringPiece(const std::string& str)
45    : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
46  StringPiece(const char* offset, int len) : ptr_(offset), length_(len) { }
47
48  // data() may return a pointer to a buffer with embedded NULs, and the
49  // returned buffer may or may not be null terminated.  Therefore it is
50  // typically a mistake to pass data() to a routine that expects a NUL
51  // terminated string.
52  const char* data() const { return ptr_; }
53  int size() const { return length_; }
54  int length() const { return length_; }
55  bool empty() const { return length_ == 0; }
56
57  void clear() { ptr_ = NULL; length_ = 0; }
58  void set(const char* data, int len) { ptr_ = data; length_ = len; }
59  void set(const char* str) {
60    ptr_ = str;
61    if (str != NULL)
62      length_ = static_cast<int>(strlen(str));
63    else
64      length_ = 0;
65  }
66  void set(const void* data, int len) {
67    ptr_ = reinterpret_cast<const char*>(data);
68    length_ = len;
69  }
70
71  char operator[](int i) const { return ptr_[i]; }
72
73  void remove_prefix(int n) {
74    ptr_ += n;
75    length_ -= n;
76  }
77
78  void remove_suffix(int n) {
79    length_ -= n;
80  }
81
82  int compare(const StringPiece& x) const {
83    int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_));
84    if (r == 0) {
85      if (length_ < x.length_) r = -1;
86      else if (length_ > x.length_) r = +1;
87    }
88    return r;
89  }
90
91  std::string as_string() const {
92    return std::string(data(), size());
93  }
94  // We also define ToString() here, since many other string-like
95  // interfaces name the routine that converts to a C++ string
96  // "ToString", and it's confusing to have the method that does that
97  // for a StringPiece be called "as_string()".  We also leave the
98  // "as_string()" method defined here for existing code.
99  std::string ToString() const {
100    return std::string(data(), size());
101  }
102
103  void CopyToString(std::string* target) const;
104  void AppendToString(std::string* target) const;
105
106  // Does "this" start with "x"
107  bool starts_with(const StringPiece& x) const {
108    return ((length_ >= x.length_) &&
109            (memcmp(ptr_, x.ptr_, x.length_) == 0));
110  }
111
112  // Does "this" end with "x"
113  bool ends_with(const StringPiece& x) const {
114    return ((length_ >= x.length_) &&
115            (memcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
116  }
117
118  // standard STL container boilerplate
119  typedef char value_type;
120  typedef const char* pointer;
121  typedef const char& reference;
122  typedef const char& const_reference;
123  typedef size_t size_type;
124  typedef ptrdiff_t difference_type;
125  static const size_type npos;
126  typedef const char* const_iterator;
127  typedef const char* iterator;
128  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
129  typedef std::reverse_iterator<iterator> reverse_iterator;
130  iterator begin() const { return ptr_; }
131  iterator end() const { return ptr_ + length_; }
132  const_reverse_iterator rbegin() const {
133    return const_reverse_iterator(ptr_ + length_);
134  }
135  const_reverse_iterator rend() const {
136    return const_reverse_iterator(ptr_);
137  }
138  // STLS says return size_type, but Google says return int
139  int max_size() const { return length_; }
140  int capacity() const { return length_; }
141
142  int copy(char* buf, size_type n, size_type pos = 0) const;
143
144  int find(const StringPiece& s, size_type pos = 0) const;
145  int find(char c, size_type pos = 0) const;
146  int rfind(const StringPiece& s, size_type pos = npos) const;
147  int rfind(char c, size_type pos = npos) const;
148
149  StringPiece substr(size_type pos, size_type n = npos) const;
150
151  static bool _equal(const StringPiece&, const StringPiece&);
152};
153
154inline bool operator==(const StringPiece& x, const StringPiece& y) {
155  return StringPiece::_equal(x, y);
156}
157
158inline bool operator!=(const StringPiece& x, const StringPiece& y) {
159  return !(x == y);
160}
161
162inline bool operator<(const StringPiece& x, const StringPiece& y) {
163  const int r = memcmp(x.data(), y.data(),
164                       std::min(x.size(), y.size()));
165  return ((r < 0) || ((r == 0) && (x.size() < y.size())));
166}
167
168inline bool operator>(const StringPiece& x, const StringPiece& y) {
169  return y < x;
170}
171
172inline bool operator<=(const StringPiece& x, const StringPiece& y) {
173  return !(x > y);
174}
175
176inline bool operator>=(const StringPiece& x, const StringPiece& y) {
177  return !(x < y);
178}
179
180}  // namespace re2
181
182// allow StringPiece to be logged
183extern std::ostream& operator<<(std::ostream& o, const re2::StringPiece& piece);
184
185#endif  // STRINGS_STRINGPIECE_H__
186