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