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 #ifndef RE2_STRINGPIECE_H_
6 #define RE2_STRINGPIECE_H_
7 
8 // A string-like object that points to a sized piece of memory.
9 //
10 // Functions or methods may use const StringPiece& parameters to accept either
11 // a "const char*" or a "string" value that will be implicitly converted to
12 // a StringPiece.  The implicit conversion means that it is often appropriate
13 // to include this .h file in other files rather than forward-declaring
14 // StringPiece as would be appropriate for most other Google classes.
15 //
16 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary
17 // conversions from "const char*" to "string" and back again.
18 //
19 //
20 // Arghh!  I wish C++ literals were "string".
21 
22 #include <stddef.h>
23 #include <string.h>
24 #include <algorithm>
25 #include <iosfwd>
26 #include <iterator>
27 #include <string>
28 #ifdef __cpp_lib_string_view
29 #include <string_view>
30 #endif
31 
32 namespace re2 {
33 
34 class StringPiece {
35  public:
36   typedef std::char_traits<char> traits_type;
37   typedef char value_type;
38   typedef char* pointer;
39   typedef const char* const_pointer;
40   typedef char& reference;
41   typedef const char& const_reference;
42   typedef const char* const_iterator;
43   typedef const_iterator iterator;
44   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
45   typedef const_reverse_iterator reverse_iterator;
46   typedef size_t size_type;
47   typedef ptrdiff_t difference_type;
48   static const size_type npos = static_cast<size_type>(-1);
49 
50   // We provide non-explicit singleton constructors so users can pass
51   // in a "const char*" or a "string" wherever a "StringPiece" is
52   // expected.
StringPiece()53   StringPiece()
54       : data_(NULL), size_(0) {}
55 #ifdef __cpp_lib_string_view
StringPiece(const std::string_view & str)56   StringPiece(const std::string_view& str)
57       : data_(str.data()), size_(str.size()) {}
58 #endif
StringPiece(const std::string & str)59   StringPiece(const std::string& str)
60       : data_(str.data()), size_(str.size()) {}
StringPiece(const char * str)61   StringPiece(const char* str)
62       : data_(str), size_(str == NULL ? 0 : strlen(str)) {}
StringPiece(const char * str,size_type len)63   StringPiece(const char* str, size_type len)
64       : data_(str), size_(len) {}
65 
begin()66   const_iterator begin() const { return data_; }
end()67   const_iterator end() const { return data_ + size_; }
rbegin()68   const_reverse_iterator rbegin() const {
69     return const_reverse_iterator(data_ + size_);
70   }
rend()71   const_reverse_iterator rend() const {
72     return const_reverse_iterator(data_);
73   }
74 
size()75   size_type size() const { return size_; }
length()76   size_type length() const { return size_; }
empty()77   bool empty() const { return size_ == 0; }
78 
79   const_reference operator[](size_type i) const { return data_[i]; }
data()80   const_pointer data() const { return data_; }
81 
remove_prefix(size_type n)82   void remove_prefix(size_type n) {
83     data_ += n;
84     size_ -= n;
85   }
86 
remove_suffix(size_type n)87   void remove_suffix(size_type n) {
88     size_ -= n;
89   }
90 
set(const char * str)91   void set(const char* str) {
92     data_ = str;
93     size_ = str == NULL ? 0 : strlen(str);
94   }
95 
set(const char * str,size_type len)96   void set(const char* str, size_type len) {
97     data_ = str;
98     size_ = len;
99   }
100 
101 #ifdef __cpp_lib_string_view
102   // Converts to `std::basic_string_view`.
103   operator std::basic_string_view<char, traits_type>() const {
104     if (!data_) return {};
105     return std::basic_string_view<char, traits_type>(data_, size_);
106   }
107 #endif
108 
109   // Converts to `std::basic_string`.
110   template <typename A>
111   explicit operator std::basic_string<char, traits_type, A>() const {
112     if (!data_) return {};
113     return std::basic_string<char, traits_type, A>(data_, size_);
114   }
115 
as_string()116   std::string as_string() const {
117     return std::string(data_, size_);
118   }
119 
120   // We also define ToString() here, since many other string-like
121   // interfaces name the routine that converts to a C++ string
122   // "ToString", and it's confusing to have the method that does that
123   // for a StringPiece be called "as_string()".  We also leave the
124   // "as_string()" method defined here for existing code.
ToString()125   std::string ToString() const {
126     return std::string(data_, size_);
127   }
128 
CopyToString(std::string * target)129   void CopyToString(std::string* target) const {
130     target->assign(data_, size_);
131   }
132 
AppendToString(std::string * target)133   void AppendToString(std::string* target) const {
134     target->append(data_, size_);
135   }
136 
137   size_type copy(char* buf, size_type n, size_type pos = 0) const;
138   StringPiece substr(size_type pos = 0, size_type n = npos) const;
139 
compare(const StringPiece & x)140   int compare(const StringPiece& x) const {
141     size_type min_size = std::min(size(), x.size());
142     if (min_size > 0) {
143       int r = memcmp(data(), x.data(), min_size);
144       if (r < 0) return -1;
145       if (r > 0) return 1;
146     }
147     if (size() < x.size()) return -1;
148     if (size() > x.size()) return 1;
149     return 0;
150   }
151 
152   // Does "this" start with "x"?
starts_with(const StringPiece & x)153   bool starts_with(const StringPiece& x) const {
154     return x.empty() ||
155            (size() >= x.size() && memcmp(data(), x.data(), x.size()) == 0);
156   }
157 
158   // Does "this" end with "x"?
ends_with(const StringPiece & x)159   bool ends_with(const StringPiece& x) const {
160     return x.empty() ||
161            (size() >= x.size() &&
162             memcmp(data() + (size() - x.size()), x.data(), x.size()) == 0);
163   }
164 
contains(const StringPiece & s)165   bool contains(const StringPiece& s) const {
166     return find(s) != npos;
167   }
168 
169   size_type find(const StringPiece& s, size_type pos = 0) const;
170   size_type find(char c, size_type pos = 0) const;
171   size_type rfind(const StringPiece& s, size_type pos = npos) const;
172   size_type rfind(char c, size_type pos = npos) const;
173 
174  private:
175   const_pointer data_;
176   size_type size_;
177 };
178 
179 inline bool operator==(const StringPiece& x, const StringPiece& y) {
180   StringPiece::size_type len = x.size();
181   if (len != y.size()) return false;
182   return x.data() == y.data() || len == 0 ||
183          memcmp(x.data(), y.data(), len) == 0;
184 }
185 
186 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
187   return !(x == y);
188 }
189 
190 inline bool operator<(const StringPiece& x, const StringPiece& y) {
191   StringPiece::size_type min_size = std::min(x.size(), y.size());
192   int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
193   return (r < 0) || (r == 0 && x.size() < y.size());
194 }
195 
196 inline bool operator>(const StringPiece& x, const StringPiece& y) {
197   return y < x;
198 }
199 
200 inline bool operator<=(const StringPiece& x, const StringPiece& y) {
201   return !(x > y);
202 }
203 
204 inline bool operator>=(const StringPiece& x, const StringPiece& y) {
205   return !(x < y);
206 }
207 
208 // Allow StringPiece to be logged.
209 std::ostream& operator<<(std::ostream& o, const StringPiece& p);
210 
211 }  // namespace re2
212 
213 #endif  // RE2_STRINGPIECE_H_
214