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

Last change on this file since a55ebcc was f2898df, checked in by Michael Brooks <mlbrooks@…>, 22 months ago

Implement string initialization and assignment from various numeric types

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[f450f2f]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
[bc9f84a]11// Last Modified By : Peter A. Buhr
[479fbe3]12// Last Modified On : Sun Jan 14 12:03:47 2024
13// Update Count : 240
[f450f2f]14//
15
16#include "string.hfa"
17#include "string_res.hfa"
18#include <stdlib.hfa>
19
[accc9df9]20#pragma GCC visibility push(default)
[f450f2f]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)
[e8b3717]32static void ?{}( string & s, string_res & src, size_t start, size_t len ) {
[681e12f]33 (s.inner) { malloc() };
[e8b3717]34 ?{}( *s.inner, src, SHARE_EDITS, start, len );
[f450f2f]35}
36
[479fbe3]37void ?{}( string & s ) {
38 (s.inner) { malloc() };
39 ?{}( *s.inner );
40}
41
[681e12f]42void ?{}( string & s, const string & c ) {
43 (s.inner) { malloc() };
44 ?{}( *s.inner, *c.inner, COPY_VALUE );
[f450f2f]45}
46
[7abc3de]47void ?{}( string & s, const string & s2, size_t maxlen) {
48 (s.inner) { malloc() };
49 ?{}( *s.inner, *s2.inner, COPY_VALUE, maxlen );
50}
51
52
[681e12f]53void ?{}( string & s, string & c ) {
54 ?{}( s, (const string &) c );
[f450f2f]55}
56
[479fbe3]57void ?{}( string & s, const char c ) {
[681e12f]58 (s.inner) { malloc() };
[7abc3de]59 ?{}( *s.inner, c );
[f450f2f]60}
61
[479fbe3]62void ?{}( string & s, const char * c ) {
[681e12f]63 (s.inner) { malloc() };
[479fbe3]64 ?{}( *s.inner, c );
65}
66
67void ?{}( string & s, const char * c, size_t size) {
68 (s.inner) { malloc() };
69 ?{}( *s.inner, c, size );
[f450f2f]70}
71
[f2898df]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
[681e12f]102void ^?{}( string & s ) {
103 ^(*s.inner){};
104 free( s.inner );
105 s.inner = 0p;
[f450f2f]106}
107
108////////////////////////////////////////////////////////
109// Alternate construction: request shared edits
110
[681e12f]111string_WithSharedEdits ?`shareEdits( string & s ) {
112 string_WithSharedEdits ret = { &s };
[f450f2f]113 return ret;
114}
115
[681e12f]116void ?{}( string & s, string_WithSharedEdits src ) {
117 ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth);
[f450f2f]118}
119
120////////////////////////////////////////////////////////
121// Assignment
122
[b1eefe50]123string & ?=?(string & s, const string & c) {
[681e12f]124 (*s.inner) = (*c.inner);
[b1eefe50]125 return s;
[f450f2f]126}
[b1eefe50]127
[906d8fa]128string & ?=?(string & s, string & c) {
129 (*s.inner) = (*c.inner);
130 return s;
131}
[f450f2f]132
[b1eefe50]133string & ?=?( string & s, const char * val ) {
[681e12f]134 (*s.inner) = val;
[b1eefe50]135 return s;
[f450f2f]136}
137
[b1eefe50]138string & ?=?( string & s, char val ) {
139 (*s.inner) = val;
140 return s;
141}
142
143string & assign(string & s, const string & c, size_t n) {
[e891349]144 assign(*s.inner, *c.inner, n);
[b1eefe50]145 return s;
[e891349]146}
[b1eefe50]147
148string & assign(string & s, const char * c, size_t n) {
[e891349]149 assign(*s.inner, c, n);
[b1eefe50]150 return s;
[e891349]151}
152
[f2898df]153string & ?=?( string & s, ssize_t rhs ) {
154 (*s.inner) = rhs;
155 return s;
156}
157
158string & ?=?( string & s, size_t rhs ) {
159 (*s.inner) = rhs;
160 return s;
161}
162
163string & ?=?( string & s, double rhs ) {
164 (*s.inner) = rhs;
165 return s;
166}
167
168string & ?=?( string & s, long double rhs ) {
169 (*s.inner) = rhs;
170 return s;
171}
172
173string & ?=?( string & s, double _Complex rhs ) {
174 (*s.inner) = rhs;
175 return s;
176}
177
178string & ?=?( string & s, long double _Complex rhs ) {
179 (*s.inner) = rhs;
180 return s;
181}
[f450f2f]182
183////////////////////////////////////////////////////////
[d32679d5]184// Input-Output
[f450f2f]185
[681e12f]186ofstream & ?|?( ofstream & out, const string & s ) {
187 return out | (*s.inner); // print internal string_res
[f450f2f]188}
189
[681e12f]190void ?|?( ofstream & out, const string & s ) {
191 (ofstream &)(out | (*s.inner)); ends( out );
[f450f2f]192}
193
[34c6e1e6]194ofstream & ?|?( 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
204void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
205 (ofstream &)(os | f); ends( os );
206}
207
[681e12f]208ifstream & ?|?(ifstream & in, string & s) {
209 return in | (*s.inner); // read to internal string_res
[d32679d5]210}
211
[681e12f]212void ?|?( ifstream & in, string & s ) {
213 in | (*s.inner);
[6264087]214}
[d32679d5]215
[34c6e1e6]216ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
[737988b]217 _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
218 return is | f2;
[7e1dbd7]219} // ?|?
220
[34c6e1e6]221void ?|?( ifstream & in, _Istream_Sstr f ) {
[f842032]222 (ifstream &)(in | f);
[7e1dbd7]223}
224
[f450f2f]225////////////////////////////////////////////////////////
226// Slicing
227
[e8b3717]228string ?()( string & s, size_t start, size_t len ) {
229 string ret = { *s.inner, start, len };
[f450f2f]230 return ret`shareEdits;
231}
232
[681e12f]233string ?()( string & s, size_t start ) {
[e8b3717]234 string ret = { *s.inner, start, size( s ) - start };
[bc9f84a]235 return ret`shareEdits;
236}
237
[f450f2f]238////////////////////////////////////////////////////////
239// Comparison
240
[681e12f]241int strcmp(const string & s1, const string & s2) { return strcmp(*s1.inner, *s2.inner); }
242bool ?==?(const string & s1, const string & s2) { return *s1.inner == *s2.inner; }
243bool ?!=?(const string & s1, const string & s2) { return *s1.inner != *s2.inner; }
244bool ?>? (const string & s1, const string & s2) { return *s1.inner > *s2.inner; }
245bool ?>=?(const string & s1, const string & s2) { return *s1.inner >= *s2.inner; }
246bool ?<=?(const string & s1, const string & s2) { return *s1.inner <= *s2.inner; }
247bool ?<? (const string & s1, const string & s2) { return *s1.inner < *s2.inner; }
248
249int strcmp(const string & s1, const char * s2) { return strcmp(*s1.inner, s2 ); }
250bool ?==?(const string & s1, const char * s2) { return *s1.inner == s2; }
251bool ?!=?(const string & s1, const char * s2) { return *s1.inner != s2; }
252bool ?>? (const string & s1, const char * s2) { return *s1.inner > s2; }
253bool ?>=?(const string & s1, const char * s2) { return *s1.inner >= s2; }
254bool ?<=?(const string & s1, const char * s2) { return *s1.inner <= s2; }
255bool ?<? (const string & s1, const char * s2) { return *s1.inner < s2; }
256
257int strcmp(const char * s1, const string & s2) { return strcmp( s1, *s2.inner); }
258bool ?==?(const char * s1, const string & s2) { return s1 == *s2.inner; }
259bool ?!=?(const char * s1, const string & s2) { return s1 != *s2.inner; }
260bool ?>? (const char * s1, const string & s2) { return s1 > *s2.inner; }
261bool ?>=?(const char * s1, const string & s2) { return s1 >= *s2.inner; }
262bool ?<=?(const char * s1, const string & s2) { return s1 <= *s2.inner; }
263bool ?<? (const char * s1, const string & s2) { return s1 < *s2.inner; }
[f450f2f]264
265
266////////////////////////////////////////////////////////
267// Getter
268
[6264087]269size_t size(const string & s) {
[681e12f]270 return size( *s.inner );
[f450f2f]271}
272
273////////////////////////////////////////////////////////
274// Concatenation
275
[681e12f]276void ?+=?(string & s, char c) {
277 (*s.inner) += c;
[f450f2f]278}
279
[6264087]280void ?+=?(string & s, const string & s2) {
[f450f2f]281 (*s.inner) += (*s2.inner);
282}
283
[e891349]284void append(string & s, const string & s2, size_t maxlen) {
285 append( (*s.inner), (*s2.inner), maxlen );
286}
287
[681e12f]288void ?+=?(string & s, const char * c) {
289 (*s.inner) += c;
[f450f2f]290}
291
[e891349]292void append(string & s, const char * buffer, size_t bsize) {
293 append( (*s.inner), buffer, bsize );
294}
295
[681e12f]296string ?+?(const string & s, char c) {
[f450f2f]297 string ret = s;
[681e12f]298 ret += c;
[f450f2f]299 return ret;
300}
301
[6264087]302string ?+?(const string & s, const string & s2) {
[f450f2f]303 string ret = s;
304 ret += s2;
305 return ret;
306}
307
[6264087]308string ?+?(const char * s1, const char * s2) {
[f450f2f]309 string ret = s1;
310 ret += s2;
311 return ret;
312}
313
[681e12f]314string ?+?(const string & s, const char * c) {
[f450f2f]315 string ret = s;
[681e12f]316 ret += c;
[f450f2f]317 return ret;
318}
319
320////////////////////////////////////////////////////////
321// Repetition
322
[38951c31]323void ?*=?(string & s, size_t factor) {
324 (*s.inner) *= factor;
[f450f2f]325}
326
[38951c31]327string ?*?(const string & s, size_t factor) {
328 string ret = s;
329 ret *= factor;
330 return ret;
[479fbe3]331}
332
333string ?*?(char c, size_t factor) {
334 string ret = c;
[38951c31]335 ret *= factor;
336 return ret;
[f450f2f]337}
338
[479fbe3]339string ?*?(const char * s, size_t factor) {
340 string ret = s;
[38951c31]341 ret *= factor;
342 return ret;
[f450f2f]343}
344
345////////////////////////////////////////////////////////
346// Character access
347
[6264087]348char ?[?](const string & s, size_t index) {
[f450f2f]349 return (*s.inner)[index];
350}
351
[6264087]352string ?[?](string & s, size_t index) {
[e8b3717]353 string ret = { *s.inner, index, 1 };
[f450f2f]354 return ret`shareEdits;
355}
356
357////////////////////////////////////////////////////////
358// Search
359
[6264087]360bool contains(const string & s, char ch) {
[f450f2f]361 return contains( *s.inner, ch );
362}
363
[6264087]364int find(const string & s, char search) {
[f450f2f]365 return find( *s.inner, search );
366}
367
[6264087]368int find(const string & s, const string & search) {
[f450f2f]369 return find( *s.inner, *search.inner );
370}
371
[6264087]372int find(const string & s, const char * search) {
[f450f2f]373 return find( *s.inner, search);
374}
375
[6264087]376int find(const string & s, const char * search, size_t searchsize) {
[f450f2f]377 return find( *s.inner, search, searchsize);
378}
379
[6264087]380int findFrom(const string & s, size_t fromPos, char search) {
[08ed947]381 return findFrom( *s.inner, fromPos, search );
382}
383
[6264087]384int findFrom(const string & s, size_t fromPos, const string & search) {
[08ed947]385 return findFrom( *s.inner, fromPos, *search.inner );
386}
387
[6264087]388int findFrom(const string & s, size_t fromPos, const char * search) {
[08ed947]389 return findFrom( *s.inner, fromPos, search );
390}
391
[6264087]392int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
[08ed947]393 return findFrom( *s.inner, fromPos, search, searchsize );
394}
395
[6264087]396bool includes(const string & s, const string & search) {
[f450f2f]397 return includes( *s.inner, *search.inner );
398}
399
[6264087]400bool includes(const string & s, const char * search) {
[f450f2f]401 return includes( *s.inner, search );
402}
403
[6264087]404bool includes(const string & s, const char * search, size_t searchsize) {
[f450f2f]405 return includes( *s.inner, search, searchsize );
406}
407
[6264087]408bool startsWith(const string & s, const string & prefix) {
[f450f2f]409 return startsWith( *s.inner, *prefix.inner );
410}
411
[6264087]412bool startsWith(const string & s, const char * prefix) {
[f450f2f]413 return startsWith( *s.inner, prefix );
414}
415
[6264087]416bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
[f450f2f]417 return startsWith( *s.inner, prefix, prefixsize );
418}
419
[6264087]420bool endsWith(const string & s, const string & suffix) {
[f450f2f]421 return endsWith( *s.inner, *suffix.inner );
422}
423
[6264087]424bool endsWith(const string & s, const char * suffix) {
[f450f2f]425 return endsWith( *s.inner, suffix );
426}
427
[6264087]428bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
[f450f2f]429 return endsWith( *s.inner, suffix, suffixsize );
430}
431
432
433///////////////////////////////////////////////////////////////////////////
434// charclass, include, exclude
435
[681e12f]436void ?{}( charclass & s, const string & chars) {
437 (s.inner) { malloc() };
438 ?{}( *s.inner, *(const string_res *)chars.inner );
[f450f2f]439}
440
[681e12f]441void ?{}( charclass & s, const char * chars ) {
442 (s.inner) { malloc() };
443 ?{}( *s.inner, chars );
[f450f2f]444}
445
[681e12f]446void ?{}( charclass & s, const char * chars, size_t charssize ) {
447 (s.inner) { malloc() };
448 ?{}( *s.inner, chars, charssize );
[f450f2f]449}
450
[681e12f]451void ^?{}( charclass & s ) {
452 ^(*s.inner){};
453 free( s.inner );
454 s.inner = 0p;
[f450f2f]455}
456
457
[6264087]458int exclude(const string & s, const charclass & mask) {
[f450f2f]459 return exclude( *s.inner, *mask.inner );
460}
461/*
[6264087]462StrSlice exclude(string & s, const charclass & mask) {
[f450f2f]463}
464*/
465
[6264087]466int include(const string & s, const charclass & mask) {
[f450f2f]467 return include( *s.inner, *mask.inner );
468}
469
470/*
[6264087]471StrSlice include(string & s, const charclass & mask) {
[f450f2f]472}
473*/
474
Note: See TracBrowser for help on using the repository browser.