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

Last change on this file since b5e725a was d32679d5, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

String input operator with chunked memory management.

  • Property mode set to 100644
File size: 7.9 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 : Sat Aug 12 15:24:18 2023
13// Update Count : 6
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
116
117////////////////////////////////////////////////////////
118// Slicing
119
120string ?()( string & this, size_t start, size_t end ) {
121 string ret = { *this.inner, start, end };
122 return ret`shareEdits;
123}
124
125string ?()( string & this, size_t start ) {
126 string ret = { *this.inner, start, size( this ) };
127 return ret`shareEdits;
128}
129
130////////////////////////////////////////////////////////
131// Comparison
132
133bool ?==?(const string &s, const string &other) {
134 return *s.inner == *other.inner;
135}
136
137bool ?!=?(const string &s, const string &other) {
138 return *s.inner != *other.inner;
139}
140
141bool ?==?(const string &s, const char* other) {
142 return *s.inner == other;
143}
144
145bool ?!=?(const string &s, const char* other) {
146 return *s.inner != other;
147}
148
149////////////////////////////////////////////////////////
150// Getter
151
152size_t size(const string &s) {
153 return size( * s.inner );
154}
155
156////////////////////////////////////////////////////////
157// Concatenation
158
159void ?+=?(string &s, char other) {
160 (*s.inner) += other;
161}
162
163void ?+=?(string &s, const string &s2) {
164 (*s.inner) += (*s2.inner);
165}
166
167void ?+=?(string &s, const char* other) {
168 (*s.inner) += other;
169}
170
171string ?+?(const string &s, char other) {
172 string ret = s;
173 ret += other;
174 return ret;
175}
176
177string ?+?(const string &s, const string &s2) {
178 string ret = s;
179 ret += s2;
180 return ret;
181}
182
183string ?+?(const char* s1, const char* s2) {
184 string ret = s1;
185 ret += s2;
186 return ret;
187}
188
189string ?+?(const string &s, const char* other) {
190 string ret = s;
191 ret += other;
192 return ret;
193}
194
195////////////////////////////////////////////////////////
196// Repetition
197
198string ?*?(const string &s, size_t factor) {
199 string ret = "";
200 for (factor) ret += s;
201 return ret;
202}
203
204string ?*?(char c, size_t size) {
205 string ret = "";
206 for ((size_t)size) ret += c;
207 return ret;
208}
209
210string ?*?(const char *s, size_t factor) {
211 string ss = s;
212 return ss * factor;
213}
214
215////////////////////////////////////////////////////////
216// Character access
217
218char ?[?](const string &s, size_t index) {
219 return (*s.inner)[index];
220}
221
222string ?[?](string &s, size_t index) {
223 string ret = { *s.inner, index, index + 1 };
224 return ret`shareEdits;
225}
226
227////////////////////////////////////////////////////////
228// Search
229
230bool contains(const string &s, char ch) {
231 return contains( *s.inner, ch );
232}
233
234int find(const string &s, char search) {
235 return find( *s.inner, search );
236}
237
238int find(const string &s, const string &search) {
239 return find( *s.inner, *search.inner );
240}
241
242int find(const string &s, const char* search) {
243 return find( *s.inner, search);
244}
245
246int find(const string &s, const char* search, size_t searchsize) {
247 return find( *s.inner, search, searchsize);
248}
249
250int findFrom(const string &s, size_t fromPos, char search) {
251 return findFrom( *s.inner, fromPos, search );
252}
253
254int findFrom(const string &s, size_t fromPos, const string &search) {
255 return findFrom( *s.inner, fromPos, *search.inner );
256}
257
258int findFrom(const string &s, size_t fromPos, const char* search) {
259 return findFrom( *s.inner, fromPos, search );
260}
261
262int findFrom(const string &s, size_t fromPos, const char* search, size_t searchsize) {
263 return findFrom( *s.inner, fromPos, search, searchsize );
264}
265
266bool includes(const string &s, const string &search) {
267 return includes( *s.inner, *search.inner );
268}
269
270bool includes(const string &s, const char* search) {
271 return includes( *s.inner, search );
272}
273
274bool includes(const string &s, const char* search, size_t searchsize) {
275 return includes( *s.inner, search, searchsize );
276}
277
278bool startsWith(const string &s, const string &prefix) {
279 return startsWith( *s.inner, *prefix.inner );
280}
281
282bool startsWith(const string &s, const char* prefix) {
283 return startsWith( *s.inner, prefix );
284}
285
286bool startsWith(const string &s, const char* prefix, size_t prefixsize) {
287 return startsWith( *s.inner, prefix, prefixsize );
288}
289
290bool endsWith(const string &s, const string &suffix) {
291 return endsWith( *s.inner, *suffix.inner );
292}
293
294bool endsWith(const string &s, const char* suffix) {
295 return endsWith( *s.inner, suffix );
296}
297
298bool endsWith(const string &s, const char* suffix, size_t suffixsize) {
299 return endsWith( *s.inner, suffix, suffixsize );
300}
301
302
303///////////////////////////////////////////////////////////////////////////
304// charclass, include, exclude
305
306void ?{}( charclass & this, const string & chars) {
307 (this.inner) { malloc() };
308 ?{}( *this.inner, *(const string_res *)chars.inner );
309}
310
311void ?{}( charclass & this, const char * chars ) {
312 (this.inner) { malloc() };
313 ?{}( *this.inner, chars );
314}
315
316void ?{}( charclass & this, const char * chars, size_t charssize ) {
317 (this.inner) { malloc() };
318 ?{}( *this.inner, chars, charssize );
319}
320
321void ^?{}( charclass & this ) {
322 ^(*this.inner){};
323 free( this.inner );
324 this.inner = 0p;
325}
326
327
328int exclude(const string &s, const charclass &mask) {
329 return exclude( *s.inner, *mask.inner );
330}
331/*
332StrSlice exclude(string &s, const charclass &mask) {
333}
334*/
335
336int include(const string &s, const charclass &mask) {
337 return include( *s.inner, *mask.inner );
338}
339
340/*
341StrSlice include(string &s, const charclass &mask) {
342}
343*/
344
Note: See TracBrowser for help on using the repository browser.