| [3f631d6] | 1 | #include <string.hfa>
|
|---|
| [6f7aff3] | 2 | #include <string_sharectx.hfa>
|
|---|
| [3f631d6] | 3 | #include <fstream.hfa>
|
|---|
| [01e6bd9] | 4 | #include <ctype.h> // isxdigit, ispunct, isupper
|
|---|
| [f450f2f] | 5 |
|
|---|
| 6 | // Purpose: call each function in string.hfa, top to bottom
|
|---|
| 7 |
|
|---|
| 8 | int main () {
|
|---|
| [6f7aff3] | 9 |
|
|---|
| 10 | #ifdef STRING_SHARING_OFF
|
|---|
| 11 | string_sharectx c = { NO_SHARING };
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| [f450f2f] | 14 | string s = "hello";
|
|---|
| 15 | string s2 = "hello";
|
|---|
| 16 | string s3 = "world";
|
|---|
| 17 | string frag = "ell";
|
|---|
| 18 |
|
|---|
| 19 | // IO operator, x2
|
|---|
| [2b30370] | 20 | sout | s | s | s; // hello hello hello
|
|---|
| 21 |
|
|---|
| 22 | // empty ctor then assign
|
|---|
| 23 | string sxx;
|
|---|
| 24 | sout | sxx; // (blank line)
|
|---|
| 25 | sxx = s;
|
|---|
| 26 | sout | sxx; // hello
|
|---|
| [f450f2f] | 27 |
|
|---|
| 28 | // Comparisons
|
|---|
| 29 | // all print "true false"
|
|---|
| 30 | sout | (s == s2) | (s == s3);
|
|---|
| 31 | sout | (s != s3) | (s != s2);
|
|---|
| 32 | sout | (s == "hello") | (s == "world");
|
|---|
| 33 | sout | (s != "world") | (s != "hello");
|
|---|
| [e8b3717] | 34 | sout | ( frag == s(1,3) ) | ( s3 == s(1,3) );
|
|---|
| 35 | sout | ( s3 != s(1,3) ) | ( frag != s(1,3) );
|
|---|
| 36 | sout | ( s2(1,3) == s(1,3) ) | ( s3(1,3) == s(1,3) );
|
|---|
| 37 | sout | ( s3(1,3) != s(1,3) ) | ( s2(1,3) != s(1,3) );
|
|---|
| 38 | sout | ( s(1,3) == frag ) | ( s(1,3) == s3 );
|
|---|
| 39 | sout | ( s(1,3) != s3 ) | ( s(1,3) != frag );
|
|---|
| 40 | sout | ( s(1,3) == "ell" ) | ( s(1,3) == "world" );
|
|---|
| 41 | sout | ( s(1,3) != "world" ) | ( s(1,3) != "ell" );
|
|---|
| [f450f2f] | 42 |
|
|---|
| 43 |
|
|---|
| 44 | //
|
|---|
| 45 | // breadth Constructors
|
|---|
| 46 | //
|
|---|
| 47 | {
|
|---|
| [7abc3de] | 48 | string b1 = "1234567";
|
|---|
| 49 | sout | b1; // 1234567
|
|---|
| 50 |
|
|---|
| 51 | string b1x = { "1234567", 3 };
|
|---|
| 52 | sout | b1x; // 123
|
|---|
| [f450f2f] | 53 |
|
|---|
| 54 | string b2 = s;
|
|---|
| 55 | sout | b2; // hello
|
|---|
| 56 |
|
|---|
| [7abc3de] | 57 | string b2x = { s, 4 };
|
|---|
| 58 | sout | b2x; // hell
|
|---|
| 59 |
|
|---|
| [f450f2f] | 60 | // todo: a plain string &
|
|---|
| 61 | const string & s_ref = s;
|
|---|
| 62 | string b3 = s_ref;
|
|---|
| 63 | sout | b3; // hello
|
|---|
| 64 |
|
|---|
| 65 | & s_ref = & s3;
|
|---|
| 66 | b3 = s_ref;
|
|---|
| 67 | sout | b3; // world
|
|---|
| 68 |
|
|---|
| 69 | const string & s_constref = s;
|
|---|
| 70 | string b4 = s_constref;
|
|---|
| 71 | sout | b4; // hello
|
|---|
| 72 |
|
|---|
| 73 | & s_constref = & s3;
|
|---|
| 74 | b4 = s_constref;
|
|---|
| 75 | sout | b4; // world
|
|---|
| [7abc3de] | 76 |
|
|---|
| 77 | string b5 = 'Q';
|
|---|
| 78 | sout | b5; // Q
|
|---|
| 79 |
|
|---|
| [f2898df] | 80 | string b6 = 42;
|
|---|
| 81 | sout | b6; // 42
|
|---|
| [7abc3de] | 82 |
|
|---|
| [f2898df] | 83 | string b7 = -42;
|
|---|
| 84 | sout | b7; // -42
|
|---|
| 85 |
|
|---|
| 86 | string b8 = 5.5;
|
|---|
| 87 | sout | b8; // 5.5
|
|---|
| 88 |
|
|---|
| 89 | string b9 = 5.5L;
|
|---|
| 90 | sout | b9; // 5.5
|
|---|
| 91 |
|
|---|
| 92 | string b10 = 5.5+3.4i;
|
|---|
| 93 | sout | b10; // 5.5+3.4i
|
|---|
| 94 |
|
|---|
| 95 | string b11 = 5.5L+3.4Li;
|
|---|
| 96 | sout | b11; // 5.5+3.4i
|
|---|
| [f450f2f] | 97 | }
|
|---|
| [b12b1ba] | 98 |
|
|---|
| [e891349] | 99 | //
|
|---|
| 100 | // Assignments
|
|---|
| 101 | //
|
|---|
| 102 | {
|
|---|
| 103 | string b = "xxx";
|
|---|
| 104 |
|
|---|
| 105 | b = "1234567";
|
|---|
| 106 | sout | b; // 1234567
|
|---|
| 107 |
|
|---|
| 108 | b = "xxx";
|
|---|
| 109 | b = s;
|
|---|
| 110 | sout | b; // hello
|
|---|
| 111 |
|
|---|
| 112 | b = "xxx";
|
|---|
| 113 | b = 'Q';
|
|---|
| 114 | sout | b; // Q
|
|---|
| 115 |
|
|---|
| 116 | b = "xxx";
|
|---|
| 117 | assign( b, "1234567", 3 );
|
|---|
| 118 | sout | b; // 123
|
|---|
| 119 |
|
|---|
| 120 | b = "xxx";
|
|---|
| 121 | assign( b, s, 4 );
|
|---|
| 122 | sout | b; // hell
|
|---|
| 123 |
|
|---|
| 124 | b = "xxx";
|
|---|
| 125 | strcpy(b, "1234567");
|
|---|
| 126 | sout | b; // 1234567
|
|---|
| 127 |
|
|---|
| 128 | b = "xxx";
|
|---|
| 129 | strcpy(b, s);
|
|---|
| 130 | sout | b; // hello
|
|---|
| 131 |
|
|---|
| 132 | b = "xxx";
|
|---|
| 133 | strncpy( b, "1234567", 3 );
|
|---|
| 134 | sout | b; // 123
|
|---|
| 135 |
|
|---|
| 136 | b = "xxx";
|
|---|
| 137 | strncpy( b, s, 4 );
|
|---|
| 138 | sout | b; // hell
|
|---|
| [f2898df] | 139 |
|
|---|
| 140 | b = 42;
|
|---|
| 141 | sout | b; // 42
|
|---|
| 142 |
|
|---|
| 143 | b = -42;
|
|---|
| 144 | sout | b; // -42
|
|---|
| 145 |
|
|---|
| 146 | b = 5.5;
|
|---|
| 147 | sout | b; // 5.5
|
|---|
| 148 |
|
|---|
| 149 | b = 5.5L;
|
|---|
| 150 | sout | b; // 5.5
|
|---|
| 151 |
|
|---|
| 152 | b = 5.5+3.4i;
|
|---|
| 153 | sout | b; // 5.5+3.4i
|
|---|
| 154 |
|
|---|
| 155 | b = 5.5L+3.4Li;
|
|---|
| 156 | sout | b; // 5.5+3.4i
|
|---|
| [e891349] | 157 | }
|
|---|
| [b12b1ba] | 158 |
|
|---|
| [e891349] | 159 |
|
|---|
| 160 |
|
|---|
| [f450f2f] | 161 |
|
|---|
| [9aa8dcc] | 162 | sout | len(s); // 5
|
|---|
| [f450f2f] | 163 |
|
|---|
| 164 | //
|
|---|
| 165 | // concatenation/append
|
|---|
| 166 | //
|
|---|
| 167 |
|
|---|
| 168 | string sx = s + s3;
|
|---|
| 169 | sout | sx; // helloworld
|
|---|
| 170 | sx = "xx";
|
|---|
| 171 | sx = s + s3;
|
|---|
| 172 | sout | sx; // helloworld
|
|---|
| 173 |
|
|---|
| 174 | sx += '!';
|
|---|
| 175 | sout | sx; // helloworld!
|
|---|
| 176 | sx = s + '!';
|
|---|
| 177 | sout | sx; // hello!
|
|---|
| 178 |
|
|---|
| 179 | sx = s;
|
|---|
| 180 | sx += s;
|
|---|
| 181 | sout | sx; // hellohello
|
|---|
| 182 | sx += ", friend";
|
|---|
| 183 | sout | sx; // hellohello, friend
|
|---|
| 184 |
|
|---|
| 185 | sx = s + ", friend";
|
|---|
| 186 | sout | sx; // hello, friend
|
|---|
| 187 |
|
|---|
| 188 | sx = "bye, " + "friend";
|
|---|
| 189 | sout | sx; // bye, friend
|
|---|
| 190 |
|
|---|
| [e891349] | 191 | sx = "o";
|
|---|
| 192 | strcat( sx, s );
|
|---|
| 193 | sout | sx; // ohello
|
|---|
| 194 |
|
|---|
| 195 | sx = "o";
|
|---|
| 196 | append( sx, s, 4 );
|
|---|
| 197 | sout | sx; // ohell
|
|---|
| 198 |
|
|---|
| 199 | sx = "o";
|
|---|
| 200 | strncat( sx, s, 4 );
|
|---|
| 201 | sout | sx; // ohell
|
|---|
| 202 |
|
|---|
| 203 | sx = "o";
|
|---|
| 204 | strcat( sx, "mydarling" );
|
|---|
| 205 | sout | sx; // omydarling
|
|---|
| 206 |
|
|---|
| 207 | sx = "o";
|
|---|
| 208 | append( sx, "mydarling", 2 );
|
|---|
| 209 | sout | sx; // omy
|
|---|
| 210 |
|
|---|
| 211 | sx = "o";
|
|---|
| 212 | strncat( sx, "mydarling", 2 );
|
|---|
| 213 | sout | sx; // omy
|
|---|
| 214 |
|
|---|
| [f450f2f] | 215 | //
|
|---|
| 216 | // repetition
|
|---|
| 217 | //
|
|---|
| [38951c31] | 218 |
|
|---|
| 219 | sx = s;
|
|---|
| 220 | sx *= 4;
|
|---|
| 221 | sout | sx; // hellohellohellohello
|
|---|
| 222 |
|
|---|
| [f450f2f] | 223 | sx = s * 3;
|
|---|
| 224 | sout | sx; // hellohellohello
|
|---|
| 225 |
|
|---|
| 226 | sx = 'Q' * (size_t)3;
|
|---|
| 227 | sout | sx; // QQQ
|
|---|
| 228 |
|
|---|
| 229 | sx = "asdf" * 3;
|
|---|
| 230 | sout | sx; // asdfasdfasdf
|
|---|
| 231 |
|
|---|
| 232 | //
|
|---|
| 233 | // slicing
|
|---|
| 234 | //
|
|---|
| 235 |
|
|---|
| [e8b3717] | 236 | // Range cases treated thoroughly in "string-overwrite" test.
|
|---|
| 237 | // Composability with comparison and search are demoed above and below.
|
|---|
| 238 | // Coverage here adds the single-argument ("rest of string") overload.
|
|---|
| 239 |
|
|---|
| 240 | sx = s;
|
|---|
| 241 | sout | sx(3); // lo
|
|---|
| 242 | sx(3) = "iocentric";
|
|---|
| 243 | sout | s | sx; // hello heliocentric
|
|---|
| [f450f2f] | 244 |
|
|---|
| 245 | //
|
|---|
| 246 | // character access
|
|---|
| 247 | //
|
|---|
| 248 |
|
|---|
| 249 | char c = s[1];
|
|---|
| 250 | sout | c; // e
|
|---|
| 251 |
|
|---|
| 252 | s[3] = "p!!!";
|
|---|
| 253 | sout | s; // help!!!o
|
|---|
| 254 |
|
|---|
| 255 | s[7] = '!';
|
|---|
| 256 | sout | s; // help!!!!
|
|---|
| 257 |
|
|---|
| 258 | s[7] = "";
|
|---|
| 259 | sout | s; // help!!!
|
|---|
| 260 |
|
|---|
| 261 | sout | s[3]; // p
|
|---|
| 262 |
|
|---|
| 263 | //
|
|---|
| 264 | // search
|
|---|
| 265 | //
|
|---|
| 266 |
|
|---|
| 267 | s += '?'; // already tested
|
|---|
| 268 | sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
|
|---|
| 269 |
|
|---|
| 270 | sout
|
|---|
| 271 | | find( s, 'h' ) // 0
|
|---|
| 272 | | find( s, '!' ) // 4
|
|---|
| 273 | | find( s, '?' ) // 7
|
|---|
| 274 | | find( s, 'o' ); // 8, not found
|
|---|
| 275 |
|
|---|
| 276 | string alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|---|
| 277 |
|
|---|
| 278 | sout
|
|---|
| 279 | | find( alphabet, "" ) // 0
|
|---|
| 280 | | find( alphabet, "a" ) // 0
|
|---|
| 281 | | find( alphabet, "z" ) // 25
|
|---|
| 282 | | find( alphabet, "abc" ) // 0
|
|---|
| 283 | | find( alphabet, "abq" ) // 26, not found
|
|---|
| 284 | | find( alphabet, "def"); // 3
|
|---|
| 285 |
|
|---|
| 286 | sout
|
|---|
| 287 | | includes( alphabet, "" ) // true
|
|---|
| 288 | | includes( alphabet, "a" ) // true
|
|---|
| 289 | | includes( alphabet, "z" ) // true
|
|---|
| 290 | | includes( alphabet, "abc" ) // true
|
|---|
| 291 | | includes( alphabet, "abq" ) // false
|
|---|
| 292 | | includes( alphabet, "def"); // true
|
|---|
| 293 |
|
|---|
| 294 | {
|
|---|
| 295 | char *empty_c = "";
|
|---|
| 296 | char *a_c = "a";
|
|---|
| 297 | char *z_c = "z";
|
|---|
| 298 | char *dex_c = "dex";
|
|---|
| 299 |
|
|---|
| 300 | sout
|
|---|
| 301 | | find( alphabet, empty_c ) // 0
|
|---|
| 302 | | find( alphabet, a_c ) // 0
|
|---|
| 303 | | find( alphabet, dex_c ) // 26, not found
|
|---|
| 304 | | find( alphabet, dex_c, 2 ); // 3
|
|---|
| 305 |
|
|---|
| 306 | sout
|
|---|
| 307 | | includes( alphabet, empty_c ) // true
|
|---|
| 308 | | includes( alphabet, a_c ) // true
|
|---|
| 309 | | includes( alphabet, dex_c ) // false
|
|---|
| 310 | | includes( alphabet, dex_c, 2 ); // true
|
|---|
| 311 |
|
|---|
| 312 | sout
|
|---|
| 313 | | startsWith( alphabet, a_c) // true
|
|---|
| 314 | | endsWith ( alphabet, a_c) // false
|
|---|
| 315 | | startsWith( alphabet, z_c) // false
|
|---|
| 316 | | endsWith ( alphabet, z_c); // true
|
|---|
| 317 |
|
|---|
| 318 | string empty = empty_c;
|
|---|
| 319 | string a = a_c;
|
|---|
| 320 | string z = z_c;
|
|---|
| 321 | string dex = dex_c;
|
|---|
| 322 |
|
|---|
| 323 | sout
|
|---|
| 324 | | find( alphabet, empty ) // 0
|
|---|
| 325 | | find( alphabet, a ) // 0
|
|---|
| 326 | | find( alphabet, dex ) // 26, not found
|
|---|
| 327 | | find( alphabet, dex(0,2) ); // 3
|
|---|
| 328 |
|
|---|
| 329 | sout
|
|---|
| 330 | | includes( alphabet, empty ) // true
|
|---|
| 331 | | includes( alphabet, a ) // true
|
|---|
| 332 | | includes( alphabet, dex ) // false
|
|---|
| 333 | | includes( alphabet, dex(0,2) ); // true
|
|---|
| 334 |
|
|---|
| 335 | sout
|
|---|
| 336 | | startsWith( alphabet, a) // true
|
|---|
| 337 | | endsWith ( alphabet, a) // false
|
|---|
| 338 | | startsWith( alphabet, z) // false
|
|---|
| 339 | | endsWith ( alphabet, z); // true
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | sout
|
|---|
| 343 | | find( alphabet , "def") // 3
|
|---|
| 344 | | find( alphabet( 0, 26), "def") // 3
|
|---|
| [e8b3717] | 345 | | find( alphabet( 2, 24), "def") // 1
|
|---|
| 346 | | find( alphabet( 3, 23), "def") // 0
|
|---|
| 347 | | find( alphabet( 4, 22), "def") // 22, not found
|
|---|
| 348 | | find( alphabet( 4, 22), "ef") // 0
|
|---|
| [f450f2f] | 349 | | find( alphabet( 0, 6), "def") // 3
|
|---|
| 350 | | find( alphabet( 0, 5), "def") // 5, not found
|
|---|
| 351 | | find( alphabet( 0, 5), "de" ); // 3
|
|---|
| 352 |
|
|---|
| 353 | sout
|
|---|
| 354 | | includes( alphabet , "def") // true
|
|---|
| 355 | | includes( alphabet( 0, 26), "def") // true
|
|---|
| [e8b3717] | 356 | | includes( alphabet( 2, 24), "def") // true
|
|---|
| 357 | | includes( alphabet( 3, 23), "def") // true
|
|---|
| 358 | | includes( alphabet( 4, 22), "def") // false
|
|---|
| 359 | | includes( alphabet( 4, 22), "ef") // true
|
|---|
| [f450f2f] | 360 | | includes( alphabet( 0, 6), "def") // true
|
|---|
| 361 | | includes( alphabet( 0, 5), "def") // false
|
|---|
| 362 | | includes( alphabet( 0, 5), "de" ); // true
|
|---|
| 363 |
|
|---|
| 364 | sout
|
|---|
| 365 | | startsWith( alphabet , "abc") // true
|
|---|
| 366 | | startsWith( alphabet( 0, 26), "abc") // true
|
|---|
| [e8b3717] | 367 | | startsWith( alphabet( 1, 25), "abc") // false
|
|---|
| 368 | | startsWith( alphabet( 1, 25), "bc") // true
|
|---|
| [f450f2f] | 369 | | startsWith( alphabet( 0, 26), "abc") // true
|
|---|
| 370 | | startsWith( alphabet( 0, 4), "abc") // true
|
|---|
| 371 | | startsWith( alphabet( 0, 3), "abc") // true
|
|---|
| 372 | | startsWith( alphabet( 0, 3), "ab" ) // true
|
|---|
| 373 | | startsWith( alphabet , "xyz"); // false
|
|---|
| 374 |
|
|---|
| 375 | sout
|
|---|
| 376 | | endsWith( alphabet , "xyz") // true
|
|---|
| 377 | | endsWith( alphabet , "xyzz") // false
|
|---|
| 378 | | endsWith( alphabet( 0, 26), "xyz") // true
|
|---|
| 379 | | endsWith( alphabet( 0, 25), "xyz") // false
|
|---|
| 380 | | endsWith( alphabet( 0, 25), "xy" ) // true
|
|---|
| 381 | | endsWith( alphabet( 0, 26), "xyz") // true
|
|---|
| [e8b3717] | 382 | | endsWith( alphabet(23, 3), "xyz") // true
|
|---|
| 383 | | endsWith( alphabet(24, 2), "xyz") // false
|
|---|
| 384 | | endsWith( alphabet(24, 2), "yz") // true
|
|---|
| [f450f2f] | 385 | | endsWith( alphabet , "abc"); // false
|
|---|
| 386 |
|
|---|
| 387 | charclass cc_cba = {"cba"};
|
|---|
| 388 | charclass cc_onml = {"onml"};
|
|---|
| 389 | charclass cc_alphabet = {alphabet};
|
|---|
| 390 |
|
|---|
| 391 | // include (rest of the) numbers: tell me where the numbers stop
|
|---|
| 392 | // exclude (until) numbers: tell me where the numbers start (include rest of the non-numbers)
|
|---|
| 393 |
|
|---|
| 394 | sout
|
|---|
| [7e4f226] | 395 | | (return size_t)include( alphabet, cc_cba ) // 3
|
|---|
| 396 | | (return size_t)exclude( alphabet, cc_cba ) // 0
|
|---|
| 397 | | (return size_t)include( alphabet, cc_onml ) // 0
|
|---|
| 398 | | (return size_t)exclude( alphabet, cc_onml ) // 11
|
|---|
| 399 | | (return size_t)include( alphabet, cc_alphabet ) // 26
|
|---|
| 400 | | (return size_t)exclude( alphabet, cc_alphabet ); // 0
|
|---|
| [f450f2f] | 401 |
|
|---|
| [7e4f226] | 402 | sout
|
|---|
| 403 | | (return string)include( alphabet, cc_cba ) // "abc"
|
|---|
| 404 | | (return string)exclude( alphabet, cc_cba ) // ""
|
|---|
| 405 | | (return string)include( alphabet, cc_onml ) // ""
|
|---|
| 406 | | (return string)exclude( alphabet, cc_onml ) // "abcdefghijk"
|
|---|
| 407 | | (return string)include( alphabet, cc_alphabet ) // "abcdefghijklmnopqrstuvwxyz"
|
|---|
| 408 | | (return string)exclude( alphabet, cc_alphabet ); // ""
|
|---|
| [01e6bd9] | 409 |
|
|---|
| 410 | sout
|
|---|
| 411 | | (return size_t)include( "1FeC34aB", isxdigit )
|
|---|
| 412 | | (return size_t)include( ".,;’!\"", ispunct )
|
|---|
| 413 | | (return size_t)include( "XXXx", isupper );
|
|---|
| 414 |
|
|---|
| 415 | sout
|
|---|
| 416 | | (return string)include( "1FeC34aB", isxdigit )
|
|---|
| 417 | | (return string)include( ".,;’!\"", ispunct )
|
|---|
| 418 | | (return string)include( "XXXx", isupper );
|
|---|
| 419 |
|
|---|
| 420 | sout
|
|---|
| 421 | | (return size_t)exclude( "1FeC34aB", isdigit )
|
|---|
| 422 | | (return size_t)exclude( ".,;’!\"", ispunct )
|
|---|
| 423 | | (return size_t)exclude( "XXXx", islower );
|
|---|
| 424 |
|
|---|
| 425 | sout
|
|---|
| 426 | | (return string)exclude( "1FeC34aB", isalpha )
|
|---|
| 427 | | (return string)exclude( ".,;’!\"", ispunct )
|
|---|
| 428 | | (return string)exclude( "XXXx", islower );
|
|---|
| [7e4f226] | 429 | }
|
|---|