00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _PCRE_STRINGPIECE_H
00039 #define _PCRE_STRINGPIECE_H
00040
00041 #include <string.h>
00042 #include <string>
00043 #include <iosfwd>
00044
00045 #if 0
00046 #define HAVE_TYPE_TRAITS
00047 #include <type_traits.h>
00048 #elif 0
00049 #define HAVE_TYPE_TRAITS
00050 #include <bits/type_traits.h>
00051 #endif
00052
00053 #include <pcre.h>
00054
00055 using std::string;
00056
00057 namespace pcrecpp {
00058
00059 class PCRECPP_EXP_DEFN StringPiece {
00060 private:
00061 const char* ptr_;
00062 int length_;
00063
00064 public:
00065
00066
00067
00068 StringPiece()
00069 : ptr_(NULL), length_(0) { }
00070 StringPiece(const char* str)
00071 : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
00072 StringPiece(const unsigned char* str)
00073 : ptr_(reinterpret_cast<const char*>(str)),
00074 length_(static_cast<int>(strlen(ptr_))) { }
00075 StringPiece(const string& str)
00076 : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
00077 StringPiece(const char* offset, int len)
00078 : ptr_(offset), length_(len) { }
00079
00080
00081
00082
00083
00084
00085
00086 const char* data() const { return ptr_; }
00087 int size() const { return length_; }
00088 bool empty() const { return length_ == 0; }
00089
00090 void clear() { ptr_ = NULL; length_ = 0; }
00091 void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
00092 void set(const char* str) {
00093 ptr_ = str;
00094 length_ = static_cast<int>(strlen(str));
00095 }
00096 void set(const void* buffer, int len) {
00097 ptr_ = reinterpret_cast<const char*>(buffer);
00098 length_ = len;
00099 }
00100
00101 char operator[](int i) const { return ptr_[i]; }
00102
00103 void remove_prefix(int n) {
00104 ptr_ += n;
00105 length_ -= n;
00106 }
00107
00108 void remove_suffix(int n) {
00109 length_ -= n;
00110 }
00111
00112 bool operator==(const StringPiece& x) const {
00113 return ((length_ == x.length_) &&
00114 (memcmp(ptr_, x.ptr_, length_) == 0));
00115 }
00116 bool operator!=(const StringPiece& x) const {
00117 return !(*this == x);
00118 }
00119
00120 #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
00121 bool operator cmp (const StringPiece& x) const { \
00122 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
00123 return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
00124 }
00125 STRINGPIECE_BINARY_PREDICATE(<, <);
00126 STRINGPIECE_BINARY_PREDICATE(<=, <);
00127 STRINGPIECE_BINARY_PREDICATE(>=, >);
00128 STRINGPIECE_BINARY_PREDICATE(>, >);
00129 #undef STRINGPIECE_BINARY_PREDICATE
00130
00131 int compare(const StringPiece& x) const {
00132 int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
00133 if (r == 0) {
00134 if (length_ < x.length_) r = -1;
00135 else if (length_ > x.length_) r = +1;
00136 }
00137 return r;
00138 }
00139
00140 string as_string() const {
00141 return string(data(), size());
00142 }
00143
00144 void CopyToString(string* target) const {
00145 target->assign(ptr_, length_);
00146 }
00147
00148
00149 bool starts_with(const StringPiece& x) const {
00150 return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
00151 }
00152 };
00153
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 #ifdef HAVE_TYPE_TRAITS
00164
00165 template<> struct __type_traits<pcrecpp::StringPiece> {
00166 typedef __true_type has_trivial_default_constructor;
00167 typedef __true_type has_trivial_copy_constructor;
00168 typedef __true_type has_trivial_assignment_operator;
00169 typedef __true_type has_trivial_destructor;
00170 typedef __true_type is_POD_type;
00171 };
00172 #endif
00173
00174
00175 std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece);
00176
00177 #endif