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 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Aug 6 07:49:52 2024 |
---|
13 | // Update Count : 130 |
---|
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 |
---|
30 | size_t size( const string & s ); |
---|
31 | static inline size_t strlen( const string & s ) { return size( s ); } |
---|
32 | |
---|
33 | // RAII, assignment |
---|
34 | void ?{}( string & s ); // empty string |
---|
35 | void ?{}( string & s, const string & s2 ); |
---|
36 | void ?{}( string & s, const string & s2, size_t maxlen ); |
---|
37 | void ?{}( string & s, string & s2 ); |
---|
38 | |
---|
39 | void ?{}( string & s, char ); |
---|
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 | |
---|
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 | |
---|
50 | string str( ssize_t rhs ); |
---|
51 | string str( size_t rhs ); |
---|
52 | string str( double rhs ); |
---|
53 | string str( long double rhs ); |
---|
54 | string str( double _Complex rhs ); |
---|
55 | string str( long double _Complex rhs ); |
---|
56 | |
---|
57 | string & ?=?( string & s, const string & c ); |
---|
58 | string & ?=?( string & s, string & c ); |
---|
59 | string & ?=?( string & s, const char * c ); // copy from "literal" |
---|
60 | string & ?=?( string & s, char c ); // copy from 'l' |
---|
61 | string & assign( string & s, const string & c, size_t n ); |
---|
62 | string & assign( string & s, const char * c, size_t n ); |
---|
63 | |
---|
64 | static inline string & strcpy( string & s, const char * c ) { s = c; return s; } |
---|
65 | static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; } |
---|
66 | static inline string & strcpy( string & s, const string & c ) { s = c; return s; } |
---|
67 | static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; } |
---|
68 | |
---|
69 | string & ?=?( string & s, ssize_t rhs ); |
---|
70 | string & ?=?( string & s, size_t rhs ); |
---|
71 | string & ?=?( string & s, double rhs ); |
---|
72 | string & ?=?( string & s, long double rhs ); |
---|
73 | string & ?=?( string & s, double _Complex rhs ); |
---|
74 | string & ?=?( string & s, long double _Complex rhs ); |
---|
75 | |
---|
76 | void ^?{}( string & s ); |
---|
77 | |
---|
78 | // Alternate construction: request shared edits |
---|
79 | struct string_WithSharedEdits { |
---|
80 | string * s; |
---|
81 | }; |
---|
82 | string_WithSharedEdits ?`shareEdits( string & s ); |
---|
83 | void ?{}( string & s, string_WithSharedEdits src ); |
---|
84 | |
---|
85 | // IO Operator |
---|
86 | ofstream & ?|?( ofstream & out, const string & s ); |
---|
87 | void ?|?( ofstream & out, const string & s ); |
---|
88 | ifstream & ?|?( ifstream & in, string & s ); |
---|
89 | |
---|
90 | static inline { |
---|
91 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; } |
---|
92 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; } |
---|
93 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; } |
---|
94 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; } |
---|
95 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; } |
---|
96 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; } |
---|
97 | _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; } |
---|
98 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; } |
---|
99 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; } |
---|
100 | } // distribution |
---|
101 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ); |
---|
102 | void ?|?( ofstream & os, _Ostream_Manip(string) ); |
---|
103 | |
---|
104 | struct _Istream_Swidth { |
---|
105 | string & s; |
---|
106 | inline _Istream_str_base; |
---|
107 | }; // _Istream_Swidth |
---|
108 | |
---|
109 | struct _Istream_Squoted { |
---|
110 | _Istream_Swidth sstr; |
---|
111 | }; // _Istream_Squoted |
---|
112 | |
---|
113 | struct _Istream_Sstr { |
---|
114 | string & s; |
---|
115 | inline _Istream_str_base; |
---|
116 | // _Istream_Swidth sstr; |
---|
117 | }; // _Istream_Sstr |
---|
118 | |
---|
119 | static inline { |
---|
120 | // read width does not include null terminator |
---|
121 | _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s : s, { {.scanset : 0p}, .wd : rwd, {.flags.rwd : true} } }; } |
---|
122 | _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { |
---|
123 | // return (_Istream_Sstr)@{ { .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } } }; |
---|
124 | return (_Istream_Sstr)@{ .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } }; |
---|
125 | } |
---|
126 | _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) { |
---|
127 | f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f; |
---|
128 | } |
---|
129 | _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) { |
---|
130 | return (_Istream_Squoted)@{ { .s : s, { {.delimiters : { Ldelimiter, Rdelimiter, '\0' }}, .wd : -1, {.flags.rwd : true} } } }; |
---|
131 | } |
---|
132 | _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { |
---|
133 | f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; |
---|
134 | return (_Istream_Squoted &)f; |
---|
135 | } |
---|
136 | // _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } } }; } |
---|
137 | _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } }; } |
---|
138 | _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; } |
---|
139 | // _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } } }; } |
---|
140 | _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } }; } |
---|
141 | _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; } |
---|
142 | // _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } } }; } |
---|
143 | _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } }; } |
---|
144 | _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
145 | _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; } |
---|
146 | // _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
147 | _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } |
---|
148 | } // distribution |
---|
149 | ifstream & ?|?( ifstream & is, _Istream_Squoted f ); |
---|
150 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ); |
---|
151 | static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; } |
---|
152 | |
---|
153 | // Concatenation |
---|
154 | void ?+=?( string & s, char c ); // append a character |
---|
155 | void ?+=?( string & s, const string & s2 ); // append-concatenate to first string |
---|
156 | void append( string & s, const string & s2, size_t maxlen ); // append-concatenate to first string, up to maxlen |
---|
157 | void ?+=?( string & s, const char * s2 ); // append-concatenate NULL-terminated string to first string |
---|
158 | void append( string & s, const char * buffer, size_t bsize ); // append-concatenate given range to first string |
---|
159 | |
---|
160 | string ?+?( const string & s, char c ); // add a character to a copy of the string |
---|
161 | string ?+?( char c, const string & s ); // add a character to a copy of the string |
---|
162 | string ?+?( const string & s, const string & s2 ); // copy and concatenate both strings |
---|
163 | string ?+?( const char * s, char c ); // add a character to a copy of the string |
---|
164 | string ?+?( char c, const char * s ); // add a character to a copy of the string |
---|
165 | string ?+?( const char * c, const char * s ); // copy and add with two NULL-terminated string |
---|
166 | string ?+?( const char * c, string & s ); // copy and add with NULL-terminated string |
---|
167 | string ?+?( const string & s, const char * c ); // copy and add with NULL-terminated string |
---|
168 | |
---|
169 | static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; } |
---|
170 | static inline string & strcat( string & s, const char * c ) { s += c; return s; } |
---|
171 | static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; } |
---|
172 | static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; } |
---|
173 | |
---|
174 | // Repetition |
---|
175 | string ?*?( const string & s, size_t factor ); |
---|
176 | void ?*=?( string & s, size_t factor ); |
---|
177 | string ?*?( char c, size_t factor ); |
---|
178 | string ?*?( const char *s, size_t factor ); |
---|
179 | |
---|
180 | // Character access |
---|
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 |
---|
184 | |
---|
185 | // Comparisons |
---|
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 & ); |
---|
209 | |
---|
210 | |
---|
211 | // Slicing |
---|
212 | string ?()( string & s, size_t start, size_t len ); // TODO const? |
---|
213 | string ?()( string & s, size_t start ); |
---|
214 | |
---|
215 | // String search |
---|
216 | bool contains( const string & s, char ch ); // single character |
---|
217 | |
---|
218 | int find( const string & s, char search ); |
---|
219 | int find( const string & s, const string & search ); |
---|
220 | int find( const string & s, const char * search ); |
---|
221 | int find( const string & s, const char * search, size_t searchsize ); |
---|
222 | |
---|
223 | int findFrom( const string & s, size_t fromPos, char search ); |
---|
224 | int findFrom( const string & s, size_t fromPos, const string & search ); |
---|
225 | int findFrom( const string & s, size_t fromPos, const char * search ); |
---|
226 | int findFrom( const string & s, size_t fromPos, const char * search, size_t searchsize ); |
---|
227 | |
---|
228 | bool includes( const string & s, const string & search ); |
---|
229 | bool includes( const string & s, const char * search ); |
---|
230 | bool includes( const string & s, const char * search, size_t searchsize ); |
---|
231 | |
---|
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 ); |
---|
235 | |
---|
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 ); |
---|
239 | |
---|
240 | // Modifiers |
---|
241 | void padStart( string & s, size_t n ); |
---|
242 | void padStart( string & s, size_t n, char padding ); |
---|
243 | void padEnd( string & s, size_t n ); |
---|
244 | void padEnd( string & s, size_t n, char padding ); |
---|
245 | |
---|
246 | |
---|
247 | struct charclass { |
---|
248 | charclass_res * inner; |
---|
249 | }; |
---|
250 | |
---|
251 | void ?{}( charclass & ) = void; |
---|
252 | void ?{}( charclass &, charclass ) = void; |
---|
253 | charclass ?=?( charclass &, charclass ) = void; |
---|
254 | |
---|
255 | void ?{}( charclass &, const string & chars ); |
---|
256 | void ?{}( charclass &, const char * chars ); |
---|
257 | void ?{}( charclass &, const char * chars, size_t charssize ); |
---|
258 | void ^?{}( charclass & ); |
---|
259 | |
---|
260 | int include( const string & s, const charclass & mask ); |
---|
261 | |
---|
262 | int exclude( const string & s, const charclass & mask ); |
---|
263 | |
---|
264 | /* |
---|
265 | What to do with? |
---|
266 | StrRet include( string & s, const charclass & mask ); |
---|
267 | StrRet exclude( string & s, const charclass & mask ); |
---|
268 | */ |
---|