basic_cstring.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // (C) Copyright Gennadiy Rozental 2004-2008.
  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: 57992 $
  10. //
  11. // Description : class basic_cstring wraps C string and provide std_string like
  12. // interface
  13. // ***************************************************************************
  14. #ifndef BOOST_TEST_BASIC_CSTRING_HPP_071894GER
  15. #define BOOST_TEST_BASIC_CSTRING_HPP_071894GER
  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. // STL
  20. #include <string>
  21. #include <boost/test/detail/suppress_warnings.hpp>
  22. //____________________________________________________________________________//
  23. namespace boost {
  24. namespace unit_test {
  25. // ************************************************************************** //
  26. // ************** basic_cstring ************** //
  27. // ************************************************************************** //
  28. template<typename CharT>
  29. class basic_cstring {
  30. typedef basic_cstring<CharT> self_type;
  31. public:
  32. // Subtypes
  33. typedef ut_detail::bcs_char_traits<CharT> traits_type;
  34. typedef typename ut_detail::bcs_char_traits<CharT>::std_string std_string;
  35. typedef CharT value_type;
  36. typedef value_type* pointer;
  37. typedef value_type const* const_pointer;
  38. typedef value_type& reference;
  39. typedef const value_type& const_reference;
  40. typedef std::size_t size_type;
  41. typedef std::ptrdiff_t difference_type;
  42. typedef value_type const* const_iterator;
  43. typedef value_type* iterator;
  44. // !! should also present reverse_iterator, const_reverse_iterator
  45. #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
  46. enum npos_type { npos = static_cast<size_type>(-1) };
  47. #else
  48. // IBM/VisualAge version 6 is not able to handle enums larger than 4 bytes.
  49. // But size_type is 8 bytes in 64bit mode.
  50. static const size_type npos = -1 ;
  51. #endif
  52. static pointer null_str();
  53. // Constructors; default copy constructor is generated by compiler
  54. basic_cstring();
  55. basic_cstring( std_string const& s );
  56. basic_cstring( pointer s );
  57. basic_cstring( pointer s, size_type arg_size );
  58. basic_cstring( pointer first, pointer last );
  59. // data access methods
  60. value_type operator[]( size_type index ) const;
  61. value_type at( size_type index ) const;
  62. // size operators
  63. size_type size() const;
  64. bool is_empty() const;
  65. void clear();
  66. void resize( size_type new_len );
  67. // !! only for STL container conformance use is_empty instead
  68. bool empty() const;
  69. // Trimming
  70. self_type& trim_right( size_type trim_size );
  71. self_type& trim_left( size_type trim_size );
  72. self_type& trim_right( iterator it );
  73. self_type& trim_left( iterator it );
  74. #ifndef __IBMCPP__
  75. self_type& trim_left( self_type exclusions = self_type() ) ;
  76. self_type& trim_right( self_type exclusions = self_type() ) ;
  77. self_type& trim( self_type exclusions = self_type() ) ;
  78. #else
  79. // VisualAge version 6 has in this case a problem with the default arguments.
  80. self_type& trim_left( self_type exclusions ) ;
  81. self_type& trim_right( self_type exclusions ) ;
  82. self_type& trim( self_type exclusions ) ;
  83. self_type& trim_left() { trim_left( self_type() ) ; }
  84. self_type& trim_right() { trim_right( self_type() ) ; }
  85. self_type& trim() { trim( self_type() ) ; }
  86. #endif
  87. // Assignment operators
  88. basic_cstring& operator=( self_type const& s );
  89. basic_cstring& operator=( std_string const& s );
  90. basic_cstring& operator=( pointer s );
  91. template<typename CharT2>
  92. basic_cstring& assign( basic_cstring<CharT2> const& s ) { *this = basic_cstring<CharT>( s.begin(), s.end() ); return *this; }
  93. basic_cstring& assign( self_type const& s, size_type pos, size_type len );
  94. basic_cstring& assign( std_string const& s );
  95. basic_cstring& assign( std_string const& s, size_type pos, size_type len );
  96. basic_cstring& assign( pointer s );
  97. basic_cstring& assign( pointer s, size_type len );
  98. basic_cstring& assign( pointer f, pointer l );
  99. // swapping
  100. void swap( self_type& s );
  101. // Iterators
  102. iterator begin();
  103. const_iterator begin() const;
  104. iterator end();
  105. const_iterator end() const;
  106. // !! should have rbegin, rend
  107. // substring search operation
  108. size_type find( basic_cstring ) const;
  109. size_type rfind( basic_cstring ) const;
  110. self_type substr( size_type beg_index, size_type end_index = npos ) const;
  111. private:
  112. static self_type default_trim_ex();
  113. // Data members
  114. iterator m_begin;
  115. iterator m_end;
  116. };
  117. //____________________________________________________________________________//
  118. template<typename CharT>
  119. inline typename basic_cstring<CharT>::pointer
  120. basic_cstring<CharT>::null_str()
  121. {
  122. static CharT null = 0;
  123. return &null;
  124. }
  125. //____________________________________________________________________________//
  126. template<typename CharT>
  127. inline
  128. basic_cstring<CharT>::basic_cstring()
  129. : m_begin( null_str() )
  130. , m_end( m_begin )
  131. {
  132. }
  133. //____________________________________________________________________________//
  134. template<typename CharT>
  135. inline
  136. basic_cstring<CharT>::basic_cstring( std_string const& s )
  137. : m_begin( s.c_str() )
  138. , m_end( m_begin + s.size() )
  139. {
  140. }
  141. //____________________________________________________________________________//
  142. template<typename CharT>
  143. inline
  144. basic_cstring<CharT>::basic_cstring( pointer s )
  145. : m_begin( s ? s : null_str() )
  146. , m_end ( m_begin + (s ? traits_type::length( s ) : 0 ) )
  147. {
  148. }
  149. //____________________________________________________________________________//
  150. template<typename CharT>
  151. inline
  152. basic_cstring<CharT>::basic_cstring( pointer s, size_type arg_size )
  153. : m_begin( s ), m_end( m_begin + arg_size )
  154. {
  155. }
  156. //____________________________________________________________________________//
  157. template<typename CharT>
  158. inline
  159. basic_cstring<CharT>::basic_cstring( pointer first, pointer last )
  160. : m_begin( first )
  161. , m_end( last )
  162. {
  163. }
  164. //____________________________________________________________________________//
  165. template<typename CharT>
  166. inline typename basic_cstring<CharT>::value_type
  167. basic_cstring<CharT>::operator[]( size_type index ) const
  168. {
  169. return m_begin[index];
  170. }
  171. //____________________________________________________________________________//
  172. template<typename CharT>
  173. inline typename basic_cstring<CharT>::value_type
  174. basic_cstring<CharT>::at( size_type index ) const
  175. {
  176. if( m_begin + index >= m_end )
  177. return static_cast<value_type>(0);
  178. return m_begin[index];
  179. }
  180. //____________________________________________________________________________//
  181. template<typename CharT>
  182. inline typename basic_cstring<CharT>::size_type
  183. basic_cstring<CharT>::size() const
  184. {
  185. return m_end - m_begin;
  186. }
  187. //____________________________________________________________________________//
  188. template<typename CharT>
  189. inline bool
  190. basic_cstring<CharT>::is_empty() const
  191. {
  192. return m_end == m_begin;
  193. }
  194. //____________________________________________________________________________//
  195. template<typename CharT>
  196. inline bool
  197. basic_cstring<CharT>::empty() const
  198. {
  199. return is_empty();
  200. }
  201. //____________________________________________________________________________//
  202. template<typename CharT>
  203. inline void
  204. basic_cstring<CharT>::clear()
  205. {
  206. m_begin = m_end;
  207. }
  208. //____________________________________________________________________________//
  209. template<typename CharT>
  210. inline void
  211. basic_cstring<CharT>::resize( size_type new_len )
  212. {
  213. if( m_begin + new_len < m_end )
  214. m_end = m_begin + new_len;
  215. }
  216. //____________________________________________________________________________//
  217. template<typename CharT>
  218. inline basic_cstring<CharT>&
  219. basic_cstring<CharT>::trim_left( size_type trim_size )
  220. {
  221. m_begin += trim_size;
  222. if( m_end <= m_begin )
  223. clear();
  224. return *this;
  225. }
  226. //____________________________________________________________________________//
  227. template<typename CharT>
  228. inline basic_cstring<CharT>&
  229. basic_cstring<CharT>::trim_left( iterator it )
  230. {
  231. m_begin = it;
  232. if( m_end <= m_begin )
  233. clear();
  234. return *this;
  235. }
  236. //____________________________________________________________________________//
  237. template<typename CharT>
  238. inline basic_cstring<CharT>&
  239. basic_cstring<CharT>::trim_left( basic_cstring exclusions )
  240. {
  241. if( exclusions.is_empty() )
  242. exclusions = default_trim_ex();
  243. iterator it;
  244. for( it = begin(); it != end(); ++it ) {
  245. if( traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
  246. break;
  247. }
  248. return trim_left( it );
  249. }
  250. //____________________________________________________________________________//
  251. template<typename CharT>
  252. inline basic_cstring<CharT>&
  253. basic_cstring<CharT>::trim_right( size_type trim_size )
  254. {
  255. m_end -= trim_size;
  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_right( iterator it )
  264. {
  265. m_end = it;
  266. if( m_end <= m_begin )
  267. clear();
  268. return *this;
  269. }
  270. //____________________________________________________________________________//
  271. template<typename CharT>
  272. inline basic_cstring<CharT>&
  273. basic_cstring<CharT>::trim_right( basic_cstring exclusions )
  274. {
  275. if( exclusions.is_empty() )
  276. exclusions = default_trim_ex();
  277. iterator it;
  278. for( it = end()-1; it != begin()-1; --it ) {
  279. if( self_type::traits_type::find( exclusions.begin(), exclusions.size(), *it ) == reinterpret_cast<pointer>(0) )
  280. break;
  281. }
  282. return trim_right( it+1 );
  283. }
  284. //____________________________________________________________________________//
  285. template<typename CharT>
  286. inline basic_cstring<CharT>&
  287. basic_cstring<CharT>::trim( basic_cstring exclusions )
  288. {
  289. trim_left( exclusions );
  290. trim_right( exclusions );
  291. return *this;
  292. }
  293. //____________________________________________________________________________//
  294. template<typename CharT>
  295. inline basic_cstring<CharT>&
  296. basic_cstring<CharT>::operator=( basic_cstring<CharT> const& s )
  297. {
  298. m_begin = s.m_begin;
  299. m_end = s.m_end;
  300. return *this;
  301. }
  302. //____________________________________________________________________________//
  303. template<typename CharT>
  304. inline basic_cstring<CharT>&
  305. basic_cstring<CharT>::operator=( std_string const& s )
  306. {
  307. return *this = self_type( s );
  308. }
  309. //____________________________________________________________________________//
  310. template<typename CharT>
  311. inline basic_cstring<CharT>&
  312. basic_cstring<CharT>::operator=( pointer s )
  313. {
  314. return *this = self_type( s );
  315. }
  316. //____________________________________________________________________________//
  317. template<typename CharT>
  318. inline basic_cstring<CharT>&
  319. basic_cstring<CharT>::assign( basic_cstring<CharT> const& s, size_type pos, size_type len )
  320. {
  321. return *this = self_type( s.m_begin + pos, len );
  322. }
  323. //____________________________________________________________________________//
  324. template<typename CharT>
  325. inline basic_cstring<CharT>&
  326. basic_cstring<CharT>::assign( 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>::assign( std_string const& s, size_type pos, size_type len )
  334. {
  335. return *this = self_type( s.c_str() + pos, len );
  336. }
  337. //____________________________________________________________________________//
  338. template<typename CharT>
  339. inline basic_cstring<CharT>&
  340. basic_cstring<CharT>::assign( pointer 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, size_type len )
  348. {
  349. return *this = self_type( s, len );
  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 ? static_cast<size_type>(npos) : 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_type
  535. first_char( basic_cstring<CharT> source )
  536. {
  537. typedef typename basic_cstring<CharT>::value_type string_value_type;
  538. return source.is_empty() ? static_cast<string_value_type>(0) : *source.begin();
  539. }
  540. //____________________________________________________________________________//
  541. // ************************************************************************** //
  542. // ************** last_char ************** //
  543. // ************************************************************************** //
  544. template<typename CharT>
  545. inline typename basic_cstring<CharT>::value_type
  546. last_char( basic_cstring<CharT> source )
  547. {
  548. typedef typename basic_cstring<CharT>::value_type string_value_type;
  549. return source.is_empty() ? static_cast<string_value_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. } // namespace unit_test
  563. } // namespace boost
  564. //____________________________________________________________________________//
  565. #include <boost/test/detail/enable_warnings.hpp>
  566. #endif // BOOST_TEST_BASIC_CSTRING_HPP_071894GER