source: libcfa/src/containers/string.cfa@ 082510e

Last change on this file since 082510e was 6264087, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

formatting

  • Property mode set to 100644
File size: 8.1 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
[6264087]12// Last Modified On : Sun Aug 13 22:50:08 2023
13// Update Count : 7
[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
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
[6264087]57void ?{}( string & this, const char * buffer, size_t bsize) {
[f450f2f]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
[4b3b352]95string & ?=?(string & this, string & other) { //// <---- straw man change
[f450f2f]96 (*this.inner) = (*other.inner);
97 return this;
98}
99
100
101////////////////////////////////////////////////////////
[d32679d5]102// Input-Output
[f450f2f]103
[9ca5e56]104ofstream & ?|?( ofstream & out, const string & this ) {
105 return out | (*this.inner); // print internal string_res
[f450f2f]106}
107
[9ca5e56]108void ?|?( ofstream & out, const string & this ) {
109 (ofstream &)(out | (*this.inner)); ends( out );
[f450f2f]110}
111
[6264087]112ifstream & ?|?(ifstream & in, string & this) {
[d32679d5]113 return in | (*this.inner); // read to internal string_res
114}
115
[6264087]116void ?|?( ifstream & in, string & this ) {
117 (ifstream &)(in | this); ends( in );
118}
[d32679d5]119
[f450f2f]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
[bc9f84a]128string ?()( string & this, size_t start ) {
129 string ret = { *this.inner, start, size( this ) };
130 return ret`shareEdits;
131}
132
[f450f2f]133////////////////////////////////////////////////////////
134// Comparison
135
[6264087]136bool ?==?(const string & s, const string & other) {
[f450f2f]137 return *s.inner == *other.inner;
138}
139
[6264087]140bool ?!=?(const string & s, const string & other) {
[f450f2f]141 return *s.inner != *other.inner;
142}
143
[6264087]144bool ?==?(const string & s, const char * other) {
[f450f2f]145 return *s.inner == other;
146}
147
[6264087]148bool ?!=?(const string & s, const char * other) {
[f450f2f]149 return *s.inner != other;
150}
151
152////////////////////////////////////////////////////////
153// Getter
154
[6264087]155size_t size(const string & s) {
[f450f2f]156 return size( * s.inner );
157}
158
159////////////////////////////////////////////////////////
160// Concatenation
161
[6264087]162void ?+=?(string & s, char other) {
[f450f2f]163 (*s.inner) += other;
164}
165
[6264087]166void ?+=?(string & s, const string & s2) {
[f450f2f]167 (*s.inner) += (*s2.inner);
168}
169
[6264087]170void ?+=?(string & s, const char * other) {
[f450f2f]171 (*s.inner) += other;
172}
173
[6264087]174string ?+?(const string & s, char other) {
[f450f2f]175 string ret = s;
176 ret += other;
177 return ret;
178}
179
[6264087]180string ?+?(const string & s, const string & s2) {
[f450f2f]181 string ret = s;
182 ret += s2;
183 return ret;
184}
185
[6264087]186string ?+?(const char * s1, const char * s2) {
[f450f2f]187 string ret = s1;
188 ret += s2;
189 return ret;
190}
191
[6264087]192string ?+?(const string & s, const char * other) {
[f450f2f]193 string ret = s;
194 ret += other;
195 return ret;
196}
197
198////////////////////////////////////////////////////////
199// Repetition
200
[6264087]201string ?*?(const string & s, size_t factor) {
[f450f2f]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
[6264087]221char ?[?](const string & s, size_t index) {
[f450f2f]222 return (*s.inner)[index];
223}
224
[6264087]225string ?[?](string & s, size_t index) {
[f450f2f]226 string ret = { *s.inner, index, index + 1 };
227 return ret`shareEdits;
228}
229
230////////////////////////////////////////////////////////
231// Search
232
[6264087]233bool contains(const string & s, char ch) {
[f450f2f]234 return contains( *s.inner, ch );
235}
236
[6264087]237int find(const string & s, char search) {
[f450f2f]238 return find( *s.inner, search );
239}
240
[6264087]241int find(const string & s, const string & search) {
[f450f2f]242 return find( *s.inner, *search.inner );
243}
244
[6264087]245int find(const string & s, const char * search) {
[f450f2f]246 return find( *s.inner, search);
247}
248
[6264087]249int find(const string & s, const char * search, size_t searchsize) {
[f450f2f]250 return find( *s.inner, search, searchsize);
251}
252
[6264087]253int findFrom(const string & s, size_t fromPos, char search) {
[08ed947]254 return findFrom( *s.inner, fromPos, search );
255}
256
[6264087]257int findFrom(const string & s, size_t fromPos, const string & search) {
[08ed947]258 return findFrom( *s.inner, fromPos, *search.inner );
259}
260
[6264087]261int findFrom(const string & s, size_t fromPos, const char * search) {
[08ed947]262 return findFrom( *s.inner, fromPos, search );
263}
264
[6264087]265int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
[08ed947]266 return findFrom( *s.inner, fromPos, search, searchsize );
267}
268
[6264087]269bool includes(const string & s, const string & search) {
[f450f2f]270 return includes( *s.inner, *search.inner );
271}
272
[6264087]273bool includes(const string & s, const char * search) {
[f450f2f]274 return includes( *s.inner, search );
275}
276
[6264087]277bool includes(const string & s, const char * search, size_t searchsize) {
[f450f2f]278 return includes( *s.inner, search, searchsize );
279}
280
[6264087]281bool startsWith(const string & s, const string & prefix) {
[f450f2f]282 return startsWith( *s.inner, *prefix.inner );
283}
284
[6264087]285bool startsWith(const string & s, const char * prefix) {
[f450f2f]286 return startsWith( *s.inner, prefix );
287}
288
[6264087]289bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
[f450f2f]290 return startsWith( *s.inner, prefix, prefixsize );
291}
292
[6264087]293bool endsWith(const string & s, const string & suffix) {
[f450f2f]294 return endsWith( *s.inner, *suffix.inner );
295}
296
[6264087]297bool endsWith(const string & s, const char * suffix) {
[f450f2f]298 return endsWith( *s.inner, suffix );
299}
300
[6264087]301bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
[f450f2f]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
[6264087]331int exclude(const string & s, const charclass & mask) {
[f450f2f]332 return exclude( *s.inner, *mask.inner );
333}
334/*
[6264087]335StrSlice exclude(string & s, const charclass & mask) {
[f450f2f]336}
337*/
338
[6264087]339int include(const string & s, const charclass & mask) {
[f450f2f]340 return include( *s.inner, *mask.inner );
341}
342
343/*
[6264087]344StrSlice include(string & s, const charclass & mask) {
[f450f2f]345}
346*/
347
Note: See TracBrowser for help on using the repository browser.