source: libcfa/src/containers/string.cfa@ 4aaac8a

Last change on this file since 4aaac8a was 88001dd, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

first attempt reading strings in chunks using C strings

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