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 : Mon Aug 28 19:00:45 2023 |
---|
13 | // Update Count : 146 |
---|
14 | // |
---|
15 | |
---|
16 | #include "string.hfa" |
---|
17 | #include "string_res.hfa" |
---|
18 | #include <stdlib.hfa> |
---|
19 | |
---|
20 | #pragma GCC visibility push(default) |
---|
21 | |
---|
22 | /* |
---|
23 | Implementation Principle: typical operation translates to the equivalent |
---|
24 | operation on `inner`. Exceptions are implementing new RAII pattern for value |
---|
25 | semantics and some const-hell handling. |
---|
26 | */ |
---|
27 | |
---|
28 | //////////////////////////////////////////////////////// |
---|
29 | // string RAII |
---|
30 | |
---|
31 | |
---|
32 | void ?{}( string & this ) { |
---|
33 | (this.inner) { malloc() }; |
---|
34 | ?{}( *this.inner ); |
---|
35 | } |
---|
36 | |
---|
37 | // private (not in header) |
---|
38 | static void ?{}( string & this, string_res & src, size_t start, size_t end ) { |
---|
39 | (this.inner) { malloc() }; |
---|
40 | ?{}( *this.inner, src, SHARE_EDITS, start, end ); |
---|
41 | } |
---|
42 | |
---|
43 | void ?{}( string & this, const string & other ) { |
---|
44 | (this.inner) { malloc() }; |
---|
45 | ?{}( *this.inner, *other.inner, COPY_VALUE ); |
---|
46 | } |
---|
47 | |
---|
48 | void ?{}( string & this, string & other ) { |
---|
49 | ?{}( this, (const string &) other ); |
---|
50 | } |
---|
51 | |
---|
52 | void ?{}( string & this, const char * val ) { |
---|
53 | (this.inner) { malloc() }; |
---|
54 | ?{}( *this.inner, val ); |
---|
55 | } |
---|
56 | |
---|
57 | void ?{}( string & this, const char * buffer, size_t bsize) { |
---|
58 | (this.inner) { malloc() }; |
---|
59 | ?{}( *this.inner, buffer, bsize ); |
---|
60 | } |
---|
61 | |
---|
62 | void ^?{}( string & this ) { |
---|
63 | ^(*this.inner){}; |
---|
64 | free( this.inner ); |
---|
65 | this.inner = 0p; |
---|
66 | } |
---|
67 | |
---|
68 | //////////////////////////////////////////////////////// |
---|
69 | // Alternate construction: request shared edits |
---|
70 | |
---|
71 | string_WithSharedEdits ?`shareEdits( string & this ) { |
---|
72 | string_WithSharedEdits ret = { &this }; |
---|
73 | return ret; |
---|
74 | } |
---|
75 | |
---|
76 | void ?{}( string & this, string_WithSharedEdits src ) { |
---|
77 | ?{}( this, *src.s->inner, 0, src.s->inner->Handle.lnth); |
---|
78 | } |
---|
79 | |
---|
80 | //////////////////////////////////////////////////////// |
---|
81 | // Assignment |
---|
82 | |
---|
83 | void ?=?( string & this, const char * val ) { |
---|
84 | (*this.inner) = val; |
---|
85 | } |
---|
86 | |
---|
87 | void ?=?(string & this, const string & other) { |
---|
88 | (*this.inner) = (*other.inner); |
---|
89 | } |
---|
90 | |
---|
91 | void ?=?( string & this, char val ) { |
---|
92 | (*this.inner) = val; |
---|
93 | } |
---|
94 | |
---|
95 | string & ?=?(string & this, string & other) { //// <---- straw man change |
---|
96 | (*this.inner) = (*other.inner); |
---|
97 | return this; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | //////////////////////////////////////////////////////// |
---|
102 | // Input-Output |
---|
103 | |
---|
104 | ofstream & ?|?( ofstream & out, const string & this ) { |
---|
105 | return out | (*this.inner); // print internal string_res |
---|
106 | } |
---|
107 | |
---|
108 | void ?|?( ofstream & out, const string & this ) { |
---|
109 | (ofstream &)(out | (*this.inner)); ends( out ); |
---|
110 | } |
---|
111 | |
---|
112 | ifstream & ?|?(ifstream & in, string & this) { |
---|
113 | return in | (*this.inner); // read to internal string_res |
---|
114 | } |
---|
115 | |
---|
116 | void ?|?( ifstream & in, string & this ) { |
---|
117 | (ifstream &)(in | this); ends( in ); |
---|
118 | } |
---|
119 | |
---|
120 | static void readstr( ifstream & is, _Istream_str f, char fmtstr[], char cstr[] ) { |
---|
121 | int check = f.rwd - 1; |
---|
122 | |
---|
123 | if ( ! f.flags.rwd ) cstr[check] = '\0'; // insert sentinel |
---|
124 | int len = fmt( is, fmtstr, cstr ); |
---|
125 | // fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, cstr[check], cstr ); |
---|
126 | |
---|
127 | if ( ! f.flags.rwd && cstr[check] != '\0' ) // sentinel overwritten ? |
---|
128 | throw (cstring_length){ &cstring_length_vt }; |
---|
129 | |
---|
130 | if ( f.flags.delimit ) { // getline ? |
---|
131 | if ( len == 0 ) cstr[0] = '\0'; // empty read => argument unchanged => set empty |
---|
132 | if ( ! eof( is ) ) fmt( is, "%*c" ); // ignore delimiter |
---|
133 | } //if |
---|
134 | } // readstr |
---|
135 | |
---|
136 | ifstream & ?|?( ifstream & is, _Istream_str f ) { |
---|
137 | // skip, same as for char * |
---|
138 | if ( ! &f.s ) { |
---|
139 | // fprintf( stderr, "skip %s %d\n", f.scanset, f.wd ); |
---|
140 | if ( f.rwd == -1 ) fmt( is, f.scanset, "" ); // no input arguments |
---|
141 | else for ( f.rwd ) fmt( is, "%*c" ); |
---|
142 | return is; |
---|
143 | } // if |
---|
144 | |
---|
145 | enum { gwd = 16 + 2, wd = gwd - 1 }; // guarded and unguarded width |
---|
146 | char cstr[gwd]; // read in chunks |
---|
147 | bool cont = false;; |
---|
148 | |
---|
149 | if ( f.rwd == -1 ) f.rwd = wd; |
---|
150 | const char * scanset = f.scanset;; |
---|
151 | if ( f.flags.delimit ) scanset = f.delimit; // getline ? |
---|
152 | |
---|
153 | size_t len = 0; |
---|
154 | if ( scanset ) len = strlen( scanset ); |
---|
155 | char fmtstr[len + 16]; |
---|
156 | int start = 1; |
---|
157 | fmtstr[0] = '%'; |
---|
158 | if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } |
---|
159 | if ( f.rwd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.rwd ); } |
---|
160 | |
---|
161 | if ( ! scanset ) { |
---|
162 | // %s, %*s, %ws, %*ws |
---|
163 | fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; |
---|
164 | // printf( "cstr %s\n", fmtstr ); |
---|
165 | } else { |
---|
166 | // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] |
---|
167 | // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] |
---|
168 | fmtstr[start] = '['; start += 1; |
---|
169 | if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } |
---|
170 | strcpy( &fmtstr[start], scanset ); // copy includes '\0' |
---|
171 | len += start; |
---|
172 | fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; |
---|
173 | // printf( "incl/excl %s\n", fmtstr ); |
---|
174 | } // if |
---|
175 | |
---|
176 | cstr[wd] = '\0'; // guard null terminate string |
---|
177 | try { |
---|
178 | readstr( is, f, fmtstr, cstr ); |
---|
179 | } catch( cstring_length * ) { |
---|
180 | cont = true; |
---|
181 | } finally { |
---|
182 | f.s = cstr; // ok to initialize string |
---|
183 | } // try |
---|
184 | for ( ; cont; ) { // overflow read ? |
---|
185 | cont = false; |
---|
186 | try { |
---|
187 | readstr( is, f, fmtstr, cstr ); |
---|
188 | } catch( cstring_length * ) { |
---|
189 | cont = true; // continue not allowed |
---|
190 | } finally { |
---|
191 | f.s += cstr; // build string chunk at a time |
---|
192 | } // try |
---|
193 | } // for |
---|
194 | return is; |
---|
195 | } // ?|? |
---|
196 | |
---|
197 | void ?|?( ifstream & in, _Istream_str f ) { |
---|
198 | (ifstream &)(in | f); ends( in ); |
---|
199 | } |
---|
200 | |
---|
201 | // void getline( ifstream & in, string & str, const char delimit = '\n' ) { |
---|
202 | // // .---------------, |
---|
203 | // // | | | | |...|0|0| check and guard |
---|
204 | // // `---------------' |
---|
205 | // enum { gwd = 128 + 2, wd = gwd - 1 }; // guarded and unguarded width |
---|
206 | // char cstr[gwd]; // read in chunks |
---|
207 | // bool cont = false;; |
---|
208 | |
---|
209 | // cstr[wd] = '\0'; // guard null terminate string |
---|
210 | // try { |
---|
211 | // in | getline( wdi( wd, cstr ), delimit ); // must have room for string terminator |
---|
212 | // if ( eof( in ) ) return; // do not change argument |
---|
213 | // } catch( cstring_length * ) { |
---|
214 | // cont = true; |
---|
215 | // } finally { |
---|
216 | // str = cstr; // ok to initialize string |
---|
217 | // } // try |
---|
218 | // for ( ; cont; ) { // overflow read ? |
---|
219 | // cont = false; |
---|
220 | // try { |
---|
221 | // in | getline( wdi( wd, cstr ), delimit ); // must have room for string terminator |
---|
222 | // if ( eof( in ) ) return; |
---|
223 | // } catch( cstring_length * ) { |
---|
224 | // cont = true; // continue not allowed |
---|
225 | // } finally { |
---|
226 | // str += cstr; // build string chunk at a time |
---|
227 | // } // try |
---|
228 | // } // for |
---|
229 | // } |
---|
230 | |
---|
231 | //////////////////////////////////////////////////////// |
---|
232 | // Slicing |
---|
233 | |
---|
234 | string ?()( string & this, size_t start, size_t end ) { |
---|
235 | string ret = { *this.inner, start, end }; |
---|
236 | return ret`shareEdits; |
---|
237 | } |
---|
238 | |
---|
239 | string ?()( string & this, size_t start ) { |
---|
240 | string ret = { *this.inner, start, size( this ) }; |
---|
241 | return ret`shareEdits; |
---|
242 | } |
---|
243 | |
---|
244 | //////////////////////////////////////////////////////// |
---|
245 | // Comparison |
---|
246 | |
---|
247 | bool ?==?(const string & s, const string & other) { |
---|
248 | return *s.inner == *other.inner; |
---|
249 | } |
---|
250 | |
---|
251 | bool ?!=?(const string & s, const string & other) { |
---|
252 | return *s.inner != *other.inner; |
---|
253 | } |
---|
254 | |
---|
255 | bool ?==?(const string & s, const char * other) { |
---|
256 | return *s.inner == other; |
---|
257 | } |
---|
258 | |
---|
259 | bool ?!=?(const string & s, const char * other) { |
---|
260 | return *s.inner != other; |
---|
261 | } |
---|
262 | |
---|
263 | //////////////////////////////////////////////////////// |
---|
264 | // Getter |
---|
265 | |
---|
266 | size_t size(const string & s) { |
---|
267 | return size( * s.inner ); |
---|
268 | } |
---|
269 | |
---|
270 | //////////////////////////////////////////////////////// |
---|
271 | // Concatenation |
---|
272 | |
---|
273 | void ?+=?(string & s, char other) { |
---|
274 | (*s.inner) += other; |
---|
275 | } |
---|
276 | |
---|
277 | void ?+=?(string & s, const string & s2) { |
---|
278 | (*s.inner) += (*s2.inner); |
---|
279 | } |
---|
280 | |
---|
281 | void ?+=?(string & s, const char * other) { |
---|
282 | (*s.inner) += other; |
---|
283 | } |
---|
284 | |
---|
285 | string ?+?(const string & s, char other) { |
---|
286 | string ret = s; |
---|
287 | ret += other; |
---|
288 | return ret; |
---|
289 | } |
---|
290 | |
---|
291 | string ?+?(const string & s, const string & s2) { |
---|
292 | string ret = s; |
---|
293 | ret += s2; |
---|
294 | return ret; |
---|
295 | } |
---|
296 | |
---|
297 | string ?+?(const char * s1, const char * s2) { |
---|
298 | string ret = s1; |
---|
299 | ret += s2; |
---|
300 | return ret; |
---|
301 | } |
---|
302 | |
---|
303 | string ?+?(const string & s, const char * other) { |
---|
304 | string ret = s; |
---|
305 | ret += other; |
---|
306 | return ret; |
---|
307 | } |
---|
308 | |
---|
309 | //////////////////////////////////////////////////////// |
---|
310 | // Repetition |
---|
311 | |
---|
312 | string ?*?(const string & s, size_t factor) { |
---|
313 | string ret = ""; |
---|
314 | for (factor) ret += s; |
---|
315 | return ret; |
---|
316 | } |
---|
317 | |
---|
318 | string ?*?(char c, size_t size) { |
---|
319 | string ret = ""; |
---|
320 | for ((size_t)size) ret += c; |
---|
321 | return ret; |
---|
322 | } |
---|
323 | |
---|
324 | string ?*?(const char *s, size_t factor) { |
---|
325 | string ss = s; |
---|
326 | return ss * factor; |
---|
327 | } |
---|
328 | |
---|
329 | //////////////////////////////////////////////////////// |
---|
330 | // Character access |
---|
331 | |
---|
332 | char ?[?](const string & s, size_t index) { |
---|
333 | return (*s.inner)[index]; |
---|
334 | } |
---|
335 | |
---|
336 | string ?[?](string & s, size_t index) { |
---|
337 | string ret = { *s.inner, index, index + 1 }; |
---|
338 | return ret`shareEdits; |
---|
339 | } |
---|
340 | |
---|
341 | //////////////////////////////////////////////////////// |
---|
342 | // Search |
---|
343 | |
---|
344 | bool contains(const string & s, char ch) { |
---|
345 | return contains( *s.inner, ch ); |
---|
346 | } |
---|
347 | |
---|
348 | int find(const string & s, char search) { |
---|
349 | return find( *s.inner, search ); |
---|
350 | } |
---|
351 | |
---|
352 | int find(const string & s, const string & search) { |
---|
353 | return find( *s.inner, *search.inner ); |
---|
354 | } |
---|
355 | |
---|
356 | int find(const string & s, const char * search) { |
---|
357 | return find( *s.inner, search); |
---|
358 | } |
---|
359 | |
---|
360 | int find(const string & s, const char * search, size_t searchsize) { |
---|
361 | return find( *s.inner, search, searchsize); |
---|
362 | } |
---|
363 | |
---|
364 | int findFrom(const string & s, size_t fromPos, char search) { |
---|
365 | return findFrom( *s.inner, fromPos, search ); |
---|
366 | } |
---|
367 | |
---|
368 | int findFrom(const string & s, size_t fromPos, const string & search) { |
---|
369 | return findFrom( *s.inner, fromPos, *search.inner ); |
---|
370 | } |
---|
371 | |
---|
372 | int findFrom(const string & s, size_t fromPos, const char * search) { |
---|
373 | return findFrom( *s.inner, fromPos, search ); |
---|
374 | } |
---|
375 | |
---|
376 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) { |
---|
377 | return findFrom( *s.inner, fromPos, search, searchsize ); |
---|
378 | } |
---|
379 | |
---|
380 | bool includes(const string & s, const string & search) { |
---|
381 | return includes( *s.inner, *search.inner ); |
---|
382 | } |
---|
383 | |
---|
384 | bool includes(const string & s, const char * search) { |
---|
385 | return includes( *s.inner, search ); |
---|
386 | } |
---|
387 | |
---|
388 | bool includes(const string & s, const char * search, size_t searchsize) { |
---|
389 | return includes( *s.inner, search, searchsize ); |
---|
390 | } |
---|
391 | |
---|
392 | bool startsWith(const string & s, const string & prefix) { |
---|
393 | return startsWith( *s.inner, *prefix.inner ); |
---|
394 | } |
---|
395 | |
---|
396 | bool startsWith(const string & s, const char * prefix) { |
---|
397 | return startsWith( *s.inner, prefix ); |
---|
398 | } |
---|
399 | |
---|
400 | bool startsWith(const string & s, const char * prefix, size_t prefixsize) { |
---|
401 | return startsWith( *s.inner, prefix, prefixsize ); |
---|
402 | } |
---|
403 | |
---|
404 | bool endsWith(const string & s, const string & suffix) { |
---|
405 | return endsWith( *s.inner, *suffix.inner ); |
---|
406 | } |
---|
407 | |
---|
408 | bool endsWith(const string & s, const char * suffix) { |
---|
409 | return endsWith( *s.inner, suffix ); |
---|
410 | } |
---|
411 | |
---|
412 | bool endsWith(const string & s, const char * suffix, size_t suffixsize) { |
---|
413 | return endsWith( *s.inner, suffix, suffixsize ); |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | /////////////////////////////////////////////////////////////////////////// |
---|
418 | // charclass, include, exclude |
---|
419 | |
---|
420 | void ?{}( charclass & this, const string & chars) { |
---|
421 | (this.inner) { malloc() }; |
---|
422 | ?{}( *this.inner, *(const string_res *)chars.inner ); |
---|
423 | } |
---|
424 | |
---|
425 | void ?{}( charclass & this, const char * chars ) { |
---|
426 | (this.inner) { malloc() }; |
---|
427 | ?{}( *this.inner, chars ); |
---|
428 | } |
---|
429 | |
---|
430 | void ?{}( charclass & this, const char * chars, size_t charssize ) { |
---|
431 | (this.inner) { malloc() }; |
---|
432 | ?{}( *this.inner, chars, charssize ); |
---|
433 | } |
---|
434 | |
---|
435 | void ^?{}( charclass & this ) { |
---|
436 | ^(*this.inner){}; |
---|
437 | free( this.inner ); |
---|
438 | this.inner = 0p; |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | int exclude(const string & s, const charclass & mask) { |
---|
443 | return exclude( *s.inner, *mask.inner ); |
---|
444 | } |
---|
445 | /* |
---|
446 | StrSlice exclude(string & s, const charclass & mask) { |
---|
447 | } |
---|
448 | */ |
---|
449 | |
---|
450 | int include(const string & s, const charclass & mask) { |
---|
451 | return include( *s.inner, *mask.inner ); |
---|
452 | } |
---|
453 | |
---|
454 | /* |
---|
455 | StrSlice include(string & s, const charclass & mask) { |
---|
456 | } |
---|
457 | */ |
---|
458 | |
---|