[f450f2f] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // string_res -- variable-length, mutable run of text, with resource semantics
|
---|
| 8 | //
|
---|
| 9 | // Author : Michael L. Brooks
|
---|
| 10 | // Created On : Fri Sep 03 11:00:00 2021
|
---|
[9ca5e56] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[5ad6f0d] | 12 | // Last Modified On : Sun Apr 13 21:03:37 2025
|
---|
| 13 | // Update Count : 79
|
---|
[f450f2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
[3f631d6] | 18 | #include <iostream.hfa>
|
---|
[4e8df745] | 19 | #include <string.h> // e.g. strlen
|
---|
[f450f2f] | 20 |
|
---|
[ed5023d1] | 21 |
|
---|
[f450f2f] | 22 | //######################### HandleNode #########################
|
---|
| 23 | //private
|
---|
| 24 |
|
---|
| 25 | struct VbyteHeap;
|
---|
| 26 |
|
---|
| 27 | struct HandleNode {
|
---|
[ed5023d1] | 28 | HandleNode * flink; // forward link
|
---|
| 29 | HandleNode * blink; // backward link
|
---|
| 30 | VbyteHeap * ulink; // upward link
|
---|
[f450f2f] | 31 |
|
---|
[ed5023d1] | 32 | char * s; // pointer to byte string
|
---|
| 33 | unsigned int lnth; // length of byte string
|
---|
[f450f2f] | 34 | }; // HandleNode
|
---|
| 35 |
|
---|
[0f781fb8] | 36 | VbyteHeap * DEBUG_string_heap();
|
---|
[7b0e8b7] | 37 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap );
|
---|
[6cc87c0] | 38 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap );
|
---|
| 39 | const char * DEBUG_string_heap_start( VbyteHeap * heap );
|
---|
| 40 |
|
---|
[08ed947] | 41 | void TUNING_set_string_heap_liveness_threshold( double val );
|
---|
[f450f2f] | 42 |
|
---|
| 43 | //######################### String #########################
|
---|
| 44 |
|
---|
| 45 | // A dynamically-sized string
|
---|
| 46 | struct string_res {
|
---|
[ed5023d1] | 47 | HandleNode Handle; // chars, start, end, global neighbours
|
---|
| 48 | bool shareSet_owns_ulink;
|
---|
| 49 | string_res * shareSet_prev;
|
---|
| 50 | string_res * shareSet_next;
|
---|
[f450f2f] | 51 | };
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | //######################### charclass_res #########################
|
---|
| 55 |
|
---|
| 56 | struct charclass_res {
|
---|
[ed5023d1] | 57 | string_res chars;
|
---|
[f450f2f] | 58 | };
|
---|
| 59 |
|
---|
| 60 | void ?{}( charclass_res & ) = void;
|
---|
| 61 | void ?{}( charclass_res &, charclass_res) = void;
|
---|
| 62 | charclass_res ?=?( charclass_res &, charclass_res) = void;
|
---|
| 63 | void ?{}( charclass_res &, const string_res & chars);
|
---|
| 64 | void ?{}( charclass_res &, const char * chars );
|
---|
| 65 | void ?{}( charclass_res &, const char * chars, size_t charssize );
|
---|
| 66 | void ^?{}( charclass_res & );
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | //######################### String #########################
|
---|
| 70 |
|
---|
| 71 | // Getters
|
---|
[9018dcf] | 72 | static inline size_t len( const string_res & s ) { return s.Handle.lnth; }
|
---|
[f450f2f] | 73 |
|
---|
| 74 | // Constructors, Assignment Operators, Destructor
|
---|
[681e12f] | 75 | void ?{}(string_res & s); // empty string
|
---|
| 76 | void ?{}(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
|
---|
| 77 | static inline void ?{}(string_res & s, const char * rhs) { // copy from string literal (NULL-terminated)
|
---|
[ed5023d1] | 78 | (s){ rhs, strlen(rhs) };
|
---|
[4e8df745] | 79 | }
|
---|
[7abc3de] | 80 | static inline void ?{}(string_res & s, char c ) {
|
---|
[ed5023d1] | 81 | ?{}( s, &c, 1);
|
---|
[7abc3de] | 82 | }
|
---|
[f450f2f] | 83 |
|
---|
[7abc3de] | 84 | // Deleting the copy constructors makes the compiler reject an attempt to call/return by value
|
---|
[681e12f] | 85 | void ?{}(string_res & s, const string_res & s2) = void;
|
---|
| 86 | void ?{}(string_res & s, string_res & s2) = void;
|
---|
[f450f2f] | 87 |
|
---|
| 88 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS };
|
---|
[e8b3717] | 89 | void ?{}(string_res & s, const string_res & src, StrResInitMode, size_t start, size_t len );
|
---|
[681e12f] | 90 | static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode ) {
|
---|
[ed5023d1] | 91 | ?{}( s, src, mode, 0, len(src));
|
---|
[f450f2f] | 92 | }
|
---|
[7abc3de] | 93 | static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode, size_t maxlen ) {
|
---|
[ed5023d1] | 94 | ?{}( s, src, mode, 0, (len(src) > maxlen)?maxlen:len(src) );
|
---|
[7abc3de] | 95 | }
|
---|
[f2898df] | 96 | void ?{}( string_res & s, ssize_t rhs );
|
---|
| 97 | void ?{}( string_res & s, size_t rhs );
|
---|
| 98 | void ?{}( string_res & s, double rhs );
|
---|
| 99 | void ?{}( string_res & s, long double rhs );
|
---|
| 100 | void ?{}( string_res & s, double _Complex rhs );
|
---|
| 101 | void ?{}( string_res & s, long double _Complex rhs );
|
---|
[f450f2f] | 102 |
|
---|
[e891349] | 103 | string_res & assign(string_res & s, const string_res & src, size_t maxlen); // copy specific length from other string
|
---|
[681e12f] | 104 | string_res & assign(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
|
---|
| 105 | static inline string_res & ?=?(string_res & s, const char * c) { // copy from string literal (NULL-terminated)
|
---|
[ed5023d1] | 106 | return assign(s, c, strlen( c));
|
---|
[4e8df745] | 107 | }
|
---|
[681e12f] | 108 | string_res & ?=?(string_res & s, const string_res & c);
|
---|
| 109 | string_res & ?=?(string_res & s, string_res & c);
|
---|
| 110 | string_res & ?=?(string_res & s, char c);
|
---|
[f450f2f] | 111 |
|
---|
[f2898df] | 112 | string_res & ?=?( string_res & s, ssize_t rhs );
|
---|
| 113 | string_res & ?=?( string_res & s, size_t rhs );
|
---|
| 114 | string_res & ?=?( string_res & s, double rhs );
|
---|
| 115 | string_res & ?=?( string_res & s, long double rhs );
|
---|
| 116 | string_res & ?=?( string_res & s, double _Complex rhs );
|
---|
| 117 | string_res & ?=?( string_res & s, long double _Complex rhs );
|
---|
| 118 |
|
---|
[681e12f] | 119 | void ^?{}(string_res & s);
|
---|
[f450f2f] | 120 |
|
---|
| 121 | // IO Operator
|
---|
[3f631d6] | 122 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
| 123 | ostype & ?|?(ostype & out, const string_res & s);
|
---|
| 124 | void ?|?(ostype & out, const string_res & s);
|
---|
| 125 | }
|
---|
| 126 | forall( istype & | basic_istream( istype ) )
|
---|
| 127 | istype & ?|?(istype & in, string_res & s);
|
---|
[211def2] | 128 |
|
---|
| 129 | struct _Istream_Rwidth {
|
---|
| 130 | string_res * s;
|
---|
| 131 | inline _Istream_str_base;
|
---|
| 132 | }; // _Istream_Rwidth
|
---|
| 133 |
|
---|
[30548de] | 134 | struct _Istream_Rquote {
|
---|
[211def2] | 135 | // string_res * s;
|
---|
| 136 | // inline _Istream_str_base;
|
---|
| 137 | _Istream_Rwidth rstr;
|
---|
[30548de] | 138 | }; // _Istream_Rquote
|
---|
[ff56dd2e] | 139 |
|
---|
| 140 | struct _Istream_Rstr {
|
---|
| 141 | string_res * s;
|
---|
| 142 | inline _Istream_str_base;
|
---|
[211def2] | 143 | // _Istream_Rwidth rstr;
|
---|
[ff56dd2e] | 144 | }; // _Istream_Rstr
|
---|
| 145 |
|
---|
| 146 | static inline {
|
---|
| 147 | // read width does not include null terminator
|
---|
[1a7203d] | 148 | _Istream_Rwidth wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rwidth)@{ .s = &s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; }
|
---|
[ff56dd2e] | 149 | _Istream_Rstr getline( string_res & s, const char delimiter = '\n' ) {
|
---|
[1a7203d] | 150 | return (_Istream_Rstr)@{ .s = &s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } };
|
---|
[211def2] | 151 | }
|
---|
| 152 | _Istream_Rstr & getline( _Istream_Rwidth & f, const char delimiter = '\n' ) {
|
---|
| 153 | f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Rstr &)f;
|
---|
| 154 | }
|
---|
[30548de] | 155 | _Istream_Rquote quote( string_res & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) {
|
---|
| 156 | return (_Istream_Rquote)@{ { .s = &s, { {.delimiters = { Ldelimiter, Rdelimiter, '\0' }}, .wd = -1, {.flags.rwd = true} } } };
|
---|
[ff56dd2e] | 157 | }
|
---|
[30548de] | 158 | _Istream_Rquote & quote( _Istream_Rwidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) {
|
---|
[211def2] | 159 | f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0';
|
---|
[30548de] | 160 | return (_Istream_Rquote &)f;
|
---|
[ff56dd2e] | 161 | }
|
---|
[1a7203d] | 162 | _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ .s = &s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } }; }
|
---|
[211def2] | 163 | _Istream_Rstr & incl( const char scanset[], _Istream_Rwidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Rstr &)f; }
|
---|
[1a7203d] | 164 | _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ .s = &s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } }; }
|
---|
[211def2] | 165 | _Istream_Rstr & excl( const char scanset[], _Istream_Rwidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Rstr &)f; }
|
---|
[1a7203d] | 166 | _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ .s = &s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } }; }
|
---|
[211def2] | 167 | _Istream_Rstr & ignore( _Istream_Rwidth & f ) { f.flags.ignore = true; return (_Istream_Rstr &)f; }
|
---|
[30548de] | 168 | _Istream_Rquote & ignore( _Istream_Rquote & f ) { f.rstr.flags.ignore = true; return (_Istream_Rquote &)f; }
|
---|
[211def2] | 169 | _Istream_Rstr & ignore( _Istream_Rstr & f ) { f.flags.ignore = true; return (_Istream_Rstr &)f; }
|
---|
[ff56dd2e] | 170 | } // distribution
|
---|
[3f631d6] | 171 | forall( istype & | basic_istream( istype ) ) {
|
---|
[30548de] | 172 | istype & ?|?( istype & is, _Istream_Rquote f );
|
---|
[3f631d6] | 173 | istype & ?|?( istype & is, _Istream_Rstr f );
|
---|
| 174 | static inline istype & ?|?( istype & is, _Istream_Rwidth f ) { return is | *(_Istream_Rstr *)&f; }
|
---|
| 175 | }
|
---|
[f450f2f] | 176 |
|
---|
| 177 | // Concatenation
|
---|
[ed5023d1] | 178 | void append( string_res & s, const char * buffer, size_t bsize );
|
---|
| 179 | void append( string_res & s, const string_res & s2, size_t maxlen );
|
---|
| 180 | static inline void ?+=?( string_res & s, const string_res & s2 ) { append( s, s2.Handle.s, s2.Handle.lnth ); }
|
---|
| 181 | static inline void ?+=?( string_res & s, char c ) { append( s, & c, 1 ); }
|
---|
| 182 | static inline void ?+=?( string_res & s, const char * c ) { append( s, c, strlen( c ) ); }
|
---|
| 183 | static inline string_res & strcat( string_res & s, const string_res & s2 ) { s += s2; return s; }
|
---|
| 184 | static inline string_res & strcat( string_res & s, const char * c ) { s += c; return s; }
|
---|
| 185 | static inline string_res & strncat( string_res & s, const string_res & s2, size_t maxlen ) { append(s, s2, maxlen); return s; }
|
---|
| 186 | static inline string_res & strncat( string_res & s, const char * buffer, size_t bsize ) { append(s, buffer, bsize); return s; }
|
---|
[f450f2f] | 187 |
|
---|
[38951c31] | 188 | // Repetition
|
---|
| 189 | void ?*=?(string_res & s, size_t factor);
|
---|
| 190 |
|
---|
[f450f2f] | 191 | // Character access
|
---|
[ed5023d1] | 192 | void assignAt( const string_res & s, size_t index, char val);
|
---|
| 193 | char ?[?]( const string_res & s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
|
---|
| 194 | //char codePointAt( const string_res & s, size_t index); // revisit under Unicode
|
---|
[f450f2f] | 195 |
|
---|
| 196 | // Comparisons
|
---|
[ed5023d1] | 197 | int strcmp$( const char * s1, size_t l1, const char * s2, size_t l2 );
|
---|
| 198 |
|
---|
| 199 | 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 ); }
|
---|
| 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 | static inline bool ?>? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) > 0; }
|
---|
| 203 | static inline bool ?>=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; }
|
---|
| 204 | static inline bool ?<=?( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; }
|
---|
| 205 | static inline bool ?<? ( const string_res & s1, const string_res & s2 ) { return strcmp( s1, s2 ) < 0; }
|
---|
| 206 |
|
---|
| 207 | static inline int strcmp( const string_res & s1, const char * s2 ) { return strcmp$( s1.Handle.s, s1.Handle.lnth, s2, strlen( s2 ) ); }
|
---|
| 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 | static inline bool ?>? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) > 0; }
|
---|
| 211 | static inline bool ?>=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) >= 0; }
|
---|
| 212 | static inline bool ?<=?( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) <= 0; }
|
---|
| 213 | static inline bool ?<? ( const string_res & s1, const char * s2 ) { return strcmp( s1, s2 ) < 0; }
|
---|
| 214 |
|
---|
| 215 | static inline int strcmp( const char * s1, const string_res & s2 ) { return strcmp$( s1, strlen( s1 ), s2.Handle.s, s2.Handle.lnth ); }
|
---|
| 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; }
|
---|
| 218 | static inline bool ?>? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) > 0; }
|
---|
| 219 | static inline bool ?>=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) >= 0; }
|
---|
| 220 | static inline bool ?<=?( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) <= 0; }
|
---|
| 221 | static inline bool ?<? ( const char * s1, const string_res & s2 ) { return strcmp( s1, s2 ) < 0; }
|
---|
[f450f2f] | 222 |
|
---|
| 223 | // String search
|
---|
[ed5023d1] | 224 | bool contains( const string_res & s, char ch); // single character
|
---|
[f450f2f] | 225 |
|
---|
[9018dcf] | 226 | int find$( const string_res & s, ssize_t start, ssize_t len, const string_res & key, ssize_t kstart, ssize_t klen );
|
---|
| 227 |
|
---|
[ed5023d1] | 228 | int find( const string_res & s, char search);
|
---|
| 229 | int find( const string_res & s, const string_res & search);
|
---|
| 230 | int find( const string_res & s, const char * search);
|
---|
| 231 | int find( const string_res & s, const char * search, size_t searchsize);
|
---|
[f450f2f] | 232 |
|
---|
[ed5023d1] | 233 | int findFrom( const string_res & s, size_t fromPos, char search);
|
---|
| 234 | int findFrom( const string_res & s, size_t fromPos, const string_res & search);
|
---|
| 235 | int findFrom( const string_res & s, size_t fromPos, const char * search);
|
---|
| 236 | int findFrom( const string_res & s, size_t fromPos, const char * search, size_t searchsize);
|
---|
[08ed947] | 237 |
|
---|
[ed5023d1] | 238 | bool includes( const string_res & s, const string_res & search);
|
---|
| 239 | bool includes( const string_res & s, const char * search);
|
---|
| 240 | bool includes( const string_res & s, const char * search, size_t searchsize);
|
---|
[f450f2f] | 241 |
|
---|
[ed5023d1] | 242 | bool startsWith( const string_res & s, const string_res & prefix);
|
---|
| 243 | bool startsWith( const string_res & s, const char * prefix);
|
---|
| 244 | bool startsWith( const string_res & s, const char * prefix, size_t prefixsize);
|
---|
[f450f2f] | 245 |
|
---|
[ed5023d1] | 246 | bool endsWith( const string_res & s, const string_res & suffix);
|
---|
| 247 | bool endsWith( const string_res & s, const char * suffix);
|
---|
| 248 | bool endsWith( const string_res & s, const char * suffix, size_t suffixsize);
|
---|
[f450f2f] | 249 |
|
---|
[ed5023d1] | 250 | int include( const string_res & s, const charclass_res & mask);
|
---|
| 251 | int exclude( const string_res & s, const charclass_res & mask);
|
---|
[f450f2f] | 252 |
|
---|