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 Feb 7 21:17:06 2024
|
---|
13 | // Update Count : 259
|
---|
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 | void ^?{}( string & s ) {
|
---|
103 | ^(*s.inner){};
|
---|
104 | free( s.inner );
|
---|
105 | s.inner = 0p;
|
---|
106 | }
|
---|
107 |
|
---|
108 | ////////////////////////////////////////////////////////
|
---|
109 | // Alternate construction: request shared edits
|
---|
110 |
|
---|
111 | string_WithSharedEdits ?`shareEdits( string & s ) {
|
---|
112 | string_WithSharedEdits ret = { &s };
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void ?{}( string & s, string_WithSharedEdits src ) {
|
---|
117 | ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth);
|
---|
118 | }
|
---|
119 |
|
---|
120 | ////////////////////////////////////////////////////////
|
---|
121 | // Assignment
|
---|
122 |
|
---|
123 | string & ?=?(string & s, const string & c) {
|
---|
124 | (*s.inner) = (*c.inner);
|
---|
125 | return s;
|
---|
126 | }
|
---|
127 |
|
---|
128 | string & ?=?(string & s, string & c) {
|
---|
129 | (*s.inner) = (*c.inner);
|
---|
130 | return s;
|
---|
131 | }
|
---|
132 |
|
---|
133 | string & ?=?( string & s, const char * val ) {
|
---|
134 | (*s.inner) = val;
|
---|
135 | return s;
|
---|
136 | }
|
---|
137 |
|
---|
138 | string & ?=?( string & s, char val ) {
|
---|
139 | (*s.inner) = val;
|
---|
140 | return s;
|
---|
141 | }
|
---|
142 |
|
---|
143 | string & assign(string & s, const string & c, size_t n) {
|
---|
144 | assign(*s.inner, *c.inner, n);
|
---|
145 | return s;
|
---|
146 | }
|
---|
147 |
|
---|
148 | string & assign(string & s, const char * c, size_t n) {
|
---|
149 | assign(*s.inner, c, n);
|
---|
150 | return s;
|
---|
151 | }
|
---|
152 |
|
---|
153 | string & ?=?( string & s, ssize_t rhs ) {
|
---|
154 | (*s.inner) = rhs;
|
---|
155 | return s;
|
---|
156 | }
|
---|
157 |
|
---|
158 | string & ?=?( string & s, size_t rhs ) {
|
---|
159 | (*s.inner) = rhs;
|
---|
160 | return s;
|
---|
161 | }
|
---|
162 |
|
---|
163 | string & ?=?( string & s, double rhs ) {
|
---|
164 | (*s.inner) = rhs;
|
---|
165 | return s;
|
---|
166 | }
|
---|
167 |
|
---|
168 | string & ?=?( string & s, long double rhs ) {
|
---|
169 | (*s.inner) = rhs;
|
---|
170 | return s;
|
---|
171 | }
|
---|
172 |
|
---|
173 | string & ?=?( string & s, double _Complex rhs ) {
|
---|
174 | (*s.inner) = rhs;
|
---|
175 | return s;
|
---|
176 | }
|
---|
177 |
|
---|
178 | string & ?=?( string & s, long double _Complex rhs ) {
|
---|
179 | (*s.inner) = rhs;
|
---|
180 | return s;
|
---|
181 | }
|
---|
182 |
|
---|
183 | ////////////////////////////////////////////////////////
|
---|
184 | // Input-Output
|
---|
185 |
|
---|
186 | ofstream & ?|?( ofstream & out, const string & s ) {
|
---|
187 | return out | (*s.inner); // print internal string_res
|
---|
188 | }
|
---|
189 |
|
---|
190 | void ?|?( ofstream & out, const string & s ) {
|
---|
191 | (ofstream &)(out | (*s.inner)); ends( out );
|
---|
192 | }
|
---|
193 |
|
---|
194 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
---|
195 | size_t len = size( f.val );
|
---|
196 | char cstr[len + 1]; // room for null terminator
|
---|
197 | for ( i; len ) cstr[i] = f.val[i]; // copy string
|
---|
198 | cstr[len] = '\0'; // terminate
|
---|
199 | _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
|
---|
200 | os | cf | nonl;
|
---|
201 | return os;
|
---|
202 | } // ?|?
|
---|
203 |
|
---|
204 | void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
---|
205 | (ofstream &)(os | f); ends( os );
|
---|
206 | }
|
---|
207 |
|
---|
208 | ifstream & ?|?(ifstream & in, string & s) {
|
---|
209 | return in | (*s.inner); // read to internal string_res
|
---|
210 | }
|
---|
211 |
|
---|
212 | ifstream & ?|?( ifstream & is, _Istream_Squoted f ) {
|
---|
213 | _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } };
|
---|
214 | return is | f2;
|
---|
215 | } // ?|?
|
---|
216 |
|
---|
217 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
|
---|
218 | // _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr};
|
---|
219 | _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
|
---|
220 | return is | f2;
|
---|
221 | } // ?|?
|
---|
222 |
|
---|
223 | ////////////////////////////////////////////////////////
|
---|
224 | // Slicing
|
---|
225 |
|
---|
226 | string ?()( string & s, size_t start, size_t len ) {
|
---|
227 | string ret = { *s.inner, start, len };
|
---|
228 | return ret`shareEdits;
|
---|
229 | }
|
---|
230 |
|
---|
231 | string ?()( string & s, size_t start ) {
|
---|
232 | string ret = { *s.inner, start, size( s ) - start };
|
---|
233 | return ret`shareEdits;
|
---|
234 | }
|
---|
235 |
|
---|
236 | ////////////////////////////////////////////////////////
|
---|
237 | // Comparison
|
---|
238 |
|
---|
239 | int strcmp(const string & s1, const string & s2) { return strcmp(*s1.inner, *s2.inner); }
|
---|
240 | bool ?==?(const string & s1, const string & s2) { return *s1.inner == *s2.inner; }
|
---|
241 | bool ?!=?(const string & s1, const string & s2) { return *s1.inner != *s2.inner; }
|
---|
242 | bool ?>? (const string & s1, const string & s2) { return *s1.inner > *s2.inner; }
|
---|
243 | bool ?>=?(const string & s1, const string & s2) { return *s1.inner >= *s2.inner; }
|
---|
244 | bool ?<=?(const string & s1, const string & s2) { return *s1.inner <= *s2.inner; }
|
---|
245 | bool ?<? (const string & s1, const string & s2) { return *s1.inner < *s2.inner; }
|
---|
246 |
|
---|
247 | int strcmp(const string & s1, const char * s2) { return strcmp(*s1.inner, s2 ); }
|
---|
248 | bool ?==?(const string & s1, const char * s2) { return *s1.inner == s2; }
|
---|
249 | bool ?!=?(const string & s1, const char * s2) { return *s1.inner != s2; }
|
---|
250 | bool ?>? (const string & s1, const char * s2) { return *s1.inner > s2; }
|
---|
251 | bool ?>=?(const string & s1, const char * s2) { return *s1.inner >= s2; }
|
---|
252 | bool ?<=?(const string & s1, const char * s2) { return *s1.inner <= s2; }
|
---|
253 | bool ?<? (const string & s1, const char * s2) { return *s1.inner < s2; }
|
---|
254 |
|
---|
255 | int strcmp(const char * s1, const string & s2) { return strcmp( s1, *s2.inner); }
|
---|
256 | bool ?==?(const char * s1, const string & s2) { return s1 == *s2.inner; }
|
---|
257 | bool ?!=?(const char * s1, const string & s2) { return s1 != *s2.inner; }
|
---|
258 | bool ?>? (const char * s1, const string & s2) { return s1 > *s2.inner; }
|
---|
259 | bool ?>=?(const char * s1, const string & s2) { return s1 >= *s2.inner; }
|
---|
260 | bool ?<=?(const char * s1, const string & s2) { return s1 <= *s2.inner; }
|
---|
261 | bool ?<? (const char * s1, const string & s2) { return s1 < *s2.inner; }
|
---|
262 |
|
---|
263 |
|
---|
264 | ////////////////////////////////////////////////////////
|
---|
265 | // Getter
|
---|
266 |
|
---|
267 | size_t size(const string & s) {
|
---|
268 | return size( *s.inner );
|
---|
269 | }
|
---|
270 |
|
---|
271 | ////////////////////////////////////////////////////////
|
---|
272 | // Concatenation
|
---|
273 |
|
---|
274 | void ?+=?(string & s, char c) {
|
---|
275 | (*s.inner) += c;
|
---|
276 | }
|
---|
277 |
|
---|
278 | void ?+=?(string & s, const string & s2) {
|
---|
279 | (*s.inner) += (*s2.inner);
|
---|
280 | }
|
---|
281 |
|
---|
282 | void append(string & s, const string & s2, size_t maxlen) {
|
---|
283 | append( (*s.inner), (*s2.inner), maxlen );
|
---|
284 | }
|
---|
285 |
|
---|
286 | void ?+=?(string & s, const char * c) {
|
---|
287 | (*s.inner) += c;
|
---|
288 | }
|
---|
289 |
|
---|
290 | void append(string & s, const char * buffer, size_t bsize) {
|
---|
291 | append( (*s.inner), buffer, bsize );
|
---|
292 | }
|
---|
293 |
|
---|
294 | string ?+?(const string & s, char c) {
|
---|
295 | string ret = s;
|
---|
296 | ret += c;
|
---|
297 | return ret;
|
---|
298 | }
|
---|
299 |
|
---|
300 | string ?+?(const string & s, const string & s2) {
|
---|
301 | string ret = s;
|
---|
302 | ret += s2;
|
---|
303 | return ret;
|
---|
304 | }
|
---|
305 |
|
---|
306 | string ?+?(const char * s1, const char * s2) {
|
---|
307 | string ret = s1;
|
---|
308 | ret += s2;
|
---|
309 | return ret;
|
---|
310 | }
|
---|
311 |
|
---|
312 | string ?+?(const string & s, const char * c) {
|
---|
313 | string ret = s;
|
---|
314 | ret += c;
|
---|
315 | return ret;
|
---|
316 | }
|
---|
317 |
|
---|
318 | ////////////////////////////////////////////////////////
|
---|
319 | // Repetition
|
---|
320 |
|
---|
321 | void ?*=?(string & s, size_t factor) {
|
---|
322 | (*s.inner) *= factor;
|
---|
323 | }
|
---|
324 |
|
---|
325 | string ?*?(const string & s, size_t factor) {
|
---|
326 | string ret = s;
|
---|
327 | ret *= factor;
|
---|
328 | return ret;
|
---|
329 | }
|
---|
330 |
|
---|
331 | string ?*?(char c, size_t factor) {
|
---|
332 | string ret = c;
|
---|
333 | ret *= factor;
|
---|
334 | return ret;
|
---|
335 | }
|
---|
336 |
|
---|
337 | string ?*?(const char * s, size_t factor) {
|
---|
338 | string ret = s;
|
---|
339 | ret *= factor;
|
---|
340 | return ret;
|
---|
341 | }
|
---|
342 |
|
---|
343 | ////////////////////////////////////////////////////////
|
---|
344 | // Character access
|
---|
345 |
|
---|
346 | char ?[?](const string & s, size_t index) {
|
---|
347 | return (*s.inner)[index];
|
---|
348 | }
|
---|
349 |
|
---|
350 | string ?[?](string & s, size_t index) {
|
---|
351 | string ret = { *s.inner, index, 1 };
|
---|
352 | return ret`shareEdits;
|
---|
353 | }
|
---|
354 |
|
---|
355 | ////////////////////////////////////////////////////////
|
---|
356 | // Search
|
---|
357 |
|
---|
358 | bool contains(const string & s, char ch) {
|
---|
359 | return contains( *s.inner, ch );
|
---|
360 | }
|
---|
361 |
|
---|
362 | int find(const string & s, char search) {
|
---|
363 | return find( *s.inner, search );
|
---|
364 | }
|
---|
365 |
|
---|
366 | int find(const string & s, const string & search) {
|
---|
367 | return find( *s.inner, *search.inner );
|
---|
368 | }
|
---|
369 |
|
---|
370 | int find(const string & s, const char * search) {
|
---|
371 | return find( *s.inner, search);
|
---|
372 | }
|
---|
373 |
|
---|
374 | int find(const string & s, const char * search, size_t searchsize) {
|
---|
375 | return find( *s.inner, search, searchsize);
|
---|
376 | }
|
---|
377 |
|
---|
378 | int findFrom(const string & s, size_t fromPos, char search) {
|
---|
379 | return findFrom( *s.inner, fromPos, search );
|
---|
380 | }
|
---|
381 |
|
---|
382 | int findFrom(const string & s, size_t fromPos, const string & search) {
|
---|
383 | return findFrom( *s.inner, fromPos, *search.inner );
|
---|
384 | }
|
---|
385 |
|
---|
386 | int findFrom(const string & s, size_t fromPos, const char * search) {
|
---|
387 | return findFrom( *s.inner, fromPos, search );
|
---|
388 | }
|
---|
389 |
|
---|
390 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
|
---|
391 | return findFrom( *s.inner, fromPos, search, searchsize );
|
---|
392 | }
|
---|
393 |
|
---|
394 | bool includes(const string & s, const string & search) {
|
---|
395 | return includes( *s.inner, *search.inner );
|
---|
396 | }
|
---|
397 |
|
---|
398 | bool includes(const string & s, const char * search) {
|
---|
399 | return includes( *s.inner, search );
|
---|
400 | }
|
---|
401 |
|
---|
402 | bool includes(const string & s, const char * search, size_t searchsize) {
|
---|
403 | return includes( *s.inner, search, searchsize );
|
---|
404 | }
|
---|
405 |
|
---|
406 | bool startsWith(const string & s, const string & prefix) {
|
---|
407 | return startsWith( *s.inner, *prefix.inner );
|
---|
408 | }
|
---|
409 |
|
---|
410 | bool startsWith(const string & s, const char * prefix) {
|
---|
411 | return startsWith( *s.inner, prefix );
|
---|
412 | }
|
---|
413 |
|
---|
414 | bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
|
---|
415 | return startsWith( *s.inner, prefix, prefixsize );
|
---|
416 | }
|
---|
417 |
|
---|
418 | bool endsWith(const string & s, const string & suffix) {
|
---|
419 | return endsWith( *s.inner, *suffix.inner );
|
---|
420 | }
|
---|
421 |
|
---|
422 | bool endsWith(const string & s, const char * suffix) {
|
---|
423 | return endsWith( *s.inner, suffix );
|
---|
424 | }
|
---|
425 |
|
---|
426 | bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
|
---|
427 | return endsWith( *s.inner, suffix, suffixsize );
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | ///////////////////////////////////////////////////////////////////////////
|
---|
432 | // charclass, include, exclude
|
---|
433 |
|
---|
434 | void ?{}( charclass & s, const string & chars) {
|
---|
435 | (s.inner) { malloc() };
|
---|
436 | ?{}( *s.inner, *(const string_res *)chars.inner );
|
---|
437 | }
|
---|
438 |
|
---|
439 | void ?{}( charclass & s, const char * chars ) {
|
---|
440 | (s.inner) { malloc() };
|
---|
441 | ?{}( *s.inner, chars );
|
---|
442 | }
|
---|
443 |
|
---|
444 | void ?{}( charclass & s, const char * chars, size_t charssize ) {
|
---|
445 | (s.inner) { malloc() };
|
---|
446 | ?{}( *s.inner, chars, charssize );
|
---|
447 | }
|
---|
448 |
|
---|
449 | void ^?{}( charclass & s ) {
|
---|
450 | ^(*s.inner){};
|
---|
451 | free( s.inner );
|
---|
452 | s.inner = 0p;
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | int exclude(const string & s, const charclass & mask) {
|
---|
457 | return exclude( *s.inner, *mask.inner );
|
---|
458 | }
|
---|
459 | /*
|
---|
460 | StrSlice exclude(string & s, const charclass & mask) {
|
---|
461 | }
|
---|
462 | */
|
---|
463 |
|
---|
464 | int include(const string & s, const charclass & mask) {
|
---|
465 | return include( *s.inner, *mask.inner );
|
---|
466 | }
|
---|
467 |
|
---|
468 | /*
|
---|
469 | StrSlice include(string & s, const charclass & mask) {
|
---|
470 | }
|
---|
471 | */
|
---|
472 |
|
---|