/[pcre]/code/tags/pcre-7.3/pcre_stringpiece.h.in
ViewVC logotype

Contents of /code/tags/pcre-7.3/pcre_stringpiece.h.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 229 - (hide annotations) (download)
Tue Aug 28 13:42:43 2007 UTC (7 years, 1 month ago) by ph10
File size: 6126 byte(s)
Tag for 7.3.

1 nigel 77 // Copyright (c) 2005, Google Inc.
2     // All rights reserved.
3     //
4     // Redistribution and use in source and binary forms, with or without
5     // modification, are permitted provided that the following conditions are
6     // met:
7     //
8     // * Redistributions of source code must retain the above copyright
9     // notice, this list of conditions and the following disclaimer.
10     // * Redistributions in binary form must reproduce the above
11     // copyright notice, this list of conditions and the following disclaimer
12     // in the documentation and/or other materials provided with the
13     // distribution.
14     // * Neither the name of Google Inc. nor the names of its
15     // contributors may be used to endorse or promote products derived from
16     // this software without specific prior written permission.
17     //
18     // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19     // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20     // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21     // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22     // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23     // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24     // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25     // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26     // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27     // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29     //
30     // Author: Sanjay Ghemawat
31     //
32     // A string like object that points into another piece of memory.
33     // Useful for providing an interface that allows clients to easily
34     // pass in either a "const char*" or a "string".
35     //
36     // Arghh! I wish C++ literals were automatically of type "string".
37    
38     #ifndef _PCRE_STRINGPIECE_H
39     #define _PCRE_STRINGPIECE_H
40    
41     #include <string.h>
42     #include <string>
43     #include <iosfwd> // for ostream forward-declaration
44    
45 nigel 83 #if @pcre_have_type_traits@
46 nigel 77 #define HAVE_TYPE_TRAITS
47     #include <type_traits.h>
48 nigel 83 #elif @pcre_have_bits_type_traits@
49 nigel 77 #define HAVE_TYPE_TRAITS
50     #include <bits/type_traits.h>
51     #endif
52    
53 ph10 199 #include <pcre.h>
54    
55 nigel 77 using std::string;
56    
57     namespace pcrecpp {
58    
59 ph10 199 class PCRECPP_EXP_DEFN StringPiece {
60 nigel 77 private:
61     const char* ptr_;
62     int length_;
63    
64     public:
65     // We provide non-explicit singleton constructors so users can pass
66     // in a "const char*" or a "string" wherever a "StringPiece" is
67     // expected.
68     StringPiece()
69     : ptr_(NULL), length_(0) { }
70     StringPiece(const char* str)
71     : ptr_(str), length_(static_cast<int>(strlen(str))) { }
72     StringPiece(const string& str)
73     : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
74     StringPiece(const char* offset, int len)
75     : ptr_(offset), length_(len) { }
76    
77     // data() may return a pointer to a buffer with embedded NULs, and the
78     // returned buffer may or may not be null terminated. Therefore it is
79     // typically a mistake to pass data() to a routine that expects a NUL
80     // terminated string. Use "as_string().c_str()" if you really need to do
81     // this. Or better yet, change your routine so it does not rely on NUL
82     // termination.
83     const char* data() const { return ptr_; }
84     int size() const { return length_; }
85     bool empty() const { return length_ == 0; }
86    
87     void clear() { ptr_ = NULL; length_ = 0; }
88     void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
89     void set(const char* str) {
90     ptr_ = str;
91     length_ = static_cast<int>(strlen(str));
92     }
93     void set(const void* buffer, int len) {
94     ptr_ = reinterpret_cast<const char*>(buffer);
95     length_ = len;
96     }
97    
98     char operator[](int i) const { return ptr_[i]; }
99    
100     void remove_prefix(int n) {
101     ptr_ += n;
102     length_ -= n;
103     }
104    
105     void remove_suffix(int n) {
106     length_ -= n;
107     }
108    
109     bool operator==(const StringPiece& x) const {
110     return ((length_ == x.length_) &&
111     (memcmp(ptr_, x.ptr_, length_) == 0));
112     }
113     bool operator!=(const StringPiece& x) const {
114     return !(*this == x);
115     }
116    
117     #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
118     bool operator cmp (const StringPiece& x) const { \
119     int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
120     return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
121     }
122     STRINGPIECE_BINARY_PREDICATE(<, <);
123     STRINGPIECE_BINARY_PREDICATE(<=, <);
124     STRINGPIECE_BINARY_PREDICATE(>=, >);
125     STRINGPIECE_BINARY_PREDICATE(>, >);
126     #undef STRINGPIECE_BINARY_PREDICATE
127    
128     int compare(const StringPiece& x) const {
129     int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
130     if (r == 0) {
131     if (length_ < x.length_) r = -1;
132     else if (length_ > x.length_) r = +1;
133     }
134     return r;
135     }
136    
137     string as_string() const {
138     return string(data(), size());
139     }
140    
141     void CopyToString(string* target) const {
142     target->assign(ptr_, length_);
143     }
144    
145     // Does "this" start with "x"
146     bool starts_with(const StringPiece& x) const {
147     return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
148     }
149     };
150    
151     } // namespace pcrecpp
152    
153     // ------------------------------------------------------------------
154     // Functions used to create STL containers that use StringPiece
155     // Remember that a StringPiece's lifetime had better be less than
156     // that of the underlying string or char*. If it is not, then you
157     // cannot safely store a StringPiece into an STL container
158     // ------------------------------------------------------------------
159    
160     #ifdef HAVE_TYPE_TRAITS
161     // This makes vector<StringPiece> really fast for some STL implementations
162     template<> struct __type_traits<pcrecpp::StringPiece> {
163     typedef __true_type has_trivial_default_constructor;
164     typedef __true_type has_trivial_copy_constructor;
165     typedef __true_type has_trivial_assignment_operator;
166     typedef __true_type has_trivial_destructor;
167     typedef __true_type is_POD_type;
168     };
169     #endif
170    
171     // allow StringPiece to be logged
172     std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece);
173    
174     #endif /* _PCRE_STRINGPIECE_H */

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12