comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/pcre_stringpiece.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
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 <cstring>
42 #include <string>
43 #include <iosfwd> // for ostream forward-declaration
44
45 #if 0
46 #define HAVE_TYPE_TRAITS
47 #include <type_traits.h>
48 #elif 0
49 #define HAVE_TYPE_TRAITS
50 #include <bits/type_traits.h>
51 #endif
52
53 #include <pcre.h>
54
55 namespace pcrecpp {
56
57 using std::memcmp;
58 using std::strlen;
59 using std::string;
60
61 class PCRECPP_EXP_DEFN StringPiece {
62 private:
63 const char* ptr_;
64 int length_;
65
66 public:
67 // We provide non-explicit singleton constructors so users can pass
68 // in a "const char*" or a "string" wherever a "StringPiece" is
69 // expected.
70 StringPiece()
71 : ptr_(NULL), length_(0) { }
72 StringPiece(const char* str)
73 : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
74 StringPiece(const unsigned char* str)
75 : ptr_(reinterpret_cast<const char*>(str)),
76 length_(static_cast<int>(strlen(ptr_))) { }
77 StringPiece(const string& str)
78 : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
79 StringPiece(const char* offset, int len)
80 : ptr_(offset), length_(len) { }
81
82 // data() may return a pointer to a buffer with embedded NULs, and the
83 // returned buffer may or may not be null terminated. Therefore it is
84 // typically a mistake to pass data() to a routine that expects a NUL
85 // terminated string. Use "as_string().c_str()" if you really need to do
86 // this. Or better yet, change your routine so it does not rely on NUL
87 // termination.
88 const char* data() const { return ptr_; }
89 int size() const { return length_; }
90 bool empty() const { return length_ == 0; }
91
92 void clear() { ptr_ = NULL; length_ = 0; }
93 void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
94 void set(const char* str) {
95 ptr_ = str;
96 length_ = static_cast<int>(strlen(str));
97 }
98 void set(const void* buffer, int len) {
99 ptr_ = reinterpret_cast<const char*>(buffer);
100 length_ = len;
101 }
102
103 char operator[](int i) const { return ptr_[i]; }
104
105 void remove_prefix(int n) {
106 ptr_ += n;
107 length_ -= n;
108 }
109
110 void remove_suffix(int n) {
111 length_ -= n;
112 }
113
114 bool operator==(const StringPiece& x) const {
115 return ((length_ == x.length_) &&
116 (memcmp(ptr_, x.ptr_, length_) == 0));
117 }
118 bool operator!=(const StringPiece& x) const {
119 return !(*this == x);
120 }
121
122 #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
123 bool operator cmp (const StringPiece& x) const { \
124 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
125 return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
126 }
127 STRINGPIECE_BINARY_PREDICATE(<, <);
128 STRINGPIECE_BINARY_PREDICATE(<=, <);
129 STRINGPIECE_BINARY_PREDICATE(>=, >);
130 STRINGPIECE_BINARY_PREDICATE(>, >);
131 #undef STRINGPIECE_BINARY_PREDICATE
132
133 int compare(const StringPiece& x) const {
134 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
135 if (r == 0) {
136 if (length_ < x.length_) r = -1;
137 else if (length_ > x.length_) r = +1;
138 }
139 return r;
140 }
141
142 string as_string() const {
143 return string(data(), size());
144 }
145
146 void CopyToString(string* target) const {
147 target->assign(ptr_, length_);
148 }
149
150 // Does "this" start with "x"
151 bool starts_with(const StringPiece& x) const {
152 return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
153 }
154 };
155
156 } // namespace pcrecpp
157
158 // ------------------------------------------------------------------
159 // Functions used to create STL containers that use StringPiece
160 // Remember that a StringPiece's lifetime had better be less than
161 // that of the underlying string or char*. If it is not, then you
162 // cannot safely store a StringPiece into an STL container
163 // ------------------------------------------------------------------
164
165 #ifdef HAVE_TYPE_TRAITS
166 // This makes vector<StringPiece> really fast for some STL implementations
167 template<> struct __type_traits<pcrecpp::StringPiece> {
168 typedef __true_type has_trivial_default_constructor;
169 typedef __true_type has_trivial_copy_constructor;
170 typedef __true_type has_trivial_assignment_operator;
171 typedef __true_type has_trivial_destructor;
172 typedef __true_type is_POD_type;
173 };
174 #endif
175
176 // allow StringPiece to be logged
177 PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
178 const pcrecpp::StringPiece& piece);
179
180 #endif /* _PCRE_STRINGPIECE_H */