basic_cstring.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 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. };
  137. //____________________________________________________________________________//
  138. template<typename CharT>
  139. inline typename basic_cstring<CharT>::pointer
  140. basic_cstring<CharT>::null_str()
  141. {
  142. static CharT null = 0;
  143. return &null;
  144. }
  145. //____________________________________________________________________________//
  146. template<typename CharT>
  147. inline
  148. basic_cstring<CharT>::basic_cstring()
  149. : m_begin( null_str() )
  150. , m_end( m_begin )
  151. {
  152. }
  153. //____________________________________________________________________________//
  154. template<typename CharT>
  155. inline
  156. basic_cstring<CharT>::basic_cstring(basic_cstring const & s)
  157. : m_begin( s.m_begin )
  158. , m_end( s.m_end )
  159. {
  160. }
  161. //____________________________________________________________________________//
  162. template<typename CharT>
  163. inline
  164. basic_cstring<CharT>::basic_cstring( std_string const& s )
  165. : m_begin( s.c_str() )
  166. , m_end( m_begin + s.size() )
  167. {
  168. }
  169. //____________________________________________________________________________//
  170. template<typename CharT>
  171. inline
  172. basic_cstring<CharT>::basic_cstring( pointer s )
  173. : m_begin( s ? s : null_str() )
  174. , m_end ( m_begin + (s ? traits_type::length( s ) : 0 ) )
  175. {
  176. }
  177. //____________________________________________________________________________//
  178. template<typename CharT>
  179. inline
  180. basic_cstring<CharT>::basic_cstring( pointer first, pointer last )
  181. : m_begin( first )
  182. , m_end( last )
  183. {
  184. }
  185. //____________________________________________________________________________//
  186. template<typename CharT>
  187. inline typename basic_cstring<CharT>::value_ret_type
  188. basic_cstring<CharT>::operator[]( size_type index ) const
  189. {
  190. return m_begin[index];
  191. }
  192. //____________________________________________________________________________//
  193. template<typename CharT>
  194. inline typename basic_cstring<CharT>::value_ret_type
  195. basic_cstring<CharT>::at( size_type index ) const
  196. {
  197. if( m_begin + index >= m_end )
  198. return static_cast<value_type>(0);
  199. return m_begin[index];
  200. }
  201. //____________________________________________________________________________//
  202. template<typename CharT>
  203. inline typename basic_cstring<CharT>::size_type
  204. basic_cstring<CharT>::size() const
  205. {
  206. return static_cast<size_type>(m_end - m_begin);
  207. }
  208. //____________________________________________________________________________//
  209. template<typename CharT>
  210. inline bool
  211. basic_cstring<CharT>::is_empty() const
  212. {
  213. return m_end == m_begin;
  214. }
  215. //____________________________________________________________________________//
  216. template<typename CharT>
  217. inline bool
  218. basic_cstring<CharT>::empty() const
  219. {
  220. return is_empty();
  221. }
  222. //____________________________________________________________________________//
  223. template<typename CharT>
  224. inline void
  225. basic_cstring<CharT>::clear()
  226. {
  227. m_begin = m_end;
  228. }
  229. //____________________________________________________________________________//
  230. template<typename CharT>
  231. inline void
  232. basic_cstring<CharT>::resize( size_type new_len )
  233. {
  234. if( m_begin + new_len < m_end )
  235. m_end = m_begin + new_len;
  236. }
  237. //____________________________________________________________________________//
  238. template<typename CharT>
  239. inline basic_cstring<CharT>&
  240. basic_cstring<CharT>::trim_left( size_type trim_size )
  241. {
  242. m_begin += trim_size;
  243. if( m_end <= m_begin )
  244. clear();
  245. return *this;
  246. }
  247. //____________________________________________________________________________//
  248. template<typename CharT>
  249. inline basic_cstring<CharT>&
  250. basic_cstring<CharT>::trim_left( iterator it )
  251. {
  252. m_begin = it;
  253. if( m_end <= m_begin )
  254. clear();
  255. return *this;
  256. }
  257. //____________________________________________________________________________//
  258. template<typename CharT>
  259. inline basic_cstring<CharT>&
  260. basic_cstring<CharT>::trim_left( basic_cstring exclusions )
  261. {
  262. if( exclusions.is_empty() )
  263. exclusions = default_trim_ex();
  264. iterator it;
  265. for( it = begin(); it != end(); ++it ) {
  266. if( traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
  267. break;
  268. }
  269. return trim_left( it );
  270. }
  271. //____________________________________________________________________________//
  272. template<typename CharT>
  273. inline basic_cstring<CharT>&
  274. basic_cstring<CharT>::trim_right( size_type trim_size )
  275. {
  276. m_end -= trim_size;
  277. if( m_end <= m_begin )
  278. clear();
  279. return *this;
  280. }
  281. //____________________________________________________________________________//
  282. template<typename CharT>
  283. inline basic_cstring<CharT>&
  284. basic_cstring<CharT>::trim_right( iterator it )
  285. {
  286. m_end = it;
  287. if( m_end <= m_begin )
  288. clear();
  289. return *this;
  290. }
  291. //____________________________________________________________________________//
  292. template<typename CharT>
  293. inline basic_cstring<CharT>&
  294. basic_cstring<CharT>::trim_right( basic_cstring exclusions )
  295. {
  296. if( exclusions.is_empty() )
  297. exclusions = default_trim_ex();
  298. iterator it;
  299. for( it = end()-1; it != begin()-1; --it ) {
  300. if( self_type::traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
  301. break;
  302. }
  303. return trim_right( it+1 );
  304. }
  305. //____________________________________________________________________________//
  306. template<typename CharT>
  307. inline basic_cstring<CharT>&
  308. basic_cstring<CharT>::trim( basic_cstring exclusions )
  309. {
  310. trim_left( exclusions );
  311. trim_right( exclusions );
  312. return *this;
  313. }
  314. //____________________________________________________________________________//
  315. template<typename CharT>
  316. inline basic_cstring<CharT>&
  317. basic_cstring<CharT>::operator=( basic_cstring<CharT> const& s )
  318. {
  319. m_begin = s.m_begin;
  320. m_end = s.m_end;
  321. return *this;
  322. }
  323. //____________________________________________________________________________//
  324. template<typename CharT>
  325. inline basic_cstring<CharT>&
  326. basic_cstring<CharT>::operator=( std_string const& s )
  327. {
  328. return *this = self_type( s );
  329. }
  330. //____________________________________________________________________________//
  331. template<typename CharT>
  332. inline basic_cstring<CharT>&
  333. basic_cstring<CharT>::operator=( pointer s )
  334. {
  335. return *this = self_type( s );
  336. }
  337. //____________________________________________________________________________//
  338. template<typename CharT>
  339. inline basic_cstring<CharT>&
  340. basic_cstring<CharT>::assign( std_string const& s )
  341. {
  342. return *this = self_type( s );
  343. }
  344. //____________________________________________________________________________//
  345. template<typename CharT>
  346. inline basic_cstring<CharT>&
  347. basic_cstring<CharT>::assign( pointer s )
  348. {
  349. return *this = self_type( s );
  350. }
  351. //____________________________________________________________________________//
  352. template<typename CharT>
  353. inline basic_cstring<CharT>&
  354. basic_cstring<CharT>::assign( pointer f, pointer l )
  355. {
  356. return *this = self_type( f, l );
  357. }
  358. //____________________________________________________________________________//
  359. template<typename CharT>
  360. inline void
  361. basic_cstring<CharT>::swap( basic_cstring<CharT>& s )
  362. {
  363. // do not want to include alogrithm
  364. pointer tmp1 = m_begin;
  365. pointer tmp2 = m_end;
  366. m_begin = s.m_begin;
  367. m_end = s.m_end;
  368. s.m_begin = tmp1;
  369. s.m_end = tmp2;
  370. }
  371. //____________________________________________________________________________//
  372. template<typename CharT>
  373. inline typename basic_cstring<CharT>::iterator
  374. basic_cstring<CharT>::begin()
  375. {
  376. return m_begin;
  377. }
  378. //____________________________________________________________________________//
  379. template<typename CharT>
  380. inline typename basic_cstring<CharT>::const_iterator
  381. basic_cstring<CharT>::begin() const
  382. {
  383. return m_begin;
  384. }
  385. //____________________________________________________________________________//
  386. template<typename CharT>
  387. inline typename basic_cstring<CharT>::iterator
  388. basic_cstring<CharT>::end()
  389. {
  390. return m_end;
  391. }
  392. //____________________________________________________________________________//
  393. template<typename CharT>
  394. inline typename basic_cstring<CharT>::const_iterator
  395. basic_cstring<CharT>::end() const
  396. {
  397. return m_end;
  398. }
  399. //____________________________________________________________________________//
  400. template<typename CharT>
  401. inline typename basic_cstring<CharT>::size_type
  402. basic_cstring<CharT>::find( basic_cstring<CharT> str ) const
  403. {
  404. if( str.is_empty() || str.size() > size() )
  405. return static_cast<size_type>(npos);
  406. const_iterator it = begin();
  407. const_iterator last = end() - str.size() + 1;
  408. while( it != last ) {
  409. if( traits_type::compare( it, str.begin(), str.size() ) == 0 )
  410. break;
  411. ++it;
  412. }
  413. return it == last ? npos : static_cast<size_type>(it - begin());
  414. }
  415. //____________________________________________________________________________//
  416. template<typename CharT>
  417. inline typename basic_cstring<CharT>::size_type
  418. basic_cstring<CharT>::rfind( basic_cstring<CharT> str ) const
  419. {
  420. if( str.is_empty() || str.size() > size() )
  421. return static_cast<size_type>(npos);
  422. const_iterator it = end() - str.size();
  423. const_iterator last = begin()-1;
  424. while( it != last ) {
  425. if( traits_type::compare( it, str.begin(), str.size() ) == 0 )
  426. break;
  427. --it;
  428. }
  429. return it == last ? static_cast<size_type>(npos) : static_cast<size_type>(it - begin());
  430. }
  431. //____________________________________________________________________________//
  432. template<typename CharT>
  433. inline basic_cstring<CharT>
  434. basic_cstring<CharT>::substr( size_type beg_index, size_type end_index ) const
  435. {
  436. return beg_index > size()
  437. ? self_type()
  438. : end_index > size()
  439. ? self_type( m_begin + beg_index, m_end )
  440. : self_type( m_begin + beg_index, m_begin + end_index );
  441. }
  442. //____________________________________________________________________________//
  443. template<typename CharT>
  444. inline basic_cstring<CharT>
  445. basic_cstring<CharT>::default_trim_ex()
  446. {
  447. static CharT ws[3] = { CharT(' '), CharT('\t'), CharT('\n') }; // !! wide case
  448. return self_type( ws, 3 );
  449. }
  450. //____________________________________________________________________________//
  451. // ************************************************************************** //
  452. // ************** comparison operators ************** //
  453. // ************************************************************************** //
  454. template<typename CharT1,typename CharT2>
  455. inline bool
  456. operator==( basic_cstring<CharT1> const& s1, basic_cstring<CharT2> const& s2 )
  457. {
  458. typedef typename basic_cstring<CharT1>::traits_type traits_type;
  459. return s1.size() == s2.size() &&
  460. traits_type::compare( s1.begin(), s2.begin(), s1.size() ) == 0;
  461. }
  462. //____________________________________________________________________________//
  463. template<typename CharT1,typename CharT2>
  464. inline bool
  465. operator==( basic_cstring<CharT1> const& s1, CharT2* s2 )
  466. {
  467. #if !defined(__DMC__)
  468. return s1 == basic_cstring<CharT2>( s2 );
  469. #else
  470. return s1 == basic_cstring<CharT2 const>( s2 );
  471. #endif
  472. }
  473. //____________________________________________________________________________//
  474. template<typename CharT>
  475. inline bool
  476. operator==( basic_cstring<CharT> const& s1, typename basic_cstring<CharT>::std_string const& s2 )
  477. {
  478. return s1 == basic_cstring<CharT>( s2 );
  479. }
  480. //____________________________________________________________________________//
  481. template<typename CharT1,typename CharT2>
  482. inline bool
  483. operator==( CharT1* s2, basic_cstring<CharT2> const& s1 )
  484. {
  485. return s1 == s2;
  486. }
  487. //____________________________________________________________________________//
  488. template<typename CharT>
  489. inline bool
  490. operator==( typename basic_cstring<CharT>::std_string const& s2, basic_cstring<CharT> const& s1 )
  491. {
  492. return s1 == s2;
  493. }
  494. //____________________________________________________________________________//
  495. template<typename CharT>
  496. inline bool
  497. operator!=( basic_cstring<CharT> const& s1, CharT* s2 )
  498. {
  499. return !(s1 == s2);
  500. }
  501. //____________________________________________________________________________//
  502. template<typename CharT>
  503. inline bool
  504. operator!=( CharT* s2, basic_cstring<CharT> const& s1 )
  505. {
  506. return !(s1 == s2);
  507. }
  508. //____________________________________________________________________________//
  509. template<typename CharT>
  510. inline bool
  511. operator!=( basic_cstring<CharT> const& s1, basic_cstring<CharT> const& s2 )
  512. {
  513. return !(s1 == s2);
  514. }
  515. //____________________________________________________________________________//
  516. template<typename CharT>
  517. inline bool
  518. operator!=( basic_cstring<CharT> const& s1, typename basic_cstring<CharT>::std_string const& s2 )
  519. {
  520. return !(s1 == s2);
  521. }
  522. //____________________________________________________________________________//
  523. template<typename CharT>
  524. inline bool
  525. operator!=( typename basic_cstring<CharT>::std_string const& s2, basic_cstring<CharT> const& s1 )
  526. {
  527. return !(s1 == s2);
  528. }
  529. //____________________________________________________________________________//
  530. // ************************************************************************** //
  531. // ************** first_char ************** //
  532. // ************************************************************************** //
  533. template<typename CharT>
  534. inline typename basic_cstring<CharT>::value_ret_type
  535. first_char( basic_cstring<CharT> source )
  536. {
  537. typedef typename basic_cstring<CharT>::value_ret_type res_type;
  538. return source.is_empty() ? static_cast<res_type>(0) : *source.begin();
  539. }
  540. //____________________________________________________________________________//
  541. // ************************************************************************** //
  542. // ************** last_char ************** //
  543. // ************************************************************************** //
  544. template<typename CharT>
  545. inline typename basic_cstring<CharT>::value_ret_type
  546. last_char( basic_cstring<CharT> source )
  547. {
  548. typedef typename basic_cstring<CharT>::value_ret_type res_type;
  549. return source.is_empty() ? static_cast<res_type>(0) : *(source.end()-1);
  550. }
  551. //____________________________________________________________________________//
  552. // ************************************************************************** //
  553. // ************** assign_op ************** //
  554. // ************************************************************************** //
  555. template<typename CharT1, typename CharT2>
  556. inline void
  557. assign_op( std::basic_string<CharT1>& target, basic_cstring<CharT2> src, int )
  558. {
  559. target.assign( src.begin(), src.size() );
  560. }
  561. //____________________________________________________________________________//
  562. template<typename CharT1, typename CharT2>
  563. inline std::basic_string<CharT1>&
  564. operator+=( std::basic_string<CharT1>& target, basic_cstring<CharT2> const& str )
  565. {
  566. target.append( str.begin(), str.end() );
  567. return target;
  568. }
  569. //____________________________________________________________________________//
  570. template<typename CharT1, typename CharT2>
  571. inline std::basic_string<CharT1>
  572. operator+( std::basic_string<CharT1> const& lhs, basic_cstring<CharT2> const& rhs )
  573. {
  574. std::basic_string<CharT1> res( lhs );
  575. res.append( rhs.begin(), rhs.end() );
  576. return res;
  577. }
  578. //____________________________________________________________________________//
  579. } // namespace unit_test
  580. } // namespace boost
  581. //____________________________________________________________________________//
  582. #include <boost/test/detail/enable_warnings.hpp>
  583. #endif // BOOST_TEST_UTILS_BASIC_CSTRING_HPP