Changeset 9018dcf for libcfa/src


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

updates to string type

Location:
libcfa/src/collections
Files:
4 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}
  • libcfa/src/collections/string.hfa

    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:16:23 2025
    13 // Update Count     : 180
     12// Last Modified On : Wed Apr  9 22:27:41 2025
     13// Update Count     : 259
    1414//
    1515
     
    1717
    1818#include <fstream.hfa>
    19 
    20 
    21 // in string_res.hfa
    22 struct string_res;
    23 struct charclass_res;
     19#include <string_res.hfa>
    2420
    2521struct string {
     
    2824
    2925// Getters
     26static inline size_t len( const string & s ) { return len( *s.inner ); }
    3027static inline size_t len( const char * cs ) { return strlen( cs ); };
    31 size_t len( const string & s );
    3228static inline size_t strlen( const string & s ) { return len( s ); }
    3329
     
    215211bool contains( const string & s, char ch );                             // single character
    216212
    217 int find( const string & s, char key );
    218 static inline int ?^? ( const string & s, char key ) { return find( s, key ); }
    219 int find( const string & s, const char * key );
    220 static inline int ?^? ( const string & s, const char * key ) { return find( s, key ); }
    221 int find( const string & s, const string & key );
    222 static inline int ?^? ( const string & s, const string & key ) { return find( s, key ); }
    223 int find( const string & s, const char * key, size_t keysize );
    224 
    225 int find( const string & s, size_t start, char key );
    226 int find( const string & s, size_t start, const string & key );
    227 int find( const string & s, size_t start, const char * key );
    228 int find( const string & s, size_t start, const char * key, size_t keysize );
     213//int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen );
     214size_t find$( const string_res & s, size_t start, size_t len, const string & key_res, size_t kstart, size_t klen );
     215
     216size_t find( const string & s, char key );
     217size_t find( const string & s, const char * key );
     218size_t find( const string & s, const string & key );
     219size_t find( const string & s, const char * key, size_t keysize );
     220
     221size_t find( const string & s, size_t start, char key );
     222size_t find( const string & s, size_t start, const string & key );
     223size_t find( const string & s, size_t start, const char * key );
     224size_t find( const string & s, size_t start, const char * key, size_t keysize );
     225static inline ?^?( const string & key, const string & s ) { return find( s, key ); }
     226static inline ?^?( const char * key, const string & s ) { return find( s, key ); }
    229227
    230228bool includes( const string & s, const string & mask );
     
    241239
    242240// Slicing
    243 string ?()( string & s, ssize_t start, ssize_t len );           // TODO const?
     241string ?()( string & s, ssize_t start, ssize_t len );
     242static inline string ?()( const string & s, ssize_t start, ssize_t len ) { string & w = (string &)s; return w( start, len ); } // FIX ME
    244243string ?()( string & s, ssize_t start );
     244static inline string ?()( const string & s, ssize_t start ) { string & w = (string &)s; return w( start ); } // FIX ME
    245245static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
     246static inline string ?()( const string & s, char m ) { string & w = (string &)s; return w( find( s, m ), 1 )`share; } // FIX ME
    246247static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
     248static inline string ?()( const string & s, const char * m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
    247249static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
    248 
    249 // Modifiers
    250 void padStart( string & s, size_t n );
    251 void padStart( string & s, size_t n, char padding );
    252 void padEnd( string & s, size_t n );
    253 void padEnd( string & s, size_t n, char padding );
    254 
     250static inline string ?()( const string & s, const string & m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
    255251
    256252struct charclass {
     
    267263void ^?{}( charclass & );
    268264
    269 int include( const string & s, const charclass & mask );
    270 
    271 int exclude( const string & s, const charclass & mask );
    272 
    273 /*
    274 What to do with?
    275 StrRet include( string & s, const charclass & mask );
    276 StrRet exclude( string & s, const charclass & mask );
    277 */
     265size_t include( const string & s, const charclass & mask );
     266static inline size_t include( const char * s, const charclass & mask ) { string temp = s; return include( temp, mask ); }
     267static inline string include( const string & s, const charclass & mask ) { ssize_t i = include( s, mask ); return s( 0, i )`share; }
     268static inline string include( const char * s, const charclass & mask ) { string temp = s; ssize_t i = include( temp, mask ); return temp( 0, i ); }
     269
     270size_t exclude( const string & s, const charclass & mask );
     271static inline size_t exclude( const char * s, const charclass & mask ) { string temp = s; return exclude( temp, mask ); }
     272static inline string exclude( const string & s, const charclass & mask ) { ssize_t i = exclude( s, mask ); return s( 0, i )`share; }
     273static inline string exclude( const char * s, const charclass & mask ) { string temp = s; ssize_t i = exclude( temp, mask ); return temp( 0, i ); }
     274
     275size_t test( const string & s, int (*f)( int ) );
     276static inline size_t test( const char * c, int (*f)( int ) ) {
     277        const string S = c;
     278        return test( S, f );
     279}
     280
     281string replace( string & s, const string & from, const string & to );
     282static inline string replace( const char * s, const char * from, const char * to ) {
     283        string S = s, From = from, To = to;
     284        return replace( S, From, To );
     285}
     286static inline string replace( string & s, const char * from, const char * to ) {
     287        string From = from, To = to;
     288        return replace( s, From, To );
     289}
     290static inline string replace( string & s, const char * from, const string & to ) {
     291        string From = from;
     292        return replace( s, From, to );
     293}
     294static inline string replace( string & s, string & from, const char * to ) {
     295        string To = to;
     296        return replace( s, from, To );
     297}
     298
     299string translate( const string & s, int (*f)( int ) );
     300static inline string translate( const char * c, int (*f)( int ) ) {
     301        const string S = c;
     302        return translate( S, f );
     303}
  • libcfa/src/collections/string_res.cfa

    r931f1b4 r9018dcf  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr  6 07:38:02 2025
    13 // Update Count     : 111
     12// Last Modified On : Wed Apr  9 08:44:17 2025
     13// Update Count     : 128
    1414//
    1515
     
    190190const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
    191191        return heap->StartVbyte;
    192 }
    193 
    194 // Returns the size of the string in bytes
    195 size_t len(const string_res & s) with(s) {
    196         return Handle.lnth;
    197192}
    198193
     
    756751}
    757752
    758 int find(const string_res & s, char search) {
     753// int find$( const string_res & s, ssize_t start, ssize_t len, const string_res & k, ssize_t kstart, ssize_t klen ) {
     754//     if ( start < 0 ) start = s.Handle.lnth + start;          // adjust negative starting locations
     755//     if ( kstart < 0 ) kstart = k.Handle.lnth + kstart;
     756
     757//      if ( start + len > s.Handle.lnth ) return start + 1;  // cannot be there
     758//      if ( kstart + len > k.Handle.lnth ) return start + 1;
     759//      if ( klen > len ) return start + 1;
     760
     761//      int i, r;
     762
     763//      for ( i = max( start, 1 ); ; i += 1 ) {
     764//              if ( i > s.Handle.lnth - k.Handle.lnth + 1 ) {
     765//                      r = s.Handle.lnth + 1;
     766//                      break;
     767//          } // exit
     768//              if ( HeapArea->ByteCmp( s.Handle.s, i, k.Handle.lnth, k.Handle.s, 1, k.Handle.lnth ) == 0 ) {
     769//                      r = i;
     770//                      break;
     771//          } // exit
     772//      } // for
     773//      return r;
     774// }
     775
     776int find( const string_res & s, char search ) {
    759777        return findFrom(s, 0, search);
    760778}
    761779
    762 int findFrom(const string_res & s, size_t fromPos, char search) {
     780int findFrom( const string_res & s, size_t fromPos, char search ) {
    763781        // FIXME: This paricular overload (find of single char) is optimized to use memchr.
    764782        // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match.
  • libcfa/src/collections/string_res.hfa

    r931f1b4 r9018dcf  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr  6 07:35:44 2025
    13 // Update Count     : 70
     12// Last Modified On : Wed Apr  9 15:16:29 2025
     13// Update Count     : 76
    1414//
    1515
     
    7070
    7171// Getters
    72 size_t len( const string_res & s);
     72static inline size_t len( const string_res & s ) { return s.Handle.lnth; }
    7373
    7474// Constructors, Assignment Operators, Destructor
     
    220220bool contains( const string_res & s, char ch); // single character
    221221
     222int find$( const string_res & s, ssize_t start, ssize_t len, const string_res & key, ssize_t kstart, ssize_t klen );
     223
    222224int find( const string_res & s, char search);
    223225int find( const string_res & s, const string_res & search);
     
    245247int exclude( const string_res & s, const charclass_res & mask);
    246248
    247 // Modifiers
    248 void padStart(string_res & s, size_t n);
    249 void padStart(string_res & s, size_t n, char padding);
    250 void padEnd(string_res & s, size_t n);
    251 void padEnd(string_res &s, size_t n, char padding);
    252 
Note: See TracChangeset for help on using the changeset viewer.