Chris@16
|
1 /*
|
Chris@16
|
2 Copyright (c) Marshall Clow 2012-2012.
|
Chris@16
|
3
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 For more information, see http://www.boost.org
|
Chris@16
|
8
|
Chris@16
|
9 Based on the StringRef implementation in LLVM (http://llvm.org) and
|
Chris@16
|
10 N3422 by Jeffrey Yasskin
|
Chris@16
|
11 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
|
Chris@16
|
12
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_STRING_REF_HPP
|
Chris@16
|
16 #define BOOST_STRING_REF_HPP
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/config.hpp>
|
Chris@16
|
19 #include <boost/detail/workaround.hpp>
|
Chris@16
|
20 #include <boost/utility/string_ref_fwd.hpp>
|
Chris@16
|
21 #include <boost/throw_exception.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstddef>
|
Chris@16
|
24 #include <stdexcept>
|
Chris@16
|
25 #include <algorithm>
|
Chris@16
|
26 #include <iterator>
|
Chris@16
|
27 #include <string>
|
Chris@16
|
28 #include <iosfwd>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31
|
Chris@16
|
32 namespace detail {
|
Chris@16
|
33 // A helper functor because sometimes we don't have lambdas
|
Chris@16
|
34 template <typename charT, typename traits>
|
Chris@16
|
35 class string_ref_traits_eq {
|
Chris@16
|
36 public:
|
Chris@16
|
37 string_ref_traits_eq ( charT ch ) : ch_(ch) {}
|
Chris@16
|
38 bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
|
Chris@16
|
39 charT ch_;
|
Chris@16
|
40 };
|
Chris@16
|
41 }
|
Chris@16
|
42
|
Chris@16
|
43 template<typename charT, typename traits>
|
Chris@16
|
44 class basic_string_ref {
|
Chris@16
|
45 public:
|
Chris@16
|
46 // types
|
Chris@16
|
47 typedef charT value_type;
|
Chris@16
|
48 typedef const charT* pointer;
|
Chris@16
|
49 typedef const charT& reference;
|
Chris@16
|
50 typedef const charT& const_reference;
|
Chris@16
|
51 typedef pointer const_iterator; // impl-defined
|
Chris@16
|
52 typedef const_iterator iterator;
|
Chris@16
|
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
Chris@16
|
54 typedef const_reverse_iterator reverse_iterator;
|
Chris@16
|
55 typedef std::size_t size_type;
|
Chris@16
|
56 typedef std::ptrdiff_t difference_type;
|
Chris@16
|
57 static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
|
Chris@16
|
58
|
Chris@16
|
59 // construct/copy
|
Chris@16
|
60 BOOST_CONSTEXPR basic_string_ref ()
|
Chris@16
|
61 : ptr_(NULL), len_(0) {}
|
Chris@16
|
62
|
Chris@16
|
63 BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs)
|
Chris@16
|
64 : ptr_(rhs.ptr_), len_(rhs.len_) {}
|
Chris@16
|
65
|
Chris@16
|
66 basic_string_ref& operator=(const basic_string_ref &rhs) {
|
Chris@16
|
67 ptr_ = rhs.ptr_;
|
Chris@16
|
68 len_ = rhs.len_;
|
Chris@16
|
69 return *this;
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 basic_string_ref(const charT* str)
|
Chris@16
|
73 : ptr_(str), len_(traits::length(str)) {}
|
Chris@16
|
74
|
Chris@16
|
75 template<typename Allocator>
|
Chris@16
|
76 basic_string_ref(const std::basic_string<charT, traits, Allocator>& str)
|
Chris@16
|
77 : ptr_(str.data()), len_(str.length()) {}
|
Chris@16
|
78
|
Chris@16
|
79 BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len)
|
Chris@16
|
80 : ptr_(str), len_(len) {}
|
Chris@16
|
81
|
Chris@16
|
82 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
|
Chris@16
|
83 template<typename Allocator>
|
Chris@16
|
84 explicit operator std::basic_string<charT, traits, Allocator>() const {
|
Chris@16
|
85 return std::basic_string<charT, traits, Allocator> ( ptr_, len_ );
|
Chris@16
|
86 }
|
Chris@16
|
87 #endif
|
Chris@16
|
88
|
Chris@16
|
89 std::basic_string<charT, traits> to_string () const {
|
Chris@16
|
90 return std::basic_string<charT, traits> ( ptr_, len_ );
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93 // iterators
|
Chris@16
|
94 BOOST_CONSTEXPR const_iterator begin() const { return ptr_; }
|
Chris@16
|
95 BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; }
|
Chris@16
|
96 BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; }
|
Chris@16
|
97 BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; }
|
Chris@16
|
98 const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
|
Chris@16
|
99 const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
|
Chris@16
|
100 const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
|
Chris@16
|
101 const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
|
Chris@16
|
102
|
Chris@16
|
103 // capacity
|
Chris@16
|
104 BOOST_CONSTEXPR size_type size() const { return len_; }
|
Chris@16
|
105 BOOST_CONSTEXPR size_type length() const { return len_; }
|
Chris@16
|
106 BOOST_CONSTEXPR size_type max_size() const { return len_; }
|
Chris@16
|
107 BOOST_CONSTEXPR bool empty() const { return len_ == 0; }
|
Chris@16
|
108
|
Chris@16
|
109 // element access
|
Chris@16
|
110 BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
|
Chris@16
|
111
|
Chris@16
|
112 const charT& at(size_t pos) const {
|
Chris@16
|
113 if ( pos >= len_ )
|
Chris@16
|
114 BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
|
Chris@16
|
115 return ptr_[pos];
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; }
|
Chris@16
|
119 BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; }
|
Chris@16
|
120 BOOST_CONSTEXPR const charT* data() const { return ptr_; }
|
Chris@16
|
121
|
Chris@16
|
122 // modifiers
|
Chris@16
|
123 void clear() { len_ = 0; }
|
Chris@16
|
124 void remove_prefix(size_type n) {
|
Chris@16
|
125 if ( n > len_ )
|
Chris@16
|
126 n = len_;
|
Chris@16
|
127 ptr_ += n;
|
Chris@16
|
128 len_ -= n;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 void remove_suffix(size_type n) {
|
Chris@16
|
132 if ( n > len_ )
|
Chris@16
|
133 n = len_;
|
Chris@16
|
134 len_ -= n;
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137
|
Chris@16
|
138 // basic_string_ref string operations
|
Chris@16
|
139 basic_string_ref substr(size_type pos, size_type n=npos) const {
|
Chris@16
|
140 if ( pos > size())
|
Chris@16
|
141 BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
|
Chris@16
|
142 if ( n == npos || pos + n > size())
|
Chris@16
|
143 n = size () - pos;
|
Chris@16
|
144 return basic_string_ref ( data() + pos, n );
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 int compare(basic_string_ref x) const {
|
Chris@16
|
148 const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
|
Chris@16
|
149 return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
|
Chris@16
|
153 bool starts_with(basic_string_ref x) const {
|
Chris@16
|
154 return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
|
Chris@16
|
155 }
|
Chris@16
|
156
|
Chris@16
|
157 bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
|
Chris@16
|
158 bool ends_with(basic_string_ref x) const {
|
Chris@16
|
159 return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
|
Chris@16
|
160 }
|
Chris@16
|
161
|
Chris@16
|
162 size_type find(basic_string_ref s) const {
|
Chris@16
|
163 const_iterator iter = std::search ( this->cbegin (), this->cend (),
|
Chris@16
|
164 s.cbegin (), s.cend (), traits::eq );
|
Chris@16
|
165 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 size_type find(charT c) const {
|
Chris@16
|
169 const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
|
Chris@16
|
170 detail::string_ref_traits_eq<charT, traits> ( c ));
|
Chris@16
|
171 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 size_type rfind(basic_string_ref s) const {
|
Chris@16
|
175 const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
|
Chris@16
|
176 s.crbegin (), s.crend (), traits::eq );
|
Chris@16
|
177 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 size_type rfind(charT c) const {
|
Chris@16
|
181 const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
|
Chris@16
|
182 detail::string_ref_traits_eq<charT, traits> ( c ));
|
Chris@16
|
183 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 size_type find_first_of(charT c) const { return find (c); }
|
Chris@16
|
187 size_type find_last_of (charT c) const { return rfind (c); }
|
Chris@16
|
188
|
Chris@16
|
189 size_type find_first_of(basic_string_ref s) const {
|
Chris@16
|
190 const_iterator iter = std::find_first_of
|
Chris@16
|
191 ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
|
Chris@16
|
192 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 size_type find_last_of(basic_string_ref s) const {
|
Chris@16
|
196 const_reverse_iterator iter = std::find_first_of
|
Chris@16
|
197 ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
|
Chris@16
|
198 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 size_type find_first_not_of(basic_string_ref s) const {
|
Chris@16
|
202 const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
|
Chris@16
|
203 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 size_type find_first_not_of(charT c) const {
|
Chris@16
|
207 for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
|
Chris@16
|
208 if ( !traits::eq ( c, *iter ))
|
Chris@16
|
209 return std::distance ( this->cbegin (), iter );
|
Chris@16
|
210 return npos;
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 size_type find_last_not_of(basic_string_ref s) const {
|
Chris@16
|
214 const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
|
Chris@16
|
215 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 size_type find_last_not_of(charT c) const {
|
Chris@16
|
219 for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
|
Chris@16
|
220 if ( !traits::eq ( c, *iter ))
|
Chris@16
|
221 return reverse_distance ( this->crbegin (), iter );
|
Chris@16
|
222 return npos;
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 private:
|
Chris@16
|
226 template <typename r_iter>
|
Chris@16
|
227 size_type reverse_distance ( r_iter first, r_iter last ) const {
|
Chris@16
|
228 return len_ - 1 - std::distance ( first, last );
|
Chris@16
|
229 }
|
Chris@16
|
230
|
Chris@16
|
231 template <typename Iterator>
|
Chris@16
|
232 Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
|
Chris@16
|
233 for ( ; first != last ; ++first )
|
Chris@16
|
234 if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
|
Chris@16
|
235 return first;
|
Chris@16
|
236 return last;
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239
|
Chris@16
|
240
|
Chris@16
|
241 const charT *ptr_;
|
Chris@16
|
242 std::size_t len_;
|
Chris@16
|
243 };
|
Chris@16
|
244
|
Chris@16
|
245
|
Chris@16
|
246 // Comparison operators
|
Chris@16
|
247 // Equality
|
Chris@16
|
248 template<typename charT, typename traits>
|
Chris@16
|
249 inline bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
250 if ( x.size () != y.size ()) return false;
|
Chris@16
|
251 return x.compare(y) == 0;
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
255 inline bool operator==(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
256 return x == basic_string_ref<charT, traits>(y);
|
Chris@16
|
257 }
|
Chris@16
|
258
|
Chris@16
|
259 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
260 inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
261 return basic_string_ref<charT, traits>(x) == y;
|
Chris@16
|
262 }
|
Chris@16
|
263
|
Chris@16
|
264 template<typename charT, typename traits>
|
Chris@16
|
265 inline bool operator==(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
266 return x == basic_string_ref<charT, traits>(y);
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 template<typename charT, typename traits>
|
Chris@16
|
270 inline bool operator==(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
271 return basic_string_ref<charT, traits>(x) == y;
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 // Inequality
|
Chris@16
|
275 template<typename charT, typename traits>
|
Chris@16
|
276 inline bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
277 if ( x.size () != y.size ()) return true;
|
Chris@16
|
278 return x.compare(y) != 0;
|
Chris@16
|
279 }
|
Chris@16
|
280
|
Chris@16
|
281 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
282 inline bool operator!=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
283 return x != basic_string_ref<charT, traits>(y);
|
Chris@16
|
284 }
|
Chris@16
|
285
|
Chris@16
|
286 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
287 inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
288 return basic_string_ref<charT, traits>(x) != y;
|
Chris@16
|
289 }
|
Chris@16
|
290
|
Chris@16
|
291 template<typename charT, typename traits>
|
Chris@16
|
292 inline bool operator!=(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
293 return x != basic_string_ref<charT, traits>(y);
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 template<typename charT, typename traits>
|
Chris@16
|
297 inline bool operator!=(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
298 return basic_string_ref<charT, traits>(x) != y;
|
Chris@16
|
299 }
|
Chris@16
|
300
|
Chris@16
|
301 // Less than
|
Chris@16
|
302 template<typename charT, typename traits>
|
Chris@16
|
303 inline bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
304 return x.compare(y) < 0;
|
Chris@16
|
305 }
|
Chris@16
|
306
|
Chris@16
|
307 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
308 inline bool operator<(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
309 return x < basic_string_ref<charT, traits>(y);
|
Chris@16
|
310 }
|
Chris@16
|
311
|
Chris@16
|
312 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
313 inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
314 return basic_string_ref<charT, traits>(x) < y;
|
Chris@16
|
315 }
|
Chris@16
|
316
|
Chris@16
|
317 template<typename charT, typename traits>
|
Chris@16
|
318 inline bool operator<(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
319 return x < basic_string_ref<charT, traits>(y);
|
Chris@16
|
320 }
|
Chris@16
|
321
|
Chris@16
|
322 template<typename charT, typename traits>
|
Chris@16
|
323 inline bool operator<(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
324 return basic_string_ref<charT, traits>(x) < y;
|
Chris@16
|
325 }
|
Chris@16
|
326
|
Chris@16
|
327 // Greater than
|
Chris@16
|
328 template<typename charT, typename traits>
|
Chris@16
|
329 inline bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
330 return x.compare(y) > 0;
|
Chris@16
|
331 }
|
Chris@16
|
332
|
Chris@16
|
333 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
334 inline bool operator>(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
335 return x > basic_string_ref<charT, traits>(y);
|
Chris@16
|
336 }
|
Chris@16
|
337
|
Chris@16
|
338 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
339 inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
340 return basic_string_ref<charT, traits>(x) > y;
|
Chris@16
|
341 }
|
Chris@16
|
342
|
Chris@16
|
343 template<typename charT, typename traits>
|
Chris@16
|
344 inline bool operator>(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
345 return x > basic_string_ref<charT, traits>(y);
|
Chris@16
|
346 }
|
Chris@16
|
347
|
Chris@16
|
348 template<typename charT, typename traits>
|
Chris@16
|
349 inline bool operator>(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
350 return basic_string_ref<charT, traits>(x) > y;
|
Chris@16
|
351 }
|
Chris@16
|
352
|
Chris@16
|
353 // Less than or equal to
|
Chris@16
|
354 template<typename charT, typename traits>
|
Chris@16
|
355 inline bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
356 return x.compare(y) <= 0;
|
Chris@16
|
357 }
|
Chris@16
|
358
|
Chris@16
|
359 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
360 inline bool operator<=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
361 return x <= basic_string_ref<charT, traits>(y);
|
Chris@16
|
362 }
|
Chris@16
|
363
|
Chris@16
|
364 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
365 inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
366 return basic_string_ref<charT, traits>(x) <= y;
|
Chris@16
|
367 }
|
Chris@16
|
368
|
Chris@16
|
369 template<typename charT, typename traits>
|
Chris@16
|
370 inline bool operator<=(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
371 return x <= basic_string_ref<charT, traits>(y);
|
Chris@16
|
372 }
|
Chris@16
|
373
|
Chris@16
|
374 template<typename charT, typename traits>
|
Chris@16
|
375 inline bool operator<=(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
376 return basic_string_ref<charT, traits>(x) <= y;
|
Chris@16
|
377 }
|
Chris@16
|
378
|
Chris@16
|
379 // Greater than or equal to
|
Chris@16
|
380 template<typename charT, typename traits>
|
Chris@16
|
381 inline bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
382 return x.compare(y) >= 0;
|
Chris@16
|
383 }
|
Chris@16
|
384
|
Chris@16
|
385 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
386 inline bool operator>=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
|
Chris@16
|
387 return x >= basic_string_ref<charT, traits>(y);
|
Chris@16
|
388 }
|
Chris@16
|
389
|
Chris@16
|
390 template<typename charT, typename traits, typename Allocator>
|
Chris@16
|
391 inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
392 return basic_string_ref<charT, traits>(x) >= y;
|
Chris@16
|
393 }
|
Chris@16
|
394
|
Chris@16
|
395 template<typename charT, typename traits>
|
Chris@16
|
396 inline bool operator>=(basic_string_ref<charT, traits> x, const charT * y) {
|
Chris@16
|
397 return x >= basic_string_ref<charT, traits>(y);
|
Chris@16
|
398 }
|
Chris@16
|
399
|
Chris@16
|
400 template<typename charT, typename traits>
|
Chris@16
|
401 inline bool operator>=(const charT * x, basic_string_ref<charT, traits> y) {
|
Chris@16
|
402 return basic_string_ref<charT, traits>(x) >= y;
|
Chris@16
|
403 }
|
Chris@16
|
404
|
Chris@16
|
405 namespace detail {
|
Chris@16
|
406
|
Chris@16
|
407 template<class charT, class traits>
|
Chris@16
|
408 inline void insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
|
Chris@16
|
409 enum { chunk_size = 8 };
|
Chris@16
|
410 charT fill_chars[chunk_size];
|
Chris@16
|
411 std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
|
Chris@16
|
412 for (; n >= chunk_size && os.good(); n -= chunk_size)
|
Chris@16
|
413 os.write(fill_chars, static_cast< std::size_t >(chunk_size));
|
Chris@16
|
414 if (n > 0 && os.good())
|
Chris@16
|
415 os.write(fill_chars, n);
|
Chris@16
|
416 }
|
Chris@16
|
417
|
Chris@16
|
418 template<class charT, class traits>
|
Chris@16
|
419 void insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
|
Chris@16
|
420 const std::size_t size = str.size();
|
Chris@16
|
421 const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size;
|
Chris@16
|
422 const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
|
Chris@16
|
423 if (!align_left) {
|
Chris@16
|
424 detail::insert_fill_chars(os, alignment_size);
|
Chris@16
|
425 if (os.good())
|
Chris@16
|
426 os.write(str.data(), size);
|
Chris@16
|
427 }
|
Chris@16
|
428 else {
|
Chris@16
|
429 os.write(str.data(), size);
|
Chris@16
|
430 if (os.good())
|
Chris@16
|
431 detail::insert_fill_chars(os, alignment_size);
|
Chris@16
|
432 }
|
Chris@16
|
433 }
|
Chris@16
|
434
|
Chris@16
|
435 } // namespace detail
|
Chris@16
|
436
|
Chris@16
|
437 // Inserter
|
Chris@16
|
438 template<class charT, class traits>
|
Chris@16
|
439 inline std::basic_ostream<charT, traits>&
|
Chris@16
|
440 operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
|
Chris@16
|
441 if (os.good()) {
|
Chris@16
|
442 const std::size_t size = str.size();
|
Chris@16
|
443 const std::size_t w = static_cast< std::size_t >(os.width());
|
Chris@16
|
444 if (w <= size)
|
Chris@16
|
445 os.write(str.data(), size);
|
Chris@16
|
446 else
|
Chris@16
|
447 detail::insert_aligned(os, str);
|
Chris@16
|
448 os.width(0);
|
Chris@16
|
449 }
|
Chris@16
|
450 return os;
|
Chris@16
|
451 }
|
Chris@16
|
452
|
Chris@16
|
453 #if 0
|
Chris@16
|
454 // numeric conversions
|
Chris@16
|
455 //
|
Chris@16
|
456 // These are short-term implementations.
|
Chris@16
|
457 // In a production environment, I would rather avoid the copying.
|
Chris@16
|
458 //
|
Chris@16
|
459 inline int stoi (string_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
460 return std::stoi ( std::string(str), idx, base );
|
Chris@16
|
461 }
|
Chris@16
|
462
|
Chris@16
|
463 inline long stol (string_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
464 return std::stol ( std::string(str), idx, base );
|
Chris@16
|
465 }
|
Chris@16
|
466
|
Chris@16
|
467 inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
468 return std::stoul ( std::string(str), idx, base );
|
Chris@16
|
469 }
|
Chris@16
|
470
|
Chris@16
|
471 inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
472 return std::stoll ( std::string(str), idx, base );
|
Chris@16
|
473 }
|
Chris@16
|
474
|
Chris@16
|
475 inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
476 return std::stoull ( std::string(str), idx, base );
|
Chris@16
|
477 }
|
Chris@16
|
478
|
Chris@16
|
479 inline float stof (string_ref str, size_t* idx=0) {
|
Chris@16
|
480 return std::stof ( std::string(str), idx );
|
Chris@16
|
481 }
|
Chris@16
|
482
|
Chris@16
|
483 inline double stod (string_ref str, size_t* idx=0) {
|
Chris@16
|
484 return std::stod ( std::string(str), idx );
|
Chris@16
|
485 }
|
Chris@16
|
486
|
Chris@16
|
487 inline long double stold (string_ref str, size_t* idx=0) {
|
Chris@16
|
488 return std::stold ( std::string(str), idx );
|
Chris@16
|
489 }
|
Chris@16
|
490
|
Chris@16
|
491 inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
492 return std::stoi ( std::wstring(str), idx, base );
|
Chris@16
|
493 }
|
Chris@16
|
494
|
Chris@16
|
495 inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
496 return std::stol ( std::wstring(str), idx, base );
|
Chris@16
|
497 }
|
Chris@16
|
498
|
Chris@16
|
499 inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
500 return std::stoul ( std::wstring(str), idx, base );
|
Chris@16
|
501 }
|
Chris@16
|
502
|
Chris@16
|
503 inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
504 return std::stoll ( std::wstring(str), idx, base );
|
Chris@16
|
505 }
|
Chris@16
|
506
|
Chris@16
|
507 inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
|
Chris@16
|
508 return std::stoull ( std::wstring(str), idx, base );
|
Chris@16
|
509 }
|
Chris@16
|
510
|
Chris@16
|
511 inline float stof (wstring_ref str, size_t* idx=0) {
|
Chris@16
|
512 return std::stof ( std::wstring(str), idx );
|
Chris@16
|
513 }
|
Chris@16
|
514
|
Chris@16
|
515 inline double stod (wstring_ref str, size_t* idx=0) {
|
Chris@16
|
516 return std::stod ( std::wstring(str), idx );
|
Chris@16
|
517 }
|
Chris@16
|
518
|
Chris@16
|
519 inline long double stold (wstring_ref str, size_t* idx=0) {
|
Chris@16
|
520 return std::stold ( std::wstring(str), idx );
|
Chris@16
|
521 }
|
Chris@16
|
522 #endif
|
Chris@16
|
523
|
Chris@16
|
524 }
|
Chris@16
|
525
|
Chris@16
|
526 #if 0
|
Chris@16
|
527 namespace std {
|
Chris@16
|
528 // Hashing
|
Chris@16
|
529 template<> struct hash<boost::string_ref>;
|
Chris@16
|
530 template<> struct hash<boost::u16string_ref>;
|
Chris@16
|
531 template<> struct hash<boost::u32string_ref>;
|
Chris@16
|
532 template<> struct hash<boost::wstring_ref>;
|
Chris@16
|
533 }
|
Chris@16
|
534 #endif
|
Chris@16
|
535
|
Chris@16
|
536 #endif
|