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