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