source: libcfa/src/collections/string.cfa @ a7efc96

Last change on this file since a7efc96 was 4dab7e8, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

formatting, add str conversion routines, add more combinations of ?+? routines

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