pcre_stringpiece.h

Go to the documentation of this file.
00001 // Copyright (c) 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: Sanjay Ghemawat
00031 //
00032 // A string like object that points into another piece of memory.
00033 // Useful for providing an interface that allows clients to easily
00034 // pass in either a "const char*" or a "string".
00035 //
00036 // Arghh!  I wish C++ literals were automatically of type "string".
00037 
00038 #ifndef _PCRE_STRINGPIECE_H
00039 #define _PCRE_STRINGPIECE_H
00040 
00041 #include <string.h>
00042 #include <string>
00043 #include <iosfwd>    // for ostream forward-declaration
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   // We provide non-explicit singleton constructors so users can pass
00066   // in a "const char*" or a "string" wherever a "StringPiece" is
00067   // expected.
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   // data() may return a pointer to a buffer with embedded NULs, and the
00081   // returned buffer may or may not be null terminated.  Therefore it is
00082   // typically a mistake to pass data() to a routine that expects a NUL
00083   // terminated string.  Use "as_string().c_str()" if you really need to do
00084   // this.  Or better yet, change your routine so it does not rely on NUL
00085   // termination.
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   // Does "this" start with "x"
00149   bool starts_with(const StringPiece& x) const {
00150     return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
00151   }
00152 };
00153 
00154 }   // namespace pcrecpp
00155 
00156 // ------------------------------------------------------------------
00157 // Functions used to create STL containers that use StringPiece
00158 //  Remember that a StringPiece's lifetime had better be less than
00159 //  that of the underlying string or char*.  If it is not, then you
00160 //  cannot safely store a StringPiece into an STL container
00161 // ------------------------------------------------------------------
00162 
00163 #ifdef HAVE_TYPE_TRAITS
00164 // This makes vector<StringPiece> really fast for some STL implementations
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 // allow StringPiece to be logged
00175 std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece);
00176 
00177 #endif /* _PCRE_STRINGPIECE_H */

Generated on Tue Jul 5 14:11:57 2011 for ROOT_528-00b_version by  doxygen 1.5.1