string_view_test2.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. Copyright (c) Marshall Clow 2012-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <new> // for placement new
  8. #include <iostream>
  9. #include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
  10. #include <cstring> // for std::strchr and std::strcmp
  11. #include <cstdlib> // for std::malloc and std::free
  12. #include <boost/utility/string_view.hpp>
  13. #include <boost/config.hpp>
  14. #include <boost/core/lightweight_test.hpp>
  15. typedef boost::string_view string_view;
  16. void ends_with ( const char *arg ) {
  17. const size_t sz = std::strlen ( arg );
  18. string_view sr ( arg );
  19. string_view sr2 ( arg );
  20. const char *p = arg;
  21. while ( *p ) {
  22. BOOST_TEST ( sr.ends_with ( p ));
  23. ++p;
  24. }
  25. while ( !sr2.empty ()) {
  26. BOOST_TEST ( sr.ends_with ( sr2 ));
  27. sr2.remove_prefix (1);
  28. }
  29. sr2 = arg;
  30. while ( !sr2.empty ()) {
  31. BOOST_TEST ( sr.ends_with ( sr2 ));
  32. sr2.remove_prefix (1);
  33. }
  34. char ch = sz == 0 ? '\0' : arg [ sz - 1 ];
  35. sr2 = arg;
  36. if ( sz > 0 )
  37. BOOST_TEST ( sr2.ends_with ( ch ));
  38. BOOST_TEST ( !sr2.ends_with ( ++ch ));
  39. BOOST_TEST ( sr2.ends_with ( string_view()));
  40. }
  41. void starts_with ( const char *arg ) {
  42. const size_t sz = std::strlen ( arg );
  43. string_view sr ( arg );
  44. string_view sr2 ( arg );
  45. const char *p = arg + std::strlen ( arg ) - 1;
  46. while ( p >= arg ) {
  47. std::string foo ( arg, p + 1 );
  48. BOOST_TEST ( sr.starts_with ( foo ));
  49. --p;
  50. }
  51. while ( !sr2.empty ()) {
  52. BOOST_TEST ( sr.starts_with ( sr2 ));
  53. sr2.remove_suffix (1);
  54. }
  55. char ch = *arg;
  56. sr2 = arg;
  57. if ( sz > 0 )
  58. BOOST_TEST ( sr2.starts_with ( ch ));
  59. BOOST_TEST ( !sr2.starts_with ( ++ch ));
  60. BOOST_TEST ( sr2.starts_with ( string_view ()));
  61. }
  62. void reverse ( const char *arg ) {
  63. // Round trip
  64. string_view sr1 ( arg );
  65. std::string string1 ( sr1.rbegin (), sr1.rend ());
  66. string_view sr2 ( string1 );
  67. std::string string2 ( sr2.rbegin (), sr2.rend ());
  68. BOOST_TEST ( std::equal ( sr2.rbegin (), sr2.rend (), arg ));
  69. BOOST_TEST ( string2 == arg );
  70. BOOST_TEST ( std::equal ( sr1.begin (), sr1.end (), string2.begin ()));
  71. }
  72. // This helper function eliminates signed vs. unsigned warnings
  73. string_view::size_type ptr_diff ( const char *res, const char *base ) {
  74. BOOST_TEST ( res >= base );
  75. return static_cast<string_view::size_type> ( res - base );
  76. }
  77. void find ( const char *arg ) {
  78. string_view sr1;
  79. string_view sr2;
  80. const char *p;
  81. // When we search for the empty string, we find it at position 0
  82. BOOST_TEST ( sr1.find (sr2) == 0 );
  83. BOOST_TEST ( sr1.rfind(sr2) == 0 );
  84. // Look for each character in the string(searching from the start)
  85. p = arg;
  86. sr1 = arg;
  87. while ( *p ) {
  88. string_view::size_type pos = sr1.find(*p);
  89. BOOST_TEST ( pos != string_view::npos && ( pos <= ptr_diff ( p, arg )));
  90. ++p;
  91. }
  92. // Look for each character in the string (searching from the end)
  93. p = arg;
  94. sr1 = arg;
  95. while ( *p ) {
  96. string_view::size_type pos = sr1.rfind(*p);
  97. BOOST_TEST ( pos != string_view::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg )));
  98. ++p;
  99. }
  100. // Look for pairs on characters (searching from the start)
  101. sr1 = arg;
  102. p = arg;
  103. while ( *p && *(p+1)) {
  104. string_view sr3 ( p, 2 );
  105. string_view::size_type pos = sr1.find ( sr3 );
  106. BOOST_TEST ( pos != string_view::npos && pos <= static_cast<string_view::size_type>( p - arg ));
  107. p++;
  108. }
  109. sr1 = arg;
  110. p = arg;
  111. // for all possible chars, see if we find them in the right place.
  112. // Note that strchr will/might do the _wrong_ thing if we search for NULL
  113. for ( int ch = 1; ch < 256; ++ch ) {
  114. string_view::size_type pos = sr1.find(ch);
  115. const char *strp = std::strchr ( arg, ch );
  116. BOOST_TEST (( strp == NULL ) == ( pos == string_view::npos ));
  117. if ( strp != NULL )
  118. BOOST_TEST ( ptr_diff ( strp, arg ) == pos );
  119. }
  120. sr1 = arg;
  121. p = arg;
  122. // for all possible chars, see if we find them in the right place.
  123. // Note that strchr will/might do the _wrong_ thing if we search for NULL
  124. for ( int ch = 1; ch < 256; ++ch ) {
  125. string_view::size_type pos = sr1.rfind(ch);
  126. const char *strp = std::strrchr ( arg, ch );
  127. BOOST_TEST (( strp == NULL ) == ( pos == string_view::npos ));
  128. if ( strp != NULL )
  129. BOOST_TEST ( ptr_diff ( strp, arg ) == pos );
  130. }
  131. // Find everything at the start
  132. p = arg;
  133. sr1 = arg;
  134. while ( !sr1.empty ()) {
  135. string_view::size_type pos = sr1.find(*p);
  136. BOOST_TEST ( pos == 0 );
  137. sr1.remove_prefix (1);
  138. ++p;
  139. }
  140. // Find everything at the end
  141. sr1 = arg;
  142. p = arg + std::strlen ( arg ) - 1;
  143. while ( !sr1.empty ()) {
  144. string_view::size_type pos = sr1.rfind(*p);
  145. BOOST_TEST ( pos == sr1.size () - 1 );
  146. sr1.remove_suffix (1);
  147. --p;
  148. }
  149. // Find everything at the start
  150. sr1 = arg;
  151. p = arg;
  152. while ( !sr1.empty ()) {
  153. string_view::size_type pos = sr1.find_first_of(*p);
  154. BOOST_TEST ( pos == 0 );
  155. sr1.remove_prefix (1);
  156. ++p;
  157. }
  158. // Find everything at the end
  159. sr1 = arg;
  160. p = arg + std::strlen ( arg ) - 1;
  161. while ( !sr1.empty ()) {
  162. string_view::size_type pos = sr1.find_last_of(*p);
  163. BOOST_TEST ( pos == sr1.size () - 1 );
  164. sr1.remove_suffix (1);
  165. --p;
  166. }
  167. // Basic sanity checking for "find_first_of / find_first_not_of"
  168. sr1 = arg;
  169. sr2 = arg;
  170. while ( !sr1.empty() ) {
  171. BOOST_TEST ( sr1.find_first_of ( sr2 ) == 0 );
  172. BOOST_TEST ( sr1.find_first_not_of ( sr2 ) == string_view::npos );
  173. sr1.remove_prefix ( 1 );
  174. }
  175. p = arg;
  176. sr1 = arg;
  177. while ( *p ) {
  178. string_view::size_type pos1 = sr1.find_first_of(*p);
  179. string_view::size_type pos2 = sr1.find_first_not_of(*p);
  180. BOOST_TEST ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg ));
  181. if ( pos2 != string_view::npos ) {
  182. for ( size_t i = 0 ; i < pos2; ++i )
  183. BOOST_TEST ( sr1[i] == *p );
  184. BOOST_TEST ( sr1 [ pos2 ] != *p );
  185. }
  186. BOOST_TEST ( pos2 != pos1 );
  187. ++p;
  188. }
  189. // Basic sanity checking for "find_last_of / find_last_not_of"
  190. sr1 = arg;
  191. sr2 = arg;
  192. while ( !sr1.empty() ) {
  193. BOOST_TEST ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 ));
  194. BOOST_TEST ( sr1.find_last_not_of ( sr2 ) == string_view::npos );
  195. sr1.remove_suffix ( 1 );
  196. }
  197. p = arg;
  198. sr1 = arg;
  199. while ( *p ) {
  200. string_view::size_type pos1 = sr1.find_last_of(*p);
  201. string_view::size_type pos2 = sr1.find_last_not_of(*p);
  202. BOOST_TEST ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg ));
  203. BOOST_TEST ( pos2 == string_view::npos || pos1 < sr1.size ());
  204. if ( pos2 != string_view::npos ) {
  205. for ( size_t i = sr1.size () -1 ; i > pos2; --i )
  206. BOOST_TEST ( sr1[i] == *p );
  207. BOOST_TEST ( sr1 [ pos2 ] != *p );
  208. }
  209. BOOST_TEST ( pos2 != pos1 );
  210. ++p;
  211. }
  212. }
  213. template <typename T>
  214. class custom_allocator {
  215. public:
  216. typedef T value_type;
  217. typedef T* pointer;
  218. typedef const T* const_pointer;
  219. typedef void* void_pointer;
  220. typedef const void* const_void_pointer;
  221. typedef std::size_t size_type;
  222. typedef std::ptrdiff_t difference_type;
  223. typedef T& reference;
  224. typedef const T& const_reference;
  225. template<class U>
  226. struct rebind {
  227. typedef custom_allocator<U> other;
  228. };
  229. custom_allocator() BOOST_NOEXCEPT {}
  230. template <typename U>
  231. custom_allocator(custom_allocator<U> const&) BOOST_NOEXCEPT {}
  232. pointer allocate(size_type n) const {
  233. return static_cast<pointer>(std::malloc(sizeof(value_type) * n));
  234. }
  235. void deallocate(pointer p, size_type) const BOOST_NOEXCEPT {
  236. std::free(p);
  237. }
  238. pointer address(reference value) const BOOST_NOEXCEPT {
  239. return &value;
  240. }
  241. const_pointer address(const_reference value) const BOOST_NOEXCEPT {
  242. return &value;
  243. }
  244. BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
  245. return (~(size_type)0u) / sizeof(value_type);
  246. }
  247. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  248. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  249. template <class U, class... Args>
  250. void construct(U* ptr, Args&&... args) const {
  251. ::new((void*)ptr) U(static_cast<Args&&>(args)...);
  252. }
  253. #else
  254. template <class U, class V>
  255. void construct(U* ptr, V&& value) const {
  256. ::new((void*)ptr) U(static_cast<V&&>(value));
  257. }
  258. #endif
  259. #else
  260. template <class U, class V>
  261. void construct(U* ptr, const V& value) const {
  262. ::new((void*)ptr) U(value);
  263. }
  264. #endif
  265. template <class U>
  266. void construct(U* ptr) const {
  267. ::new((void*)ptr) U();
  268. }
  269. template <class U>
  270. void destroy(U* ptr) const {
  271. (void)ptr;
  272. ptr->~U();
  273. }
  274. };
  275. template <typename T, typename U>
  276. BOOST_CONSTEXPR bool operator==(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
  277. return true;
  278. }
  279. template <typename T, typename U>
  280. BOOST_CONSTEXPR bool operator!=(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
  281. return false;
  282. }
  283. void to_string ( const char *arg ) {
  284. string_view sr1;
  285. std::string str1;
  286. std::string str2;
  287. str1.assign ( arg );
  288. sr1 = arg;
  289. // str2 = sr1.to_string<std::allocator<char> > ();
  290. str2 = sr1.to_string ();
  291. BOOST_TEST ( str1 == str2 );
  292. std::basic_string<char, std::char_traits<char>, custom_allocator<char> > str3 = sr1.to_string(custom_allocator<char>());
  293. BOOST_TEST ( std::strcmp(str1.c_str(), str3.c_str()) == 0 );
  294. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  295. std::string str4 = static_cast<std::string> ( sr1 );
  296. BOOST_TEST ( str1 == str4 );
  297. #endif
  298. }
  299. void compare ( const char *arg ) {
  300. string_view sr1;
  301. std::string str1;
  302. std::string str2 = str1;
  303. str1.assign ( arg );
  304. sr1 = arg;
  305. BOOST_TEST ( sr1 == sr1); // compare string_view and string_view
  306. BOOST_TEST ( sr1 == str1); // compare string and string_view
  307. BOOST_TEST ( str1 == sr1 ); // compare string_view and string
  308. BOOST_TEST ( sr1 == arg ); // compare string_view and pointer
  309. BOOST_TEST ( arg == sr1 ); // compare pointer and string_view
  310. if ( sr1.size () > 0 ) {
  311. (*str1.rbegin())++;
  312. BOOST_TEST ( sr1 != str1 );
  313. BOOST_TEST ( str1 != sr1 );
  314. BOOST_TEST ( sr1 < str1 );
  315. BOOST_TEST ( sr1 <= str1 );
  316. BOOST_TEST ( str1 > sr1 );
  317. BOOST_TEST ( str1 >= sr1 );
  318. (*str1.rbegin()) -= 2;
  319. BOOST_TEST ( sr1 != str1 );
  320. BOOST_TEST ( str1 != sr1 );
  321. BOOST_TEST ( sr1 > str1 );
  322. BOOST_TEST ( sr1 >= str1 );
  323. BOOST_TEST ( str1 < sr1 );
  324. BOOST_TEST ( str1 <= sr1 );
  325. }
  326. }
  327. const char *test_strings [] = {
  328. "",
  329. "0",
  330. "abc",
  331. "AAA", // all the same
  332. "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
  333. "abc\0asdfadsfasf",
  334. NULL
  335. };
  336. int main()
  337. {
  338. const char **p = &test_strings[0];
  339. while ( *p != NULL ) {
  340. starts_with ( *p );
  341. ends_with ( *p );
  342. reverse ( *p );
  343. find ( *p );
  344. to_string ( *p );
  345. compare ( *p );
  346. p++;
  347. }
  348. return boost::report_errors();
  349. }