File indexing completed on 2025-01-17 09:55:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #ifndef _PCRE_STRINGPIECE_H
0039 #define _PCRE_STRINGPIECE_H
0040
0041 #include <cstring>
0042 #include <string>
0043 #include <iosfwd> // for ostream forward-declaration
0044
0045 #if 0
0046 #define HAVE_TYPE_TRAITS
0047 #include <type_traits.h>
0048 #elif 0
0049 #define HAVE_TYPE_TRAITS
0050 #include <bits/type_traits.h>
0051 #endif
0052
0053 #include <pcre.h>
0054
0055 namespace pcrecpp {
0056
0057 using std::memcmp;
0058 using std::strlen;
0059 using std::string;
0060
0061 class PCRECPP_EXP_DEFN StringPiece {
0062 private:
0063 const char* ptr_;
0064 int length_;
0065
0066 public:
0067
0068
0069
0070 StringPiece()
0071 : ptr_(NULL), length_(0) { }
0072 StringPiece(const char* str)
0073 : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
0074 StringPiece(const unsigned char* str)
0075 : ptr_(reinterpret_cast<const char*>(str)),
0076 length_(static_cast<int>(strlen(ptr_))) { }
0077 StringPiece(const string& str)
0078 : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
0079 StringPiece(const char* offset, int len)
0080 : ptr_(offset), length_(len) { }
0081
0082
0083
0084
0085
0086
0087
0088 const char* data() const { return ptr_; }
0089 int size() const { return length_; }
0090 bool empty() const { return length_ == 0; }
0091
0092 void clear() { ptr_ = NULL; length_ = 0; }
0093 void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
0094 void set(const char* str) {
0095 ptr_ = str;
0096 length_ = static_cast<int>(strlen(str));
0097 }
0098 void set(const void* buffer, int len) {
0099 ptr_ = reinterpret_cast<const char*>(buffer);
0100 length_ = len;
0101 }
0102
0103 char operator[](int i) const { return ptr_[i]; }
0104
0105 void remove_prefix(int n) {
0106 ptr_ += n;
0107 length_ -= n;
0108 }
0109
0110 void remove_suffix(int n) {
0111 length_ -= n;
0112 }
0113
0114 bool operator==(const StringPiece& x) const {
0115 return ((length_ == x.length_) &&
0116 (memcmp(ptr_, x.ptr_, length_) == 0));
0117 }
0118 bool operator!=(const StringPiece& x) const {
0119 return !(*this == x);
0120 }
0121
0122 #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
0123 bool operator cmp (const StringPiece& x) const { \
0124 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
0125 return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
0126 }
0127 STRINGPIECE_BINARY_PREDICATE(<, <);
0128 STRINGPIECE_BINARY_PREDICATE(<=, <);
0129 STRINGPIECE_BINARY_PREDICATE(>=, >);
0130 STRINGPIECE_BINARY_PREDICATE(>, >);
0131 #undef STRINGPIECE_BINARY_PREDICATE
0132
0133 int compare(const StringPiece& x) const {
0134 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
0135 if (r == 0) {
0136 if (length_ < x.length_) r = -1;
0137 else if (length_ > x.length_) r = +1;
0138 }
0139 return r;
0140 }
0141
0142 string as_string() const {
0143 return string(data(), size());
0144 }
0145
0146 void CopyToString(string* target) const {
0147 target->assign(ptr_, length_);
0148 }
0149
0150
0151 bool starts_with(const StringPiece& x) const {
0152 return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
0153 }
0154 };
0155
0156 }
0157
0158
0159
0160
0161
0162
0163
0164
0165 #ifdef HAVE_TYPE_TRAITS
0166
0167 template<> struct __type_traits<pcrecpp::StringPiece> {
0168 typedef __true_type has_trivial_default_constructor;
0169 typedef __true_type has_trivial_copy_constructor;
0170 typedef __true_type has_trivial_assignment_operator;
0171 typedef __true_type has_trivial_destructor;
0172 typedef __true_type is_POD_type;
0173 };
0174 #endif
0175
0176
0177 PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
0178 const pcrecpp::StringPiece& piece);
0179
0180 #endif