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

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since efc8f3e was f450f2f, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Baseline of the string implementation.

  • Property mode set to 100644
File size: 7.0 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 : Michael L. Brooks
12// Last Modified On : Fri Sep 03 11:00:00 2021
13// Update Count     : 1
14//
15
16#include "string.hfa"
17#include "string_res.hfa"
18#include <stdlib.hfa>
19
20
21/*
22Implementation Principle: typical operation translates to the equivalent
23operation on `inner`.  Exceptions are implementing new RAII pattern for value
24semantics and some const-hell handling.
25*/
26
27////////////////////////////////////////////////////////
28// string RAII
29
30
31void ?{}( string & this ) {
32    (this.inner) { malloc() };
33    ?{}( *this.inner );
34}
35
36// private (not in header)
37static void ?{}( string & this, string_res & src, size_t start, size_t end ) {
38    (this.inner) { malloc() };
39    ?{}( *this.inner, src, SHARE_EDITS, start, end );
40}
41
42void ?{}( string & this, const string & other ) {
43    (this.inner) { malloc() };
44    ?{}( *this.inner, *other.inner, COPY_VALUE );
45}
46
47void ?{}( string & this, string & other ) {
48    ?{}( this, (const string &) other );
49}
50
51void ?{}( string & this, const char * val ) {
52    (this.inner) { malloc() };
53    ?{}( *this.inner, val );
54}
55
56void ?{}( string & this, const char* buffer, size_t bsize) {
57    (this.inner) { malloc() };
58    ?{}( *this.inner, buffer, bsize );
59}
60
61void ^?{}( string & this ) {
62    ^(*this.inner){};
63    free( this.inner );
64    this.inner = 0p;
65}
66
67////////////////////////////////////////////////////////
68// Alternate construction: request shared edits
69
70string_WithSharedEdits ?`shareEdits( string & this ) {
71    string_WithSharedEdits ret = { &this };
72    return ret;
73}
74
75void ?{}( string & this, string_WithSharedEdits src ) {
76    ?{}( this, *src.s->inner, 0, src.s->inner->Handle.lnth);
77}
78
79////////////////////////////////////////////////////////
80// Assignment
81
82void ?=?( string & this, const char * val ) {
83    (*this.inner) = val;
84}
85
86void ?=?(string & this, const string & other) {
87    (*this.inner) = (*other.inner);
88}
89
90void ?=?( string & this, char val ) {
91    (*this.inner) = val;
92}
93
94string ?=?(string & this, string other) {
95    (*this.inner) = (*other.inner);
96    return this;
97}
98
99
100////////////////////////////////////////////////////////
101// Output
102
103ofstream & ?|?( ofstream & fs, const string & this ) {
104    return fs | (*this.inner);
105}
106
107void ?|?( ofstream & fs, const string & this ) {
108    fs | (*this.inner);
109}
110
111////////////////////////////////////////////////////////
112// Slicing
113
114string ?()( string & this, size_t start, size_t end ) {
115    string ret = { *this.inner, start, end };
116    return ret`shareEdits;
117}
118
119////////////////////////////////////////////////////////
120// Comparison
121
122bool ?==?(const string &s, const string &other) {
123    return *s.inner == *other.inner;
124}
125
126bool ?!=?(const string &s, const string &other) {
127    return *s.inner != *other.inner;
128}
129
130bool ?==?(const string &s, const char* other) {
131    return *s.inner == other;
132}
133
134bool ?!=?(const string &s, const char* other) {
135    return *s.inner != other;
136}
137
138////////////////////////////////////////////////////////
139// Getter
140
141size_t size(const string &s) {
142    return size( * s.inner );
143}
144
145////////////////////////////////////////////////////////
146// Concatenation
147
148void ?+=?(string &s, char other) {
149    (*s.inner) += other;
150}
151
152void ?+=?(string &s, const string &s2) {
153    (*s.inner) += (*s2.inner);
154}
155
156void ?+=?(string &s, const char* other) {
157    (*s.inner) += other;
158}
159
160string ?+?(const string &s, char other) {
161    string ret = s;
162    ret += other;
163    return ret;
164}
165
166string ?+?(const string &s, const string &s2) {
167    string ret = s;
168    ret += s2;
169    return ret;
170}
171
172string ?+?(const char* s1, const char* s2) {
173    string ret = s1;
174    ret += s2;
175    return ret;
176}
177
178string ?+?(const string &s, const char* other) {
179    string ret = s;
180    ret += other;
181    return ret;
182}
183
184////////////////////////////////////////////////////////
185// Repetition
186
187string ?*?(const string &s, size_t factor) {
188    string ret = "";
189    for (factor) ret += s;
190    return ret;
191}
192
193string ?*?(char c, size_t size) {
194    string ret = "";
195    for ((size_t)size) ret += c;
196    return ret;
197}
198
199string ?*?(const char *s, size_t factor) {
200    string ss = s;
201    return ss * factor;
202}
203
204////////////////////////////////////////////////////////
205// Character access
206
207char ?[?](const string &s, size_t index) {
208    return (*s.inner)[index];
209}
210
211string ?[?](string &s, size_t index) {
212    string ret = { *s.inner, index, index + 1 };
213    return ret`shareEdits;
214}
215
216////////////////////////////////////////////////////////
217// Search
218
219bool contains(const string &s, char ch) {
220    return contains( *s.inner, ch );
221}
222
223int find(const string &s, char search) {
224    return find( *s.inner, search );
225}
226
227int find(const string &s, const string &search) {
228    return find( *s.inner, *search.inner );
229}
230
231int find(const string &s, const char* search) {
232    return find( *s.inner, search);
233}
234
235int find(const string &s, const char* search, size_t searchsize) {
236    return find( *s.inner, search, searchsize);
237}
238
239bool includes(const string &s, const string &search) {
240    return includes( *s.inner, *search.inner );
241}
242
243bool includes(const string &s, const char* search) {
244    return includes( *s.inner, search );
245}
246
247bool includes(const string &s, const char* search, size_t searchsize) {
248    return includes( *s.inner, search, searchsize );
249}
250
251bool startsWith(const string &s, const string &prefix) {
252    return startsWith( *s.inner, *prefix.inner );
253}
254
255bool startsWith(const string &s, const char* prefix) {
256    return startsWith( *s.inner, prefix );
257}
258
259bool startsWith(const string &s, const char* prefix, size_t prefixsize) {
260    return startsWith( *s.inner, prefix, prefixsize );
261}
262
263bool endsWith(const string &s, const string &suffix) {
264    return endsWith( *s.inner, *suffix.inner );
265}
266
267bool endsWith(const string &s, const char* suffix) {
268    return endsWith( *s.inner, suffix );
269}
270
271bool endsWith(const string &s, const char* suffix, size_t suffixsize) {
272    return endsWith( *s.inner, suffix, suffixsize );
273}
274
275
276///////////////////////////////////////////////////////////////////////////
277// charclass, include, exclude
278
279void ?{}( charclass & this, const string & chars) {
280    (this.inner) { malloc() };
281    ?{}( *this.inner, *(const string_res *)chars.inner );
282}
283
284void ?{}( charclass & this, const char * chars ) {
285    (this.inner) { malloc() };
286    ?{}( *this.inner, chars );
287}
288
289void ?{}( charclass & this, const char * chars, size_t charssize ) {
290    (this.inner) { malloc() };
291    ?{}( *this.inner, chars, charssize );
292}
293
294void ^?{}( charclass & this ) {
295    ^(*this.inner){};
296    free( this.inner );
297    this.inner = 0p;
298}
299
300
301int exclude(const string &s, const charclass &mask) {
302    return exclude( *s.inner, *mask.inner );
303}
304/*
305StrSlice exclude(string &s, const charclass &mask) {
306}
307*/
308
309int include(const string &s, const charclass &mask) {
310    return include( *s.inner, *mask.inner );
311}
312
313/*
314StrSlice include(string &s, const charclass &mask) {
315}
316*/
317
Note: See TracBrowser for help on using the repository browser.