source: libcfa/src/containers/string.cfa @ 6264087

Last change on this file since 6264087 was 6264087, checked in by Peter A. Buhr <pabuhr@…>, 12 months ago

formatting

  • Property mode set to 100644
File size: 8.1 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 : Sun Aug 13 22:50:08 2023
13// Update Count     : 7
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
32void ?{}( string & this ) {
33    (this.inner) { malloc() };
34    ?{}( *this.inner );
35}
36
37// private (not in header)
38static 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
43void ?{}( string & this, const string & other ) {
44    (this.inner) { malloc() };
45    ?{}( *this.inner, *other.inner, COPY_VALUE );
46}
47
48void ?{}( string & this, string & other ) {
49    ?{}( this, (const string &) other );
50}
51
52void ?{}( string & this, const char * val ) {
53    (this.inner) { malloc() };
54    ?{}( *this.inner, val );
55}
56
57void ?{}( string & this, const char * buffer, size_t bsize) {
58    (this.inner) { malloc() };
59    ?{}( *this.inner, buffer, bsize );
60}
61
62void ^?{}( string & this ) {
63    ^(*this.inner){};
64    free( this.inner );
65    this.inner = 0p;
66}
67
68////////////////////////////////////////////////////////
69// Alternate construction: request shared edits
70
71string_WithSharedEdits ?`shareEdits( string & this ) {
72    string_WithSharedEdits ret = { &this };
73    return ret;
74}
75
76void ?{}( string & this, string_WithSharedEdits src ) {
77    ?{}( this, *src.s->inner, 0, src.s->inner->Handle.lnth);
78}
79
80////////////////////////////////////////////////////////
81// Assignment
82
83void ?=?( string & this, const char * val ) {
84    (*this.inner) = val;
85}
86
87void ?=?(string & this, const string & other) {
88    (*this.inner) = (*other.inner);
89}
90
91void ?=?( string & this, char val ) {
92    (*this.inner) = val;
93}
94
95string & ?=?(string & this, string & other) { //// <---- straw man change
96    (*this.inner) = (*other.inner);
97    return this;
98}
99
100
101////////////////////////////////////////////////////////
102// Input-Output
103
104ofstream & ?|?( ofstream & out, const string & this ) {
105    return out | (*this.inner); // print internal string_res
106}
107
108void ?|?( ofstream & out, const string & this ) {
109    (ofstream &)(out | (*this.inner)); ends( out );
110}
111
112ifstream & ?|?(ifstream & in, string & this) {
113    return in | (*this.inner); // read to internal string_res
114}
115
116void ?|?( ifstream & in, string & this ) {
117    (ifstream &)(in | this); ends( in );
118}
119
120////////////////////////////////////////////////////////
121// Slicing
122
123string ?()( string & this, size_t start, size_t end ) {
124    string ret = { *this.inner, start, end };
125    return ret`shareEdits;
126}
127
128string ?()( string & this, size_t start ) {
129    string ret = { *this.inner, start, size( this ) };
130    return ret`shareEdits;
131}
132
133////////////////////////////////////////////////////////
134// Comparison
135
136bool ?==?(const string & s, const string & other) {
137    return *s.inner == *other.inner;
138}
139
140bool ?!=?(const string & s, const string & other) {
141    return *s.inner != *other.inner;
142}
143
144bool ?==?(const string & s, const char * other) {
145    return *s.inner == other;
146}
147
148bool ?!=?(const string & s, const char * other) {
149    return *s.inner != other;
150}
151
152////////////////////////////////////////////////////////
153// Getter
154
155size_t size(const string & s) {
156    return size( * s.inner );
157}
158
159////////////////////////////////////////////////////////
160// Concatenation
161
162void ?+=?(string & s, char other) {
163    (*s.inner) += other;
164}
165
166void ?+=?(string & s, const string & s2) {
167    (*s.inner) += (*s2.inner);
168}
169
170void ?+=?(string & s, const char * other) {
171    (*s.inner) += other;
172}
173
174string ?+?(const string & s, char other) {
175    string ret = s;
176    ret += other;
177    return ret;
178}
179
180string ?+?(const string & s, const string & s2) {
181    string ret = s;
182    ret += s2;
183    return ret;
184}
185
186string ?+?(const char * s1, const char * s2) {
187    string ret = s1;
188    ret += s2;
189    return ret;
190}
191
192string ?+?(const string & s, const char * other) {
193    string ret = s;
194    ret += other;
195    return ret;
196}
197
198////////////////////////////////////////////////////////
199// Repetition
200
201string ?*?(const string & s, size_t factor) {
202    string ret = "";
203    for (factor) ret += s;
204    return ret;
205}
206
207string ?*?(char c, size_t size) {
208    string ret = "";
209    for ((size_t)size) ret += c;
210    return ret;
211}
212
213string ?*?(const char *s, size_t factor) {
214    string ss = s;
215    return ss * factor;
216}
217
218////////////////////////////////////////////////////////
219// Character access
220
221char ?[?](const string & s, size_t index) {
222    return (*s.inner)[index];
223}
224
225string ?[?](string & s, size_t index) {
226    string ret = { *s.inner, index, index + 1 };
227    return ret`shareEdits;
228}
229
230////////////////////////////////////////////////////////
231// Search
232
233bool contains(const string & s, char ch) {
234    return contains( *s.inner, ch );
235}
236
237int find(const string & s, char search) {
238    return find( *s.inner, search );
239}
240
241int find(const string & s, const string & search) {
242    return find( *s.inner, *search.inner );
243}
244
245int find(const string & s, const char * search) {
246    return find( *s.inner, search);
247}
248
249int find(const string & s, const char * search, size_t searchsize) {
250    return find( *s.inner, search, searchsize);
251}
252
253int findFrom(const string & s, size_t fromPos, char search) {
254    return findFrom( *s.inner, fromPos, search );
255}
256
257int findFrom(const string & s, size_t fromPos, const string & search) {
258    return findFrom( *s.inner, fromPos, *search.inner );
259}
260
261int findFrom(const string & s, size_t fromPos, const char * search) {
262    return findFrom( *s.inner, fromPos, search );
263}
264
265int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
266    return findFrom( *s.inner, fromPos, search, searchsize );
267}
268
269bool includes(const string & s, const string & search) {
270    return includes( *s.inner, *search.inner );
271}
272
273bool includes(const string & s, const char * search) {
274    return includes( *s.inner, search );
275}
276
277bool includes(const string & s, const char * search, size_t searchsize) {
278    return includes( *s.inner, search, searchsize );
279}
280
281bool startsWith(const string & s, const string & prefix) {
282    return startsWith( *s.inner, *prefix.inner );
283}
284
285bool startsWith(const string & s, const char * prefix) {
286    return startsWith( *s.inner, prefix );
287}
288
289bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
290    return startsWith( *s.inner, prefix, prefixsize );
291}
292
293bool endsWith(const string & s, const string & suffix) {
294    return endsWith( *s.inner, *suffix.inner );
295}
296
297bool endsWith(const string & s, const char * suffix) {
298    return endsWith( *s.inner, suffix );
299}
300
301bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
302    return endsWith( *s.inner, suffix, suffixsize );
303}
304
305
306///////////////////////////////////////////////////////////////////////////
307// charclass, include, exclude
308
309void ?{}( charclass & this, const string & chars) {
310    (this.inner) { malloc() };
311    ?{}( *this.inner, *(const string_res *)chars.inner );
312}
313
314void ?{}( charclass & this, const char * chars ) {
315    (this.inner) { malloc() };
316    ?{}( *this.inner, chars );
317}
318
319void ?{}( charclass & this, const char * chars, size_t charssize ) {
320    (this.inner) { malloc() };
321    ?{}( *this.inner, chars, charssize );
322}
323
324void ^?{}( charclass & this ) {
325    ^(*this.inner){};
326    free( this.inner );
327    this.inner = 0p;
328}
329
330
331int exclude(const string & s, const charclass & mask) {
332    return exclude( *s.inner, *mask.inner );
333}
334/*
335StrSlice exclude(string & s, const charclass & mask) {
336}
337*/
338
339int include(const string & s, const charclass & mask) {
340    return include( *s.inner, *mask.inner );
341}
342
343/*
344StrSlice include(string & s, const charclass & mask) {
345}
346*/
347
Note: See TracBrowser for help on using the repository browser.