Changeset 4223317
- Timestamp:
- Apr 1, 2025, 9:19:26 AM (6 months ago)
- Branches:
- master
- Children:
- f8913b7c
- Parents:
- 3181a33
- Location:
- libcfa/src/collections
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/collections/string.cfa
r3181a33 r4223317 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 14 15:41:33202513 // Update Count : 2 7512 // Last Modified On : Tue Apr 1 09:17:34 2025 13 // Update Count : 288 14 14 // 15 15 … … 31 31 // private (not in header) 32 32 static void ?{}( string & s, string_res & src, size_t start, size_t len ) { 33 34 33 (s.inner) { malloc() }; 34 ?{}( *s.inner, src, SHARE_EDITS, start, len ); 35 35 } 36 36 37 37 void ?{}( string & s ) { 38 39 38 (s.inner) { malloc() }; 39 ?{}( *s.inner ); 40 40 } 41 41 42 42 void ?{}( string & s, const string & c ) { 43 44 43 (s.inner) { malloc() }; 44 ?{}( *s.inner, *c.inner, COPY_VALUE ); 45 45 } 46 46 47 47 void ?{}( string & s, const string & s2, size_t maxlen ) { 48 49 48 (s.inner) { malloc() }; 49 ?{}( *s.inner, *s2.inner, COPY_VALUE, maxlen ); 50 50 } 51 51 52 52 53 53 void ?{}( string & s, string & c ) { 54 54 ?{}( s, (const string &) c ); 55 55 } 56 56 57 57 void ?{}( string & s, const char c ) { 58 59 58 (s.inner) { malloc() }; 59 ?{}( *s.inner, c ); 60 60 } 61 61 62 62 void ?{}( string & s, const char * c ) { 63 64 63 (s.inner) { malloc() }; 64 ?{}( *s.inner, c ); 65 65 } 66 66 67 67 void ?{}( string & s, const char * c, size_t size ) { 68 69 68 (s.inner) { malloc() }; 69 ?{}( *s.inner, c, size ); 70 70 } 71 71 72 72 void ?{}( string & s, ssize_t rhs ) { 73 74 73 (s.inner) { malloc() }; 74 ?{}( *s.inner, rhs ); 75 75 } 76 76 77 77 void ?{}( string & s, size_t rhs ) { 78 79 78 (s.inner) { malloc() }; 79 ?{}( *s.inner, rhs ); 80 80 } 81 81 82 82 void ?{}( string & s, double rhs ) { 83 84 83 (s.inner) { malloc() }; 84 ?{}( *s.inner, rhs ); 85 85 } 86 86 87 87 void ?{}( string & s, long double rhs ) { 88 89 88 (s.inner) { malloc() }; 89 ?{}( *s.inner, rhs ); 90 90 } 91 91 92 92 void ?{}( string & s, double _Complex rhs ) { 93 94 93 (s.inner) { malloc() }; 94 ?{}( *s.inner, rhs ); 95 95 } 96 96 97 97 void ?{}( string & s, long double _Complex rhs ) { 98 99 98 (s.inner) { malloc() }; 99 ?{}( *s.inner, rhs ); 100 100 } 101 101 … … 131 131 132 132 void ^?{}( string & s ) { 133 134 135 133 ^(*s.inner){}; 134 free( s.inner ); 135 s.inner = 0p; 136 136 } 137 137 … … 140 140 141 141 string_Share ?`share( string & s ) { 142 143 142 string_Share ret = { &s }; 143 return ret; 144 144 } 145 145 146 146 void ?{}( string & s, string_Share src ) { 147 147 ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth ); 148 148 } 149 149 … … 152 152 153 153 string & ?=?( string & s, const string & c ) { 154 155 154 (*s.inner) = (*c.inner); 155 return s; 156 156 } 157 157 158 158 string & ?=?( string & s, string & c ) { 159 160 159 (*s.inner) = (*c.inner); 160 return s; 161 161 } 162 162 163 163 string & ?=?( string & s, const char * val ) { 164 165 164 (*s.inner) = val; 165 return s; 166 166 } 167 167 168 168 string & ?=?( string & s, char val ) { 169 170 169 (*s.inner) = val; 170 return s; 171 171 } 172 172 173 173 string & assign( string & s, const string & c, size_t n ) { 174 175 174 assign( *s.inner, *c.inner, n ); 175 return s; 176 176 } 177 177 178 178 string & assign( string & s, const char * c, size_t n ) { 179 180 179 assign( *s.inner, c, n ); 180 return s; 181 181 } 182 182 183 183 string & ?=?( string & s, ssize_t rhs ) { 184 185 184 (*s.inner) = rhs; 185 return s; 186 186 } 187 187 188 188 string & ?=?( string & s, size_t rhs ) { 189 190 189 (*s.inner) = rhs; 190 return s; 191 191 } 192 192 193 193 string & ?=?( string & s, double rhs ) { 194 195 194 (*s.inner) = rhs; 195 return s; 196 196 } 197 197 198 198 string & ?=?( string & s, long double rhs ) { 199 200 199 (*s.inner) = rhs; 200 return s; 201 201 } 202 202 203 203 string & ?=?( string & s, double _Complex rhs ) { 204 205 204 (*s.inner) = rhs; 205 return s; 206 206 } 207 207 208 208 string & ?=?( string & s, long double _Complex rhs ) { 209 210 209 (*s.inner) = rhs; 210 return s; 211 211 } 212 212 … … 215 215 216 216 ofstream & ?|?( ofstream & out, const string & s ) { 217 217 return out | (*s.inner); // print internal string_res 218 218 } 219 219 220 220 void ?|?( ofstream & out, const string & s ) { 221 221 (ofstream &)(out | (*s.inner)); ends( out ); 222 222 } 223 223 … … 236 236 237 237 ifstream & ?|?( ifstream & in, string & s ) { 238 238 return in | (*s.inner); // read to internal string_res 239 239 } 240 240 241 241 ifstream & ?|?( ifstream & is, _Istream_Squoted f ) { 242 242 _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } }; 243 243 return is | f2; 244 244 } // ?|? 245 245 … … 247 247 // _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr}; 248 248 _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f}; 249 249 return is | f2; 250 250 } // ?|? 251 251 … … 253 253 // Slicing 254 254 255 string ?()( string & s, size_t start, size_t len ) { 256 string ret = { *s.inner, start, len }; 257 return ret`share; 258 } 259 260 string ?()( string & s, size_t start ) { 261 string ret = { *s.inner, start, size( s ) - start }; 262 return ret`share; 255 string ?()( string & s, ssize_t start, ssize_t len ) { 256 if ( start < 0 ) { start += s`len; } 257 if ( len < 0 ) { len = -len; start -= len; } 258 if ( start > s`len ) return (string){ "" }; 259 if ( start + len > s`len ) len = s`len - start; 260 string ret = { *s.inner, start, len }; 261 return ret`share; 262 } 263 264 string ?()( string & s, ssize_t start ) { 265 if ( start < 0 ) { start += s`len; } 266 string ret = { *s.inner, start, size( s ) - start }; 267 return ret`share; 263 268 } 264 269 … … 295 300 296 301 size_t size( const string & s ) { 297 302 return size( *s.inner ); 298 303 } 299 304 … … 302 307 303 308 void ?+=?( string & s, char c ) { 304 309 (*s.inner) += c; 305 310 } 306 311 307 312 void ?+=?( string & s, const string & s2 ) { 308 313 (*s.inner) += (*s2.inner); 309 314 } 310 315 311 316 void append( string & s, const string & s2, size_t maxlen ) { 312 317 append( (*s.inner), (*s2.inner), maxlen ); 313 318 } 314 319 315 320 void ?+=?( string & s, const char * c ) { 316 321 (*s.inner) += c; 317 322 } 318 323 319 324 void append( string & s, const char * buffer, size_t bsize ) { 320 325 append( (*s.inner), buffer, bsize ); 321 326 } 322 327 323 328 string ?+?( const string & s, char c ) { 324 325 326 329 string ret = s; 330 ret += c; 331 return ret; 327 332 } 328 333 329 334 string ?+?( char c, const string & s ) { 330 331 332 335 string ret = s; 336 ret += c; 337 return ret; 333 338 } 334 339 335 340 string ?+?( const string & s, const string & s2 ) { 336 337 338 341 string ret = s; 342 ret += s2; 343 return ret; 339 344 } 340 345 341 346 string ?+?( const char * s, char c ) { 342 343 344 347 string ret = s; 348 ret += c; 349 return ret; 345 350 } 346 351 347 352 string ?+?( char c, const char * s ) { 348 349 350 353 string ret = c; 354 ret += s; 355 return ret; 351 356 } 352 357 353 358 string ?+?( const char * s1, const char * s2 ) { 354 355 356 359 string ret = s1; 360 ret += s2; 361 return ret; 357 362 } 358 363 359 364 string ?+?( const char * s1, string & s2 ) { 360 361 362 365 string ret = s1; 366 ret += s2; 367 return ret; 363 368 } 364 369 365 370 string ?+?( const string & s, const char * c ) { 366 367 368 371 string ret = s; 372 ret += c; 373 return ret; 369 374 } 370 375 … … 373 378 374 379 void ?*=?( string & s, size_t factor ) { 375 380 (*s.inner) *= factor; 376 381 } 377 382 378 383 string ?*?( const string & s, size_t factor ) { 379 380 381 384 string ret = s; 385 ret *= factor; 386 return ret; 382 387 } 383 388 384 389 string ?*?( char c, size_t factor ) { 385 386 387 390 string ret = c; 391 ret *= factor; 392 return ret; 388 393 } 389 394 390 395 string ?*?( const char * s, size_t factor ) { 391 392 393 396 string ret = s; 397 ret *= factor; 398 return ret; 394 399 } 395 400 … … 398 403 399 404 char ?[?]( const string & s, size_t index ) { 400 405 return (*s.inner)[index]; 401 406 } 402 407 403 408 string ?[?]( string & s, size_t index ) { 404 405 409 string ret = { *s.inner, index, 1 }; 410 return ret`share; 406 411 } 407 412 … … 410 415 411 416 bool contains( const string & s, char ch ) { 412 417 return contains( *s.inner, ch ); 413 418 } 414 419 415 420 int find( const string & s, char search ) { 416 421 return find( *s.inner, search ); 417 422 } 418 423 419 424 int find( const string & s, const string & search ) { 420 425 return find( *s.inner, *search.inner ); 421 426 } 422 427 423 428 int find( const string & s, const char * search ) { 424 429 return find( *s.inner, search ); 425 430 } 426 431 427 432 int find( const string & s, const char * search, size_t searchsize ) { 428 429 } 430 431 int find From( const string & s, size_t fromPos, char search ) {432 433 } 434 435 int find From( const string & s, size_t fromPos, const string & search ) {436 437 } 438 439 int find From( const string & s, size_t fromPos, const char * search ) {440 441 } 442 443 int find From( const string & s, size_t fromPos, const char * search, size_t searchsize ) {444 433 return find( *s.inner, search, searchsize ); 434 } 435 436 int find( const string & s, size_t fromPos, char search ) { 437 return findFrom( *s.inner, fromPos, search ); 438 } 439 440 int find( const string & s, size_t fromPos, const string & search ) { 441 return findFrom( *s.inner, fromPos, *search.inner ); 442 } 443 444 int find( const string & s, size_t fromPos, const char * search ) { 445 return findFrom( *s.inner, fromPos, search ); 446 } 447 448 int find( const string & s, size_t fromPos, const char * search, size_t searchsize ) { 449 return findFrom( *s.inner, fromPos, search, searchsize ); 445 450 } 446 451 447 452 bool includes( const string & s, const string & search ) { 448 453 return includes( *s.inner, *search.inner ); 449 454 } 450 455 451 456 bool includes( const string & s, const char * search ) { 452 457 return includes( *s.inner, search ); 453 458 } 454 459 455 460 bool includes( const string & s, const char * search, size_t searchsize ) { 456 461 return includes( *s.inner, search, searchsize ); 457 462 } 458 463 459 464 bool startsWith( const string & s, const string & prefix ) { 460 465 return startsWith( *s.inner, *prefix.inner ); 461 466 } 462 467 463 468 bool startsWith( const string & s, const char * prefix ) { 464 469 return startsWith( *s.inner, prefix ); 465 470 } 466 471 467 472 bool startsWith( const string & s, const char * prefix, size_t prefixsize ) { 468 473 return startsWith( *s.inner, prefix, prefixsize ); 469 474 } 470 475 471 476 bool endsWith( const string & s, const string & suffix ) { 472 477 return endsWith( *s.inner, *suffix.inner ); 473 478 } 474 479 475 480 bool endsWith( const string & s, const char * suffix ) { 476 481 return endsWith( *s.inner, suffix ); 477 482 } 478 483 479 484 bool endsWith( const string & s, const char * suffix, size_t suffixsize ) { 480 485 return endsWith( *s.inner, suffix, suffixsize ); 481 486 } 482 487 … … 486 491 487 492 void ?{}( charclass & s, const string & chars ) { 488 489 493 (s.inner) { malloc() }; 494 ?{}( *s.inner, *(const string_res *)chars.inner ); 490 495 } 491 496 492 497 void ?{}( charclass & s, const char * chars ) { 493 494 498 (s.inner) { malloc() }; 499 ?{}( *s.inner, chars ); 495 500 } 496 501 497 502 void ?{}( charclass & s, const char * chars, size_t charssize ) { 498 499 503 (s.inner) { malloc() }; 504 ?{}( *s.inner, chars, charssize ); 500 505 } 501 506 502 507 void ^?{}( charclass & s ) { 503 504 505 508 ^(*s.inner){}; 509 free( s.inner ); 510 s.inner = 0p; 506 511 } 507 512 508 513 509 514 int exclude( const string & s, const charclass & mask ) { 510 515 return exclude( *s.inner, *mask.inner ); 511 516 } 512 517 /* … … 516 521 517 522 int include( const string & s, const charclass & mask ) { 518 523 return include( *s.inner, *mask.inner ); 519 524 } 520 525 -
libcfa/src/collections/string.hfa
r3181a33 r4223317 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 14 15:33:38202513 // Update Count : 1 3212 // Last Modified On : Tue Apr 1 09:16:39 2025 13 // Update Count : 155 14 14 // 15 15 … … 24 24 25 25 struct string { 26 26 string_res * inner; 27 27 }; 28 28 29 29 // Getters 30 //static size_t inline __attribute__((always_inline)) size( char c ) { return sizeof( c ); }; // base case 31 //static size_t inline __attribute__((always_inline)) size( wchar_t wc ) { return sizeof( wc ); }; // base case 32 static inline size_t size( const char * cs ) { return strlen( cs ); }; 33 static inline size_t ?`len( const char * cs ) { return size( cs ); }; 30 34 size_t size( const string & s ); 35 size_t ?`len( const string & s ) { return size( s ); } 31 36 static inline size_t strlen( const string & s ) { return size( s ); } 32 37 33 38 // RAII, assignment 34 void ?{}( string & s ); 39 void ?{}( string & s ); // empty string 35 40 void ?{}( string & s, const string & s2 ); 36 41 void ?{}( string & s, const string & s2, size_t maxlen ); … … 48 53 void ?{}( string & s, long double _Complex rhs ); 49 54 50 string str( ssize_t rhs );51 string str( size_t rhs );52 string str( double rhs );53 string str( long double rhs );54 string str( double _Complex rhs );55 string str( long double _Complex rhs );55 // string str( ssize_t rhs ); 56 // string str( size_t rhs ); 57 // string str( double rhs ); 58 // string str( long double rhs ); 59 // string str( double _Complex rhs ); 60 // string str( long double _Complex rhs ); 56 61 57 62 string & ?=?( string & s, const string & c ); … … 78 83 // Alternate construction: request shared edits 79 84 struct string_Share { 80 85 string * s; 81 86 }; 82 87 string_Share ?`share( string & s ); … … 210 215 211 216 // Slicing 212 string ?()( string & s, s ize_t start,size_t len ); // TODO const?213 string ?()( string & s, s ize_t start );217 string ?()( string & s, ssize_t start, ssize_t len ); // TODO const? 218 string ?()( string & s, ssize_t start ); 214 219 215 220 // String search … … 221 226 int find( const string & s, const char * search, size_t searchsize ); 222 227 223 int find From( const string & s, size_t fromPos, char search );224 int find From( const string & s, size_t fromPos, const string & search );225 int find From( const string & s, size_t fromPos, const char * search );226 int find From( const string & s, size_t fromPos, const char * search, size_t searchsize );228 int find( const string & s, size_t fromPos, char search ); 229 int find( const string & s, size_t fromPos, const string & search ); 230 int find( const string & s, size_t fromPos, const char * search ); 231 int find( const string & s, size_t fromPos, const char * search, size_t searchsize ); 227 232 228 233 bool includes( const string & s, const string & search ); … … 246 251 247 252 struct charclass { 248 253 charclass_res * inner; 249 254 }; 250 255
Note:
See TracChangeset
for help on using the changeset viewer.