/[pcre]/code/trunk/pcre_stringpiece.h.in
ViewVC logotype

Contents of /code/trunk/pcre_stringpiece.h.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (hide annotations) (download)
Fri Mar 2 13:10:43 2007 UTC (7 years, 6 months ago) by nigel
File size: 6090 byte(s)
 r6896@hex:  nm | 2007-03-02 13:09:14 +0000
 Added EOL and keywork properties throughout

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     using std::string;
54    
55     namespace pcrecpp {
56    
57     class StringPiece {
58     private:
59     const char* ptr_;
60     int length_;
61    
62     public:
63     // We provide non-explicit singleton constructors so users can pass
64     // in a "const char*" or a "string" wherever a "StringPiece" is
65     // expected.
66     StringPiece()
67     : ptr_(NULL), length_(0) { }
68     StringPiece(const char* str)
69     : ptr_(str), length_(static_cast<int>(strlen(str))) { }
70     StringPiece(const string& str)
71     : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
72     StringPiece(const char* offset, int len)
73     : ptr_(offset), length_(len) { }
74    
75     // data() may return a pointer to a buffer with embedded NULs, and the
76     // returned buffer may or may not be null terminated. Therefore it is
77     // typically a mistake to pass data() to a routine that expects a NUL
78     // terminated string. Use "as_string().c_str()" if you really need to do
79     // this. Or better yet, change your routine so it does not rely on NUL
80     // termination.
81     const char* data() const { return ptr_; }
82     int size() const { return length_; }
83     bool empty() const { return length_ == 0; }
84    
85     void clear() { ptr_ = NULL; length_ = 0; }
86     void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
87     void set(const char* str) {
88     ptr_ = str;
89     length_ = static_cast<int>(strlen(str));
90     }
91     void set(const void* buffer, int len) {
92     ptr_ = reinterpret_cast<const char*>(buffer);
93     length_ = len;
94     }
95    
96     char operator[](int i) const { return ptr_[i]; }
97    
98     void remove_prefix(int n) {
99     ptr_ += n;
100     length_ -= n;
101     }
102    
103     void remove_suffix(int n) {
104     length_ -= n;
105     }
106    
107     bool operator==(const StringPiece& x) const {
108     return ((length_ == x.length_) &&
109     (memcmp(ptr_, x.ptr_, length_) == 0));
110     }
111     bool operator!=(const StringPiece& x) const {
112     return !(*this == x);
113     }
114    
115     #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
116     bool operator cmp (const StringPiece& x) const { \
117     int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
118     return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
119     }
120     STRINGPIECE_BINARY_PREDICATE(<, <);
121     STRINGPIECE_BINARY_PREDICATE(<=, <);
122     STRINGPIECE_BINARY_PREDICATE(>=, >);
123     STRINGPIECE_BINARY_PREDICATE(>, >);
124     #undef STRINGPIECE_BINARY_PREDICATE
125    
126     int compare(const StringPiece& x) const {
127     int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
128     if (r == 0) {
129     if (length_ < x.length_) r = -1;
130     else if (length_ > x.length_) r = +1;
131     }
132     return r;
133     }
134    
135     string as_string() const {
136     return string(data(), size());
137     }
138    
139     void CopyToString(string* target) const {
140     target->assign(ptr_, length_);
141     }
142    
143     // Does "this" start with "x"
144     bool starts_with(const StringPiece& x) const {
145     return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
146     }
147     };
148    
149     } // namespace pcrecpp
150    
151     // ------------------------------------------------------------------
152     // Functions used to create STL containers that use StringPiece
153     // Remember that a StringPiece's lifetime had better be less than
154     // that of the underlying string or char*. If it is not, then you
155     // cannot safely store a StringPiece into an STL container
156     // ------------------------------------------------------------------
157    
158     #ifdef HAVE_TYPE_TRAITS
159     // This makes vector<StringPiece> really fast for some STL implementations
160     template<> struct __type_traits<pcrecpp::StringPiece> {
161     typedef __true_type has_trivial_default_constructor;
162     typedef __true_type has_trivial_copy_constructor;
163     typedef __true_type has_trivial_assignment_operator;
164     typedef __true_type has_trivial_destructor;
165     typedef __true_type is_POD_type;
166     };
167     #endif
168    
169     // allow StringPiece to be logged
170     std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece);
171    
172     #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