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 : Wed Apr 9 22:27:41 2025
|
---|
13 | // Update Count : 259
|
---|
14 | //
|
---|
15 |
|
---|
16 | #pragma once
|
---|
17 |
|
---|
18 | #include <iostream.hfa>
|
---|
19 | #include <string_res.hfa>
|
---|
20 |
|
---|
21 | struct string {
|
---|
22 | string_res * inner;
|
---|
23 | };
|
---|
24 |
|
---|
25 | // RAII, assignment
|
---|
26 | void ^?{}( string & s );
|
---|
27 |
|
---|
28 | void ?{}( string & s ); // empty string
|
---|
29 | void ?{}( string & s, string s2, size_t maxlen );
|
---|
30 | void ?{}( string & s, string s2 );
|
---|
31 | void ?{}( string & s, char );
|
---|
32 | void ?{}( string & s, const char * c ); // copy from string literal (NULL-terminated)
|
---|
33 | void ?{}( string & s, const char * c, size_t size ); // copy specific length from buffer
|
---|
34 |
|
---|
35 | void ?{}( string & s, signed long int rhs );
|
---|
36 | void ?{}( string & s, size_t rhs );
|
---|
37 | void ?{}( string & s, double rhs );
|
---|
38 | void ?{}( string & s, long double rhs );
|
---|
39 | void ?{}( string & s, double _Complex rhs );
|
---|
40 | void ?{}( string & s, long double _Complex rhs );
|
---|
41 | static inline void ?{}( string & s, int rhs ) { (s){(signed long int) rhs}; }
|
---|
42 |
|
---|
43 | // string str( ssize_t rhs );
|
---|
44 | // string str( size_t rhs );
|
---|
45 | // string str( double rhs );
|
---|
46 | // string str( long double rhs );
|
---|
47 | // string str( double _Complex rhs );
|
---|
48 | // string str( long double _Complex rhs );
|
---|
49 |
|
---|
50 | string & ?=?( string & s, string c );
|
---|
51 | string & ?=?( string & s, const char * c ); // copy from "literal"
|
---|
52 | string & ?=?( string & s, char c ); // copy from 'l'
|
---|
53 | string & assign( string & s, const string & c, size_t n );
|
---|
54 | string & assign( string & s, const char * c, size_t n );
|
---|
55 | string & ?=?( string & s, signed long int rhs );
|
---|
56 | string & ?=?( string & s, size_t rhs );
|
---|
57 | string & ?=?( string & s, double rhs );
|
---|
58 | string & ?=?( string & s, long double rhs );
|
---|
59 | string & ?=?( string & s, double _Complex rhs );
|
---|
60 | string & ?=?( string & s, long double _Complex rhs );
|
---|
61 | static inline string & ?=?( string & s, int rhs ) { return s = ((signed long int) rhs); } // to match cost of (char * int): int
|
---|
62 |
|
---|
63 | static inline string & strcpy( string & s, const char * c ) { s = c; return s; }
|
---|
64 | static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; }
|
---|
65 | static inline string & strcpy( string & s, const string & c ) { s = c; return s; }
|
---|
66 | static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; }
|
---|
67 |
|
---|
68 | // Alternate construction: request shared edits
|
---|
69 | struct string_Share {
|
---|
70 | string * s;
|
---|
71 | };
|
---|
72 | string_Share ?`share( string & s );
|
---|
73 | void ?{}( string & s, string_Share src );
|
---|
74 |
|
---|
75 | // Getters
|
---|
76 | static inline size_t len( const string & s ) { return len( *s.inner ); }
|
---|
77 | static inline size_t len( const char * cs ) { return strlen( cs ); };
|
---|
78 | static inline size_t strlen( const string & s ) { return len( s ); }
|
---|
79 |
|
---|
80 | // IO Operator
|
---|
81 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
82 | ostype & ?|?( ostype & out, string s );
|
---|
83 | void ?|?( ostype & out, string s );
|
---|
84 | }
|
---|
85 | forall( istype & | basic_istream( istype ) )
|
---|
86 | istype & ?|?( istype & in, string & s );
|
---|
87 |
|
---|
88 | static inline {
|
---|
89 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all = 0 } }; }
|
---|
90 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all = 0 } }; }
|
---|
91 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all = 0 } }; }
|
---|
92 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all = 0 } }; }
|
---|
93 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc = true } }; }
|
---|
94 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
|
---|
95 | _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; }
|
---|
96 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
|
---|
97 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
|
---|
98 | } // distribution
|
---|
99 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
100 | ostype & ?|?( ostype & os, _Ostream_Manip(string) f );
|
---|
101 | void ?|?( ostype & os, _Ostream_Manip(string) );
|
---|
102 | }
|
---|
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 | forall( istype & | basic_istream( istype ) ) {
|
---|
150 | istype & ?|?( istype & is, _Istream_Squoted f );
|
---|
151 | istype & ?|?( istype & is, _Istream_Sstr f );
|
---|
152 | static inline istype & ?|?( istype & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Concatenation
|
---|
156 | void ?+=?( string & s, char c );
|
---|
157 | void ?+=?( string & s, const string & s2 );
|
---|
158 | void append( string & s, const string & s2, size_t maxlen );
|
---|
159 | void ?+=?( string & s, const char * s2 );
|
---|
160 | void append( string & s, const char * buffer, size_t bsize );
|
---|
161 |
|
---|
162 | string ?+?( string s, char c );
|
---|
163 | string ?+?( char c, string s );
|
---|
164 | string ?+?( string s, string s2 );
|
---|
165 | string ?+?( const char * s, char c ); // not backwards compatible
|
---|
166 | string ?+?( char c, const char * s );
|
---|
167 | string ?+?( const char * c, const char * s );
|
---|
168 | string ?+?( const char * c, string s );
|
---|
169 | string ?+?( string s, const char * c );
|
---|
170 | string ?+?( char, char ); // not being called 8-(
|
---|
171 |
|
---|
172 | static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; }
|
---|
173 | static inline string & strcat( string & s, const char * c ) { s += c; return s; }
|
---|
174 | static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; }
|
---|
175 | static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; }
|
---|
176 |
|
---|
177 | // Repetition
|
---|
178 |
|
---|
179 | // Type `signed long long int` chosen for `factor` argument to achieve cost detente.
|
---|
180 | // This way, the call `'a' * 3` gets the same safe conversion cost calling here as for
|
---|
181 | // the built-in definition `int * int`.
|
---|
182 | typedef signed long long int strmul_factor_t;
|
---|
183 |
|
---|
184 | void ?*=?( string & s, strmul_factor_t factor );
|
---|
185 | string ?*?( char c, strmul_factor_t factor ); // not backwards compatible
|
---|
186 | string ?*?( string s, strmul_factor_t factor );
|
---|
187 | string ?*?( const char * s, strmul_factor_t factor );
|
---|
188 | static inline string ?*?( strmul_factor_t factor, char s ) { return s * factor; }
|
---|
189 | static inline string ?*?( strmul_factor_t factor, string s ) { return s * factor; }
|
---|
190 | static inline string ?*?( strmul_factor_t factor, const char * s ) { return s * factor; }
|
---|
191 |
|
---|
192 | // Character access
|
---|
193 | char ?[?]( const string & s, size_t index );
|
---|
194 | string ?[?]( string & s, size_t index ); // mutable length-1 slice of original
|
---|
195 | //char codePointAt(const string & s, size_t index ); // to revisit under Unicode
|
---|
196 |
|
---|
197 | // Comparisons
|
---|
198 | int strcmp ( const string &, const string & );
|
---|
199 | bool ?==?( const string &, const string & );
|
---|
200 | bool ?!=?( const string &, const string & );
|
---|
201 | bool ?>? ( const string &, const string & );
|
---|
202 | bool ?>=?( const string &, const string & );
|
---|
203 | bool ?<=?( const string &, const string & );
|
---|
204 | bool ?<? ( const string &, const string & );
|
---|
205 |
|
---|
206 | int strcmp( const string &, const char * );
|
---|
207 | bool ?==?( const string &, const char * );
|
---|
208 | bool ?!=?( const string &, const char * );
|
---|
209 | bool ?>? ( const string &, const char * );
|
---|
210 | bool ?>=?( const string &, const char * );
|
---|
211 | bool ?<=?( const string &, const char * );
|
---|
212 | bool ?<? ( const string &, const char * );
|
---|
213 |
|
---|
214 | int strcmp( const char *, const string & );
|
---|
215 | bool ?==?( const char *, const string & );
|
---|
216 | bool ?!=?( const char *, const string & );
|
---|
217 | bool ?>? ( const char *, const string & );
|
---|
218 | bool ?>=?( const char *, const string & );
|
---|
219 | bool ?<=?( const char *, const string & );
|
---|
220 | bool ?<? ( const char *, const string & );
|
---|
221 |
|
---|
222 | // String search
|
---|
223 | bool contains( const string & s, char ch ); // single character
|
---|
224 |
|
---|
225 | //int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen );
|
---|
226 | size_t find$( const string_res & s, size_t start, size_t len, const string & key_res, size_t kstart, size_t klen );
|
---|
227 |
|
---|
228 | size_t find( const string & s, char key );
|
---|
229 | size_t find( const string & s, const char * key );
|
---|
230 | size_t find( const string & s, const string & key );
|
---|
231 | size_t find( const string & s, const char * key, size_t keysize );
|
---|
232 |
|
---|
233 | size_t find( const string & s, size_t start, char key );
|
---|
234 | size_t find( const string & s, size_t start, const string & key );
|
---|
235 | size_t find( const string & s, size_t start, const char * key );
|
---|
236 | size_t find( const string & s, size_t start, const char * key, size_t keysize );
|
---|
237 | static inline ?^?( const string & key, const string & s ) { return find( s, key ); }
|
---|
238 | static inline ?^?( const char * key, const string & s ) { return find( s, key ); }
|
---|
239 |
|
---|
240 | bool includes( const string & s, const string & mask );
|
---|
241 | bool includes( const string & s, const char * mask );
|
---|
242 | bool includes( const string & s, const char * mask, size_t masksize );
|
---|
243 |
|
---|
244 | bool startsWith( const string & s, const string & prefix );
|
---|
245 | bool startsWith( const string & s, const char * prefix );
|
---|
246 | bool startsWith( const string & s, const char * prefix, size_t prefixsize );
|
---|
247 |
|
---|
248 | bool endsWith( const string & s, const string & suffix );
|
---|
249 | bool endsWith( const string & s, const char * suffix );
|
---|
250 | bool endsWith( const string & s, const char * suffix, size_t suffixsize );
|
---|
251 |
|
---|
252 | // Slicing
|
---|
253 | string ?()( string & s, ssize_t start, ssize_t len );
|
---|
254 | static inline string ?()( const string & s, ssize_t start, ssize_t len ) { string & w = (string &)s; return w( start, len ); } // FIX ME
|
---|
255 | string ?()( string & s, ssize_t start );
|
---|
256 | static inline string ?()( const string & s, ssize_t start ) { string & w = (string &)s; return w( start ); } // FIX ME
|
---|
257 | static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
|
---|
258 | static inline string ?()( const string & s, char m ) { string & w = (string &)s; return w( find( s, m ), 1 )`share; } // FIX ME
|
---|
259 | static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
|
---|
260 | static inline string ?()( const string & s, const char * m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
|
---|
261 | static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
|
---|
262 | static inline string ?()( const string & s, const string & m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
|
---|
263 |
|
---|
264 | struct charclass {
|
---|
265 | charclass_res * inner;
|
---|
266 | };
|
---|
267 |
|
---|
268 | void ?{}( charclass & ) = void;
|
---|
269 | void ?{}( charclass &, charclass ) = void;
|
---|
270 | charclass ?=?( charclass &, charclass ) = void;
|
---|
271 |
|
---|
272 | void ?{}( charclass &, const string & chars );
|
---|
273 | void ?{}( charclass &, const char * chars );
|
---|
274 | void ?{}( charclass &, const char * chars, size_t charssize );
|
---|
275 | void ^?{}( charclass & );
|
---|
276 |
|
---|
277 | size_t include( const string & s, const charclass & mask );
|
---|
278 | static inline size_t include( const char * s, const charclass & mask ) { string temp = s; return include( temp, mask ); }
|
---|
279 | static inline string include( const string & s, const charclass & mask ) { ssize_t i = include( s, mask ); return s( 0, i )`share; }
|
---|
280 | static inline string include( const char * s, const charclass & mask ) { string temp = s; ssize_t i = include( temp, mask ); return temp( 0, i ); }
|
---|
281 |
|
---|
282 | size_t exclude( const string & s, const charclass & mask );
|
---|
283 | static inline size_t exclude( const char * s, const charclass & mask ) { string temp = s; return exclude( temp, mask ); }
|
---|
284 | static inline string exclude( const string & s, const charclass & mask ) { ssize_t i = exclude( s, mask ); return s( 0, i )`share; }
|
---|
285 | static inline string exclude( const char * s, const charclass & mask ) { string temp = s; ssize_t i = exclude( temp, mask ); return temp( 0, i ); }
|
---|
286 |
|
---|
287 | size_t test( const string & s, int (*f)( int ) );
|
---|
288 | static inline size_t test( const char * c, int (*f)( int ) ) {
|
---|
289 | const string S = c;
|
---|
290 | return test( S, f );
|
---|
291 | }
|
---|
292 |
|
---|
293 | string replace( string & s, const string & from, const string & to );
|
---|
294 | static inline string replace( const char * s, const char * from, const char * to ) {
|
---|
295 | string S = s, From = from, To = to;
|
---|
296 | return replace( S, From, To );
|
---|
297 | }
|
---|
298 | static inline string replace( string & s, const char * from, const char * to ) {
|
---|
299 | string From = from, To = to;
|
---|
300 | return replace( s, From, To );
|
---|
301 | }
|
---|
302 | static inline string replace( string & s, const char * from, const string & to ) {
|
---|
303 | string From = from;
|
---|
304 | return replace( s, From, to );
|
---|
305 | }
|
---|
306 | static inline string replace( string & s, string & from, const char * to ) {
|
---|
307 | string To = to;
|
---|
308 | return replace( s, from, To );
|
---|
309 | }
|
---|
310 |
|
---|
311 | string translate( const string & s, int (*f)( int ) );
|
---|
312 | static inline string translate( const char * c, int (*f)( int ) ) {
|
---|
313 | const string S = c;
|
---|
314 | return translate( S, f );
|
---|
315 | }
|
---|