Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-05-18 08:30:10

0001 // Copyright (c) 2005, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: Sanjay Ghemawat
0031 //
0032 // A string like object that points into another piece of memory.
0033 // Useful for providing an interface that allows clients to easily
0034 // pass in either a "const char*" or a "string".
0035 //
0036 // Arghh!  I wish C++ literals were automatically of type "string".
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   // We provide non-explicit singleton constructors so users can pass
0068   // in a "const char*" or a "string" wherever a "StringPiece" is
0069   // expected.
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   // data() may return a pointer to a buffer with embedded NULs, and the
0083   // returned buffer may or may not be null terminated.  Therefore it is
0084   // typically a mistake to pass data() to a routine that expects a NUL
0085   // terminated string.  Use "as_string().c_str()" if you really need to do
0086   // this.  Or better yet, change your routine so it does not rely on NUL
0087   // termination.
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   // Does "this" start with "x"
0151   bool starts_with(const StringPiece& x) const {
0152     return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
0153   }
0154 };
0155 
0156 }   // namespace pcrecpp
0157 
0158 // ------------------------------------------------------------------
0159 // Functions used to create STL containers that use StringPiece
0160 //  Remember that a StringPiece's lifetime had better be less than
0161 //  that of the underlying string or char*.  If it is not, then you
0162 //  cannot safely store a StringPiece into an STL container
0163 // ------------------------------------------------------------------
0164 
0165 #ifdef HAVE_TYPE_TRAITS
0166 // This makes vector<StringPiece> really fast for some STL implementations
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 // allow StringPiece to be logged
0177 PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
0178                                           const pcrecpp::StringPiece& piece);
0179 
0180 #endif /* _PCRE_STRINGPIECE_H */