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 5 23:12:05 2024
|
---|
13 | // Update Count : 273
|
---|
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 | // private (not in header)
|
---|
32 | static void ?{}( string & s, string_res & src, size_t start, size_t len ) {
|
---|
33 | (s.inner) { malloc() };
|
---|
34 | ?{}( *s.inner, src, SHARE_EDITS, start, len );
|
---|
35 | }
|
---|
36 |
|
---|
37 | void ?{}( string & s ) {
|
---|
38 | (s.inner) { malloc() };
|
---|
39 | ?{}( *s.inner );
|
---|
40 | }
|
---|
41 |
|
---|
42 | void ?{}( string & s, const string & c ) {
|
---|
43 | (s.inner) { malloc() };
|
---|
44 | ?{}( *s.inner, *c.inner, COPY_VALUE );
|
---|
45 | }
|
---|
46 |
|
---|
47 | void ?{}( string & s, const string & s2, size_t maxlen ) {
|
---|
48 | (s.inner) { malloc() };
|
---|
49 | ?{}( *s.inner, *s2.inner, COPY_VALUE, maxlen );
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | void ?{}( string & s, string & c ) {
|
---|
54 | ?{}( s, (const string &) c );
|
---|
55 | }
|
---|
56 |
|
---|
57 | void ?{}( string & s, const char c ) {
|
---|
58 | (s.inner) { malloc() };
|
---|
59 | ?{}( *s.inner, c );
|
---|
60 | }
|
---|
61 |
|
---|
62 | void ?{}( string & s, const char * c ) {
|
---|
63 | (s.inner) { malloc() };
|
---|
64 | ?{}( *s.inner, c );
|
---|
65 | }
|
---|
66 |
|
---|
67 | void ?{}( string & s, const char * c, size_t size ) {
|
---|
68 | (s.inner) { malloc() };
|
---|
69 | ?{}( *s.inner, c, size );
|
---|
70 | }
|
---|
71 |
|
---|
72 | void ?{}( string & s, ssize_t rhs ) {
|
---|
73 | (s.inner) { malloc() };
|
---|
74 | ?{}( *s.inner, rhs );
|
---|
75 | }
|
---|
76 |
|
---|
77 | void ?{}( string & s, size_t rhs ) {
|
---|
78 | (s.inner) { malloc() };
|
---|
79 | ?{}( *s.inner, rhs );
|
---|
80 | }
|
---|
81 |
|
---|
82 | void ?{}( string & s, double rhs ) {
|
---|
83 | (s.inner) { malloc() };
|
---|
84 | ?{}( *s.inner, rhs );
|
---|
85 | }
|
---|
86 |
|
---|
87 | void ?{}( string & s, long double rhs ) {
|
---|
88 | (s.inner) { malloc() };
|
---|
89 | ?{}( *s.inner, rhs );
|
---|
90 | }
|
---|
91 |
|
---|
92 | void ?{}( string & s, double _Complex rhs ) {
|
---|
93 | (s.inner) { malloc() };
|
---|
94 | ?{}( *s.inner, rhs );
|
---|
95 | }
|
---|
96 |
|
---|
97 | void ?{}( string & s, long double _Complex rhs ) {
|
---|
98 | (s.inner) { malloc() };
|
---|
99 | ?{}( *s.inner, rhs );
|
---|
100 | }
|
---|
101 |
|
---|
102 | string str( ssize_t rhs ) {
|
---|
103 | string s = rhs;
|
---|
104 | return s;
|
---|
105 | }
|
---|
106 |
|
---|
107 | string str( size_t rhs ) {
|
---|
108 | string s = rhs;
|
---|
109 | return s;
|
---|
110 | }
|
---|
111 |
|
---|
112 | string str( double rhs ) {
|
---|
113 | string s = rhs;
|
---|
114 | return s;
|
---|
115 | }
|
---|
116 |
|
---|
117 | string str( long double rhs ) {
|
---|
118 | string s = rhs;
|
---|
119 | return s;
|
---|
120 | }
|
---|
121 |
|
---|
122 | string str( double _Complex rhs ) {
|
---|
123 | string s = rhs;
|
---|
124 | return s;
|
---|
125 | }
|
---|
126 |
|
---|
127 | string str( long double _Complex rhs ) {
|
---|
128 | string s = rhs;
|
---|
129 | return s;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void ^?{}( string & s ) {
|
---|
133 | ^(*s.inner){};
|
---|
134 | free( s.inner );
|
---|
135 | s.inner = 0p;
|
---|
136 | }
|
---|
137 |
|
---|
138 | ////////////////////////////////////////////////////////
|
---|
139 | // Alternate construction: request shared edits
|
---|
140 |
|
---|
141 | string_WithSharedEdits ?`shareEdits( string & s ) {
|
---|
142 | string_WithSharedEdits ret = { &s };
|
---|
143 | return ret;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void ?{}( string & s, string_WithSharedEdits src ) {
|
---|
147 | ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth );
|
---|
148 | }
|
---|
149 |
|
---|
150 | ////////////////////////////////////////////////////////
|
---|
151 | // Assignment
|
---|
152 |
|
---|
153 | string & ?=?( string & s, const string & c ) {
|
---|
154 | (*s.inner) = (*c.inner);
|
---|
155 | return s;
|
---|
156 | }
|
---|
157 |
|
---|
158 | string & ?=?( string & s, string & c ) {
|
---|
159 | (*s.inner) = (*c.inner);
|
---|
160 | return s;
|
---|
161 | }
|
---|
162 |
|
---|
163 | string & ?=?( string & s, const char * val ) {
|
---|
164 | (*s.inner) = val;
|
---|
165 | return s;
|
---|
166 | }
|
---|
167 |
|
---|
168 | string & ?=?( string & s, char val ) {
|
---|
169 | (*s.inner) = val;
|
---|
170 | return s;
|
---|
171 | }
|
---|
172 |
|
---|
173 | string & assign( string & s, const string & c, size_t n ) {
|
---|
174 | assign( *s.inner, *c.inner, n );
|
---|
175 | return s;
|
---|
176 | }
|
---|
177 |
|
---|
178 | string & assign( string & s, const char * c, size_t n ) {
|
---|
179 | assign( *s.inner, c, n );
|
---|
180 | return s;
|
---|
181 | }
|
---|
182 |
|
---|
183 | string & ?=?( string & s, ssize_t rhs ) {
|
---|
184 | (*s.inner) = rhs;
|
---|
185 | return s;
|
---|
186 | }
|
---|
187 |
|
---|
188 | string & ?=?( string & s, size_t rhs ) {
|
---|
189 | (*s.inner) = rhs;
|
---|
190 | return s;
|
---|
191 | }
|
---|
192 |
|
---|
193 | string & ?=?( string & s, double rhs ) {
|
---|
194 | (*s.inner) = rhs;
|
---|
195 | return s;
|
---|
196 | }
|
---|
197 |
|
---|
198 | string & ?=?( string & s, long double rhs ) {
|
---|
199 | (*s.inner) = rhs;
|
---|
200 | return s;
|
---|
201 | }
|
---|
202 |
|
---|
203 | string & ?=?( string & s, double _Complex rhs ) {
|
---|
204 | (*s.inner) = rhs;
|
---|
205 | return s;
|
---|
206 | }
|
---|
207 |
|
---|
208 | string & ?=?( string & s, long double _Complex rhs ) {
|
---|
209 | (*s.inner) = rhs;
|
---|
210 | return s;
|
---|
211 | }
|
---|
212 |
|
---|
213 | ////////////////////////////////////////////////////////
|
---|
214 | // Input-Output
|
---|
215 |
|
---|
216 | ofstream & ?|?( ofstream & out, const string & s ) {
|
---|
217 | return out | (*s.inner); // print internal string_res
|
---|
218 | }
|
---|
219 |
|
---|
220 | void ?|?( ofstream & out, const string & s ) {
|
---|
221 | (ofstream &)(out | (*s.inner)); ends( out );
|
---|
222 | }
|
---|
223 |
|
---|
224 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
---|
225 | size_t len = size( f.val );
|
---|
226 | char cstr[len + 1]; // room for null terminator
|
---|
227 | for ( i; len ) cstr[i] = f.val[i]; // copy string
|
---|
228 | cstr[len] = '\0'; // terminate
|
---|
229 | _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
|
---|
230 | return os | cf | nonl;
|
---|
231 | } // ?|?
|
---|
232 |
|
---|
233 | void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
---|
234 | (ofstream &)(os | f); ends( os );
|
---|
235 | }
|
---|
236 |
|
---|
237 | ifstream & ?|?( ifstream & in, string & s ) {
|
---|
238 | return in | (*s.inner); // read to internal string_res
|
---|
239 | }
|
---|
240 |
|
---|
241 | ifstream & ?|?( ifstream & is, _Istream_Squoted f ) {
|
---|
242 | _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } };
|
---|
243 | return is | f2;
|
---|
244 | } // ?|?
|
---|
245 |
|
---|
246 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
|
---|
247 | // _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr};
|
---|
248 | _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
|
---|
249 | return is | f2;
|
---|
250 | } // ?|?
|
---|
251 |
|
---|
252 | ////////////////////////////////////////////////////////
|
---|
253 | // Slicing
|
---|
254 |
|
---|
255 | string ?()( string & s, size_t start, size_t len ) {
|
---|
256 | string ret = { *s.inner, start, len };
|
---|
257 | return ret`shareEdits;
|
---|
258 | }
|
---|
259 |
|
---|
260 | string ?()( string & s, size_t start ) {
|
---|
261 | string ret = { *s.inner, start, size( s ) - start };
|
---|
262 | return ret`shareEdits;
|
---|
263 | }
|
---|
264 |
|
---|
265 | ////////////////////////////////////////////////////////
|
---|
266 | // Comparison
|
---|
267 |
|
---|
268 | int strcmp( const string & s1, const string & s2 ) { return strcmp( *s1.inner, *s2.inner ); }
|
---|
269 | bool ?==?( const string & s1, const string & s2 ) { return *s1.inner == *s2.inner; }
|
---|
270 | bool ?!=?( const string & s1, const string & s2 ) { return *s1.inner != *s2.inner; }
|
---|
271 | bool ?>? ( const string & s1, const string & s2 ) { return *s1.inner > *s2.inner; }
|
---|
272 | bool ?>=?( const string & s1, const string & s2 ) { return *s1.inner >= *s2.inner; }
|
---|
273 | bool ?<=?( const string & s1, const string & s2 ) { return *s1.inner <= *s2.inner; }
|
---|
274 | bool ?<? ( const string & s1, const string & s2 ) { return *s1.inner < *s2.inner; }
|
---|
275 |
|
---|
276 | int strcmp( const string & s1, const char * s2 ) { return strcmp( *s1.inner, s2 ); }
|
---|
277 | bool ?==?( const string & s1, const char * s2 ) { return *s1.inner == s2; }
|
---|
278 | bool ?!=?( const string & s1, const char * s2 ) { return *s1.inner != s2; }
|
---|
279 | bool ?>? ( const string & s1, const char * s2 ) { return *s1.inner > s2; }
|
---|
280 | bool ?>=?( const string & s1, const char * s2 ) { return *s1.inner >= s2; }
|
---|
281 | bool ?<=?( const string & s1, const char * s2 ) { return *s1.inner <= s2; }
|
---|
282 | bool ?<? ( const string & s1, const char * s2 ) { return *s1.inner < s2; }
|
---|
283 |
|
---|
284 | int strcmp( const char * s1, const string & s2 ) { return strcmp( s1, *s2.inner ); }
|
---|
285 | bool ?==?( const char * s1, const string & s2 ) { return s1 == *s2.inner; }
|
---|
286 | bool ?!=?( const char * s1, const string & s2 ) { return s1 != *s2.inner; }
|
---|
287 | bool ?>? ( const char * s1, const string & s2 ) { return s1 > *s2.inner; }
|
---|
288 | bool ?>=?( const char * s1, const string & s2 ) { return s1 >= *s2.inner; }
|
---|
289 | bool ?<=?( const char * s1, const string & s2 ) { return s1 <= *s2.inner; }
|
---|
290 | bool ?<? ( const char * s1, const string & s2 ) { return s1 < *s2.inner; }
|
---|
291 |
|
---|
292 |
|
---|
293 | ////////////////////////////////////////////////////////
|
---|
294 | // Getter
|
---|
295 |
|
---|
296 | size_t size( const string & s ) {
|
---|
297 | return size( *s.inner );
|
---|
298 | }
|
---|
299 |
|
---|
300 | ////////////////////////////////////////////////////////
|
---|
301 | // Concatenation
|
---|
302 |
|
---|
303 | void ?+=?( string & s, char c ) {
|
---|
304 | (*s.inner) += c;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void ?+=?( string & s, const string & s2 ) {
|
---|
308 | (*s.inner) += (*s2.inner);
|
---|
309 | }
|
---|
310 |
|
---|
311 | void append( string & s, const string & s2, size_t maxlen ) {
|
---|
312 | append( (*s.inner), (*s2.inner), maxlen );
|
---|
313 | }
|
---|
314 |
|
---|
315 | void ?+=?( string & s, const char * c ) {
|
---|
316 | (*s.inner) += c;
|
---|
317 | }
|
---|
318 |
|
---|
319 | void append( string & s, const char * buffer, size_t bsize ) {
|
---|
320 | append( (*s.inner), buffer, bsize );
|
---|
321 | }
|
---|
322 |
|
---|
323 | string ?+?( const string & s, char c ) {
|
---|
324 | string ret = s;
|
---|
325 | ret += c;
|
---|
326 | return ret;
|
---|
327 | }
|
---|
328 |
|
---|
329 | string ?+?( char c, const string & s ) {
|
---|
330 | string ret = s;
|
---|
331 | ret += c;
|
---|
332 | return ret;
|
---|
333 | }
|
---|
334 |
|
---|
335 | string ?+?( const string & s, const string & s2 ) {
|
---|
336 | string ret = s;
|
---|
337 | ret += s2;
|
---|
338 | return ret;
|
---|
339 | }
|
---|
340 |
|
---|
341 | string ?+?( const char * s, char c ) {
|
---|
342 | string ret = s;
|
---|
343 | ret += c;
|
---|
344 | return ret;
|
---|
345 | }
|
---|
346 |
|
---|
347 | string ?+?( char c, const char * s ) {
|
---|
348 | string ret = c;
|
---|
349 | ret += s;
|
---|
350 | return ret;
|
---|
351 | }
|
---|
352 |
|
---|
353 | string ?+?( const char * s1, const char * s2 ) {
|
---|
354 | string ret = s1;
|
---|
355 | ret += s2;
|
---|
356 | return ret;
|
---|
357 | }
|
---|
358 |
|
---|
359 | string ?+?( const char * s1, string & s2 ) {
|
---|
360 | string ret = s1;
|
---|
361 | ret += s2;
|
---|
362 | return ret;
|
---|
363 | }
|
---|
364 |
|
---|
365 | string ?+?( const string & s, const char * c ) {
|
---|
366 | string ret = s;
|
---|
367 | ret += c;
|
---|
368 | return ret;
|
---|
369 | }
|
---|
370 |
|
---|
371 | ////////////////////////////////////////////////////////
|
---|
372 | // Repetition
|
---|
373 |
|
---|
374 | void ?*=?( string & s, size_t factor ) {
|
---|
375 | (*s.inner) *= factor;
|
---|
376 | }
|
---|
377 |
|
---|
378 | string ?*?( const string & s, size_t factor ) {
|
---|
379 | string ret = s;
|
---|
380 | ret *= factor;
|
---|
381 | return ret;
|
---|
382 | }
|
---|
383 |
|
---|
384 | string ?*?( char c, size_t factor ) {
|
---|
385 | string ret = c;
|
---|
386 | ret *= factor;
|
---|
387 | return ret;
|
---|
388 | }
|
---|
389 |
|
---|
390 | string ?*?( const char * s, size_t factor ) {
|
---|
391 | string ret = s;
|
---|
392 | ret *= factor;
|
---|
393 | return ret;
|
---|
394 | }
|
---|
395 |
|
---|
396 | ////////////////////////////////////////////////////////
|
---|
397 | // Character access
|
---|
398 |
|
---|
399 | char ?[?]( const string & s, size_t index ) {
|
---|
400 | return (*s.inner)[index];
|
---|
401 | }
|
---|
402 |
|
---|
403 | string ?[?]( string & s, size_t index ) {
|
---|
404 | string ret = { *s.inner, index, 1 };
|
---|
405 | return ret`shareEdits;
|
---|
406 | }
|
---|
407 |
|
---|
408 | ////////////////////////////////////////////////////////
|
---|
409 | // Search
|
---|
410 |
|
---|
411 | bool contains( const string & s, char ch ) {
|
---|
412 | return contains( *s.inner, ch );
|
---|
413 | }
|
---|
414 |
|
---|
415 | int find( const string & s, char search ) {
|
---|
416 | return find( *s.inner, search );
|
---|
417 | }
|
---|
418 |
|
---|
419 | int find( const string & s, const string & search ) {
|
---|
420 | return find( *s.inner, *search.inner );
|
---|
421 | }
|
---|
422 |
|
---|
423 | int find( const string & s, const char * search ) {
|
---|
424 | return find( *s.inner, search );
|
---|
425 | }
|
---|
426 |
|
---|
427 | int find( const string & s, const char * search, size_t searchsize ) {
|
---|
428 | return find( *s.inner, search, searchsize );
|
---|
429 | }
|
---|
430 |
|
---|
431 | int findFrom( const string & s, size_t fromPos, char search ) {
|
---|
432 | return findFrom( *s.inner, fromPos, search );
|
---|
433 | }
|
---|
434 |
|
---|
435 | int findFrom( const string & s, size_t fromPos, const string & search ) {
|
---|
436 | return findFrom( *s.inner, fromPos, *search.inner );
|
---|
437 | }
|
---|
438 |
|
---|
439 | int findFrom( const string & s, size_t fromPos, const char * search ) {
|
---|
440 | return findFrom( *s.inner, fromPos, search );
|
---|
441 | }
|
---|
442 |
|
---|
443 | int findFrom( const string & s, size_t fromPos, const char * search, size_t searchsize ) {
|
---|
444 | return findFrom( *s.inner, fromPos, search, searchsize );
|
---|
445 | }
|
---|
446 |
|
---|
447 | bool includes( const string & s, const string & search ) {
|
---|
448 | return includes( *s.inner, *search.inner );
|
---|
449 | }
|
---|
450 |
|
---|
451 | bool includes( const string & s, const char * search ) {
|
---|
452 | return includes( *s.inner, search );
|
---|
453 | }
|
---|
454 |
|
---|
455 | bool includes( const string & s, const char * search, size_t searchsize ) {
|
---|
456 | return includes( *s.inner, search, searchsize );
|
---|
457 | }
|
---|
458 |
|
---|
459 | bool startsWith( const string & s, const string & prefix ) {
|
---|
460 | return startsWith( *s.inner, *prefix.inner );
|
---|
461 | }
|
---|
462 |
|
---|
463 | bool startsWith( const string & s, const char * prefix ) {
|
---|
464 | return startsWith( *s.inner, prefix );
|
---|
465 | }
|
---|
466 |
|
---|
467 | bool startsWith( const string & s, const char * prefix, size_t prefixsize ) {
|
---|
468 | return startsWith( *s.inner, prefix, prefixsize );
|
---|
469 | }
|
---|
470 |
|
---|
471 | bool endsWith( const string & s, const string & suffix ) {
|
---|
472 | return endsWith( *s.inner, *suffix.inner );
|
---|
473 | }
|
---|
474 |
|
---|
475 | bool endsWith( const string & s, const char * suffix ) {
|
---|
476 | return endsWith( *s.inner, suffix );
|
---|
477 | }
|
---|
478 |
|
---|
479 | bool endsWith( const string & s, const char * suffix, size_t suffixsize ) {
|
---|
480 | return endsWith( *s.inner, suffix, suffixsize );
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | ///////////////////////////////////////////////////////////////////////////
|
---|
485 | // charclass, include, exclude
|
---|
486 |
|
---|
487 | void ?{}( charclass & s, const string & chars ) {
|
---|
488 | (s.inner) { malloc() };
|
---|
489 | ?{}( *s.inner, *(const string_res *)chars.inner );
|
---|
490 | }
|
---|
491 |
|
---|
492 | void ?{}( charclass & s, const char * chars ) {
|
---|
493 | (s.inner) { malloc() };
|
---|
494 | ?{}( *s.inner, chars );
|
---|
495 | }
|
---|
496 |
|
---|
497 | void ?{}( charclass & s, const char * chars, size_t charssize ) {
|
---|
498 | (s.inner) { malloc() };
|
---|
499 | ?{}( *s.inner, chars, charssize );
|
---|
500 | }
|
---|
501 |
|
---|
502 | void ^?{}( charclass & s ) {
|
---|
503 | ^(*s.inner){};
|
---|
504 | free( s.inner );
|
---|
505 | s.inner = 0p;
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | int exclude( const string & s, const charclass & mask ) {
|
---|
510 | return exclude( *s.inner, *mask.inner );
|
---|
511 | }
|
---|
512 | /*
|
---|
513 | StrSlice exclude( string & s, const charclass & mask ) {
|
---|
514 | }
|
---|
515 | */
|
---|
516 |
|
---|
517 | int include( const string & s, const charclass & mask ) {
|
---|
518 | return include( *s.inner, *mask.inner );
|
---|
519 | }
|
---|
520 |
|
---|
521 | /*
|
---|
522 | StrSlice include( string & s, const charclass & mask ) {
|
---|
523 | }
|
---|
524 | */
|
---|