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