| [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
|
|---|
| [ed5023d1] | 12 | // Last Modified On : Sat Apr 5 15:18:30 2025
|
|---|
| 13 | // Update Count : 318
|
|---|
| [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 | // private (not in header)
|
|---|
| [e8b3717] | 32 | static void ?{}( string & s, string_res & src, size_t start, size_t len ) {
|
|---|
| [4223317] | 33 | (s.inner) { malloc() };
|
|---|
| 34 | ?{}( *s.inner, src, SHARE_EDITS, start, len );
|
|---|
| [f450f2f] | 35 | }
|
|---|
| 36 |
|
|---|
| [479fbe3] | 37 | void ?{}( string & s ) {
|
|---|
| [4223317] | 38 | (s.inner) { malloc() };
|
|---|
| 39 | ?{}( *s.inner );
|
|---|
| [479fbe3] | 40 | }
|
|---|
| 41 |
|
|---|
| [681e12f] | 42 | void ?{}( string & s, const string & c ) {
|
|---|
| [4223317] | 43 | (s.inner) { malloc() };
|
|---|
| 44 | ?{}( *s.inner, *c.inner, COPY_VALUE );
|
|---|
| [f450f2f] | 45 | }
|
|---|
| 46 |
|
|---|
| [4dab7e8] | 47 | void ?{}( string & s, const string & s2, size_t maxlen ) {
|
|---|
| [4223317] | 48 | (s.inner) { malloc() };
|
|---|
| 49 | ?{}( *s.inner, *s2.inner, COPY_VALUE, maxlen );
|
|---|
| [7abc3de] | 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| [681e12f] | 53 | void ?{}( string & s, string & c ) {
|
|---|
| [4223317] | 54 | ?{}( s, (const string &) c );
|
|---|
| [f450f2f] | 55 | }
|
|---|
| 56 |
|
|---|
| [479fbe3] | 57 | void ?{}( string & s, const char c ) {
|
|---|
| [4223317] | 58 | (s.inner) { malloc() };
|
|---|
| 59 | ?{}( *s.inner, c );
|
|---|
| [f450f2f] | 60 | }
|
|---|
| 61 |
|
|---|
| [479fbe3] | 62 | void ?{}( string & s, const char * c ) {
|
|---|
| [4223317] | 63 | (s.inner) { malloc() };
|
|---|
| 64 | ?{}( *s.inner, c );
|
|---|
| [479fbe3] | 65 | }
|
|---|
| 66 |
|
|---|
| [4dab7e8] | 67 | void ?{}( string & s, const char * c, size_t size ) {
|
|---|
| [4223317] | 68 | (s.inner) { malloc() };
|
|---|
| 69 | ?{}( *s.inner, c, size );
|
|---|
| [f450f2f] | 70 | }
|
|---|
| 71 |
|
|---|
| [f2898df] | 72 | void ?{}( string & s, ssize_t rhs ) {
|
|---|
| [4223317] | 73 | (s.inner) { malloc() };
|
|---|
| 74 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void ?{}( string & s, size_t rhs ) {
|
|---|
| [4223317] | 78 | (s.inner) { malloc() };
|
|---|
| 79 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void ?{}( string & s, double rhs ) {
|
|---|
| [4223317] | 83 | (s.inner) { malloc() };
|
|---|
| 84 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void ?{}( string & s, long double rhs ) {
|
|---|
| [4223317] | 88 | (s.inner) { malloc() };
|
|---|
| 89 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void ?{}( string & s, double _Complex rhs ) {
|
|---|
| [4223317] | 93 | (s.inner) { malloc() };
|
|---|
| 94 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void ?{}( string & s, long double _Complex rhs ) {
|
|---|
| [4223317] | 98 | (s.inner) { malloc() };
|
|---|
| 99 | ?{}( *s.inner, rhs );
|
|---|
| [f2898df] | 100 | }
|
|---|
| 101 |
|
|---|
| [4dab7e8] | 102 | string str( ssize_t rhs ) {
|
|---|
| 103 | string s = rhs;
|
|---|
| 104 | return s;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | string str( size_t rhs ) {
|
|---|
| 108 | string s = rhs;
|
|---|
| 109 | return s;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | string str( double rhs ) {
|
|---|
| 113 | string s = rhs;
|
|---|
| 114 | return s;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | string str( long double rhs ) {
|
|---|
| 118 | string s = rhs;
|
|---|
| 119 | return s;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | string str( double _Complex rhs ) {
|
|---|
| 123 | string s = rhs;
|
|---|
| 124 | return s;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | string str( long double _Complex rhs ) {
|
|---|
| 128 | string s = rhs;
|
|---|
| 129 | return s;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| [681e12f] | 132 | void ^?{}( string & s ) {
|
|---|
| [4223317] | 133 | ^(*s.inner){};
|
|---|
| 134 | free( s.inner );
|
|---|
| 135 | s.inner = 0p;
|
|---|
| [f450f2f] | 136 | }
|
|---|
| 137 |
|
|---|
| 138 | ////////////////////////////////////////////////////////
|
|---|
| 139 | // Alternate construction: request shared edits
|
|---|
| 140 |
|
|---|
| [8c2723f] | 141 | string_Share ?`share( string & s ) {
|
|---|
| [4223317] | 142 | string_Share ret = { &s };
|
|---|
| 143 | return ret;
|
|---|
| [f450f2f] | 144 | }
|
|---|
| 145 |
|
|---|
| [8c2723f] | 146 | void ?{}( string & s, string_Share src ) {
|
|---|
| [4223317] | 147 | ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth );
|
|---|
| [f450f2f] | 148 | }
|
|---|
| 149 |
|
|---|
| 150 | ////////////////////////////////////////////////////////
|
|---|
| 151 | // Assignment
|
|---|
| 152 |
|
|---|
| [4dab7e8] | 153 | string & ?=?( string & s, const string & c ) {
|
|---|
| [4223317] | 154 | (*s.inner) = (*c.inner);
|
|---|
| 155 | return s;
|
|---|
| [f450f2f] | 156 | }
|
|---|
| [b1eefe50] | 157 |
|
|---|
| [4dab7e8] | 158 | string & ?=?( string & s, string & c ) {
|
|---|
| [4223317] | 159 | (*s.inner) = (*c.inner);
|
|---|
| 160 | return s;
|
|---|
| [906d8fa] | 161 | }
|
|---|
| [f450f2f] | 162 |
|
|---|
| [b1eefe50] | 163 | string & ?=?( string & s, const char * val ) {
|
|---|
| [4223317] | 164 | (*s.inner) = val;
|
|---|
| 165 | return s;
|
|---|
| [f450f2f] | 166 | }
|
|---|
| 167 |
|
|---|
| [b1eefe50] | 168 | string & ?=?( string & s, char val ) {
|
|---|
| [4223317] | 169 | (*s.inner) = val;
|
|---|
| 170 | return s;
|
|---|
| [b1eefe50] | 171 | }
|
|---|
| 172 |
|
|---|
| [4dab7e8] | 173 | string & assign( string & s, const string & c, size_t n ) {
|
|---|
| [4223317] | 174 | assign( *s.inner, *c.inner, n );
|
|---|
| 175 | return s;
|
|---|
| [e891349] | 176 | }
|
|---|
| [b1eefe50] | 177 |
|
|---|
| [4dab7e8] | 178 | string & assign( string & s, const char * c, size_t n ) {
|
|---|
| [4223317] | 179 | assign( *s.inner, c, n );
|
|---|
| 180 | return s;
|
|---|
| [e891349] | 181 | }
|
|---|
| 182 |
|
|---|
| [f2898df] | 183 | string & ?=?( string & s, ssize_t rhs ) {
|
|---|
| [4223317] | 184 | (*s.inner) = rhs;
|
|---|
| 185 | return s;
|
|---|
| [f2898df] | 186 | }
|
|---|
| 187 |
|
|---|
| 188 | string & ?=?( string & s, size_t rhs ) {
|
|---|
| [4223317] | 189 | (*s.inner) = rhs;
|
|---|
| 190 | return s;
|
|---|
| [f2898df] | 191 | }
|
|---|
| 192 |
|
|---|
| 193 | string & ?=?( string & s, double rhs ) {
|
|---|
| [4223317] | 194 | (*s.inner) = rhs;
|
|---|
| 195 | return s;
|
|---|
| [f2898df] | 196 | }
|
|---|
| 197 |
|
|---|
| 198 | string & ?=?( string & s, long double rhs ) {
|
|---|
| [4223317] | 199 | (*s.inner) = rhs;
|
|---|
| 200 | return s;
|
|---|
| [f2898df] | 201 | }
|
|---|
| 202 |
|
|---|
| 203 | string & ?=?( string & s, double _Complex rhs ) {
|
|---|
| [4223317] | 204 | (*s.inner) = rhs;
|
|---|
| 205 | return s;
|
|---|
| [f2898df] | 206 | }
|
|---|
| 207 |
|
|---|
| 208 | string & ?=?( string & s, long double _Complex rhs ) {
|
|---|
| [4223317] | 209 | (*s.inner) = rhs;
|
|---|
| 210 | return s;
|
|---|
| [f2898df] | 211 | }
|
|---|
| [f450f2f] | 212 |
|
|---|
| 213 | ////////////////////////////////////////////////////////
|
|---|
| [d32679d5] | 214 | // Input-Output
|
|---|
| [f450f2f] | 215 |
|
|---|
| [681e12f] | 216 | ofstream & ?|?( ofstream & out, const string & s ) {
|
|---|
| [4223317] | 217 | return out | (*s.inner); // print internal string_res
|
|---|
| [f450f2f] | 218 | }
|
|---|
| 219 |
|
|---|
| [681e12f] | 220 | void ?|?( ofstream & out, const string & s ) {
|
|---|
| [4223317] | 221 | (ofstream &)(out | (*s.inner)); ends( out );
|
|---|
| [f450f2f] | 222 | }
|
|---|
| 223 |
|
|---|
| [34c6e1e6] | 224 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
|---|
| [ee70ff5] | 225 | size_t l = len( f.val );
|
|---|
| 226 | char cstr[l + 1]; // room for null terminator
|
|---|
| 227 | for ( i; l ) cstr[i] = f.val[i]; // copy string
|
|---|
| 228 | cstr[l] = '\0'; // terminate
|
|---|
| [34c6e1e6] | 229 | _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
|
|---|
| [fbe3f03] | 230 | return os | cf | nonl;
|
|---|
| [34c6e1e6] | 231 | } // ?|?
|
|---|
| 232 |
|
|---|
| 233 | void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
|
|---|
| 234 | (ofstream &)(os | f); ends( os );
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| [4dab7e8] | 237 | ifstream & ?|?( ifstream & in, string & s ) {
|
|---|
| [4223317] | 238 | return in | (*s.inner); // read to internal string_res
|
|---|
| [d32679d5] | 239 | }
|
|---|
| 240 |
|
|---|
| [211def2] | 241 | ifstream & ?|?( ifstream & is, _Istream_Squoted f ) {
|
|---|
| 242 | _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } };
|
|---|
| [4223317] | 243 | return is | f2;
|
|---|
| [211def2] | 244 | } // ?|?
|
|---|
| [d32679d5] | 245 |
|
|---|
| [34c6e1e6] | 246 | ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
|
|---|
| [211def2] | 247 | // _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr};
|
|---|
| [737988b] | 248 | _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
|
|---|
| [4223317] | 249 | return is | f2;
|
|---|
| [7e1dbd7] | 250 | } // ?|?
|
|---|
| 251 |
|
|---|
| [f450f2f] | 252 | ////////////////////////////////////////////////////////
|
|---|
| 253 | // Slicing
|
|---|
| 254 |
|
|---|
| [4223317] | 255 | string ?()( string & s, ssize_t start, ssize_t len ) {
|
|---|
| [ee70ff5] | 256 | if ( start < 0 ) { start += len( s ); }
|
|---|
| [4223317] | 257 | if ( len < 0 ) { len = -len; start -= len; }
|
|---|
| [ed5023d1] | 258 | if ( start >= len( s ) ) return (string){ "" };
|
|---|
| [ee70ff5] | 259 | if ( start + len > len( s ) ) len = len( s ) - start;
|
|---|
| [4223317] | 260 | string ret = { *s.inner, start, len };
|
|---|
| 261 | return ret`share;
|
|---|
| [f450f2f] | 262 | }
|
|---|
| 263 |
|
|---|
| [4223317] | 264 | string ?()( string & s, ssize_t start ) {
|
|---|
| [ee70ff5] | 265 | if ( start < 0 ) { start += len( s ); }
|
|---|
| 266 | string ret = { *s.inner, start, len( s ) - start };
|
|---|
| [4223317] | 267 | return ret`share;
|
|---|
| [bc9f84a] | 268 | }
|
|---|
| 269 |
|
|---|
| [f450f2f] | 270 | ////////////////////////////////////////////////////////
|
|---|
| 271 | // Comparison
|
|---|
| 272 |
|
|---|
| [4dab7e8] | 273 | int strcmp( const string & s1, const string & s2 ) { return strcmp( *s1.inner, *s2.inner ); }
|
|---|
| 274 | bool ?==?( const string & s1, const string & s2 ) { return *s1.inner == *s2.inner; }
|
|---|
| 275 | bool ?!=?( const string & s1, const string & s2 ) { return *s1.inner != *s2.inner; }
|
|---|
| 276 | bool ?>? ( const string & s1, const string & s2 ) { return *s1.inner > *s2.inner; }
|
|---|
| 277 | bool ?>=?( const string & s1, const string & s2 ) { return *s1.inner >= *s2.inner; }
|
|---|
| 278 | bool ?<=?( const string & s1, const string & s2 ) { return *s1.inner <= *s2.inner; }
|
|---|
| 279 | bool ?<? ( const string & s1, const string & s2 ) { return *s1.inner < *s2.inner; }
|
|---|
| 280 |
|
|---|
| 281 | int strcmp( const string & s1, const char * s2 ) { return strcmp( *s1.inner, s2 ); }
|
|---|
| 282 | bool ?==?( const string & s1, const char * s2 ) { return *s1.inner == s2; }
|
|---|
| 283 | bool ?!=?( const string & s1, const char * s2 ) { return *s1.inner != s2; }
|
|---|
| 284 | bool ?>? ( const string & s1, const char * s2 ) { return *s1.inner > s2; }
|
|---|
| 285 | bool ?>=?( const string & s1, const char * s2 ) { return *s1.inner >= s2; }
|
|---|
| 286 | bool ?<=?( const string & s1, const char * s2 ) { return *s1.inner <= s2; }
|
|---|
| 287 | bool ?<? ( const string & s1, const char * s2 ) { return *s1.inner < s2; }
|
|---|
| 288 |
|
|---|
| 289 | int strcmp( const char * s1, const string & s2 ) { return strcmp( s1, *s2.inner ); }
|
|---|
| 290 | bool ?==?( const char * s1, const string & s2 ) { return s1 == *s2.inner; }
|
|---|
| 291 | bool ?!=?( const char * s1, const string & s2 ) { return s1 != *s2.inner; }
|
|---|
| 292 | bool ?>? ( const char * s1, const string & s2 ) { return s1 > *s2.inner; }
|
|---|
| 293 | bool ?>=?( const char * s1, const string & s2 ) { return s1 >= *s2.inner; }
|
|---|
| 294 | bool ?<=?( const char * s1, const string & s2 ) { return s1 <= *s2.inner; }
|
|---|
| 295 | bool ?<? ( const char * s1, const string & s2 ) { return s1 < *s2.inner; }
|
|---|
| [f450f2f] | 296 |
|
|---|
| 297 |
|
|---|
| 298 | ////////////////////////////////////////////////////////
|
|---|
| 299 | // Getter
|
|---|
| 300 |
|
|---|
| [ee70ff5] | 301 | size_t len( const string & s ) {
|
|---|
| 302 | return len( *s.inner );
|
|---|
| [f450f2f] | 303 | }
|
|---|
| 304 |
|
|---|
| 305 | ////////////////////////////////////////////////////////
|
|---|
| 306 | // Concatenation
|
|---|
| 307 |
|
|---|
| [4dab7e8] | 308 | void ?+=?( string & s, char c ) {
|
|---|
| [4223317] | 309 | (*s.inner) += c;
|
|---|
| [f450f2f] | 310 | }
|
|---|
| 311 |
|
|---|
| [4dab7e8] | 312 | void ?+=?( string & s, const string & s2 ) {
|
|---|
| [4223317] | 313 | (*s.inner) += (*s2.inner);
|
|---|
| [f450f2f] | 314 | }
|
|---|
| 315 |
|
|---|
| [4dab7e8] | 316 | void append( string & s, const string & s2, size_t maxlen ) {
|
|---|
| [4223317] | 317 | append( (*s.inner), (*s2.inner), maxlen );
|
|---|
| [e891349] | 318 | }
|
|---|
| 319 |
|
|---|
| [4dab7e8] | 320 | void ?+=?( string & s, const char * c ) {
|
|---|
| [4223317] | 321 | (*s.inner) += c;
|
|---|
| [f450f2f] | 322 | }
|
|---|
| 323 |
|
|---|
| [4dab7e8] | 324 | void append( string & s, const char * buffer, size_t bsize ) {
|
|---|
| [4223317] | 325 | append( (*s.inner), buffer, bsize );
|
|---|
| [e891349] | 326 | }
|
|---|
| 327 |
|
|---|
| [4dab7e8] | 328 | string ?+?( const string & s, char c ) {
|
|---|
| [4223317] | 329 | string ret = s;
|
|---|
| 330 | ret += c;
|
|---|
| 331 | return ret;
|
|---|
| [4dab7e8] | 332 | }
|
|---|
| 333 |
|
|---|
| 334 | string ?+?( char c, const string & s ) {
|
|---|
| [96a11655] | 335 | string ret = c;
|
|---|
| 336 | ret += s;
|
|---|
| [4223317] | 337 | return ret;
|
|---|
| [f450f2f] | 338 | }
|
|---|
| 339 |
|
|---|
| [4dab7e8] | 340 | string ?+?( const string & s, const string & s2 ) {
|
|---|
| [4223317] | 341 | string ret = s;
|
|---|
| 342 | ret += s2;
|
|---|
| 343 | return ret;
|
|---|
| [f450f2f] | 344 | }
|
|---|
| 345 |
|
|---|
| [4dab7e8] | 346 | string ?+?( const char * s, char c ) {
|
|---|
| [4223317] | 347 | string ret = s;
|
|---|
| 348 | ret += c;
|
|---|
| 349 | return ret;
|
|---|
| [4dab7e8] | 350 | }
|
|---|
| 351 |
|
|---|
| 352 | string ?+?( char c, const char * s ) {
|
|---|
| [4223317] | 353 | string ret = c;
|
|---|
| 354 | ret += s;
|
|---|
| 355 | return ret;
|
|---|
| [4dab7e8] | 356 | }
|
|---|
| 357 |
|
|---|
| 358 | string ?+?( const char * s1, const char * s2 ) {
|
|---|
| [4223317] | 359 | string ret = s1;
|
|---|
| 360 | ret += s2;
|
|---|
| 361 | return ret;
|
|---|
| [4dab7e8] | 362 | }
|
|---|
| 363 |
|
|---|
| [ee70ff5] | 364 | string ?+?( const char * s1, const string & s2 ) {
|
|---|
| [4223317] | 365 | string ret = s1;
|
|---|
| 366 | ret += s2;
|
|---|
| 367 | return ret;
|
|---|
| [f450f2f] | 368 | }
|
|---|
| 369 |
|
|---|
| [4dab7e8] | 370 | string ?+?( const string & s, const char * c ) {
|
|---|
| [4223317] | 371 | string ret = s;
|
|---|
| 372 | ret += c;
|
|---|
| 373 | return ret;
|
|---|
| [f450f2f] | 374 | }
|
|---|
| 375 |
|
|---|
| [ee70ff5] | 376 | string ?+?( char c1, char c2 ) {
|
|---|
| 377 | string ret = c1;
|
|---|
| 378 | ret += c2;
|
|---|
| 379 | return ret;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| [f450f2f] | 382 | ////////////////////////////////////////////////////////
|
|---|
| 383 | // Repetition
|
|---|
| 384 |
|
|---|
| [4dab7e8] | 385 | void ?*=?( string & s, size_t factor ) {
|
|---|
| [4223317] | 386 | (*s.inner) *= factor;
|
|---|
| [f450f2f] | 387 | }
|
|---|
| 388 |
|
|---|
| [4dab7e8] | 389 | string ?*?( const string & s, size_t factor ) {
|
|---|
| [4223317] | 390 | string ret = s;
|
|---|
| 391 | ret *= factor;
|
|---|
| 392 | return ret;
|
|---|
| [479fbe3] | 393 | }
|
|---|
| 394 |
|
|---|
| [4dab7e8] | 395 | string ?*?( char c, size_t factor ) {
|
|---|
| [4223317] | 396 | string ret = c;
|
|---|
| 397 | ret *= factor;
|
|---|
| 398 | return ret;
|
|---|
| [f450f2f] | 399 | }
|
|---|
| 400 |
|
|---|
| [4dab7e8] | 401 | string ?*?( const char * s, size_t factor ) {
|
|---|
| [4223317] | 402 | string ret = s;
|
|---|
| 403 | ret *= factor;
|
|---|
| 404 | return ret;
|
|---|
| [f450f2f] | 405 | }
|
|---|
| 406 |
|
|---|
| 407 | ////////////////////////////////////////////////////////
|
|---|
| 408 | // Character access
|
|---|
| 409 |
|
|---|
| [4dab7e8] | 410 | char ?[?]( const string & s, size_t index ) {
|
|---|
| [4223317] | 411 | return (*s.inner)[index];
|
|---|
| [f450f2f] | 412 | }
|
|---|
| 413 |
|
|---|
| [4dab7e8] | 414 | string ?[?]( string & s, size_t index ) {
|
|---|
| [4223317] | 415 | string ret = { *s.inner, index, 1 };
|
|---|
| 416 | return ret`share;
|
|---|
| [f450f2f] | 417 | }
|
|---|
| 418 |
|
|---|
| 419 | ////////////////////////////////////////////////////////
|
|---|
| 420 | // Search
|
|---|
| 421 |
|
|---|
| [4dab7e8] | 422 | bool contains( const string & s, char ch ) {
|
|---|
| [4223317] | 423 | return contains( *s.inner, ch );
|
|---|
| [f450f2f] | 424 | }
|
|---|
| 425 |
|
|---|
| [ed5023d1] | 426 | int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen ) {
|
|---|
| 427 | if ( start < 0 ) { start += len( s ); }
|
|---|
| 428 | if ( len < 0 ) { len = -len; start -= len; }
|
|---|
| 429 | if ( start >= len( s ) ) return 0;
|
|---|
| 430 | if ( start + len > len( s ) ) len = len( s ) - start;
|
|---|
| 431 |
|
|---|
| 432 | if ( kstart < 0 ) { kstart += len( key ); }
|
|---|
| 433 | if ( klen < 0 ) { klen = -klen; kstart -= klen; }
|
|---|
| 434 | if ( kstart >= len( key ) ) return 0;
|
|---|
| 435 | if ( kstart + klen > len( key ) ) klen = len( key ) - kstart;
|
|---|
| 436 |
|
|---|
| 437 | return findFrom( *s.inner, start, *key.inner );
|
|---|
| [f450f2f] | 438 | }
|
|---|
| 439 |
|
|---|
| [ed5023d1] | 440 | int find( const string & s, char key ) {
|
|---|
| 441 | return find( *s.inner, key );
|
|---|
| [f450f2f] | 442 | }
|
|---|
| 443 |
|
|---|
| [ed5023d1] | 444 | int find( const string & s, const string & key ) {
|
|---|
| 445 | return find( *s.inner, *key.inner );
|
|---|
| [f450f2f] | 446 | }
|
|---|
| 447 |
|
|---|
| [ed5023d1] | 448 | int find( const string & s, const char * key ) {
|
|---|
| 449 | return find( *s.inner, key );
|
|---|
| [f450f2f] | 450 | }
|
|---|
| 451 |
|
|---|
| [ed5023d1] | 452 | int find( const string & s, const char * key, size_t keysize ) {
|
|---|
| 453 | return find( *s.inner, key, keysize );
|
|---|
| [08ed947] | 454 | }
|
|---|
| 455 |
|
|---|
| [ed5023d1] | 456 | int find( const string & s, size_t start, char key ) {
|
|---|
| 457 | return findFrom( *s.inner, start, key );
|
|---|
| [08ed947] | 458 | }
|
|---|
| 459 |
|
|---|
| [ed5023d1] | 460 | int find( const string & s, size_t start, const char * key ) {
|
|---|
| 461 | return findFrom( *s.inner, start, key );
|
|---|
| [08ed947] | 462 | }
|
|---|
| 463 |
|
|---|
| [ed5023d1] | 464 | int find( const string & s, size_t start, const char * key, size_t keysize ) {
|
|---|
| 465 | return findFrom( *s.inner, start, key, keysize );
|
|---|
| [08ed947] | 466 | }
|
|---|
| 467 |
|
|---|
| [ed5023d1] | 468 | bool includes( const string & s, const string & mask ) {
|
|---|
| 469 | return includes( *s.inner, *mask.inner );
|
|---|
| [f450f2f] | 470 | }
|
|---|
| 471 |
|
|---|
| [ed5023d1] | 472 | bool includes( const string & s, const char * mask ) {
|
|---|
| 473 | return includes( *s.inner, mask );
|
|---|
| [f450f2f] | 474 | }
|
|---|
| 475 |
|
|---|
| [ed5023d1] | 476 | bool includes( const string & s, const char * mask, size_t masksize ) {
|
|---|
| 477 | return includes( *s.inner, mask, masksize );
|
|---|
| [f450f2f] | 478 | }
|
|---|
| 479 |
|
|---|
| [4dab7e8] | 480 | bool startsWith( const string & s, const string & prefix ) {
|
|---|
| [4223317] | 481 | return startsWith( *s.inner, *prefix.inner );
|
|---|
| [f450f2f] | 482 | }
|
|---|
| 483 |
|
|---|
| [4dab7e8] | 484 | bool startsWith( const string & s, const char * prefix ) {
|
|---|
| [4223317] | 485 | return startsWith( *s.inner, prefix );
|
|---|
| [f450f2f] | 486 | }
|
|---|
| 487 |
|
|---|
| [4dab7e8] | 488 | bool startsWith( const string & s, const char * prefix, size_t prefixsize ) {
|
|---|
| [4223317] | 489 | return startsWith( *s.inner, prefix, prefixsize );
|
|---|
| [f450f2f] | 490 | }
|
|---|
| 491 |
|
|---|
| [4dab7e8] | 492 | bool endsWith( const string & s, const string & suffix ) {
|
|---|
| [4223317] | 493 | return endsWith( *s.inner, *suffix.inner );
|
|---|
| [f450f2f] | 494 | }
|
|---|
| 495 |
|
|---|
| [4dab7e8] | 496 | bool endsWith( const string & s, const char * suffix ) {
|
|---|
| [4223317] | 497 | return endsWith( *s.inner, suffix );
|
|---|
| [f450f2f] | 498 | }
|
|---|
| 499 |
|
|---|
| [4dab7e8] | 500 | bool endsWith( const string & s, const char * suffix, size_t suffixsize ) {
|
|---|
| [4223317] | 501 | return endsWith( *s.inner, suffix, suffixsize );
|
|---|
| [f450f2f] | 502 | }
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 | ///////////////////////////////////////////////////////////////////////////
|
|---|
| 506 | // charclass, include, exclude
|
|---|
| 507 |
|
|---|
| [4dab7e8] | 508 | void ?{}( charclass & s, const string & chars ) {
|
|---|
| [4223317] | 509 | (s.inner) { malloc() };
|
|---|
| 510 | ?{}( *s.inner, *(const string_res *)chars.inner );
|
|---|
| [f450f2f] | 511 | }
|
|---|
| 512 |
|
|---|
| [681e12f] | 513 | void ?{}( charclass & s, const char * chars ) {
|
|---|
| [4223317] | 514 | (s.inner) { malloc() };
|
|---|
| 515 | ?{}( *s.inner, chars );
|
|---|
| [f450f2f] | 516 | }
|
|---|
| 517 |
|
|---|
| [681e12f] | 518 | void ?{}( charclass & s, const char * chars, size_t charssize ) {
|
|---|
| [4223317] | 519 | (s.inner) { malloc() };
|
|---|
| 520 | ?{}( *s.inner, chars, charssize );
|
|---|
| [f450f2f] | 521 | }
|
|---|
| 522 |
|
|---|
| [681e12f] | 523 | void ^?{}( charclass & s ) {
|
|---|
| [4223317] | 524 | ^(*s.inner){};
|
|---|
| 525 | free( s.inner );
|
|---|
| 526 | s.inner = 0p;
|
|---|
| [f450f2f] | 527 | }
|
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| [4dab7e8] | 530 | int exclude( const string & s, const charclass & mask ) {
|
|---|
| [4223317] | 531 | return exclude( *s.inner, *mask.inner );
|
|---|
| [f450f2f] | 532 | }
|
|---|
| 533 | /*
|
|---|
| [4dab7e8] | 534 | StrSlice exclude( string & s, const charclass & mask ) {
|
|---|
| [f450f2f] | 535 | }
|
|---|
| 536 | */
|
|---|
| 537 |
|
|---|
| [4dab7e8] | 538 | int include( const string & s, const charclass & mask ) {
|
|---|
| [4223317] | 539 | return include( *s.inner, *mask.inner );
|
|---|
| [f450f2f] | 540 | }
|
|---|
| 541 |
|
|---|
| 542 | /*
|
|---|
| [4dab7e8] | 543 | StrSlice include( string & s, const charclass & mask ) {
|
|---|
| [f450f2f] | 544 | }
|
|---|
| 545 | */
|
|---|