basic_cstring.hpp 21 KB

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