]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/utils/basic_cstring/basic_cstring.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / test / utils / basic_cstring / basic_cstring.hpp
1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision$
11 //
12 // Description : class basic_cstring wraps C string and provide std_string like
13 // interface
14 // ***************************************************************************
15
16 #ifndef BOOST_TEST_UTILS_BASIC_CSTRING_HPP
17 #define BOOST_TEST_UTILS_BASIC_CSTRING_HPP
18
19 // Boost.Test
20 #include <boost/test/utils/basic_cstring/basic_cstring_fwd.hpp>
21 #include <boost/test/utils/basic_cstring/bcs_char_traits.hpp>
22
23 // Boost
24 #include <boost/type_traits/remove_cv.hpp>
25
26 // STL
27 #include <string>
28
29 #include <boost/test/detail/suppress_warnings.hpp>
30
31 //____________________________________________________________________________//
32
33 namespace boost {
34
35 namespace unit_test {
36
37 // ************************************************************************** //
38 // ************** basic_cstring ************** //
39 // ************************************************************************** //
40
41 template<typename CharT>
42 class basic_cstring {
43 typedef basic_cstring<CharT> self_type;
44 public:
45 // Subtypes
46 typedef ut_detail::bcs_char_traits<CharT> traits_type;
47 typedef typename traits_type::std_string std_string;
48
49 typedef CharT value_type;
50 typedef typename remove_cv<value_type>::type value_ret_type;
51 typedef value_type* pointer;
52 typedef value_type const* const_pointer;
53 typedef value_type& reference;
54 typedef const value_type& const_reference;
55 typedef std::size_t size_type;
56 typedef std::ptrdiff_t difference_type;
57
58 typedef value_type const* const_iterator;
59 typedef value_type* iterator;
60
61 // !! should also present reverse_iterator, const_reverse_iterator
62
63 #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
64 enum npos_type { npos = static_cast<size_type>(-1) };
65 #else
66 // IBM/VisualAge version 6 is not able to handle enums larger than 4 bytes.
67 // But size_type is 8 bytes in 64bit mode.
68 static const size_type npos = -1 ;
69 #endif
70
71 static pointer null_str();
72
73 // Constructors; default copy constructor is generated by compiler
74 basic_cstring();
75 basic_cstring( basic_cstring const & );
76 basic_cstring( std_string const& s );
77 basic_cstring( pointer s );
78 template<typename LenType>
79 basic_cstring( pointer s, LenType len ) : m_begin( s ), m_end( m_begin + len ) {}
80 basic_cstring( pointer first, pointer last );
81
82 // data access methods
83 value_ret_type operator[]( size_type index ) const;
84 value_ret_type at( size_type index ) const;
85
86 // size operators
87 size_type size() const;
88 bool is_empty() const;
89 void clear();
90 void resize( size_type new_len );
91
92 // !! only for STL container conformance use is_empty instead
93 bool empty() const;
94
95 // Trimming
96 self_type& trim_right( size_type trim_size );
97 self_type& trim_left( size_type trim_size );
98 self_type& trim_right( iterator it );
99 self_type& trim_left( iterator it );
100 #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(800))
101 self_type& trim_left( self_type exclusions = self_type() ) ;
102 self_type& trim_right( self_type exclusions = self_type() ) ;
103 self_type& trim( self_type exclusions = self_type() ) ;
104 #else
105 // VA C++/XL C++ v6 and v8 has in this case a problem with the default arguments.
106 self_type& trim_left( self_type exclusions );
107 self_type& trim_right( self_type exclusions );
108 self_type& trim( self_type exclusions );
109 self_type& trim_left() { return trim_left( self_type() ); }
110 self_type& trim_right() { return trim_right( self_type() ); }
111 self_type& trim() { return trim( self_type() ); }
112 #endif
113
114 // Assignment operators
115 basic_cstring& operator=( self_type const& s );
116 basic_cstring& operator=( std_string const& s );
117 basic_cstring& operator=( pointer s );
118
119 template<typename CharT2>
120 basic_cstring& assign( basic_cstring<CharT2> const& s )
121 {
122 return *this = basic_cstring<CharT>( s.begin(), s.end() );
123 }
124 template<typename PosType, typename LenType>
125 basic_cstring& assign( self_type const& s, PosType pos, LenType len )
126 {
127 return *this = self_type( s.m_begin + pos, len );
128 }
129
130 basic_cstring& assign( std_string const& s );
131 template<typename PosType, typename LenType>
132 basic_cstring& assign( std_string const& s, PosType pos, LenType len )
133 {
134 return *this = self_type( s.c_str() + pos, len );
135 }
136 basic_cstring& assign( pointer s );
137 template<typename LenType>
138 basic_cstring& assign( pointer s, LenType len )
139 {
140 return *this = self_type( s, len );
141 }
142 basic_cstring& assign( pointer f, pointer l );
143
144 // swapping
145 void swap( self_type& s );
146
147 // Iterators
148 iterator begin();
149 const_iterator begin() const;
150 iterator end();
151 const_iterator end() const;
152
153 // !! should have rbegin, rend
154
155 // substring search operation
156 size_type find( basic_cstring ) const;
157 size_type rfind( basic_cstring ) const;
158 self_type substr( size_type beg_index, size_type end_index = npos ) const;
159
160 private:
161 static self_type default_trim_ex();
162
163 // Data members
164 iterator m_begin;
165 iterator m_end;
166 };
167
168 //____________________________________________________________________________//
169
170 template<typename CharT>
171 inline typename basic_cstring<CharT>::pointer
172 basic_cstring<CharT>::null_str()
173 {
174 static CharT null = 0;
175 return &null;
176 }
177
178 //____________________________________________________________________________//
179
180 template<typename CharT>
181 inline
182 basic_cstring<CharT>::basic_cstring()
183 : m_begin( null_str() )
184 , m_end( m_begin )
185 {
186 }
187
188 //____________________________________________________________________________//
189
190 template<typename CharT>
191 inline
192 basic_cstring<CharT>::basic_cstring(basic_cstring const & s)
193 : m_begin( s.m_begin )
194 , m_end( s.m_end )
195 {
196 }
197
198 //____________________________________________________________________________//
199
200 template<typename CharT>
201 inline
202 basic_cstring<CharT>::basic_cstring( std_string const& s )
203 : m_begin( s.c_str() )
204 , m_end( m_begin + s.size() )
205 {
206 }
207
208 //____________________________________________________________________________//
209
210 template<typename CharT>
211 inline
212 basic_cstring<CharT>::basic_cstring( pointer s )
213 : m_begin( s ? s : null_str() )
214 , m_end ( m_begin + (s ? traits_type::length( s ) : 0 ) )
215 {
216 }
217
218 //____________________________________________________________________________//
219
220 template<typename CharT>
221 inline
222 basic_cstring<CharT>::basic_cstring( pointer first, pointer last )
223 : m_begin( first )
224 , m_end( last )
225 {
226 }
227
228 //____________________________________________________________________________//
229
230 template<typename CharT>
231 inline typename basic_cstring<CharT>::value_ret_type
232 basic_cstring<CharT>::operator[]( size_type index ) const
233 {
234 return m_begin[index];
235 }
236
237 //____________________________________________________________________________//
238
239 template<typename CharT>
240 inline typename basic_cstring<CharT>::value_ret_type
241 basic_cstring<CharT>::at( size_type index ) const
242 {
243 if( m_begin + index >= m_end )
244 return static_cast<value_type>(0);
245
246 return m_begin[index];
247 }
248
249 //____________________________________________________________________________//
250
251 template<typename CharT>
252 inline typename basic_cstring<CharT>::size_type
253 basic_cstring<CharT>::size() const
254 {
255 return static_cast<size_type>(m_end - m_begin);
256 }
257
258 //____________________________________________________________________________//
259
260 template<typename CharT>
261 inline bool
262 basic_cstring<CharT>::is_empty() const
263 {
264 return m_end == m_begin;
265 }
266
267 //____________________________________________________________________________//
268
269 template<typename CharT>
270 inline bool
271 basic_cstring<CharT>::empty() const
272 {
273 return is_empty();
274 }
275
276 //____________________________________________________________________________//
277
278 template<typename CharT>
279 inline void
280 basic_cstring<CharT>::clear()
281 {
282 m_begin = m_end;
283 }
284
285 //____________________________________________________________________________//
286
287 template<typename CharT>
288 inline void
289 basic_cstring<CharT>::resize( size_type new_len )
290 {
291 if( m_begin + new_len < m_end )
292 m_end = m_begin + new_len;
293 }
294
295 //____________________________________________________________________________//
296
297 template<typename CharT>
298 inline basic_cstring<CharT>&
299 basic_cstring<CharT>::trim_left( size_type trim_size )
300 {
301 m_begin += trim_size;
302 if( m_end <= m_begin )
303 clear();
304
305 return *this;
306 }
307
308 //____________________________________________________________________________//
309
310 template<typename CharT>
311 inline basic_cstring<CharT>&
312 basic_cstring<CharT>::trim_left( iterator it )
313 {
314 m_begin = it;
315 if( m_end <= m_begin )
316 clear();
317
318 return *this;
319 }
320
321 //____________________________________________________________________________//
322
323 template<typename CharT>
324 inline basic_cstring<CharT>&
325 basic_cstring<CharT>::trim_left( basic_cstring exclusions )
326 {
327 if( exclusions.is_empty() )
328 exclusions = default_trim_ex();
329
330 iterator it;
331 for( it = begin(); it != end(); ++it ) {
332 if( traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
333 break;
334 }
335
336 return trim_left( it );
337 }
338
339 //____________________________________________________________________________//
340
341 template<typename CharT>
342 inline basic_cstring<CharT>&
343 basic_cstring<CharT>::trim_right( size_type trim_size )
344 {
345 m_end -= trim_size;
346 if( m_end <= m_begin )
347 clear();
348
349 return *this;
350 }
351
352 //____________________________________________________________________________//
353
354 template<typename CharT>
355 inline basic_cstring<CharT>&
356 basic_cstring<CharT>::trim_right( iterator it )
357 {
358 m_end = it;
359 if( m_end <= m_begin )
360 clear();
361
362 return *this;
363 }
364
365 //____________________________________________________________________________//
366
367 template<typename CharT>
368 inline basic_cstring<CharT>&
369 basic_cstring<CharT>::trim_right( basic_cstring exclusions )
370 {
371 if( exclusions.is_empty() )
372 exclusions = default_trim_ex();
373
374 iterator it;
375
376 for( it = end()-1; it != begin()-1; --it ) {
377 if( self_type::traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
378 break;
379 }
380
381 return trim_right( it+1 );
382 }
383
384 //____________________________________________________________________________//
385
386 template<typename CharT>
387 inline basic_cstring<CharT>&
388 basic_cstring<CharT>::trim( basic_cstring exclusions )
389 {
390 trim_left( exclusions );
391 trim_right( exclusions );
392
393 return *this;
394 }
395
396 //____________________________________________________________________________//
397
398 template<typename CharT>
399 inline basic_cstring<CharT>&
400 basic_cstring<CharT>::operator=( basic_cstring<CharT> const& s )
401 {
402 m_begin = s.m_begin;
403 m_end = s.m_end;
404
405 return *this;
406 }
407
408 //____________________________________________________________________________//
409
410 template<typename CharT>
411 inline basic_cstring<CharT>&
412 basic_cstring<CharT>::operator=( std_string const& s )
413 {
414 return *this = self_type( s );
415 }
416
417 //____________________________________________________________________________//
418
419 template<typename CharT>
420 inline basic_cstring<CharT>&
421 basic_cstring<CharT>::operator=( pointer s )
422 {
423 return *this = self_type( s );
424 }
425
426 //____________________________________________________________________________//
427
428 template<typename CharT>
429 inline basic_cstring<CharT>&
430 basic_cstring<CharT>::assign( std_string const& s )
431 {
432 return *this = self_type( s );
433 }
434
435 //____________________________________________________________________________//
436
437 template<typename CharT>
438 inline basic_cstring<CharT>&
439 basic_cstring<CharT>::assign( pointer s )
440 {
441 return *this = self_type( s );
442 }
443
444 //____________________________________________________________________________//
445
446 template<typename CharT>
447 inline basic_cstring<CharT>&
448 basic_cstring<CharT>::assign( pointer f, pointer l )
449 {
450 return *this = self_type( f, l );
451 }
452
453 //____________________________________________________________________________//
454
455 template<typename CharT>
456 inline void
457 basic_cstring<CharT>::swap( basic_cstring<CharT>& s )
458 {
459 // do not want to include alogrithm
460 pointer tmp1 = m_begin;
461 pointer tmp2 = m_end;
462
463 m_begin = s.m_begin;
464 m_end = s.m_end;
465
466 s.m_begin = tmp1;
467 s.m_end = tmp2;
468 }
469
470 //____________________________________________________________________________//
471
472 template<typename CharT>
473 inline typename basic_cstring<CharT>::iterator
474 basic_cstring<CharT>::begin()
475 {
476 return m_begin;
477 }
478
479 //____________________________________________________________________________//
480
481 template<typename CharT>
482 inline typename basic_cstring<CharT>::const_iterator
483 basic_cstring<CharT>::begin() const
484 {
485 return m_begin;
486 }
487
488 //____________________________________________________________________________//
489
490 template<typename CharT>
491 inline typename basic_cstring<CharT>::iterator
492 basic_cstring<CharT>::end()
493 {
494 return m_end;
495 }
496
497 //____________________________________________________________________________//
498
499 template<typename CharT>
500 inline typename basic_cstring<CharT>::const_iterator
501 basic_cstring<CharT>::end() const
502 {
503 return m_end;
504 }
505
506 //____________________________________________________________________________//
507
508 template<typename CharT>
509 inline typename basic_cstring<CharT>::size_type
510 basic_cstring<CharT>::find( basic_cstring<CharT> str ) const
511 {
512 if( str.is_empty() || str.size() > size() )
513 return static_cast<size_type>(npos);
514
515 const_iterator it = begin();
516 const_iterator last = end() - str.size() + 1;
517
518 while( it != last ) {
519 if( traits_type::compare( it, str.begin(), str.size() ) == 0 )
520 break;
521
522 ++it;
523 }
524
525 return it == last ? npos : static_cast<size_type>(it - begin());
526 }
527
528 //____________________________________________________________________________//
529
530 template<typename CharT>
531 inline typename basic_cstring<CharT>::size_type
532 basic_cstring<CharT>::rfind( basic_cstring<CharT> str ) const
533 {
534 if( str.is_empty() || str.size() > size() )
535 return static_cast<size_type>(npos);
536
537 const_iterator it = end() - str.size();
538 const_iterator last = begin()-1;
539
540 while( it != last ) {
541 if( traits_type::compare( it, str.begin(), str.size() ) == 0 )
542 break;
543
544 --it;
545 }
546
547 return it == last ? static_cast<size_type>(npos) : static_cast<size_type>(it - begin());
548 }
549
550 //____________________________________________________________________________//
551
552 template<typename CharT>
553 inline basic_cstring<CharT>
554 basic_cstring<CharT>::substr( size_type beg_index, size_type end_index ) const
555 {
556 return beg_index > size()
557 ? self_type()
558 : end_index > size()
559 ? self_type( m_begin + beg_index, m_end )
560 : self_type( m_begin + beg_index, m_begin + end_index );
561 }
562
563 //____________________________________________________________________________//
564
565 template<typename CharT>
566 inline basic_cstring<CharT>
567 basic_cstring<CharT>::default_trim_ex()
568 {
569 static CharT ws[3] = { CharT(' '), CharT('\t'), CharT('\n') }; // !! wide case
570
571 return self_type( ws, 3 );
572 }
573
574 //____________________________________________________________________________//
575
576 // ************************************************************************** //
577 // ************** comparison operators ************** //
578 // ************************************************************************** //
579
580 template<typename CharT1,typename CharT2>
581 inline bool
582 operator==( basic_cstring<CharT1> const& s1, basic_cstring<CharT2> const& s2 )
583 {
584 typedef typename basic_cstring<CharT1>::traits_type traits_type;
585 return s1.size() == s2.size() &&
586 traits_type::compare( s1.begin(), s2.begin(), s1.size() ) == 0;
587 }
588
589 //____________________________________________________________________________//
590
591 template<typename CharT1,typename CharT2>
592 inline bool
593 operator==( basic_cstring<CharT1> const& s1, CharT2* s2 )
594 {
595 #if !defined(__DMC__)
596 return s1 == basic_cstring<CharT2>( s2 );
597 #else
598 return s1 == basic_cstring<CharT2 const>( s2 );
599 #endif
600 }
601
602 //____________________________________________________________________________//
603
604 template<typename CharT>
605 inline bool
606 operator==( basic_cstring<CharT> const& s1, typename basic_cstring<CharT>::std_string const& s2 )
607 {
608 return s1 == basic_cstring<CharT>( s2 );
609 }
610
611 //____________________________________________________________________________//
612
613 template<typename CharT1,typename CharT2>
614 inline bool
615 operator==( CharT1* s2, basic_cstring<CharT2> const& s1 )
616 {
617 return s1 == s2;
618 }
619
620 //____________________________________________________________________________//
621
622 template<typename CharT>
623 inline bool
624 operator==( typename basic_cstring<CharT>::std_string const& s2, basic_cstring<CharT> const& s1 )
625 {
626 return s1 == s2;
627 }
628
629 //____________________________________________________________________________//
630
631 template<typename CharT>
632 inline bool
633 operator!=( basic_cstring<CharT> const& s1, CharT* s2 )
634 {
635 return !(s1 == s2);
636 }
637
638 //____________________________________________________________________________//
639
640 template<typename CharT>
641 inline bool
642 operator!=( CharT* s2, basic_cstring<CharT> const& s1 )
643 {
644 return !(s1 == s2);
645 }
646
647 //____________________________________________________________________________//
648
649 template<typename CharT>
650 inline bool
651 operator!=( basic_cstring<CharT> const& s1, basic_cstring<CharT> const& s2 )
652 {
653 return !(s1 == s2);
654 }
655
656 //____________________________________________________________________________//
657
658 template<typename CharT>
659 inline bool
660 operator!=( basic_cstring<CharT> const& s1, typename basic_cstring<CharT>::std_string const& s2 )
661 {
662 return !(s1 == s2);
663 }
664
665 //____________________________________________________________________________//
666
667 template<typename CharT>
668 inline bool
669 operator!=( typename basic_cstring<CharT>::std_string const& s2, basic_cstring<CharT> const& s1 )
670 {
671 return !(s1 == s2);
672 }
673
674 //____________________________________________________________________________//
675
676 // ************************************************************************** //
677 // ************** first_char ************** //
678 // ************************************************************************** //
679
680 template<typename CharT>
681 inline typename basic_cstring<CharT>::value_ret_type
682 first_char( basic_cstring<CharT> source )
683 {
684 typedef typename basic_cstring<CharT>::value_ret_type res_type;
685
686 return source.is_empty() ? static_cast<res_type>(0) : *source.begin();
687 }
688
689 //____________________________________________________________________________//
690
691 // ************************************************************************** //
692 // ************** last_char ************** //
693 // ************************************************************************** //
694
695 template<typename CharT>
696 inline typename basic_cstring<CharT>::value_ret_type
697 last_char( basic_cstring<CharT> source )
698 {
699 typedef typename basic_cstring<CharT>::value_ret_type res_type;
700
701 return source.is_empty() ? static_cast<res_type>(0) : *(source.end()-1);
702 }
703
704 //____________________________________________________________________________//
705
706 // ************************************************************************** //
707 // ************** assign_op ************** //
708 // ************************************************************************** //
709
710 template<typename CharT1, typename CharT2>
711 inline void
712 assign_op( std::basic_string<CharT1>& target, basic_cstring<CharT2> src, int )
713 {
714 target.assign( src.begin(), src.size() );
715 }
716
717 //____________________________________________________________________________//
718
719 template<typename CharT1, typename CharT2>
720 inline std::basic_string<CharT1>&
721 operator+=( std::basic_string<CharT1>& target, basic_cstring<CharT2> const& str )
722 {
723 target.append( str.begin(), str.end() );
724 return target;
725 }
726
727 //____________________________________________________________________________//
728
729 template<typename CharT1, typename CharT2>
730 inline std::basic_string<CharT1>
731 operator+( std::basic_string<CharT1> const& lhs, basic_cstring<CharT2> const& rhs )
732 {
733 std::basic_string<CharT1> res( lhs );
734
735 res.append( rhs.begin(), rhs.end() );
736 return res;
737 }
738
739 //____________________________________________________________________________//
740
741 } // namespace unit_test
742
743 } // namespace boost
744
745 //____________________________________________________________________________//
746
747 #include <boost/test/detail/enable_warnings.hpp>
748
749 #endif // BOOST_TEST_UTILS_BASIC_CSTRING_HPP