Changeset ed5023d1 for libcfa/src


Ignore:
Timestamp:
Apr 6, 2025, 10:46:19 PM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
0393fda8
Parents:
56ec508
Message:

fix substring error being outside of string, simplify comparison operations, start refactoring string search operations

Location:
libcfa/src/collections
Files:
4 edited

Legend:

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

    r56ec508 red5023d1  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr  3 21:27:08 2025
    13 // Update Count     : 297
     12// Last Modified On : Sat Apr  5 15:18:30 2025
     13// Update Count     : 318
    1414//
    1515
     
    256256        if ( start < 0 ) { start += len( s ); }
    257257        if ( len < 0 ) { len = -len; start -= len; }
    258         if ( start > len( s ) ) return (string){ "" };
     258        if ( start >= len( s ) ) return (string){ "" };
    259259        if ( start + len > len( s ) ) len = len( s ) - start;
    260260        string ret = { *s.inner, start, len };
     
    424424}
    425425
    426 int find( const string & s, char search ) {
    427         return find( *s.inner, search );
    428 }
    429 
    430 int find( const string & s, const string & search ) {
    431         return find( *s.inner, *search.inner );
    432 }
    433 
    434 int find( const string & s, const char * search ) {
    435         return find( *s.inner, search );
    436 }
    437 
    438 int find( const string & s, const char * search, size_t searchsize ) {
    439         return find( *s.inner, search, searchsize );
    440 }
    441 
    442 int find( const string & s, size_t fromPos, char search ) {
    443         return findFrom( *s.inner, fromPos, search );
    444 }
    445 
    446 int find( const string & s, size_t fromPos, const string & search ) {
    447         return findFrom( *s.inner, fromPos, *search.inner );
    448 }
    449 
    450 int find( const string & s, size_t fromPos, const char * search ) {
    451         return findFrom( *s.inner, fromPos, search );
    452 }
    453 
    454 int find( const string & s, size_t fromPos, const char * search, size_t searchsize ) {
    455         return findFrom( *s.inner, fromPos, search, searchsize );
    456 }
    457 
    458 bool includes( const string & s, const string & search ) {
    459         return includes( *s.inner, *search.inner );
    460 }
    461 
    462 bool includes( const string & s, const char * search ) {
    463         return includes( *s.inner, search );
    464 }
    465 
    466 bool includes( const string & s, const char * search, size_t searchsize ) {
    467         return includes( *s.inner, search, searchsize );
     426int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen ) {
     427        if ( start < 0 ) { start += len( s ); }
     428        if ( len < 0 ) { len = -len; start -= len; }
     429        if ( start >= len( s ) ) return 0;
     430        if ( start + len > len( s ) ) len = len( s ) - start;
     431
     432        if ( kstart < 0 ) { kstart += len( key ); }
     433        if ( klen < 0 ) { klen = -klen; kstart -= klen; }
     434        if ( kstart >= len( key ) ) return 0;
     435        if ( kstart + klen > len( key ) ) klen = len( key ) - kstart;
     436       
     437        return findFrom( *s.inner, start, *key.inner );
     438}
     439
     440int find( const string & s, char key ) {
     441        return find( *s.inner, key );
     442}
     443
     444int find( const string & s, const string & key ) {
     445        return find( *s.inner, *key.inner );
     446}
     447
     448int find( const string & s, const char * key ) {
     449        return find( *s.inner, key );
     450}
     451
     452int find( const string & s, const char * key, size_t keysize ) {
     453        return find( *s.inner, key, keysize );
     454}
     455
     456int find( const string & s, size_t start, char key ) {
     457        return findFrom( *s.inner, start, key );
     458}
     459
     460int find( const string & s, size_t start, const char * key ) {
     461        return findFrom( *s.inner, start, key );
     462}
     463
     464int find( const string & s, size_t start, const char * key, size_t keysize ) {
     465        return findFrom( *s.inner, start, key, keysize );
     466}
     467
     468bool includes( const string & s, const string & mask ) {
     469        return includes( *s.inner, *mask.inner );
     470}
     471
     472bool includes( const string & s, const char * mask ) {
     473        return includes( *s.inner, mask );
     474}
     475
     476bool includes( const string & s, const char * mask, size_t masksize ) {
     477        return includes( *s.inner, mask, masksize );
    468478}
    469479
  • libcfa/src/collections/string.hfa

    r56ec508 red5023d1  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr  3 21:45:53 2025
    13 // Update Count     : 170
     12// Last Modified On : Sat Apr  5 15:16:23 2025
     13// Update Count     : 180
    1414//
    1515
     
    212212bool ?<? ( const char *, const string & );
    213213
     214// String search
     215bool contains( const string & s, char ch );                             // single character
     216
     217int find( const string & s, char key );
     218static inline int ?^? ( const string & s, char key ) { return find( s, key ); }
     219int find( const string & s, const char * key );
     220static inline int ?^? ( const string & s, const char * key ) { return find( s, key ); }
     221int find( const string & s, const string & key );
     222static inline int ?^? ( const string & s, const string & key ) { return find( s, key ); }
     223int find( const string & s, const char * key, size_t keysize );
     224
     225int find( const string & s, size_t start, char key );
     226int find( const string & s, size_t start, const string & key );
     227int find( const string & s, size_t start, const char * key );
     228int find( const string & s, size_t start, const char * key, size_t keysize );
     229
     230bool includes( const string & s, const string & mask );
     231bool includes( const string & s, const char * mask );
     232bool includes( const string & s, const char * mask, size_t masksize );
     233
     234bool startsWith( const string & s, const string & prefix );
     235bool startsWith( const string & s, const char * prefix );
     236bool startsWith( const string & s, const char * prefix, size_t prefixsize );
     237
     238bool endsWith( const string & s, const string & suffix );
     239bool endsWith( const string & s, const char * suffix );
     240bool endsWith( const string & s, const char * suffix, size_t suffixsize );
    214241
    215242// Slicing
    216243string ?()( string & s, ssize_t start, ssize_t len );           // TODO const?
    217244string ?()( string & s, ssize_t start );
    218 
    219 // String search
    220 bool contains( const string & s, char ch );                             // single character
    221 
    222 int find( const string & s, char search );
    223 int find( const string & s, const string & search );
    224 int find( const string & s, const char * search );
    225 int find( const string & s, const char * search, size_t searchsize );
    226 
    227 int find( const string & s, size_t fromPos, char search );
    228 int find( const string & s, size_t fromPos, const string & search );
    229 int find( const string & s, size_t fromPos, const char * search );
    230 int find( const string & s, size_t fromPos, const char * search, size_t searchsize );
    231 
    232 bool includes( const string & s, const string & search );
    233 bool includes( const string & s, const char * search );
    234 bool includes( const string & s, const char * search, size_t searchsize );
    235 
    236 bool startsWith( const string & s, const string & prefix );
    237 bool startsWith( const string & s, const char * prefix );
    238 bool startsWith( const string & s, const char * prefix, size_t prefixsize );
    239 
    240 bool endsWith( const string & s, const string & suffix );
    241 bool endsWith( const string & s, const char * suffix );
    242 bool endsWith( const string & s, const char * suffix, size_t suffixsize );
     245static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
     246static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
     247static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
    243248
    244249// Modifiers
  • libcfa/src/collections/string_res.cfa

    r56ec508 red5023d1  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Apr  1 23:29:58 2025
    13 // Update Count     : 91
     12// Last Modified On : Sun Apr  6 07:38:02 2025
     13// Update Count     : 111
    1414//
    1515
     
    724724}
    725725
    726 void ?+=?(string_res & str1, const string_res & str2) {
    727         append( str1, str2.Handle.s, str2.Handle.lnth );
    728 }
    729 
    730 void append(string_res & str1, const string_res & str2, size_t maxlen) {
    731         append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) );
    732 }
    733 
    734 void ?+=?(string_res & s, char c) {
    735         append( s, & c, 1 );
    736 }
    737 void ?+=?(string_res & s, const char * c) {
    738         append( s, c, strlen(c) );
     726void append( string_res & s, const string_res & s2, size_t maxlen ) {
     727        append( s, s2.Handle.s, min( s2.Handle.lnth, maxlen ) );
    739728}
    740729
     
    751740// Comparisons
    752741
    753 int strcmp(const string_res & s1, const string_res & s2) {
    754         // return 0;
    755         int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth));
    756         if (ans1 != 0) return ans1;
    757         return s1.Handle.lnth - s2.Handle.lnth;
    758 }
    759 
    760 bool ?==?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) == 0; }
    761 bool ?!=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) != 0; }
    762 bool ?>? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) >  0; }
    763 bool ?>=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) >= 0; }
    764 bool ?<=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) <= 0; }
    765 bool ?<? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) <  0; }
    766 
    767 int strcmp (const string_res & s1, const char * s2) {
    768         string_res s2x = s2;
    769         return strcmp(s1, s2x);
    770 }
    771 
    772 bool ?==?(const string_res & s1, const char * s2) { return strcmp(s1, s2) == 0; }
    773 bool ?!=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) != 0; }
    774 bool ?>? (const string_res & s1, const char * s2) { return strcmp(s1, s2) >  0; }
    775 bool ?>=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) >= 0; }
    776 bool ?<=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) <= 0; }
    777 bool ?<? (const string_res & s1, const char * s2) { return strcmp(s1, s2) <  0; }
    778 
    779 int strcmp (const char * s1, const string_res & s2) {
    780         string_res s1x = s1;
    781         return strcmp(s1x, s2);
    782 }
    783 
    784 bool ?==?(const char * s1, const string_res & s2) { return strcmp(s1, s2) == 0; }
    785 bool ?!=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) != 0; }
    786 bool ?>? (const char * s1, const string_res & s2) { return strcmp(s1, s2) >  0; }
    787 bool ?>=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) >= 0; }
    788 bool ?<=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) <= 0; }
    789 bool ?<? (const char * s1, const string_res & s2) { return strcmp(s1, s2) <  0; }
    790 
     742int strcmp$( const char * s1, size_t l1, const char * s2, size_t l2 ) {
     743        int ret = memcmp( s1, s2, min( l1, l2 ) );
     744        if ( ret != 0 ) return ret;
     745        return l1 - l2;
     746}
    791747
    792748//////////////////////////////////////////////////////////
  • libcfa/src/collections/string_res.hfa

    r56ec508 red5023d1  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Apr  1 23:24:20 2025
    13 // Update Count     : 62
     12// Last Modified On : Sun Apr  6 07:35:44 2025
     13// Update Count     : 70
    1414//
    1515
     
    1919#include <string.h>    // e.g. strlen
    2020
    21    
     21
    2222//######################### HandleNode #########################
    2323//private
     
    2626
    2727struct HandleNode {
    28     HandleNode *flink;                                  // forward link
    29     HandleNode *blink;                                  // backward link
    30     VbyteHeap *ulink;                   // upward link
    31 
    32     char *s;                                                    // pointer to byte string
    33     unsigned int lnth;                                  // length of byte string
     28        HandleNode * flink;                                                                     // forward link
     29        HandleNode * blink;                                                                     // backward link
     30        VbyteHeap * ulink;                                                                      // upward link
     31
     32        char * s;                                                                                       // pointer to byte string
     33        unsigned int lnth;                                                                      // length of byte string
    3434}; // HandleNode
    3535
     
    4545// A dynamically-sized string
    4646struct string_res {
    47     HandleNode Handle; // chars, start, end, global neighbours
    48     bool shareSet_owns_ulink;
    49     string_res * shareSet_prev;
    50     string_res * shareSet_next;
     47        HandleNode Handle; // chars, start, end, global neighbours
     48        bool shareSet_owns_ulink;
     49        string_res * shareSet_prev;
     50        string_res * shareSet_next;
    5151};
    5252
     
    5555
    5656struct charclass_res {
    57     string_res chars;
     57        string_res chars;
    5858};
    5959
     
    7070
    7171// Getters
    72 size_t len(const string_res & s);
     72size_t len( const string_res & s);
    7373
    7474// Constructors, Assignment Operators, Destructor
     
    7676void ?{}(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
    7777static inline void ?{}(string_res & s, const char * rhs) { // copy from string literal (NULL-terminated)
    78     (s){ rhs, strlen(rhs) };
     78        (s){ rhs, strlen(rhs) };
    7979}
    8080static inline void ?{}(string_res & s, char c ) {
    81     ?{}( s, &c, 1);
     81        ?{}( s, &c, 1);
    8282}
    8383
     
    8989void ?{}(string_res & s, const string_res & src, StrResInitMode, size_t start, size_t len );
    9090static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode ) {
    91     ?{}( s, src, mode, 0, len(src));
     91        ?{}( s, src, mode, 0, len(src));
    9292}
    9393static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode, size_t maxlen ) {
    94     ?{}( s, src, mode, 0, (len(src) > maxlen)?maxlen:len(src) );
     94        ?{}( s, src, mode, 0, (len(src) > maxlen)?maxlen:len(src) );
    9595}
    9696void ?{}( string_res & s, ssize_t rhs );
     
    104104string_res & assign(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
    105105static inline string_res & ?=?(string_res & s, const char * c) {  // copy from string literal (NULL-terminated)
    106     return assign(s, c, strlen(c));
     106        return assign(s, c, strlen( c));
    107107}
    108108string_res & ?=?(string_res & s, const string_res & c);
     
    172172
    173173// Concatenation
    174 void ?+=?(string_res & s, const string_res & s2);
    175 void ?+=?(string_res & s, char c);
    176 void append(string_res & s, const string_res & s2, size_t maxlen);
    177 void ?+=?(string_res & s, const char * c);
    178 void append(string_res & s, const char * buffer, size_t bsize);
    179 
    180 static inline string_res & strcat(string_res & s, const string_res & s2) { s += s2; return s; }
    181 static inline string_res & strcat(string_res & s, const char * c) { s += c; return s; }
    182 static inline string_res & strncat(string_res & s, const string_res & s2, size_t maxlen) { append(s, s2, maxlen); return s; }
    183 static inline string_res & strncat(string_res & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; }
     174void append( string_res & s, const char * buffer, size_t bsize );
     175void append( string_res & s, const string_res & s2, size_t maxlen );
     176static inline void ?+=?( string_res & s, const string_res & s2 ) { append( s, s2.Handle.s, s2.Handle.lnth ); }
     177static inline void ?+=?( string_res & s, char c ) { append( s, & c, 1 ); }
     178static inline void ?+=?( string_res & s, const char * c ) { append( s, c, strlen( c ) ); }
     179static inline string_res & strcat( string_res & s, const string_res & s2 ) { s += s2; return s; }
     180static inline string_res & strcat( string_res & s, const char * c ) { s += c; return s; }
     181static inline string_res & strncat( string_res & s, const string_res & s2, size_t maxlen ) { append(s, s2, maxlen); return s; }
     182static inline string_res & strncat( string_res & s, const char * buffer, size_t bsize ) { append(s, buffer, bsize); return s; }
    184183
    185184// Repetition
     
    187186
    188187// Character access
    189 void assignAt(const string_res & s, size_t index, char val);
    190 char ?[?](const string_res & s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
    191 //char codePointAt(const string_res & s, size_t index); // revisit under Unicode
     188void assignAt( const string_res & s, size_t index, char val);
     189char ?[?]( const string_res & s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
     190//char codePointAt( const string_res & s, size_t index); // revisit under Unicode
    192191
    193192// Comparisons
    194 int  strcmp (const string_res &, const string_res &);
    195 bool ?==?(const string_res &, const string_res &);
    196 bool ?!=?(const string_res &, const string_res &);
    197 bool ?>? (const string_res &, const string_res &);
    198 bool ?>=?(const string_res &, const string_res &);
    199 bool ?<=?(const string_res &, const string_res &);
    200 bool ?<? (const string_res &, const string_res &);
    201 
    202 int  strcmp(const string_res &, const char *);
    203 bool ?==?(const string_res &, const char *);
    204 bool ?!=?(const string_res &, const char *);
    205 bool ?>? (const string_res &, const char *);
    206 bool ?>=?(const string_res &, const char *);
    207 bool ?<=?(const string_res &, const char *);
    208 bool ?<? (const string_res &, const char *);
    209 
    210 int  strcmp(const char *, const string_res &);
    211 bool ?==?(const char *, const string_res &);
    212 bool ?!=?(const char *, const string_res &);
    213 bool ?>? (const char *, const string_res &);
    214 bool ?>=?(const char *, const string_res &);
    215 bool ?<=?(const char *, const string_res &);
    216 bool ?<? (const char *, const string_res &);
     193int strcmp$( const char * s1, size_t l1, const char * s2, size_t l2 );
     194
     195static inline int strcmp( const string_res & s1, const string_res & s2 ) { return strcmp$( s1.Handle.s, s1.Handle.lnth, s2.Handle.s, s2.Handle.lnth ); }
     196static inline bool ?==?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) == 0; }
     197static inline bool ?!=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) != 0; }
     198static inline bool ?>? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) >  0; }
     199static inline bool ?>=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; }
     200static inline bool ?<=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; }
     201static inline bool ?<? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) <  0; }
     202
     203static inline int strcmp( const string_res & s1, const char * s2 ) { return strcmp$( s1.Handle.s, s1.Handle.lnth, s2, strlen( s2 ) ); }
     204static inline bool ?==?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) == 0; }
     205static inline bool ?!=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) != 0; }
     206static inline bool ?>? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) >  0; }
     207static inline bool ?>=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) >= 0; }
     208static inline bool ?<=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) <= 0; }
     209static inline bool ?<? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) <  0; }
     210
     211static inline int strcmp( const char * s1, const string_res & s2 ) { return strcmp$( s1, strlen( s1 ), s2.Handle.s, s2.Handle.lnth ); }
     212static inline bool ?==?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) == 0; }
     213static inline bool ?!=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) != 0; }
     214static inline bool ?>? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) >  0; }
     215static inline bool ?>=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; }
     216static inline bool ?<=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; }
     217static inline bool ?<? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) <  0; }
    217218
    218219// String search
    219 bool contains(const string_res & s, char ch); // single character
    220 
    221 int find(const string_res & s, char search);
    222 int find(const string_res & s, const string_res & search);
    223 int find(const string_res & s, const char * search);
    224 int find(const string_res & s, const char * search, size_t searchsize);
    225 
    226 int findFrom(const string_res & s, size_t fromPos, char search);
    227 int findFrom(const string_res & s, size_t fromPos, const string_res & search);
    228 int findFrom(const string_res & s, size_t fromPos, const char * search);
    229 int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize);
    230 
    231 bool includes(const string_res & s, const string_res & search);
    232 bool includes(const string_res & s, const char * search);
    233 bool includes(const string_res & s, const char * search, size_t searchsize);
    234 
    235 bool startsWith(const string_res & s, const string_res & prefix);
    236 bool startsWith(const string_res & s, const char * prefix);
    237 bool startsWith(const string_res & s, const char * prefix, size_t prefixsize);
    238 
    239 bool endsWith(const string_res & s, const string_res & suffix);
    240 bool endsWith(const string_res & s, const char * suffix);
    241 bool endsWith(const string_res & s, const char * suffix, size_t suffixsize);
    242 
    243 int include(const string_res & s, const charclass_res & mask);
    244 int exclude(const string_res & s, const charclass_res & mask);
     220bool contains( const string_res & s, char ch); // single character
     221
     222int find( const string_res & s, char search);
     223int find( const string_res & s, const string_res & search);
     224int find( const string_res & s, const char * search);
     225int find( const string_res & s, const char * search, size_t searchsize);
     226
     227int findFrom( const string_res & s, size_t fromPos, char search);
     228int findFrom( const string_res & s, size_t fromPos, const string_res & search);
     229int findFrom( const string_res & s, size_t fromPos, const char * search);
     230int findFrom( const string_res & s, size_t fromPos, const char * search, size_t searchsize);
     231
     232bool includes( const string_res & s, const string_res & search);
     233bool includes( const string_res & s, const char * search);
     234bool includes( const string_res & s, const char * search, size_t searchsize);
     235
     236bool startsWith( const string_res & s, const string_res & prefix);
     237bool startsWith( const string_res & s, const char * prefix);
     238bool startsWith( const string_res & s, const char * prefix, size_t prefixsize);
     239
     240bool endsWith( const string_res & s, const string_res & suffix);
     241bool endsWith( const string_res & s, const char * suffix);
     242bool endsWith( const string_res & s, const char * suffix, size_t suffixsize);
     243
     244int include( const string_res & s, const charclass_res & mask);
     245int exclude( const string_res & s, const charclass_res & mask);
    245246
    246247// Modifiers
Note: See TracChangeset for help on using the changeset viewer.