[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 |
---|
[479fbe3] | 12 | // Last Modified On : Sun Jan 14 12:03:46 2024 |
---|
| 13 | // Update Count : 81 |
---|
[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 | |
---|
[681e12f] | 43 | void ?=?(string & s, const char * c); // copy assignment from literal |
---|
| 44 | void ?=?(string & s, const string & c); |
---|
| 45 | void ?=?(string & s, char c); |
---|
| 46 | string & ?=?(string & s, string & c); // surprising ret seems to help avoid calls to autogen |
---|
[e891349] | 47 | void assign(string & s, const string & c, size_t n); |
---|
| 48 | void assign(string & s, const char * c, size_t n); |
---|
[4b3b352] | 49 | //string ?=?( string &, string ) = void; |
---|
[e891349] | 50 | |
---|
| 51 | static inline string & strcpy(string & s, const char * c) { s = c; return s; } |
---|
| 52 | static inline string & strncpy(string & s, const char * c, size_t n) { assign( s, c, n); return s; } |
---|
| 53 | static inline string & strcpy(string & s, const string & c) { s = c; return s; } |
---|
| 54 | static inline string & strncpy(string & s, const string & c, size_t n) { assign(s, c, n); return s; } |
---|
| 55 | |
---|
[6264087] | 56 | void ^?{}(string & s); |
---|
[f450f2f] | 57 | |
---|
| 58 | // Alternate construction: request shared edits |
---|
| 59 | struct string_WithSharedEdits { |
---|
| 60 | string * s; |
---|
| 61 | }; |
---|
[681e12f] | 62 | string_WithSharedEdits ?`shareEdits( string & s ); |
---|
| 63 | void ?{}( string & s, string_WithSharedEdits src ); |
---|
[f450f2f] | 64 | |
---|
| 65 | // IO Operator |
---|
[6264087] | 66 | ofstream & ?|?(ofstream & out, const string & s); |
---|
| 67 | void ?|?(ofstream & out, const string & s); |
---|
| 68 | ifstream & ?|?(ifstream & in, string & s); |
---|
[681e12f] | 69 | void ?|?( ifstream & in, string & s ); |
---|
[7e1dbd7] | 70 | |
---|
[34c6e1e6] | 71 | static inline { |
---|
| 72 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; } |
---|
| 73 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; } |
---|
| 74 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; } |
---|
| 75 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; } |
---|
| 76 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; } |
---|
| 77 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; } |
---|
| 78 | _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; } |
---|
| 79 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; } |
---|
| 80 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; } |
---|
| 81 | } // distribution |
---|
| 82 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ); |
---|
| 83 | void ?|?( ofstream & os, _Ostream_Manip(string) ); |
---|
| 84 | |
---|
| 85 | struct _Istream_Sstr { |
---|
[7e1dbd7] | 86 | string & s; |
---|
[38de914] | 87 | inline _Istream_str_base; |
---|
[34c6e1e6] | 88 | }; // _Istream_Sstr |
---|
[7e1dbd7] | 89 | |
---|
| 90 | static inline { |
---|
| 91 | // read width does not include null terminator |
---|
[34c6e1e6] | 92 | _Istream_Sstr wdi( unsigned int rwd, string & s ) { return (_Istream_Sstr)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; } |
---|
| 93 | _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { |
---|
[681e12f] | 94 | return (_Istream_Sstr)@{ s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} }; |
---|
[7e1dbd7] | 95 | } |
---|
[34c6e1e6] | 96 | _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) { |
---|
[681e12f] | 97 | fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; |
---|
[7e1dbd7] | 98 | } |
---|
[34c6e1e6] | 99 | _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : false}} }; } |
---|
| 100 | _Istream_Sstr & incl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } |
---|
| 101 | _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : true}} }; } |
---|
| 102 | _Istream_Sstr & excl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } |
---|
| 103 | _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ s, {{0p}, -1, {.flags.ignore : true}} }; } |
---|
| 104 | _Istream_Sstr & ignore( _Istream_Sstr & fmt ) { fmt.flags.ignore = true; return fmt; } |
---|
[7e1dbd7] | 105 | } // distribution |
---|
[34c6e1e6] | 106 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ); |
---|
| 107 | void ?|?( ifstream & is, _Istream_Sstr t ); |
---|
[f450f2f] | 108 | |
---|
| 109 | // Concatenation |
---|
[681e12f] | 110 | void ?+=?(string & s, char c); // append a character |
---|
[6264087] | 111 | void ?+=?(string & s, const string & s2); // append-concatenate to first string |
---|
[e891349] | 112 | void append(string & s, const string & s2, size_t maxlen); // append-concatenate to first string, up to maxlen |
---|
| 113 | void ?+=?(string & s, const char * s2); // append-concatenate NULL-terminated string to first string |
---|
| 114 | void append(string & s, const char * buffer, size_t bsize); // append-concatenate given range to first string |
---|
| 115 | |
---|
[681e12f] | 116 | string ?+?(const string & s, char c); // add a character to a copy of the string |
---|
[6264087] | 117 | string ?+?(const string & s, const string & s2); // copy and concatenate both strings |
---|
[e891349] | 118 | string ?+?(const char * s1, const char * s2); // copy and concatenate both strings |
---|
[681e12f] | 119 | string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string |
---|
[f450f2f] | 120 | |
---|
[e891349] | 121 | static inline string & strcat(string & s, const string & s2) { s += s2; return s; } |
---|
| 122 | static inline string & strcat(string & s, const char * c) { s += c; return s; } |
---|
| 123 | static inline string & strncat(string & s, const string & s2, size_t maxlen) { append(s, s2, maxlen); return s; } |
---|
| 124 | static inline string & strncat(string & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; } |
---|
| 125 | |
---|
[f450f2f] | 126 | // Repetition |
---|
[6264087] | 127 | string ?*?(const string & s, size_t factor); |
---|
[479fbe3] | 128 | void ?*=?(string & s, size_t factor); |
---|
| 129 | string ?*?(char c, size_t factor); |
---|
| 130 | string ?*?(const char *s, size_t factor); |
---|
[f450f2f] | 131 | |
---|
| 132 | // Character access |
---|
[6264087] | 133 | char ?[?](const string & s, size_t index); |
---|
| 134 | string ?[?](string & s, size_t index); // mutable length-1 slice of original |
---|
| 135 | //char codePointAt(const string & s, size_t index); // to revisit under Unicode |
---|
[f450f2f] | 136 | |
---|
| 137 | // Comparisons |
---|
[681e12f] | 138 | int strcmp (const string &, const string &); |
---|
[416b443] | 139 | bool ?==?(const string &, const string &); |
---|
| 140 | bool ?!=?(const string &, const string &); |
---|
| 141 | bool ?>? (const string &, const string &); |
---|
| 142 | bool ?>=?(const string &, const string &); |
---|
| 143 | bool ?<=?(const string &, const string &); |
---|
| 144 | bool ?<? (const string &, const string &); |
---|
| 145 | |
---|
[681e12f] | 146 | int strcmp (const string &, const char *); |
---|
| 147 | bool ?==?(const string &, const char *); |
---|
| 148 | bool ?!=?(const string &, const char *); |
---|
| 149 | bool ?>? (const string &, const char *); |
---|
| 150 | bool ?>=?(const string &, const char *); |
---|
| 151 | bool ?<=?(const string &, const char *); |
---|
| 152 | bool ?<? (const string &, const char *); |
---|
[416b443] | 153 | |
---|
[681e12f] | 154 | int strcmp (const char *, const string &); |
---|
| 155 | bool ?==?(const char *, const string &); |
---|
| 156 | bool ?!=?(const char *, const string &); |
---|
| 157 | bool ?>? (const char *, const string &); |
---|
| 158 | bool ?>=?(const char *, const string &); |
---|
| 159 | bool ?<=?(const char *, const string &); |
---|
| 160 | bool ?<? (const char *, const string &); |
---|
[416b443] | 161 | |
---|
[f450f2f] | 162 | |
---|
| 163 | // Slicing |
---|
[e8b3717] | 164 | string ?()( string & s, size_t start, size_t len ); // TODO const? |
---|
[681e12f] | 165 | string ?()( string & s, size_t start); |
---|
[f450f2f] | 166 | |
---|
| 167 | // String search |
---|
[6264087] | 168 | bool contains(const string & s, char ch); // single character |
---|
[f450f2f] | 169 | |
---|
[6264087] | 170 | int find(const string & s, char search); |
---|
| 171 | int find(const string & s, const string & search); |
---|
| 172 | int find(const string & s, const char * search); |
---|
| 173 | int find(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 174 | |
---|
[6264087] | 175 | int findFrom(const string & s, size_t fromPos, char search); |
---|
| 176 | int findFrom(const string & s, size_t fromPos, const string & search); |
---|
| 177 | int findFrom(const string & s, size_t fromPos, const char * search); |
---|
| 178 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize); |
---|
[08ed947] | 179 | |
---|
[6264087] | 180 | bool includes(const string & s, const string & search); |
---|
| 181 | bool includes(const string & s, const char * search); |
---|
| 182 | bool includes(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 183 | |
---|
[6264087] | 184 | bool startsWith(const string & s, const string & prefix); |
---|
| 185 | bool startsWith(const string & s, const char * prefix); |
---|
| 186 | bool startsWith(const string & s, const char * prefix, size_t prefixsize); |
---|
[f450f2f] | 187 | |
---|
[6264087] | 188 | bool endsWith(const string & s, const string & suffix); |
---|
| 189 | bool endsWith(const string & s, const char * suffix); |
---|
| 190 | bool endsWith(const string & s, const char * suffix, size_t suffixsize); |
---|
[f450f2f] | 191 | |
---|
| 192 | // Modifiers |
---|
[6264087] | 193 | void padStart(string & s, size_t n); |
---|
| 194 | void padStart(string & s, size_t n, char padding); |
---|
| 195 | void padEnd(string & s, size_t n); |
---|
| 196 | void padEnd(string & s, size_t n, char padding); |
---|
[f450f2f] | 197 | |
---|
| 198 | |
---|
| 199 | struct charclass { |
---|
| 200 | charclass_res * inner; |
---|
| 201 | }; |
---|
| 202 | |
---|
| 203 | void ?{}( charclass & ) = void; |
---|
| 204 | void ?{}( charclass &, charclass) = void; |
---|
| 205 | charclass ?=?( charclass &, charclass) = void; |
---|
| 206 | |
---|
| 207 | void ?{}( charclass &, const string & chars); |
---|
| 208 | void ?{}( charclass &, const char * chars ); |
---|
| 209 | void ?{}( charclass &, const char * chars, size_t charssize ); |
---|
| 210 | void ^?{}( charclass & ); |
---|
| 211 | |
---|
[6264087] | 212 | int include(const string & s, const charclass & mask); |
---|
[f450f2f] | 213 | |
---|
[6264087] | 214 | int exclude(const string & s, const charclass & mask); |
---|
[f450f2f] | 215 | |
---|
| 216 | /* |
---|
| 217 | What to do with? |
---|
[6264087] | 218 | StrRet include(string & s, const charclass & mask); |
---|
| 219 | StrRet exclude(string & s, const charclass & mask); |
---|
[f450f2f] | 220 | */ |
---|