Ignore:
Timestamp:
Apr 10, 2025, 7:33:31 AM (9 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
7e4f226
Parents:
931f1b4
Message:

updates to string type

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/collections/string.cfa

    r931f1b4 r9018dcf  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr  5 15:18:30 2025
    13 // Update Count     : 318
     12// Last Modified On : Wed Apr  9 22:27:40 2025
     13// Update Count     : 368
    1414//
    1515
     
    295295bool ?<? ( const char * s1, const string & s2 ) { return s1 <  *s2.inner; }
    296296
    297 
    298 ////////////////////////////////////////////////////////
    299 // Getter
    300 
    301 size_t len( const string & s ) {
    302         return len( *s.inner );
    303 }
    304 
    305297////////////////////////////////////////////////////////
    306298// Concatenation
     
    424416}
    425417
    426 int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen ) {
     418size_t find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen ) {
    427419        if ( start < 0 ) { start += len( s ); }
    428420        if ( len < 0 ) { len = -len; start -= len; }
     
    434426        if ( kstart >= len( key ) ) return 0;
    435427        if ( kstart + klen > len( key ) ) klen = len( key ) - kstart;
    436        
     428
    437429        return findFrom( *s.inner, start, *key.inner );
    438430}
    439431
    440 int find( const string & s, char key ) {
     432size_t find( const string & s, char key ) {
    441433        return find( *s.inner, key );
    442434}
    443435
    444 int find( const string & s, const string & key ) {
     436size_t find( const string & s, const string & key ) {
    445437        return find( *s.inner, *key.inner );
    446438}
    447439
    448 int find( const string & s, const char * key ) {
     440size_t find( const string & s, const char * key ) {
    449441        return find( *s.inner, key );
    450442}
    451443
    452 int find( const string & s, const char * key, size_t keysize ) {
     444size_t find( const string & s, const char * key, size_t keysize ) {
    453445        return find( *s.inner, key, keysize );
    454446}
    455447
    456 int find( const string & s, size_t start, char key ) {
     448size_t find( const string & s, size_t start, char key ) {
    457449        return findFrom( *s.inner, start, key );
    458450}
    459451
    460 int find( const string & s, size_t start, const char * key ) {
     452size_t find( const string & s, size_t start, const char * key ) {
    461453        return findFrom( *s.inner, start, key );
    462454}
    463455
    464 int find( const string & s, size_t start, const char * key, size_t keysize ) {
     456size_t find( const string & s, size_t start, const char * key, size_t keysize ) {
    465457        return findFrom( *s.inner, start, key, keysize );
    466458}
     
    527519}
    528520
    529 
    530 int exclude( const string & s, const charclass & mask ) {
     521size_t exclude( const string & s, const charclass & mask ) {
    531522        return exclude( *s.inner, *mask.inner );
    532523}
    533 /*
    534 StrSlice exclude( string & s, const charclass & mask ) {
    535 }
    536 */
    537 
    538 int include( const string & s, const charclass & mask ) {
     524
     525size_t include( const string & s, const charclass & mask ) {
    539526        return include( *s.inner, *mask.inner );
    540527}
    541528
    542 /*
    543 StrSlice include( string & s, const charclass & mask ) {
    544 }
    545 */
     529size_t test( const string & s, int (*f)( int ) ) {
     530        size_t l = len( s );
     531        for ( i; l ) {
     532                if ( ! f( s[i] ) ) return i;
     533        } // for
     534        return l;
     535}
     536
     537string replace( string & s, const string & from, const string & to ) {
     538        ssize_t pos;
     539    string r;
     540
     541    pos = find( s, from );
     542    if ( pos < len( s ) ) {
     543                r = s( 0, pos ) + to + replace( s( pos + (ssize_t)len( from ) ), from, to );
     544                string front = s( 0, pos );
     545                string back = s( pos + (ssize_t)len( from ) );
     546                r = front + to + replace( back, from, to );
     547    } else {
     548                r = s;
     549    } // if
     550    return r;
     551}
     552
     553string translate( const string & s, int (*f)( int ) ) {
     554        string r = s;
     555        size_t l = len( r );
     556        for ( i; l ) {
     557                r[i] = (char)f( r[i] );
     558        } // for
     559        return r;
     560}
Note: See TracChangeset for help on using the changeset viewer.