| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |    Copyright (c) Marshall Clow 2012-2012. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Distributed under the Boost Software License, Version 1.0. (See accompanying | 
					
						
							|  |  |  |    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For more information, see http://www.boost.org
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/utility/string_ref.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | #include <boost/core/lightweight_test.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef boost::string_ref string_ref; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  Should be equal
 | 
					
						
							|  |  |  | void interop ( const std::string &str, string_ref ref ) { | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | //  BOOST_TEST ( str == ref );
 | 
					
						
							|  |  |  |     BOOST_TEST ( str.size () == ref.size ()); | 
					
						
							|  |  |  |     BOOST_TEST ( std::equal ( str.begin (),  str.end (),  ref.begin ())); | 
					
						
							|  |  |  |     BOOST_TEST ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ())); | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void null_tests ( const char *p ) { | 
					
						
							|  |  |  | //  All zero-length string-refs should be equal
 | 
					
						
							|  |  |  |     string_ref sr1; // NULL, 0
 | 
					
						
							|  |  |  |     string_ref sr2 ( NULL, 0 ); | 
					
						
							|  |  |  |     string_ref sr3 ( p, 0 ); | 
					
						
							|  |  |  |     string_ref sr4 ( p ); | 
					
						
							|  |  |  |     sr4.clear (); | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |     BOOST_TEST ( sr1 == sr2 ); | 
					
						
							|  |  |  |     BOOST_TEST ( sr1 == sr3 ); | 
					
						
							|  |  |  |     BOOST_TEST ( sr2 == sr3 ); | 
					
						
							|  |  |  |     BOOST_TEST ( sr1 == sr4 ); | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  make sure that substrings work just like strings
 | 
					
						
							|  |  |  | void test_substr ( const std::string &str ) { | 
					
						
							|  |  |  |     const size_t sz = str.size (); | 
					
						
							|  |  |  |     string_ref ref ( str ); | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  | //  Substrings at the end
 | 
					
						
							|  |  |  |     for ( size_t i = 0; i <= sz; ++ i ) | 
					
						
							|  |  |  |         interop ( str.substr ( i ), ref.substr ( i )); | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  | //  Substrings at the beginning
 | 
					
						
							|  |  |  |     for ( size_t i = 0; i <= sz; ++ i ) | 
					
						
							|  |  |  |         interop ( str.substr ( 0, i ), ref.substr ( 0, i )); | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  | //  All possible substrings
 | 
					
						
							|  |  |  |     for ( size_t i = 0; i < sz; ++i ) | 
					
						
							|  |  |  |         for ( size_t j = i; j < sz; ++j ) | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  |             interop ( str.substr ( i, j ), ref.substr ( i, j )); | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  make sure that removing prefixes and suffixes work just like strings
 | 
					
						
							|  |  |  | void test_remove ( const std::string &str ) { | 
					
						
							|  |  |  |     const size_t sz = str.size (); | 
					
						
							|  |  |  |     std::string work; | 
					
						
							|  |  |  |     string_ref ref; | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     for ( size_t i = 1; i <= sz; ++i ) { | 
					
						
							|  |  |  |       work = str; | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  |       ref  = str; | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |       while ( ref.size () >= i ) { | 
					
						
							|  |  |  |           interop ( work, ref ); | 
					
						
							|  |  |  |           work.erase ( 0, i ); | 
					
						
							|  |  |  |           ref.remove_prefix (i); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     for ( size_t i = 1; i < sz; ++ i ) { | 
					
						
							|  |  |  |       work = str; | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  |       ref  = str; | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |       while ( ref.size () >= i ) { | 
					
						
							|  |  |  |           interop ( work, ref ); | 
					
						
							|  |  |  |           work.erase ( work.size () - i, i ); | 
					
						
							|  |  |  |           ref.remove_suffix (i); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *test_strings [] = { | 
					
						
							|  |  |  |     "", | 
					
						
							|  |  |  |     "1", | 
					
						
							|  |  |  |     "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | 
					
						
							|  |  |  |     "0123456789", | 
					
						
							|  |  |  |     NULL | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | int main() | 
					
						
							| 
									
										
										
										
											2013-02-11 21:49:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |     const char **p = &test_strings[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ( *p != NULL ) { | 
					
						
							|  |  |  |         interop ( *p, *p ); | 
					
						
							|  |  |  |         test_substr ( *p ); | 
					
						
							|  |  |  |         test_remove ( *p ); | 
					
						
							|  |  |  |         null_tests ( *p ); | 
					
						
							| 
									
										
										
										
											2013-02-25 06:30:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 16:38:07 +00:00
										 |  |  |         p++; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return boost::report_errors(); | 
					
						
							| 
									
										
										
										
											2013-02-11 21:49:56 +00:00
										 |  |  | } |