[f450f2f] | 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
|
---|
[bc9f84a] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[f842032] | 12 | // Last Modified On : Wed Oct 18 21:52:09 2023
|
---|
| 13 | // Update Count : 208
|
---|
[f450f2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "string.hfa"
|
---|
| 17 | #include "string_res.hfa"
|
---|
| 18 | #include <stdlib.hfa>
|
---|
| 19 |
|
---|
[accc9df9] | 20 | #pragma GCC visibility push(default)
|
---|
[f450f2f] | 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 |
|
---|
[6264087] | 57 | void ?{}( string & this, const char * buffer, size_t bsize) {
|
---|
[f450f2f] | 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 |
|
---|
[4b3b352] | 95 | string & ?=?(string & this, string & other) { //// <---- straw man change
|
---|
[f450f2f] | 96 | (*this.inner) = (*other.inner);
|
---|
| 97 | return this;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | ////////////////////////////////////////////////////////
|
---|
[d32679d5] | 102 | // Input-Output
|
---|
[f450f2f] | 103 |
|
---|
[9ca5e56] | 104 | ofstream & ?|?( ofstream & out, const string & this ) {
|
---|
| 105 | return out | (*this.inner); // print internal string_res
|
---|
[f450f2f] | 106 | }
|
---|
| 107 |
|
---|
[9ca5e56] | 108 | void ?|?( ofstream & out, const string & this ) {
|
---|
| 109 | (ofstream &)(out | (*this.inner)); ends( out );
|
---|
[f450f2f] | 110 | }
|
---|
| 111 |
|
---|
[34c6e1e6] | 112 | ofstream & ?|?( 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 |
|
---|
| 122 | void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
---|
| 123 | (ofstream &)(os | f); ends( os );
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[6264087] | 126 | ifstream & ?|?(ifstream & in, string & this) {
|
---|
[d32679d5] | 127 | return in | (*this.inner); // read to internal string_res
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[6264087] | 130 | void ?|?( ifstream & in, string & this ) {
|
---|
[737988b] | 131 | in | (*this.inner);
|
---|
[6264087] | 132 | }
|
---|
[d32679d5] | 133 |
|
---|
[34c6e1e6] | 134 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
|
---|
[737988b] | 135 | _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
|
---|
| 136 | return is | f2;
|
---|
[7e1dbd7] | 137 | } // ?|?
|
---|
| 138 |
|
---|
[34c6e1e6] | 139 | void ?|?( ifstream & in, _Istream_Sstr f ) {
|
---|
[f842032] | 140 | (ifstream &)(in | f);
|
---|
[7e1dbd7] | 141 | }
|
---|
| 142 |
|
---|
[f450f2f] | 143 | ////////////////////////////////////////////////////////
|
---|
| 144 | // Slicing
|
---|
| 145 |
|
---|
| 146 | string ?()( string & this, size_t start, size_t end ) {
|
---|
| 147 | string ret = { *this.inner, start, end };
|
---|
| 148 | return ret`shareEdits;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[bc9f84a] | 151 | string ?()( string & this, size_t start ) {
|
---|
| 152 | string ret = { *this.inner, start, size( this ) };
|
---|
| 153 | return ret`shareEdits;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[f450f2f] | 156 | ////////////////////////////////////////////////////////
|
---|
| 157 | // Comparison
|
---|
| 158 |
|
---|
[416b443] | 159 | int cmp (const string &s1, const string &s2) { return cmp(*s1.inner , *s2.inner); }
|
---|
| 160 | bool ?==?(const string &s1, const string &s2) { return *s1.inner == *s2.inner ; }
|
---|
| 161 | bool ?!=?(const string &s1, const string &s2) { return *s1.inner != *s2.inner ; }
|
---|
| 162 | bool ?>? (const string &s1, const string &s2) { return *s1.inner > *s2.inner ; }
|
---|
| 163 | bool ?>=?(const string &s1, const string &s2) { return *s1.inner >= *s2.inner ; }
|
---|
| 164 | bool ?<=?(const string &s1, const string &s2) { return *s1.inner <= *s2.inner ; }
|
---|
| 165 | bool ?<? (const string &s1, const string &s2) { return *s1.inner < *s2.inner ; }
|
---|
| 166 |
|
---|
| 167 | int cmp (const string &s1, const char* s2) { return cmp(*s1.inner , s2 ); }
|
---|
| 168 | bool ?==?(const string &s1, const char* s2) { return *s1.inner == s2 ; }
|
---|
| 169 | bool ?!=?(const string &s1, const char* s2) { return *s1.inner != s2 ; }
|
---|
| 170 | bool ?>? (const string &s1, const char* s2) { return *s1.inner > s2 ; }
|
---|
| 171 | bool ?>=?(const string &s1, const char* s2) { return *s1.inner >= s2 ; }
|
---|
| 172 | bool ?<=?(const string &s1, const char* s2) { return *s1.inner <= s2 ; }
|
---|
| 173 | bool ?<? (const string &s1, const char* s2) { return *s1.inner < s2 ; }
|
---|
| 174 |
|
---|
| 175 | int cmp (const char* s1, const string &s2) { return cmp( s1 , *s2.inner); }
|
---|
| 176 | bool ?==?(const char* s1, const string &s2) { return s1 == *s2.inner ; }
|
---|
| 177 | bool ?!=?(const char* s1, const string &s2) { return s1 != *s2.inner ; }
|
---|
| 178 | bool ?>? (const char* s1, const string &s2) { return s1 > *s2.inner ; }
|
---|
| 179 | bool ?>=?(const char* s1, const string &s2) { return s1 >= *s2.inner ; }
|
---|
| 180 | bool ?<=?(const char* s1, const string &s2) { return s1 <= *s2.inner ; }
|
---|
| 181 | bool ?<? (const char* s1, const string &s2) { return s1 < *s2.inner ; }
|
---|
[f450f2f] | 182 |
|
---|
| 183 |
|
---|
| 184 | ////////////////////////////////////////////////////////
|
---|
| 185 | // Getter
|
---|
| 186 |
|
---|
[6264087] | 187 | size_t size(const string & s) {
|
---|
[f450f2f] | 188 | return size( * s.inner );
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | ////////////////////////////////////////////////////////
|
---|
| 192 | // Concatenation
|
---|
| 193 |
|
---|
[6264087] | 194 | void ?+=?(string & s, char other) {
|
---|
[f450f2f] | 195 | (*s.inner) += other;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[6264087] | 198 | void ?+=?(string & s, const string & s2) {
|
---|
[f450f2f] | 199 | (*s.inner) += (*s2.inner);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[6264087] | 202 | void ?+=?(string & s, const char * other) {
|
---|
[f450f2f] | 203 | (*s.inner) += other;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[6264087] | 206 | string ?+?(const string & s, char other) {
|
---|
[f450f2f] | 207 | string ret = s;
|
---|
| 208 | ret += other;
|
---|
| 209 | return ret;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[6264087] | 212 | string ?+?(const string & s, const string & s2) {
|
---|
[f450f2f] | 213 | string ret = s;
|
---|
| 214 | ret += s2;
|
---|
| 215 | return ret;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[6264087] | 218 | string ?+?(const char * s1, const char * s2) {
|
---|
[f450f2f] | 219 | string ret = s1;
|
---|
| 220 | ret += s2;
|
---|
| 221 | return ret;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[6264087] | 224 | string ?+?(const string & s, const char * other) {
|
---|
[f450f2f] | 225 | string ret = s;
|
---|
| 226 | ret += other;
|
---|
| 227 | return ret;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | ////////////////////////////////////////////////////////
|
---|
| 231 | // Repetition
|
---|
| 232 |
|
---|
[6264087] | 233 | string ?*?(const string & s, size_t factor) {
|
---|
[f450f2f] | 234 | string ret = "";
|
---|
| 235 | for (factor) ret += s;
|
---|
| 236 | return ret;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | string ?*?(char c, size_t size) {
|
---|
| 240 | string ret = "";
|
---|
| 241 | for ((size_t)size) ret += c;
|
---|
| 242 | return ret;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | string ?*?(const char *s, size_t factor) {
|
---|
| 246 | string ss = s;
|
---|
| 247 | return ss * factor;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | ////////////////////////////////////////////////////////
|
---|
| 251 | // Character access
|
---|
| 252 |
|
---|
[6264087] | 253 | char ?[?](const string & s, size_t index) {
|
---|
[f450f2f] | 254 | return (*s.inner)[index];
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[6264087] | 257 | string ?[?](string & s, size_t index) {
|
---|
[f450f2f] | 258 | string ret = { *s.inner, index, index + 1 };
|
---|
| 259 | return ret`shareEdits;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | ////////////////////////////////////////////////////////
|
---|
| 263 | // Search
|
---|
| 264 |
|
---|
[6264087] | 265 | bool contains(const string & s, char ch) {
|
---|
[f450f2f] | 266 | return contains( *s.inner, ch );
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[6264087] | 269 | int find(const string & s, char search) {
|
---|
[f450f2f] | 270 | return find( *s.inner, search );
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[6264087] | 273 | int find(const string & s, const string & search) {
|
---|
[f450f2f] | 274 | return find( *s.inner, *search.inner );
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[6264087] | 277 | int find(const string & s, const char * search) {
|
---|
[f450f2f] | 278 | return find( *s.inner, search);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[6264087] | 281 | int find(const string & s, const char * search, size_t searchsize) {
|
---|
[f450f2f] | 282 | return find( *s.inner, search, searchsize);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[6264087] | 285 | int findFrom(const string & s, size_t fromPos, char search) {
|
---|
[08ed947] | 286 | return findFrom( *s.inner, fromPos, search );
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[6264087] | 289 | int findFrom(const string & s, size_t fromPos, const string & search) {
|
---|
[08ed947] | 290 | return findFrom( *s.inner, fromPos, *search.inner );
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[6264087] | 293 | int findFrom(const string & s, size_t fromPos, const char * search) {
|
---|
[08ed947] | 294 | return findFrom( *s.inner, fromPos, search );
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[6264087] | 297 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
|
---|
[08ed947] | 298 | return findFrom( *s.inner, fromPos, search, searchsize );
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[6264087] | 301 | bool includes(const string & s, const string & search) {
|
---|
[f450f2f] | 302 | return includes( *s.inner, *search.inner );
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[6264087] | 305 | bool includes(const string & s, const char * search) {
|
---|
[f450f2f] | 306 | return includes( *s.inner, search );
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[6264087] | 309 | bool includes(const string & s, const char * search, size_t searchsize) {
|
---|
[f450f2f] | 310 | return includes( *s.inner, search, searchsize );
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[6264087] | 313 | bool startsWith(const string & s, const string & prefix) {
|
---|
[f450f2f] | 314 | return startsWith( *s.inner, *prefix.inner );
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[6264087] | 317 | bool startsWith(const string & s, const char * prefix) {
|
---|
[f450f2f] | 318 | return startsWith( *s.inner, prefix );
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[6264087] | 321 | bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
|
---|
[f450f2f] | 322 | return startsWith( *s.inner, prefix, prefixsize );
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[6264087] | 325 | bool endsWith(const string & s, const string & suffix) {
|
---|
[f450f2f] | 326 | return endsWith( *s.inner, *suffix.inner );
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[6264087] | 329 | bool endsWith(const string & s, const char * suffix) {
|
---|
[f450f2f] | 330 | return endsWith( *s.inner, suffix );
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[6264087] | 333 | bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
|
---|
[f450f2f] | 334 | return endsWith( *s.inner, suffix, suffixsize );
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 339 | // charclass, include, exclude
|
---|
| 340 |
|
---|
| 341 | void ?{}( charclass & this, const string & chars) {
|
---|
| 342 | (this.inner) { malloc() };
|
---|
| 343 | ?{}( *this.inner, *(const string_res *)chars.inner );
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void ?{}( charclass & this, const char * chars ) {
|
---|
| 347 | (this.inner) { malloc() };
|
---|
| 348 | ?{}( *this.inner, chars );
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | void ?{}( charclass & this, const char * chars, size_t charssize ) {
|
---|
| 352 | (this.inner) { malloc() };
|
---|
| 353 | ?{}( *this.inner, chars, charssize );
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | void ^?{}( charclass & this ) {
|
---|
| 357 | ^(*this.inner){};
|
---|
| 358 | free( this.inner );
|
---|
| 359 | this.inner = 0p;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
[6264087] | 363 | int exclude(const string & s, const charclass & mask) {
|
---|
[f450f2f] | 364 | return exclude( *s.inner, *mask.inner );
|
---|
| 365 | }
|
---|
| 366 | /*
|
---|
[6264087] | 367 | StrSlice exclude(string & s, const charclass & mask) {
|
---|
[f450f2f] | 368 | }
|
---|
| 369 | */
|
---|
| 370 |
|
---|
[6264087] | 371 | int include(const string & s, const charclass & mask) {
|
---|
[f450f2f] | 372 | return include( *s.inner, *mask.inner );
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /*
|
---|
[6264087] | 376 | StrSlice include(string & s, const charclass & mask) {
|
---|
[f450f2f] | 377 | }
|
---|
| 378 | */
|
---|
| 379 |
|
---|