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 | /*
|
---|
23 | Implementation Principle: typical operation translates to the equivalent
|
---|
24 | operation on `inner`. Exceptions are implementing new RAII pattern for value
|
---|
25 | semantics and some const-hell handling.
|
---|
26 | */
|
---|
27 |
|
---|
28 | ////////////////////////////////////////////////////////
|
---|
29 | // string RAII
|
---|
30 |
|
---|
31 |
|
---|
32 | void ?{}( string & this ) {
|
---|
33 | (this.inner) { malloc() };
|
---|
34 | ?{}( *this.inner );
|
---|
35 | }
|
---|
36 |
|
---|
37 | // private (not in header)
|
---|
38 | static 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 |
|
---|
43 | void ?{}( string & this, const string & other ) {
|
---|
44 | (this.inner) { malloc() };
|
---|
45 | ?{}( *this.inner, *other.inner, COPY_VALUE );
|
---|
46 | }
|
---|
47 |
|
---|
48 | void ?{}( string & this, string & other ) {
|
---|
49 | ?{}( this, (const string &) other );
|
---|
50 | }
|
---|
51 |
|
---|
52 | void ?{}( string & this, const char * val ) {
|
---|
53 | (this.inner) { malloc() };
|
---|
54 | ?{}( *this.inner, val );
|
---|
55 | }
|
---|
56 |
|
---|
57 | void ?{}( string & this, const char * buffer, size_t bsize) {
|
---|
58 | (this.inner) { malloc() };
|
---|
59 | ?{}( *this.inner, buffer, bsize );
|
---|
60 | }
|
---|
61 |
|
---|
62 | void ^?{}( string & this ) {
|
---|
63 | ^(*this.inner){};
|
---|
64 | free( this.inner );
|
---|
65 | this.inner = 0p;
|
---|
66 | }
|
---|
67 |
|
---|
68 | ////////////////////////////////////////////////////////
|
---|
69 | // Alternate construction: request shared edits
|
---|
70 |
|
---|
71 | string_WithSharedEdits ?`shareEdits( string & this ) {
|
---|
72 | string_WithSharedEdits ret = { &this };
|
---|
73 | return ret;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void ?{}( string & this, string_WithSharedEdits src ) {
|
---|
77 | ?{}( this, *src.s->inner, 0, src.s->inner->Handle.lnth);
|
---|
78 | }
|
---|
79 |
|
---|
80 | ////////////////////////////////////////////////////////
|
---|
81 | // Assignment
|
---|
82 |
|
---|
83 | void ?=?( string & this, const char * val ) {
|
---|
84 | (*this.inner) = val;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void ?=?(string & this, const string & other) {
|
---|
88 | (*this.inner) = (*other.inner);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void ?=?( string & this, char val ) {
|
---|
92 | (*this.inner) = val;
|
---|
93 | }
|
---|
94 |
|
---|
95 | string & ?=?(string & this, string & other) { //// <---- straw man change
|
---|
96 | (*this.inner) = (*other.inner);
|
---|
97 | return this;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | ////////////////////////////////////////////////////////
|
---|
102 | // Input-Output
|
---|
103 |
|
---|
104 | ofstream & ?|?( ofstream & out, const string & this ) {
|
---|
105 | return out | (*this.inner); // print internal string_res
|
---|
106 | }
|
---|
107 |
|
---|
108 | void ?|?( ofstream & out, const string & this ) {
|
---|
109 | (ofstream &)(out | (*this.inner)); ends( out );
|
---|
110 | }
|
---|
111 |
|
---|
112 | ifstream & ?|?(ifstream & in, string & this) {
|
---|
113 | return in | (*this.inner); // read to internal string_res
|
---|
114 | }
|
---|
115 |
|
---|
116 | void ?|?( ifstream & in, string & this ) {
|
---|
117 | (ifstream &)(in | this); ends( in );
|
---|
118 | }
|
---|
119 |
|
---|
120 | void 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 |
|
---|
153 | string ?()( string & this, size_t start, size_t end ) {
|
---|
154 | string ret = { *this.inner, start, end };
|
---|
155 | return ret`shareEdits;
|
---|
156 | }
|
---|
157 |
|
---|
158 | string ?()( string & this, size_t start ) {
|
---|
159 | string ret = { *this.inner, start, size( this ) };
|
---|
160 | return ret`shareEdits;
|
---|
161 | }
|
---|
162 |
|
---|
163 | ////////////////////////////////////////////////////////
|
---|
164 | // Comparison
|
---|
165 |
|
---|
166 | bool ?==?(const string & s, const string & other) {
|
---|
167 | return *s.inner == *other.inner;
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool ?!=?(const string & s, const string & other) {
|
---|
171 | return *s.inner != *other.inner;
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool ?==?(const string & s, const char * other) {
|
---|
175 | return *s.inner == other;
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool ?!=?(const string & s, const char * other) {
|
---|
179 | return *s.inner != other;
|
---|
180 | }
|
---|
181 |
|
---|
182 | ////////////////////////////////////////////////////////
|
---|
183 | // Getter
|
---|
184 |
|
---|
185 | size_t size(const string & s) {
|
---|
186 | return size( * s.inner );
|
---|
187 | }
|
---|
188 |
|
---|
189 | ////////////////////////////////////////////////////////
|
---|
190 | // Concatenation
|
---|
191 |
|
---|
192 | void ?+=?(string & s, char other) {
|
---|
193 | (*s.inner) += other;
|
---|
194 | }
|
---|
195 |
|
---|
196 | void ?+=?(string & s, const string & s2) {
|
---|
197 | (*s.inner) += (*s2.inner);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void ?+=?(string & s, const char * other) {
|
---|
201 | (*s.inner) += other;
|
---|
202 | }
|
---|
203 |
|
---|
204 | string ?+?(const string & s, char other) {
|
---|
205 | string ret = s;
|
---|
206 | ret += other;
|
---|
207 | return ret;
|
---|
208 | }
|
---|
209 |
|
---|
210 | string ?+?(const string & s, const string & s2) {
|
---|
211 | string ret = s;
|
---|
212 | ret += s2;
|
---|
213 | return ret;
|
---|
214 | }
|
---|
215 |
|
---|
216 | string ?+?(const char * s1, const char * s2) {
|
---|
217 | string ret = s1;
|
---|
218 | ret += s2;
|
---|
219 | return ret;
|
---|
220 | }
|
---|
221 |
|
---|
222 | string ?+?(const string & s, const char * other) {
|
---|
223 | string ret = s;
|
---|
224 | ret += other;
|
---|
225 | return ret;
|
---|
226 | }
|
---|
227 |
|
---|
228 | ////////////////////////////////////////////////////////
|
---|
229 | // Repetition
|
---|
230 |
|
---|
231 | string ?*?(const string & s, size_t factor) {
|
---|
232 | string ret = "";
|
---|
233 | for (factor) ret += s;
|
---|
234 | return ret;
|
---|
235 | }
|
---|
236 |
|
---|
237 | string ?*?(char c, size_t size) {
|
---|
238 | string ret = "";
|
---|
239 | for ((size_t)size) ret += c;
|
---|
240 | return ret;
|
---|
241 | }
|
---|
242 |
|
---|
243 | string ?*?(const char *s, size_t factor) {
|
---|
244 | string ss = s;
|
---|
245 | return ss * factor;
|
---|
246 | }
|
---|
247 |
|
---|
248 | ////////////////////////////////////////////////////////
|
---|
249 | // Character access
|
---|
250 |
|
---|
251 | char ?[?](const string & s, size_t index) {
|
---|
252 | return (*s.inner)[index];
|
---|
253 | }
|
---|
254 |
|
---|
255 | string ?[?](string & s, size_t index) {
|
---|
256 | string ret = { *s.inner, index, index + 1 };
|
---|
257 | return ret`shareEdits;
|
---|
258 | }
|
---|
259 |
|
---|
260 | ////////////////////////////////////////////////////////
|
---|
261 | // Search
|
---|
262 |
|
---|
263 | bool contains(const string & s, char ch) {
|
---|
264 | return contains( *s.inner, ch );
|
---|
265 | }
|
---|
266 |
|
---|
267 | int find(const string & s, char search) {
|
---|
268 | return find( *s.inner, search );
|
---|
269 | }
|
---|
270 |
|
---|
271 | int find(const string & s, const string & search) {
|
---|
272 | return find( *s.inner, *search.inner );
|
---|
273 | }
|
---|
274 |
|
---|
275 | int find(const string & s, const char * search) {
|
---|
276 | return find( *s.inner, search);
|
---|
277 | }
|
---|
278 |
|
---|
279 | int find(const string & s, const char * search, size_t searchsize) {
|
---|
280 | return find( *s.inner, search, searchsize);
|
---|
281 | }
|
---|
282 |
|
---|
283 | int findFrom(const string & s, size_t fromPos, char search) {
|
---|
284 | return findFrom( *s.inner, fromPos, search );
|
---|
285 | }
|
---|
286 |
|
---|
287 | int findFrom(const string & s, size_t fromPos, const string & search) {
|
---|
288 | return findFrom( *s.inner, fromPos, *search.inner );
|
---|
289 | }
|
---|
290 |
|
---|
291 | int findFrom(const string & s, size_t fromPos, const char * search) {
|
---|
292 | return findFrom( *s.inner, fromPos, search );
|
---|
293 | }
|
---|
294 |
|
---|
295 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
|
---|
296 | return findFrom( *s.inner, fromPos, search, searchsize );
|
---|
297 | }
|
---|
298 |
|
---|
299 | bool includes(const string & s, const string & search) {
|
---|
300 | return includes( *s.inner, *search.inner );
|
---|
301 | }
|
---|
302 |
|
---|
303 | bool includes(const string & s, const char * search) {
|
---|
304 | return includes( *s.inner, search );
|
---|
305 | }
|
---|
306 |
|
---|
307 | bool includes(const string & s, const char * search, size_t searchsize) {
|
---|
308 | return includes( *s.inner, search, searchsize );
|
---|
309 | }
|
---|
310 |
|
---|
311 | bool startsWith(const string & s, const string & prefix) {
|
---|
312 | return startsWith( *s.inner, *prefix.inner );
|
---|
313 | }
|
---|
314 |
|
---|
315 | bool startsWith(const string & s, const char * prefix) {
|
---|
316 | return startsWith( *s.inner, prefix );
|
---|
317 | }
|
---|
318 |
|
---|
319 | bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
|
---|
320 | return startsWith( *s.inner, prefix, prefixsize );
|
---|
321 | }
|
---|
322 |
|
---|
323 | bool endsWith(const string & s, const string & suffix) {
|
---|
324 | return endsWith( *s.inner, *suffix.inner );
|
---|
325 | }
|
---|
326 |
|
---|
327 | bool endsWith(const string & s, const char * suffix) {
|
---|
328 | return endsWith( *s.inner, suffix );
|
---|
329 | }
|
---|
330 |
|
---|
331 | bool 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 |
|
---|
339 | void ?{}( charclass & this, const string & chars) {
|
---|
340 | (this.inner) { malloc() };
|
---|
341 | ?{}( *this.inner, *(const string_res *)chars.inner );
|
---|
342 | }
|
---|
343 |
|
---|
344 | void ?{}( charclass & this, const char * chars ) {
|
---|
345 | (this.inner) { malloc() };
|
---|
346 | ?{}( *this.inner, chars );
|
---|
347 | }
|
---|
348 |
|
---|
349 | void ?{}( charclass & this, const char * chars, size_t charssize ) {
|
---|
350 | (this.inner) { malloc() };
|
---|
351 | ?{}( *this.inner, chars, charssize );
|
---|
352 | }
|
---|
353 |
|
---|
354 | void ^?{}( charclass & this ) {
|
---|
355 | ^(*this.inner){};
|
---|
356 | free( this.inner );
|
---|
357 | this.inner = 0p;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | int exclude(const string & s, const charclass & mask) {
|
---|
362 | return exclude( *s.inner, *mask.inner );
|
---|
363 | }
|
---|
364 | /*
|
---|
365 | StrSlice exclude(string & s, const charclass & mask) {
|
---|
366 | }
|
---|
367 | */
|
---|
368 |
|
---|
369 | int include(const string & s, const charclass & mask) {
|
---|
370 | return include( *s.inner, *mask.inner );
|
---|
371 | }
|
---|
372 |
|
---|
373 | /*
|
---|
374 | StrSlice include(string & s, const charclass & mask) {
|
---|
375 | }
|
---|
376 | */
|
---|
377 |
|
---|