[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 |
---|
[211def2] | 12 | // Last Modified On : Tue Feb 6 20:59:18 2024 |
---|
| 13 | // Update Count : 118 |
---|
[f450f2f] | 14 | // |
---|
| 15 | |
---|
| 16 | #pragma once |
---|
| 17 | |
---|
| 18 | #include <fstream.hfa> |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | // in string_res.hfa |
---|
| 22 | struct string_res; |
---|
| 23 | struct charclass_res; |
---|
| 24 | |
---|
| 25 | struct string { |
---|
| 26 | string_res * inner; |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | // Getters |
---|
[6264087] | 30 | size_t size(const string & s); |
---|
[681e12f] | 31 | static inline size_t strlen(const string & s) { return size( s ); } |
---|
[f450f2f] | 32 | |
---|
| 33 | // RAII, assignment |
---|
[681e12f] | 34 | void ?{}(string & s); // empty string |
---|
[6264087] | 35 | void ?{}(string & s, const string & s2); |
---|
[7abc3de] | 36 | void ?{}(string & s, const string & s2, size_t maxlen); |
---|
[6264087] | 37 | void ?{}(string & s, string & s2); |
---|
[f450f2f] | 38 | |
---|
[e891349] | 39 | void ?{}(string & s, char); |
---|
[479fbe3] | 40 | void ?{}(string & s, const char * c); // copy from string literal (NULL-terminated) |
---|
| 41 | void ?{}(string & s, const char * c, size_t size); // copy specific length from buffer |
---|
| 42 | |
---|
[f2898df] | 43 | void ?{}( string & s, ssize_t rhs ); |
---|
| 44 | void ?{}( string & s, size_t rhs ); |
---|
| 45 | void ?{}( string & s, double rhs ); |
---|
| 46 | void ?{}( string & s, long double rhs ); |
---|
| 47 | void ?{}( string & s, double _Complex rhs ); |
---|
| 48 | void ?{}( string & s, long double _Complex rhs ); |
---|
| 49 | |
---|
[b1eefe50] | 50 | string & ?=?(string & s, const string & c); |
---|
| 51 | string & ?=?(string & s, string & c); |
---|
| 52 | string & ?=?(string & s, const char * c); // copy from "literal" |
---|
| 53 | string & ?=?(string & s, char c); // copy from 'l' |
---|
| 54 | string & assign(string & s, const string & c, size_t n); |
---|
| 55 | string & assign(string & s, const char * c, size_t n); |
---|
[e891349] | 56 | |
---|
| 57 | static inline string & strcpy(string & s, const char * c) { s = c; return s; } |
---|
| 58 | static inline string & strncpy(string & s, const char * c, size_t n) { assign( s, c, n); return s; } |
---|
| 59 | static inline string & strcpy(string & s, const string & c) { s = c; return s; } |
---|
| 60 | static inline string & strncpy(string & s, const string & c, size_t n) { assign(s, c, n); return s; } |
---|
| 61 | |
---|
[f2898df] | 62 | string & ?=?( string & s, ssize_t rhs ); |
---|
| 63 | string & ?=?( string & s, size_t rhs ); |
---|
| 64 | string & ?=?( string & s, double rhs ); |
---|
| 65 | string & ?=?( string & s, long double rhs ); |
---|
| 66 | string & ?=?( string & s, double _Complex rhs ); |
---|
| 67 | string & ?=?( string & s, long double _Complex rhs ); |
---|
| 68 | |
---|
[6264087] | 69 | void ^?{}(string & s); |
---|
[f450f2f] | 70 | |
---|
| 71 | // Alternate construction: request shared edits |
---|
| 72 | struct string_WithSharedEdits { |
---|
| 73 | string * s; |
---|
| 74 | }; |
---|
[681e12f] | 75 | string_WithSharedEdits ?`shareEdits( string & s ); |
---|
| 76 | void ?{}( string & s, string_WithSharedEdits src ); |
---|
[f450f2f] | 77 | |
---|
| 78 | // IO Operator |
---|
[6264087] | 79 | ofstream & ?|?(ofstream & out, const string & s); |
---|
| 80 | void ?|?(ofstream & out, const string & s); |
---|
| 81 | ifstream & ?|?(ifstream & in, string & s); |
---|
[7e1dbd7] | 82 | |
---|
[34c6e1e6] | 83 | static inline { |
---|
| 84 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; } |
---|
| 85 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; } |
---|
| 86 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; } |
---|
| 87 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; } |
---|
| 88 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; } |
---|
| 89 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; } |
---|
| 90 | _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; } |
---|
| 91 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; } |
---|
| 92 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; } |
---|
| 93 | } // distribution |
---|
| 94 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ); |
---|
| 95 | void ?|?( ofstream & os, _Ostream_Manip(string) ); |
---|
| 96 | |
---|
[211def2] | 97 | struct _Istream_Swidth { |
---|
| 98 | string & s; |
---|
| 99 | inline _Istream_str_base; |
---|
| 100 | }; // _Istream_Swidth |
---|
| 101 | |
---|
| 102 | struct _Istream_Squoted { |
---|
| 103 | _Istream_Swidth sstr; |
---|
| 104 | }; // _Istream_Squoted |
---|
| 105 | |
---|
[34c6e1e6] | 106 | struct _Istream_Sstr { |
---|
[7e1dbd7] | 107 | string & s; |
---|
[38de914] | 108 | inline _Istream_str_base; |
---|
[211def2] | 109 | // _Istream_Swidth sstr; |
---|
[34c6e1e6] | 110 | }; // _Istream_Sstr |
---|
[7e1dbd7] | 111 | |
---|
| 112 | static inline { |
---|
| 113 | // read width does not include null terminator |
---|
[211def2] | 114 | _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s : s, { {.scanset : 0p}, .wd : rwd, {.flags.rwd : true} } }; } |
---|
[34c6e1e6] | 115 | _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { |
---|
[211def2] | 116 | // return (_Istream_Sstr)@{ { .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } } }; |
---|
| 117 | return (_Istream_Sstr)@{ .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } }; |
---|
| 118 | } |
---|
| 119 | _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) { |
---|
| 120 | f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f; |
---|
| 121 | } |
---|
| 122 | _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) { |
---|
| 123 | return (_Istream_Squoted)@{ { .s : s, { {.delimiters : { Ldelimiter, Rdelimiter, '\0' }}, .wd : -1, {.flags.rwd : true} } } }; |
---|
[7e1dbd7] | 124 | } |
---|
[211def2] | 125 | _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { |
---|
| 126 | f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; |
---|
| 127 | return (_Istream_Squoted &)f; |
---|
[7e1dbd7] | 128 | } |
---|
[211def2] | 129 | // _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } } }; } |
---|
| 130 | _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } }; } |
---|
| 131 | _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; } |
---|
| 132 | // _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } } }; } |
---|
| 133 | _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } }; } |
---|
| 134 | _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; } |
---|
| 135 | // _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } } }; } |
---|
| 136 | _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } }; } |
---|
| 137 | _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
| 138 | _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; } |
---|
| 139 | // _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
| 140 | _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
[7e1dbd7] | 141 | } // distribution |
---|
[211def2] | 142 | ifstream & ?|?( ifstream & is, _Istream_Squoted f ); |
---|
[34c6e1e6] | 143 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ); |
---|
[211def2] | 144 | static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; } |
---|
[f450f2f] | 145 | |
---|
| 146 | // Concatenation |
---|
[681e12f] | 147 | void ?+=?(string & s, char c); // append a character |
---|
[6264087] | 148 | void ?+=?(string & s, const string & s2); // append-concatenate to first string |
---|
[e891349] | 149 | void append(string & s, const string & s2, size_t maxlen); // append-concatenate to first string, up to maxlen |
---|
| 150 | void ?+=?(string & s, const char * s2); // append-concatenate NULL-terminated string to first string |
---|
| 151 | void append(string & s, const char * buffer, size_t bsize); // append-concatenate given range to first string |
---|
| 152 | |
---|
[681e12f] | 153 | string ?+?(const string & s, char c); // add a character to a copy of the string |
---|
[6264087] | 154 | string ?+?(const string & s, const string & s2); // copy and concatenate both strings |
---|
[e891349] | 155 | string ?+?(const char * s1, const char * s2); // copy and concatenate both strings |
---|
[681e12f] | 156 | string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string |
---|
[f450f2f] | 157 | |
---|
[e891349] | 158 | static inline string & strcat(string & s, const string & s2) { s += s2; return s; } |
---|
| 159 | static inline string & strcat(string & s, const char * c) { s += c; return s; } |
---|
| 160 | static inline string & strncat(string & s, const string & s2, size_t maxlen) { append(s, s2, maxlen); return s; } |
---|
| 161 | static inline string & strncat(string & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; } |
---|
| 162 | |
---|
[f450f2f] | 163 | // Repetition |
---|
[6264087] | 164 | string ?*?(const string & s, size_t factor); |
---|
[479fbe3] | 165 | void ?*=?(string & s, size_t factor); |
---|
| 166 | string ?*?(char c, size_t factor); |
---|
| 167 | string ?*?(const char *s, size_t factor); |
---|
[f450f2f] | 168 | |
---|
| 169 | // Character access |
---|
[6264087] | 170 | char ?[?](const string & s, size_t index); |
---|
| 171 | string ?[?](string & s, size_t index); // mutable length-1 slice of original |
---|
| 172 | //char codePointAt(const string & s, size_t index); // to revisit under Unicode |
---|
[f450f2f] | 173 | |
---|
| 174 | // Comparisons |
---|
[681e12f] | 175 | int strcmp (const string &, const string &); |
---|
[416b443] | 176 | bool ?==?(const string &, const string &); |
---|
| 177 | bool ?!=?(const string &, const string &); |
---|
| 178 | bool ?>? (const string &, const string &); |
---|
| 179 | bool ?>=?(const string &, const string &); |
---|
| 180 | bool ?<=?(const string &, const string &); |
---|
| 181 | bool ?<? (const string &, const string &); |
---|
| 182 | |
---|
[681e12f] | 183 | int strcmp (const string &, const char *); |
---|
| 184 | bool ?==?(const string &, const char *); |
---|
| 185 | bool ?!=?(const string &, const char *); |
---|
| 186 | bool ?>? (const string &, const char *); |
---|
| 187 | bool ?>=?(const string &, const char *); |
---|
| 188 | bool ?<=?(const string &, const char *); |
---|
| 189 | bool ?<? (const string &, const char *); |
---|
[416b443] | 190 | |
---|
[681e12f] | 191 | int strcmp (const char *, const string &); |
---|
| 192 | bool ?==?(const char *, const string &); |
---|
| 193 | bool ?!=?(const char *, const string &); |
---|
| 194 | bool ?>? (const char *, const string &); |
---|
| 195 | bool ?>=?(const char *, const string &); |
---|
| 196 | bool ?<=?(const char *, const string &); |
---|
| 197 | bool ?<? (const char *, const string &); |
---|
[416b443] | 198 | |
---|
[f450f2f] | 199 | |
---|
| 200 | // Slicing |
---|
[e8b3717] | 201 | string ?()( string & s, size_t start, size_t len ); // TODO const? |
---|
[681e12f] | 202 | string ?()( string & s, size_t start); |
---|
[f450f2f] | 203 | |
---|
| 204 | // String search |
---|
[6264087] | 205 | bool contains(const string & s, char ch); // single character |
---|
[f450f2f] | 206 | |
---|
[6264087] | 207 | int find(const string & s, char search); |
---|
| 208 | int find(const string & s, const string & search); |
---|
| 209 | int find(const string & s, const char * search); |
---|
| 210 | int find(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 211 | |
---|
[6264087] | 212 | int findFrom(const string & s, size_t fromPos, char search); |
---|
| 213 | int findFrom(const string & s, size_t fromPos, const string & search); |
---|
| 214 | int findFrom(const string & s, size_t fromPos, const char * search); |
---|
| 215 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize); |
---|
[08ed947] | 216 | |
---|
[6264087] | 217 | bool includes(const string & s, const string & search); |
---|
| 218 | bool includes(const string & s, const char * search); |
---|
| 219 | bool includes(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 220 | |
---|
[6264087] | 221 | bool startsWith(const string & s, const string & prefix); |
---|
| 222 | bool startsWith(const string & s, const char * prefix); |
---|
| 223 | bool startsWith(const string & s, const char * prefix, size_t prefixsize); |
---|
[f450f2f] | 224 | |
---|
[6264087] | 225 | bool endsWith(const string & s, const string & suffix); |
---|
| 226 | bool endsWith(const string & s, const char * suffix); |
---|
| 227 | bool endsWith(const string & s, const char * suffix, size_t suffixsize); |
---|
[f450f2f] | 228 | |
---|
| 229 | // Modifiers |
---|
[6264087] | 230 | void padStart(string & s, size_t n); |
---|
| 231 | void padStart(string & s, size_t n, char padding); |
---|
| 232 | void padEnd(string & s, size_t n); |
---|
| 233 | void padEnd(string & s, size_t n, char padding); |
---|
[f450f2f] | 234 | |
---|
| 235 | |
---|
| 236 | struct charclass { |
---|
| 237 | charclass_res * inner; |
---|
| 238 | }; |
---|
| 239 | |
---|
| 240 | void ?{}( charclass & ) = void; |
---|
| 241 | void ?{}( charclass &, charclass) = void; |
---|
| 242 | charclass ?=?( charclass &, charclass) = void; |
---|
| 243 | |
---|
| 244 | void ?{}( charclass &, const string & chars); |
---|
| 245 | void ?{}( charclass &, const char * chars ); |
---|
| 246 | void ?{}( charclass &, const char * chars, size_t charssize ); |
---|
| 247 | void ^?{}( charclass & ); |
---|
| 248 | |
---|
[6264087] | 249 | int include(const string & s, const charclass & mask); |
---|
[f450f2f] | 250 | |
---|
[6264087] | 251 | int exclude(const string & s, const charclass & mask); |
---|
[f450f2f] | 252 | |
---|
| 253 | /* |
---|
| 254 | What to do with? |
---|
[6264087] | 255 | StrRet include(string & s, const charclass & mask); |
---|
| 256 | StrRet exclude(string & s, const charclass & mask); |
---|
[f450f2f] | 257 | */ |
---|