Changeset ed5023d1 for libcfa/src
- Timestamp:
- Apr 6, 2025, 10:46:19 PM (6 months ago)
- Branches:
- master
- Children:
- 0393fda8
- Parents:
- 56ec508
- Location:
- libcfa/src/collections
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/collections/string.cfa
r56ec508 red5023d1 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 3 21:27:08202513 // Update Count : 29712 // Last Modified On : Sat Apr 5 15:18:30 2025 13 // Update Count : 318 14 14 // 15 15 … … 256 256 if ( start < 0 ) { start += len( s ); } 257 257 if ( len < 0 ) { len = -len; start -= len; } 258 if ( start > len( s ) ) return (string){ "" };258 if ( start >= len( s ) ) return (string){ "" }; 259 259 if ( start + len > len( s ) ) len = len( s ) - start; 260 260 string ret = { *s.inner, start, len }; … … 424 424 } 425 425 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 ); 426 int 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 440 int find( const string & s, char key ) { 441 return find( *s.inner, key ); 442 } 443 444 int find( const string & s, const string & key ) { 445 return find( *s.inner, *key.inner ); 446 } 447 448 int find( const string & s, const char * key ) { 449 return find( *s.inner, key ); 450 } 451 452 int find( const string & s, const char * key, size_t keysize ) { 453 return find( *s.inner, key, keysize ); 454 } 455 456 int find( const string & s, size_t start, char key ) { 457 return findFrom( *s.inner, start, key ); 458 } 459 460 int find( const string & s, size_t start, const char * key ) { 461 return findFrom( *s.inner, start, key ); 462 } 463 464 int find( const string & s, size_t start, const char * key, size_t keysize ) { 465 return findFrom( *s.inner, start, key, keysize ); 466 } 467 468 bool includes( const string & s, const string & mask ) { 469 return includes( *s.inner, *mask.inner ); 470 } 471 472 bool includes( const string & s, const char * mask ) { 473 return includes( *s.inner, mask ); 474 } 475 476 bool includes( const string & s, const char * mask, size_t masksize ) { 477 return includes( *s.inner, mask, masksize ); 468 478 } 469 479 -
libcfa/src/collections/string.hfa
r56ec508 red5023d1 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 3 21:45:53 202513 // Update Count : 1 7012 // Last Modified On : Sat Apr 5 15:16:23 2025 13 // Update Count : 180 14 14 // 15 15 … … 212 212 bool ?<? ( const char *, const string & ); 213 213 214 // String search 215 bool contains( const string & s, char ch ); // single character 216 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 ); 229 230 bool includes( const string & s, const string & mask ); 231 bool includes( const string & s, const char * mask ); 232 bool includes( const string & s, const char * mask, size_t masksize ); 233 234 bool startsWith( const string & s, const string & prefix ); 235 bool startsWith( const string & s, const char * prefix ); 236 bool startsWith( const string & s, const char * prefix, size_t prefixsize ); 237 238 bool endsWith( const string & s, const string & suffix ); 239 bool endsWith( const string & s, const char * suffix ); 240 bool endsWith( const string & s, const char * suffix, size_t suffixsize ); 214 241 215 242 // Slicing 216 243 string ?()( string & s, ssize_t start, ssize_t len ); // TODO const? 217 244 string ?()( 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 ); 245 static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; } 246 static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; } 247 static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; } 243 248 244 249 // Modifiers -
libcfa/src/collections/string_res.cfa
r56ec508 red5023d1 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Apr 1 23:29:58202513 // Update Count : 9112 // Last Modified On : Sun Apr 6 07:38:02 2025 13 // Update Count : 111 14 14 // 15 15 … … 724 724 } 725 725 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) ); 726 void append( string_res & s, const string_res & s2, size_t maxlen ) { 727 append( s, s2.Handle.s, min( s2.Handle.lnth, maxlen ) ); 739 728 } 740 729 … … 751 740 // Comparisons 752 741 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 742 int 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 } 791 747 792 748 ////////////////////////////////////////////////////////// -
libcfa/src/collections/string_res.hfa
r56ec508 red5023d1 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Apr 1 23:24:20202513 // Update Count : 6212 // Last Modified On : Sun Apr 6 07:35:44 2025 13 // Update Count : 70 14 14 // 15 15 … … 19 19 #include <string.h> // e.g. strlen 20 20 21 21 22 22 //######################### HandleNode ######################### 23 23 //private … … 26 26 27 27 struct HandleNode { 28 HandleNode *flink;// forward link29 HandleNode *blink;// backward link30 VbyteHeap *ulink;// upward link31 32 char *s;// pointer to byte string33 unsigned int lnth;// length of byte string28 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 34 34 }; // HandleNode 35 35 … … 45 45 // A dynamically-sized string 46 46 struct string_res { 47 48 49 50 47 HandleNode Handle; // chars, start, end, global neighbours 48 bool shareSet_owns_ulink; 49 string_res * shareSet_prev; 50 string_res * shareSet_next; 51 51 }; 52 52 … … 55 55 56 56 struct charclass_res { 57 57 string_res chars; 58 58 }; 59 59 … … 70 70 71 71 // Getters 72 size_t len( const string_res & s);72 size_t len( const string_res & s); 73 73 74 74 // Constructors, Assignment Operators, Destructor … … 76 76 void ?{}(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer 77 77 static inline void ?{}(string_res & s, const char * rhs) { // copy from string literal (NULL-terminated) 78 78 (s){ rhs, strlen(rhs) }; 79 79 } 80 80 static inline void ?{}(string_res & s, char c ) { 81 81 ?{}( s, &c, 1); 82 82 } 83 83 … … 89 89 void ?{}(string_res & s, const string_res & src, StrResInitMode, size_t start, size_t len ); 90 90 static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode ) { 91 91 ?{}( s, src, mode, 0, len(src)); 92 92 } 93 93 static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode, size_t maxlen ) { 94 94 ?{}( s, src, mode, 0, (len(src) > maxlen)?maxlen:len(src) ); 95 95 } 96 96 void ?{}( string_res & s, ssize_t rhs ); … … 104 104 string_res & assign(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer 105 105 static 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)); 107 107 } 108 108 string_res & ?=?(string_res & s, const string_res & c); … … 172 172 173 173 // 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; } 174 void append( string_res & s, const char * buffer, size_t bsize ); 175 void append( string_res & s, const string_res & s2, size_t maxlen ); 176 static inline void ?+=?( string_res & s, const string_res & s2 ) { append( s, s2.Handle.s, s2.Handle.lnth ); } 177 static inline void ?+=?( string_res & s, char c ) { append( s, & c, 1 ); } 178 static inline void ?+=?( string_res & s, const char * c ) { append( s, c, strlen( c ) ); } 179 static inline string_res & strcat( string_res & s, const string_res & s2 ) { s += s2; return s; } 180 static inline string_res & strcat( string_res & s, const char * c ) { s += c; return s; } 181 static inline string_res & strncat( string_res & s, const string_res & s2, size_t maxlen ) { append(s, s2, maxlen); return s; } 182 static inline string_res & strncat( string_res & s, const char * buffer, size_t bsize ) { append(s, buffer, bsize); return s; } 184 183 185 184 // Repetition … … 187 186 188 187 // 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's191 //char codePointAt( const string_res & s, size_t index); // revisit under Unicode188 void assignAt( const string_res & s, size_t index, char val); 189 char ?[?]( 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 192 191 193 192 // 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 &); 193 int strcmp$( const char * s1, size_t l1, const char * s2, size_t l2 ); 194 195 static 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 ); } 196 static inline bool ?==?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) == 0; } 197 static inline bool ?!=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) != 0; } 198 static inline bool ?>? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) > 0; } 199 static inline bool ?>=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; } 200 static inline bool ?<=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; } 201 static inline bool ?<? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) < 0; } 202 203 static inline int strcmp( const string_res & s1, const char * s2 ) { return strcmp$( s1.Handle.s, s1.Handle.lnth, s2, strlen( s2 ) ); } 204 static inline bool ?==?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) == 0; } 205 static inline bool ?!=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) != 0; } 206 static inline bool ?>? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) > 0; } 207 static inline bool ?>=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) >= 0; } 208 static inline bool ?<=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) <= 0; } 209 static inline bool ?<? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) < 0; } 210 211 static inline int strcmp( const char * s1, const string_res & s2 ) { return strcmp$( s1, strlen( s1 ), s2.Handle.s, s2.Handle.lnth ); } 212 static inline bool ?==?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) == 0; } 213 static inline bool ?!=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) != 0; } 214 static inline bool ?>? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) > 0; } 215 static inline bool ?>=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; } 216 static inline bool ?<=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; } 217 static inline bool ?<? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) < 0; } 217 218 218 219 // String search 219 bool contains( const string_res & s, char ch); // single character220 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);220 bool contains( const string_res & s, char ch); // single character 221 222 int find( const string_res & s, char search); 223 int find( const string_res & s, const string_res & search); 224 int find( const string_res & s, const char * search); 225 int find( const string_res & s, const char * search, size_t searchsize); 226 227 int findFrom( const string_res & s, size_t fromPos, char search); 228 int findFrom( const string_res & s, size_t fromPos, const string_res & search); 229 int findFrom( const string_res & s, size_t fromPos, const char * search); 230 int findFrom( const string_res & s, size_t fromPos, const char * search, size_t searchsize); 231 232 bool includes( const string_res & s, const string_res & search); 233 bool includes( const string_res & s, const char * search); 234 bool includes( const string_res & s, const char * search, size_t searchsize); 235 236 bool startsWith( const string_res & s, const string_res & prefix); 237 bool startsWith( const string_res & s, const char * prefix); 238 bool startsWith( const string_res & s, const char * prefix, size_t prefixsize); 239 240 bool endsWith( const string_res & s, const string_res & suffix); 241 bool endsWith( const string_res & s, const char * suffix); 242 bool endsWith( const string_res & s, const char * suffix, size_t suffixsize); 243 244 int include( const string_res & s, const charclass_res & mask); 245 int exclude( const string_res & s, const charclass_res & mask); 245 246 246 247 // Modifiers
Note:
See TracChangeset
for help on using the changeset viewer.