|
1 // Copyright (c) 2005, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 // |
|
30 // Author: Sanjay Ghemawat |
|
31 // |
|
32 // A string like object that points into another piece of memory. |
|
33 // Useful for providing an interface that allows clients to easily |
|
34 // pass in either a "const char*" or a "string". |
|
35 // |
|
36 // Arghh! I wish C++ literals were automatically of type "string". |
|
37 |
|
38 #ifndef _PCRE_STRINGPIECE_H |
|
39 #define _PCRE_STRINGPIECE_H |
|
40 |
|
41 #include <string.h> |
|
42 #include <string> |
|
43 #include <iosfwd> // for ostream forward-declaration |
|
44 |
|
45 #if @pcre_have_type_traits@ |
|
46 #define HAVE_TYPE_TRAITS |
|
47 #include <type_traits.h> |
|
48 #elif @pcre_have_bits_type_traits@ |
|
49 #define HAVE_TYPE_TRAITS |
|
50 #include <bits/type_traits.h> |
|
51 #endif |
|
52 |
|
53 #include <pcre.h> |
|
54 |
|
55 using std::string; |
|
56 |
|
57 namespace pcrecpp { |
|
58 |
|
59 class PCRECPP_EXP_DEFN StringPiece { |
|
60 private: |
|
61 const char* ptr_; |
|
62 int length_; |
|
63 |
|
64 public: |
|
65 // We provide non-explicit singleton constructors so users can pass |
|
66 // in a "const char*" or a "string" wherever a "StringPiece" is |
|
67 // expected. |
|
68 StringPiece() |
|
69 : ptr_(NULL), length_(0) { } |
|
70 StringPiece(const char* str) |
|
71 : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { } |
|
72 StringPiece(const unsigned char* str) |
|
73 : ptr_(reinterpret_cast<const char*>(str)), |
|
74 length_(static_cast<int>(strlen(ptr_))) { } |
|
75 StringPiece(const string& str) |
|
76 : ptr_(str.data()), length_(static_cast<int>(str.size())) { } |
|
77 StringPiece(const char* offset, int len) |
|
78 : ptr_(offset), length_(len) { } |
|
79 |
|
80 // data() may return a pointer to a buffer with embedded NULs, and the |
|
81 // returned buffer may or may not be null terminated. Therefore it is |
|
82 // typically a mistake to pass data() to a routine that expects a NUL |
|
83 // terminated string. Use "as_string().c_str()" if you really need to do |
|
84 // this. Or better yet, change your routine so it does not rely on NUL |
|
85 // termination. |
|
86 const char* data() const { return ptr_; } |
|
87 int size() const { return length_; } |
|
88 bool empty() const { return length_ == 0; } |
|
89 |
|
90 void clear() { ptr_ = NULL; length_ = 0; } |
|
91 void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; } |
|
92 void set(const char* str) { |
|
93 ptr_ = str; |
|
94 length_ = static_cast<int>(strlen(str)); |
|
95 } |
|
96 void set(const void* buffer, int len) { |
|
97 ptr_ = reinterpret_cast<const char*>(buffer); |
|
98 length_ = len; |
|
99 } |
|
100 |
|
101 char operator[](int i) const { return ptr_[i]; } |
|
102 |
|
103 void remove_prefix(int n) { |
|
104 ptr_ += n; |
|
105 length_ -= n; |
|
106 } |
|
107 |
|
108 void remove_suffix(int n) { |
|
109 length_ -= n; |
|
110 } |
|
111 |
|
112 bool operator==(const StringPiece& x) const { |
|
113 return ((length_ == x.length_) && |
|
114 (memcmp(ptr_, x.ptr_, length_) == 0)); |
|
115 } |
|
116 bool operator!=(const StringPiece& x) const { |
|
117 return !(*this == x); |
|
118 } |
|
119 |
|
120 #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \ |
|
121 bool operator cmp (const StringPiece& x) const { \ |
|
122 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \ |
|
123 return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \ |
|
124 } |
|
125 STRINGPIECE_BINARY_PREDICATE(<, <); |
|
126 STRINGPIECE_BINARY_PREDICATE(<=, <); |
|
127 STRINGPIECE_BINARY_PREDICATE(>=, >); |
|
128 STRINGPIECE_BINARY_PREDICATE(>, >); |
|
129 #undef STRINGPIECE_BINARY_PREDICATE |
|
130 |
|
131 int compare(const StringPiece& x) const { |
|
132 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); |
|
133 if (r == 0) { |
|
134 if (length_ < x.length_) r = -1; |
|
135 else if (length_ > x.length_) r = +1; |
|
136 } |
|
137 return r; |
|
138 } |
|
139 |
|
140 string as_string() const { |
|
141 return string(data(), size()); |
|
142 } |
|
143 |
|
144 void CopyToString(string* target) const { |
|
145 target->assign(ptr_, length_); |
|
146 } |
|
147 |
|
148 // Does "this" start with "x" |
|
149 bool starts_with(const StringPiece& x) const { |
|
150 return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0)); |
|
151 } |
|
152 }; |
|
153 |
|
154 } // namespace pcrecpp |
|
155 |
|
156 // ------------------------------------------------------------------ |
|
157 // Functions used to create STL containers that use StringPiece |
|
158 // Remember that a StringPiece's lifetime had better be less than |
|
159 // that of the underlying string or char*. If it is not, then you |
|
160 // cannot safely store a StringPiece into an STL container |
|
161 // ------------------------------------------------------------------ |
|
162 |
|
163 #ifdef HAVE_TYPE_TRAITS |
|
164 // This makes vector<StringPiece> really fast for some STL implementations |
|
165 template<> struct __type_traits<pcrecpp::StringPiece> { |
|
166 typedef __true_type has_trivial_default_constructor; |
|
167 typedef __true_type has_trivial_copy_constructor; |
|
168 typedef __true_type has_trivial_assignment_operator; |
|
169 typedef __true_type has_trivial_destructor; |
|
170 typedef __true_type is_POD_type; |
|
171 }; |
|
172 #endif |
|
173 |
|
174 // allow StringPiece to be logged |
|
175 std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece); |
|
176 |
|
177 #endif /* _PCRE_STRINGPIECE_H */ |