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