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