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