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

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

add missing slicing routine with one argument

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