123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #ifndef _PCRE_STRINGPIECE_H
- #define _PCRE_STRINGPIECE_H
- #include <cstring>
- #include <string>
- #include <iosfwd> // for ostream forward-declaration
- #if 0
- #define HAVE_TYPE_TRAITS
- #include <type_traits.h>
- #elif 0
- #define HAVE_TYPE_TRAITS
- #include <bits/type_traits.h>
- #endif
- #include <pcre.h>
- namespace pcrecpp {
- using std::memcmp;
- using std::strlen;
- using std::string;
- class PCRECPP_EXP_DEFN StringPiece {
- private:
- const char* ptr_;
- int length_;
- public:
-
-
-
- StringPiece()
- : ptr_(NULL), length_(0) { }
- StringPiece(const char* str)
- : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
- StringPiece(const unsigned char* str)
- : ptr_(reinterpret_cast<const char*>(str)),
- length_(static_cast<int>(strlen(ptr_))) { }
- StringPiece(const string& str)
- : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
- StringPiece(const char* offset, int len)
- : ptr_(offset), length_(len) { }
-
-
-
-
-
-
- const char* data() const { return ptr_; }
- int size() const { return length_; }
- bool empty() const { return length_ == 0; }
- void clear() { ptr_ = NULL; length_ = 0; }
- void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
- void set(const char* str) {
- ptr_ = str;
- length_ = static_cast<int>(strlen(str));
- }
- void set(const void* buffer, int len) {
- ptr_ = reinterpret_cast<const char*>(buffer);
- length_ = len;
- }
- char operator[](int i) const { return ptr_[i]; }
- void remove_prefix(int n) {
- ptr_ += n;
- length_ -= n;
- }
- void remove_suffix(int n) {
- length_ -= n;
- }
- bool operator==(const StringPiece& x) const {
- return ((length_ == x.length_) &&
- (memcmp(ptr_, x.ptr_, length_) == 0));
- }
- bool operator!=(const StringPiece& x) const {
- return !(*this == x);
- }
- #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
- bool operator cmp (const StringPiece& x) const { \
- int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
- return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
- }
- STRINGPIECE_BINARY_PREDICATE(<, <);
- STRINGPIECE_BINARY_PREDICATE(<=, <);
- STRINGPIECE_BINARY_PREDICATE(>=, >);
- STRINGPIECE_BINARY_PREDICATE(>, >);
- #undef STRINGPIECE_BINARY_PREDICATE
- int compare(const StringPiece& x) const {
- int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
- if (r == 0) {
- if (length_ < x.length_) r = -1;
- else if (length_ > x.length_) r = +1;
- }
- return r;
- }
- string as_string() const {
- return string(data(), size());
- }
- void CopyToString(string* target) const {
- target->assign(ptr_, length_);
- }
-
- bool starts_with(const StringPiece& x) const {
- return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
- }
- };
- }
- #ifdef HAVE_TYPE_TRAITS
- template<> struct __type_traits<pcrecpp::StringPiece> {
- typedef __true_type has_trivial_default_constructor;
- typedef __true_type has_trivial_copy_constructor;
- typedef __true_type has_trivial_assignment_operator;
- typedef __true_type has_trivial_destructor;
- typedef __true_type is_POD_type;
- };
- #endif
- PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
- const pcrecpp::StringPiece& piece);
- #endif
|