source: libcfa/src/collections/string.cfa@ 0ac8d07

Last change on this file since 0ac8d07 was 34c6e1e6, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add string output manipulators, third attempt at input manipulators for strings

  • Property mode set to 100644
File size: 9.4 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 Sep 2 12:05:57 2023
13// Update Count : 206
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
112ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
113 size_t len = size( f.val );
114 char cstr[len + 1]; // room for null terminator
115 for ( i; len ) cstr[i] = f.val[i]; // copy string
116 cstr[len] = '\0'; // terminate
117 _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
118 os | cf | nonl;
119 return os;
120} // ?|?
121
122void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
123 (ofstream &)(os | f); ends( os );
124}
125
126ifstream & ?|?(ifstream & in, string & this) {
127 return in | (*this.inner); // read to internal string_res
128}
129
130void ?|?( ifstream & in, string & this ) {
131 (ifstream &)(in | this); ends( in );
132}
133
134ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
135 // .---------------,
136 // | | | | |...|0|0| null terminator and guard if missing
137 // `---------------'
138 enum { gwd = 128 + 1, wd = gwd - 1 }; // guard and unguard width
139 char cstr[gwd]; // read in chunks
140 bool cont = false;
141
142 _Istream_Cstr cf = { cstr, (_Istream_str_base)f };
143 if ( ! cf.flags.rwd ) cf.wd = wd;
144
145 cstr[wd] = '\0'; // guard null terminate string
146 try {
147 is | cf;
148 } catch( cstring_length * ) {
149 cont = true;
150 } finally {
151 f.s = cstr; // ok to initialize string
152 } // try
153 for ( ; cont; ) { // overflow read ?
154 cont = false;
155 try {
156 is | cf;
157 } catch( cstring_length * ) {
158 cont = true; // continue not allowed
159 } finally {
160 f.s += cstr; // build string chunk at a time
161 } // try
162 } // for
163 return is;
164} // ?|?
165
166void ?|?( ifstream & in, _Istream_Sstr f ) {
167 (ifstream &)(in | f); ends( in );
168}
169
170////////////////////////////////////////////////////////
171// Slicing
172
173string ?()( string & this, size_t start, size_t end ) {
174 string ret = { *this.inner, start, end };
175 return ret`shareEdits;
176}
177
178string ?()( string & this, size_t start ) {
179 string ret = { *this.inner, start, size( this ) };
180 return ret`shareEdits;
181}
182
183////////////////////////////////////////////////////////
184// Comparison
185
186bool ?==?(const string & s, const string & other) {
187 return *s.inner == *other.inner;
188}
189
190bool ?!=?(const string & s, const string & other) {
191 return *s.inner != *other.inner;
192}
193
194bool ?==?(const string & s, const char * other) {
195 return *s.inner == other;
196}
197
198bool ?!=?(const string & s, const char * other) {
199 return *s.inner != other;
200}
201
202////////////////////////////////////////////////////////
203// Getter
204
205size_t size(const string & s) {
206 return size( * s.inner );
207}
208
209////////////////////////////////////////////////////////
210// Concatenation
211
212void ?+=?(string & s, char other) {
213 (*s.inner) += other;
214}
215
216void ?+=?(string & s, const string & s2) {
217 (*s.inner) += (*s2.inner);
218}
219
220void ?+=?(string & s, const char * other) {
221 (*s.inner) += other;
222}
223
224string ?+?(const string & s, char other) {
225 string ret = s;
226 ret += other;
227 return ret;
228}
229
230string ?+?(const string & s, const string & s2) {
231 string ret = s;
232 ret += s2;
233 return ret;
234}
235
236string ?+?(const char * s1, const char * s2) {
237 string ret = s1;
238 ret += s2;
239 return ret;
240}
241
242string ?+?(const string & s, const char * other) {
243 string ret = s;
244 ret += other;
245 return ret;
246}
247
248////////////////////////////////////////////////////////
249// Repetition
250
251string ?*?(const string & s, size_t factor) {
252 string ret = "";
253 for (factor) ret += s;
254 return ret;
255}
256
257string ?*?(char c, size_t size) {
258 string ret = "";
259 for ((size_t)size) ret += c;
260 return ret;
261}
262
263string ?*?(const char *s, size_t factor) {
264 string ss = s;
265 return ss * factor;
266}
267
268////////////////////////////////////////////////////////
269// Character access
270
271char ?[?](const string & s, size_t index) {
272 return (*s.inner)[index];
273}
274
275string ?[?](string & s, size_t index) {
276 string ret = { *s.inner, index, index + 1 };
277 return ret`shareEdits;
278}
279
280////////////////////////////////////////////////////////
281// Search
282
283bool contains(const string & s, char ch) {
284 return contains( *s.inner, ch );
285}
286
287int find(const string & s, char search) {
288 return find( *s.inner, search );
289}
290
291int find(const string & s, const string & search) {
292 return find( *s.inner, *search.inner );
293}
294
295int find(const string & s, const char * search) {
296 return find( *s.inner, search);
297}
298
299int find(const string & s, const char * search, size_t searchsize) {
300 return find( *s.inner, search, searchsize);
301}
302
303int findFrom(const string & s, size_t fromPos, char search) {
304 return findFrom( *s.inner, fromPos, search );
305}
306
307int findFrom(const string & s, size_t fromPos, const string & search) {
308 return findFrom( *s.inner, fromPos, *search.inner );
309}
310
311int findFrom(const string & s, size_t fromPos, const char * search) {
312 return findFrom( *s.inner, fromPos, search );
313}
314
315int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
316 return findFrom( *s.inner, fromPos, search, searchsize );
317}
318
319bool includes(const string & s, const string & search) {
320 return includes( *s.inner, *search.inner );
321}
322
323bool includes(const string & s, const char * search) {
324 return includes( *s.inner, search );
325}
326
327bool includes(const string & s, const char * search, size_t searchsize) {
328 return includes( *s.inner, search, searchsize );
329}
330
331bool startsWith(const string & s, const string & prefix) {
332 return startsWith( *s.inner, *prefix.inner );
333}
334
335bool startsWith(const string & s, const char * prefix) {
336 return startsWith( *s.inner, prefix );
337}
338
339bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
340 return startsWith( *s.inner, prefix, prefixsize );
341}
342
343bool endsWith(const string & s, const string & suffix) {
344 return endsWith( *s.inner, *suffix.inner );
345}
346
347bool endsWith(const string & s, const char * suffix) {
348 return endsWith( *s.inner, suffix );
349}
350
351bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
352 return endsWith( *s.inner, suffix, suffixsize );
353}
354
355
356///////////////////////////////////////////////////////////////////////////
357// charclass, include, exclude
358
359void ?{}( charclass & this, const string & chars) {
360 (this.inner) { malloc() };
361 ?{}( *this.inner, *(const string_res *)chars.inner );
362}
363
364void ?{}( charclass & this, const char * chars ) {
365 (this.inner) { malloc() };
366 ?{}( *this.inner, chars );
367}
368
369void ?{}( charclass & this, const char * chars, size_t charssize ) {
370 (this.inner) { malloc() };
371 ?{}( *this.inner, chars, charssize );
372}
373
374void ^?{}( charclass & this ) {
375 ^(*this.inner){};
376 free( this.inner );
377 this.inner = 0p;
378}
379
380
381int exclude(const string & s, const charclass & mask) {
382 return exclude( *s.inner, *mask.inner );
383}
384/*
385StrSlice exclude(string & s, const charclass & mask) {
386}
387*/
388
389int include(const string & s, const charclass & mask) {
390 return include( *s.inner, *mask.inner );
391}
392
393/*
394StrSlice include(string & s, const charclass & mask) {
395}
396*/
397
Note: See TracBrowser for help on using the repository browser.