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.
5 // A string-like object that points to a sized piece of memory.
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.
13 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary
14 // conversions from "const char*" to "string" and back again.
17 // Arghh! I wish C++ literals were "string".
19 #ifndef STRINGS_STRINGPIECE_H__
20 #define STRINGS_STRINGPIECE_H__
38 // We provide non-explicit singleton constructors so users can pass
39 // in a "const char*" or a "string" wherever a "StringPiece" is
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) { }
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
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; }
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) {
62 length_ = static_cast<int>(strlen(str));
66 void set(const void* data, int len) {
67 ptr_ = reinterpret_cast<const char*>(data);
71 char operator[](int i) const { return ptr_[i]; }
73 void remove_prefix(int n) {
78 void remove_suffix(int n) {
82 int compare(const StringPiece& x) const {
83 int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_));
85 if (length_ < x.length_) r = -1;
86 else if (length_ > x.length_) r = +1;
91 std::string as_string() const {
92 return std::string(data(), size());
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());
103 void CopyToString(std::string* target) const;
104 void AppendToString(std::string* target) const;
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));
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));
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_);
135 const_reverse_iterator rend() const {
136 return const_reverse_iterator(ptr_);
138 // STLS says return size_type, but Google says return int
139 int max_size() const { return length_; }
140 int capacity() const { return length_; }
142 int copy(char* buf, size_type n, size_type pos = 0) const;
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;
149 StringPiece substr(size_type pos, size_type n = npos) const;
151 static bool _equal(const StringPiece&, const StringPiece&);
154 inline bool operator==(const StringPiece& x, const StringPiece& y) {
155 return StringPiece::_equal(x, y);
158 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
162 inline 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())));
168 inline bool operator>(const StringPiece& x, const StringPiece& y) {
172 inline bool operator<=(const StringPiece& x, const StringPiece& y) {
176 inline bool operator>=(const StringPiece& x, const StringPiece& y) {
182 // allow StringPiece to be logged
183 extern std::ostream& operator<<(std::ostream& o, const re2::StringPiece& piece);
185 #endif // STRINGS_STRINGPIECE_H__