[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 -- variable-length, mutable run of text, with value semantics
|
---|
| 8 | //
|
---|
| 9 | // Author : Michael L. Brooks
|
---|
| 10 | // Created On : Fri Sep 03 11:00:00 2021
|
---|
[6264087] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[9018dcf] | 12 | // Last Modified On : Wed Apr 9 22:27:41 2025
|
---|
| 13 | // Update Count : 259
|
---|
[f450f2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <fstream.hfa>
|
---|
[9018dcf] | 19 | #include <string_res.hfa>
|
---|
[f450f2f] | 20 |
|
---|
| 21 | struct string {
|
---|
[4223317] | 22 | string_res * inner;
|
---|
[f450f2f] | 23 | };
|
---|
| 24 |
|
---|
| 25 | // Getters
|
---|
[9018dcf] | 26 | static inline size_t len( const string & s ) { return len( *s.inner ); }
|
---|
[ee70ff5] | 27 | static inline size_t len( const char * cs ) { return strlen( cs ); };
|
---|
| 28 | static inline size_t strlen( const string & s ) { return len( s ); }
|
---|
[f450f2f] | 29 |
|
---|
| 30 | // RAII, assignment
|
---|
[4223317] | 31 | void ?{}( string & s ); // empty string
|
---|
[4dab7e8] | 32 | void ?{}( string & s, const string & s2 );
|
---|
| 33 | void ?{}( string & s, const string & s2, size_t maxlen );
|
---|
| 34 | void ?{}( string & s, string & s2 );
|
---|
[f450f2f] | 35 |
|
---|
[4dab7e8] | 36 | void ?{}( string & s, char );
|
---|
| 37 | void ?{}( string & s, const char * c ); // copy from string literal (NULL-terminated)
|
---|
| 38 | void ?{}( string & s, const char * c, size_t size ); // copy specific length from buffer
|
---|
[479fbe3] | 39 |
|
---|
[f2898df] | 40 | void ?{}( string & s, ssize_t rhs );
|
---|
| 41 | void ?{}( string & s, size_t rhs );
|
---|
| 42 | void ?{}( string & s, double rhs );
|
---|
| 43 | void ?{}( string & s, long double rhs );
|
---|
| 44 | void ?{}( string & s, double _Complex rhs );
|
---|
| 45 | void ?{}( string & s, long double _Complex rhs );
|
---|
| 46 |
|
---|
[4223317] | 47 | // string str( ssize_t rhs );
|
---|
| 48 | // string str( size_t rhs );
|
---|
| 49 | // string str( double rhs );
|
---|
| 50 | // string str( long double rhs );
|
---|
| 51 | // string str( double _Complex rhs );
|
---|
| 52 | // string str( long double _Complex rhs );
|
---|
[4dab7e8] | 53 |
|
---|
| 54 | string & ?=?( string & s, const string & c );
|
---|
| 55 | string & ?=?( string & s, string & c );
|
---|
| 56 | string & ?=?( string & s, const char * c ); // copy from "literal"
|
---|
| 57 | string & ?=?( string & s, char c ); // copy from 'l'
|
---|
| 58 | string & assign( string & s, const string & c, size_t n );
|
---|
| 59 | string & assign( string & s, const char * c, size_t n );
|
---|
| 60 |
|
---|
| 61 | static inline string & strcpy( string & s, const char * c ) { s = c; return s; }
|
---|
| 62 | static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; }
|
---|
| 63 | static inline string & strcpy( string & s, const string & c ) { s = c; return s; }
|
---|
| 64 | static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; }
|
---|
[e891349] | 65 |
|
---|
[f2898df] | 66 | string & ?=?( string & s, ssize_t rhs );
|
---|
| 67 | string & ?=?( string & s, size_t rhs );
|
---|
| 68 | string & ?=?( string & s, double rhs );
|
---|
| 69 | string & ?=?( string & s, long double rhs );
|
---|
| 70 | string & ?=?( string & s, double _Complex rhs );
|
---|
| 71 | string & ?=?( string & s, long double _Complex rhs );
|
---|
| 72 |
|
---|
[4dab7e8] | 73 | void ^?{}( string & s );
|
---|
[f450f2f] | 74 |
|
---|
| 75 | // Alternate construction: request shared edits
|
---|
[8c2723f] | 76 | struct string_Share {
|
---|
[4223317] | 77 | string * s;
|
---|
[f450f2f] | 78 | };
|
---|
[8c2723f] | 79 | string_Share ?`share( string & s );
|
---|
| 80 | void ?{}( string & s, string_Share src );
|
---|
[f450f2f] | 81 |
|
---|
| 82 | // IO Operator
|
---|
[4dab7e8] | 83 | ofstream & ?|?( ofstream & out, const string & s );
|
---|
| 84 | void ?|?( ofstream & out, const string & s );
|
---|
| 85 | ifstream & ?|?( ifstream & in, string & s );
|
---|
[7e1dbd7] | 86 |
|
---|
[34c6e1e6] | 87 | static inline {
|
---|
[1a7203d] | 88 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all = 0 } }; }
|
---|
| 89 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all = 0 } }; }
|
---|
| 90 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all = 0 } }; }
|
---|
| 91 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all = 0 } }; }
|
---|
| 92 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc = true } }; }
|
---|
[34c6e1e6] | 93 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
|
---|
| 94 | _Ostream_Manip(string) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(string) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
|
---|
| 95 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
|
---|
| 96 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
|
---|
| 97 | } // distribution
|
---|
| 98 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f );
|
---|
| 99 | void ?|?( ofstream & os, _Ostream_Manip(string) );
|
---|
| 100 |
|
---|
[211def2] | 101 | struct _Istream_Swidth {
|
---|
| 102 | string & s;
|
---|
| 103 | inline _Istream_str_base;
|
---|
| 104 | }; // _Istream_Swidth
|
---|
| 105 |
|
---|
| 106 | struct _Istream_Squoted {
|
---|
| 107 | _Istream_Swidth sstr;
|
---|
| 108 | }; // _Istream_Squoted
|
---|
| 109 |
|
---|
[34c6e1e6] | 110 | struct _Istream_Sstr {
|
---|
[7e1dbd7] | 111 | string & s;
|
---|
[38de914] | 112 | inline _Istream_str_base;
|
---|
[211def2] | 113 | // _Istream_Swidth sstr;
|
---|
[34c6e1e6] | 114 | }; // _Istream_Sstr
|
---|
[7e1dbd7] | 115 |
|
---|
| 116 | static inline {
|
---|
| 117 | // read width does not include null terminator
|
---|
[1a7203d] | 118 | _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; }
|
---|
[34c6e1e6] | 119 | _Istream_Sstr getline( string & s, const char delimiter = '\n' ) {
|
---|
[1a7203d] | 120 | // return (_Istream_Sstr)@{ { .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } } };
|
---|
| 121 | return (_Istream_Sstr)@{ .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } };
|
---|
[211def2] | 122 | }
|
---|
| 123 | _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) {
|
---|
| 124 | f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f;
|
---|
| 125 | }
|
---|
| 126 | _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) {
|
---|
[1a7203d] | 127 | return (_Istream_Squoted)@{ { .s = s, { {.delimiters = { Ldelimiter, Rdelimiter, '\0' }}, .wd = -1, {.flags.rwd = true} } } };
|
---|
[7e1dbd7] | 128 | }
|
---|
[211def2] | 129 | _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) {
|
---|
| 130 | f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0';
|
---|
| 131 | return (_Istream_Squoted &)f;
|
---|
[7e1dbd7] | 132 | }
|
---|
[1a7203d] | 133 | // _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } } }; }
|
---|
| 134 | _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } }; }
|
---|
[211def2] | 135 | _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; }
|
---|
[1a7203d] | 136 | // _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } } }; }
|
---|
| 137 | _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } }; }
|
---|
[211def2] | 138 | _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; }
|
---|
[1a7203d] | 139 | // _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } } }; }
|
---|
| 140 | _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } }; }
|
---|
[211def2] | 141 | _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
|
---|
| 142 | _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; }
|
---|
| 143 | // _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; }
|
---|
| 144 | _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
|
---|
[7e1dbd7] | 145 | } // distribution
|
---|
[211def2] | 146 | ifstream & ?|?( ifstream & is, _Istream_Squoted f );
|
---|
[34c6e1e6] | 147 | ifstream & ?|?( ifstream & is, _Istream_Sstr f );
|
---|
[211def2] | 148 | static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
|
---|
[f450f2f] | 149 |
|
---|
| 150 | // Concatenation
|
---|
[6b765d5] | 151 | void ?+=?( string & s, char c );
|
---|
| 152 | void ?+=?( string & s, const string & s2 );
|
---|
| 153 | void append( string & s, const string & s2, size_t maxlen );
|
---|
| 154 | void ?+=?( string & s, const char * s2 );
|
---|
| 155 | void append( string & s, const char * buffer, size_t bsize );
|
---|
| 156 |
|
---|
| 157 | string ?+?( const string & s, char c );
|
---|
| 158 | string ?+?( char c, const string & s );
|
---|
| 159 | string ?+?( const string & s, const string & s2 );
|
---|
| 160 | string ?+?( const char * s, char c ); // not backwards compatible
|
---|
| 161 | string ?+?( char c, const char * s );
|
---|
| 162 | string ?+?( const char * c, const char * s );
|
---|
| 163 | string ?+?( const char * c, const string & s );
|
---|
| 164 | string ?+?( const string & s, const char * c );
|
---|
| 165 | string ?+?( char, char ); // not being called 8-(
|
---|
[4dab7e8] | 166 |
|
---|
| 167 | static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; }
|
---|
| 168 | static inline string & strcat( string & s, const char * c ) { s += c; return s; }
|
---|
| 169 | static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; }
|
---|
| 170 | static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; }
|
---|
[e891349] | 171 |
|
---|
[f450f2f] | 172 | // Repetition
|
---|
[4dab7e8] | 173 | void ?*=?( string & s, size_t factor );
|
---|
[6b765d5] | 174 | string ?*?( char c, size_t factor ); // not backwards compatible
|
---|
| 175 | string ?*?( const string & s, size_t factor );
|
---|
| 176 | static inline string ?*?( size_t factor, const string & s ) { return s * factor; }
|
---|
| 177 | string ?*?( const char * s, size_t factor );
|
---|
| 178 | static inline string ?*?( size_t factor, const char * s ) { return s * factor; }
|
---|
[f450f2f] | 179 |
|
---|
| 180 | // Character access
|
---|
[4dab7e8] | 181 | char ?[?]( const string & s, size_t index );
|
---|
| 182 | string ?[?]( string & s, size_t index ); // mutable length-1 slice of original
|
---|
| 183 | //char codePointAt(const string & s, size_t index ); // to revisit under Unicode
|
---|
[f450f2f] | 184 |
|
---|
| 185 | // Comparisons
|
---|
[c4d6c90] | 186 | int strcmp ( const string &, const string & );
|
---|
| 187 | bool ?==?( const string &, const string & );
|
---|
| 188 | bool ?!=?( const string &, const string & );
|
---|
| 189 | bool ?>? ( const string &, const string & );
|
---|
| 190 | bool ?>=?( const string &, const string & );
|
---|
| 191 | bool ?<=?( const string &, const string & );
|
---|
| 192 | bool ?<? ( const string &, const string & );
|
---|
| 193 |
|
---|
| 194 | int strcmp( const string &, const char * );
|
---|
| 195 | bool ?==?( const string &, const char * );
|
---|
| 196 | bool ?!=?( const string &, const char * );
|
---|
| 197 | bool ?>? ( const string &, const char * );
|
---|
| 198 | bool ?>=?( const string &, const char * );
|
---|
| 199 | bool ?<=?( const string &, const char * );
|
---|
| 200 | bool ?<? ( const string &, const char * );
|
---|
| 201 |
|
---|
| 202 | int strcmp( const char *, const string & );
|
---|
| 203 | bool ?==?( const char *, const string & );
|
---|
| 204 | bool ?!=?( const char *, const string & );
|
---|
| 205 | bool ?>? ( const char *, const string & );
|
---|
| 206 | bool ?>=?( const char *, const string & );
|
---|
| 207 | bool ?<=?( const char *, const string & );
|
---|
| 208 | bool ?<? ( const char *, const string & );
|
---|
[416b443] | 209 |
|
---|
[f450f2f] | 210 | // String search
|
---|
[4dab7e8] | 211 | bool contains( const string & s, char ch ); // single character
|
---|
[f450f2f] | 212 |
|
---|
[9018dcf] | 213 | //int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen );
|
---|
| 214 | size_t find$( const string_res & s, size_t start, size_t len, const string & key_res, size_t kstart, size_t klen );
|
---|
[f450f2f] | 215 |
|
---|
[9018dcf] | 216 | size_t find( const string & s, char key );
|
---|
| 217 | size_t find( const string & s, const char * key );
|
---|
| 218 | size_t find( const string & s, const string & key );
|
---|
| 219 | size_t find( const string & s, const char * key, size_t keysize );
|
---|
| 220 |
|
---|
| 221 | size_t find( const string & s, size_t start, char key );
|
---|
| 222 | size_t find( const string & s, size_t start, const string & key );
|
---|
| 223 | size_t find( const string & s, size_t start, const char * key );
|
---|
| 224 | size_t find( const string & s, size_t start, const char * key, size_t keysize );
|
---|
| 225 | static inline ?^?( const string & key, const string & s ) { return find( s, key ); }
|
---|
| 226 | static inline ?^?( const char * key, const string & s ) { return find( s, key ); }
|
---|
[08ed947] | 227 |
|
---|
[ed5023d1] | 228 | bool includes( const string & s, const string & mask );
|
---|
| 229 | bool includes( const string & s, const char * mask );
|
---|
| 230 | bool includes( const string & s, const char * mask, size_t masksize );
|
---|
[f450f2f] | 231 |
|
---|
[4dab7e8] | 232 | bool startsWith( const string & s, const string & prefix );
|
---|
| 233 | bool startsWith( const string & s, const char * prefix );
|
---|
| 234 | bool startsWith( const string & s, const char * prefix, size_t prefixsize );
|
---|
[f450f2f] | 235 |
|
---|
[4dab7e8] | 236 | bool endsWith( const string & s, const string & suffix );
|
---|
| 237 | bool endsWith( const string & s, const char * suffix );
|
---|
| 238 | bool endsWith( const string & s, const char * suffix, size_t suffixsize );
|
---|
[f450f2f] | 239 |
|
---|
[ed5023d1] | 240 | // Slicing
|
---|
[9018dcf] | 241 | string ?()( string & s, ssize_t start, ssize_t len );
|
---|
| 242 | static inline string ?()( const string & s, ssize_t start, ssize_t len ) { string & w = (string &)s; return w( start, len ); } // FIX ME
|
---|
[ed5023d1] | 243 | string ?()( string & s, ssize_t start );
|
---|
[9018dcf] | 244 | static inline string ?()( const string & s, ssize_t start ) { string & w = (string &)s; return w( start ); } // FIX ME
|
---|
[ed5023d1] | 245 | static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
|
---|
[9018dcf] | 246 | static inline string ?()( const string & s, char m ) { string & w = (string &)s; return w( find( s, m ), 1 )`share; } // FIX ME
|
---|
[ed5023d1] | 247 | static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
|
---|
[9018dcf] | 248 | static inline string ?()( const string & s, const char * m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
|
---|
[ed5023d1] | 249 | static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
|
---|
[9018dcf] | 250 | static inline string ?()( const string & s, const string & m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
|
---|
[f450f2f] | 251 |
|
---|
| 252 | struct charclass {
|
---|
[4223317] | 253 | charclass_res * inner;
|
---|
[f450f2f] | 254 | };
|
---|
| 255 |
|
---|
| 256 | void ?{}( charclass & ) = void;
|
---|
[4dab7e8] | 257 | void ?{}( charclass &, charclass ) = void;
|
---|
| 258 | charclass ?=?( charclass &, charclass ) = void;
|
---|
[f450f2f] | 259 |
|
---|
[4dab7e8] | 260 | void ?{}( charclass &, const string & chars );
|
---|
[f450f2f] | 261 | void ?{}( charclass &, const char * chars );
|
---|
| 262 | void ?{}( charclass &, const char * chars, size_t charssize );
|
---|
| 263 | void ^?{}( charclass & );
|
---|
| 264 |
|
---|
[9018dcf] | 265 | size_t include( const string & s, const charclass & mask );
|
---|
| 266 | static inline size_t include( const char * s, const charclass & mask ) { string temp = s; return include( temp, mask ); }
|
---|
| 267 | static inline string include( const string & s, const charclass & mask ) { ssize_t i = include( s, mask ); return s( 0, i )`share; }
|
---|
| 268 | static inline string include( const char * s, const charclass & mask ) { string temp = s; ssize_t i = include( temp, mask ); return temp( 0, i ); }
|
---|
| 269 |
|
---|
| 270 | size_t exclude( const string & s, const charclass & mask );
|
---|
| 271 | static inline size_t exclude( const char * s, const charclass & mask ) { string temp = s; return exclude( temp, mask ); }
|
---|
| 272 | static inline string exclude( const string & s, const charclass & mask ) { ssize_t i = exclude( s, mask ); return s( 0, i )`share; }
|
---|
| 273 | static inline string exclude( const char * s, const charclass & mask ) { string temp = s; ssize_t i = exclude( temp, mask ); return temp( 0, i ); }
|
---|
| 274 |
|
---|
| 275 | size_t test( const string & s, int (*f)( int ) );
|
---|
| 276 | static inline size_t test( const char * c, int (*f)( int ) ) {
|
---|
| 277 | const string S = c;
|
---|
| 278 | return test( S, f );
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | string replace( string & s, const string & from, const string & to );
|
---|
| 282 | static 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 | }
|
---|
| 286 | static inline string replace( string & s, const char * from, const char * to ) {
|
---|
| 287 | string From = from, To = to;
|
---|
| 288 | return replace( s, From, To );
|
---|
| 289 | }
|
---|
| 290 | static inline string replace( string & s, const char * from, const string & to ) {
|
---|
| 291 | string From = from;
|
---|
| 292 | return replace( s, From, to );
|
---|
| 293 | }
|
---|
| 294 | static inline string replace( string & s, string & from, const char * to ) {
|
---|
| 295 | string To = to;
|
---|
| 296 | return replace( s, from, To );
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | string translate( const string & s, int (*f)( int ) );
|
---|
| 300 | static inline string translate( const char * c, int (*f)( int ) ) {
|
---|
| 301 | const string S = c;
|
---|
| 302 | return translate( S, f );
|
---|
| 303 | }
|
---|