[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
|
---|
[6264087] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[34c6e1e6] | 12 | // Last Modified On : Sat Sep 2 11:26:28 2023
|
---|
| 13 | // Update Count : 55
|
---|
[f450f2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <fstream.hfa>
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | // in string_res.hfa
|
---|
| 22 | struct string_res;
|
---|
| 23 | struct charclass_res;
|
---|
| 24 |
|
---|
| 25 | struct string {
|
---|
| 26 | string_res * inner;
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | // Getters
|
---|
[6264087] | 30 | size_t size(const string & s);
|
---|
[f450f2f] | 31 |
|
---|
| 32 | // RAII, assignment
|
---|
[6264087] | 33 | void ?{}(string & this); // empty string
|
---|
| 34 | void ?{}(string & s, const char * initial); // copy from string literal (NULL-terminated)
|
---|
| 35 | void ?{}(string & s, const char * buffer, size_t bsize); // copy specific length from buffer
|
---|
[f450f2f] | 36 |
|
---|
[6264087] | 37 | void ?{}(string & s, const string & s2);
|
---|
| 38 | void ?{}(string & s, string & s2);
|
---|
[f450f2f] | 39 |
|
---|
[6264087] | 40 | void ?=?(string & s, const char * other); // copy assignment from literal
|
---|
| 41 | void ?=?(string & s, const string & other);
|
---|
| 42 | void ?=?(string & s, char other);
|
---|
| 43 | string & ?=?(string & s, string & other); // surprising ret seems to help avoid calls to autogen
|
---|
[4b3b352] | 44 | //string ?=?( string &, string ) = void;
|
---|
[6264087] | 45 | void ^?{}(string & s);
|
---|
[f450f2f] | 46 |
|
---|
| 47 | // Alternate construction: request shared edits
|
---|
| 48 | struct string_WithSharedEdits {
|
---|
| 49 | string * s;
|
---|
| 50 | };
|
---|
| 51 | string_WithSharedEdits ?`shareEdits( string & this );
|
---|
| 52 | void ?{}( string & this, string_WithSharedEdits src );
|
---|
| 53 |
|
---|
| 54 | // IO Operator
|
---|
[6264087] | 55 | ofstream & ?|?(ofstream & out, const string & s);
|
---|
| 56 | void ?|?(ofstream & out, const string & s);
|
---|
| 57 | ifstream & ?|?(ifstream & in, string & s);
|
---|
| 58 | void ?|?( ifstream & in, string & this );
|
---|
[7e1dbd7] | 59 |
|
---|
[34c6e1e6] | 60 | static inline {
|
---|
| 61 | _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; }
|
---|
| 62 | _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; }
|
---|
| 63 | _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; }
|
---|
| 64 | _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; }
|
---|
| 65 | _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; }
|
---|
| 66 | _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
|
---|
| 67 | _Ostream_Manip(string) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(string) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
|
---|
| 68 | _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
|
---|
| 69 | _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
|
---|
| 70 | } // distribution
|
---|
| 71 | ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f );
|
---|
| 72 | void ?|?( ofstream & os, _Ostream_Manip(string) );
|
---|
| 73 |
|
---|
| 74 | struct _Istream_Sstr {
|
---|
[7e1dbd7] | 75 | string & s;
|
---|
[38de914] | 76 | inline _Istream_str_base;
|
---|
[34c6e1e6] | 77 | }; // _Istream_Sstr
|
---|
[7e1dbd7] | 78 |
|
---|
| 79 | static inline {
|
---|
| 80 | // read width does not include null terminator
|
---|
[34c6e1e6] | 81 | _Istream_Sstr wdi( unsigned int rwd, string & s ) { return (_Istream_Sstr)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; }
|
---|
| 82 | _Istream_Sstr getline( string & s, const char delimiter = '\n' ) {
|
---|
| 83 | return (_Istream_Sstr)@{ s, {{.delimiter : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
|
---|
[7e1dbd7] | 84 | }
|
---|
[34c6e1e6] | 85 | _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) {
|
---|
| 86 | fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
|
---|
[7e1dbd7] | 87 | }
|
---|
[34c6e1e6] | 88 | _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : false}} }; }
|
---|
| 89 | _Istream_Sstr & incl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
|
---|
| 90 | _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : true}} }; }
|
---|
| 91 | _Istream_Sstr & excl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
|
---|
| 92 | _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ s, {{0p}, -1, {.flags.ignore : true}} }; }
|
---|
| 93 | _Istream_Sstr & ignore( _Istream_Sstr & fmt ) { fmt.flags.ignore = true; return fmt; }
|
---|
[7e1dbd7] | 94 | } // distribution
|
---|
[34c6e1e6] | 95 | ifstream & ?|?( ifstream & is, _Istream_Sstr f );
|
---|
| 96 | void ?|?( ifstream & is, _Istream_Sstr t );
|
---|
[f450f2f] | 97 |
|
---|
| 98 | // Concatenation
|
---|
[6264087] | 99 | void ?+=?(string & s, char other); // append a character
|
---|
| 100 | void ?+=?(string & s, const string & s2); // append-concatenate to first string
|
---|
| 101 | void ?+=?(string & s, const char * other); // append-concatenate to first string
|
---|
| 102 | string ?+?(const string & s, char other); // add a character to a copy of the string
|
---|
| 103 | string ?+?(const string & s, const string & s2); // copy and concatenate both strings
|
---|
| 104 | string ?+?(const char * s1, const char * s2); // concatenate both strings
|
---|
| 105 | string ?+?(const string & s, const char * other); // copy and concatenate with NULL-terminated string
|
---|
[f450f2f] | 106 |
|
---|
| 107 | // Repetition
|
---|
[6264087] | 108 | string ?*?(const string & s, size_t factor);
|
---|
[f450f2f] | 109 | string ?*?(char c, size_t size);
|
---|
| 110 | string ?*?(const char *s, size_t size);
|
---|
| 111 |
|
---|
| 112 | // Character access
|
---|
[6264087] | 113 | char ?[?](const string & s, size_t index);
|
---|
| 114 | string ?[?](string & s, size_t index); // mutable length-1 slice of original
|
---|
| 115 | //char codePointAt(const string & s, size_t index); // to revisit under Unicode
|
---|
[f450f2f] | 116 |
|
---|
| 117 | // Comparisons
|
---|
[416b443] | 118 | int cmp (const string &, const string &);
|
---|
| 119 | bool ?==?(const string &, const string &);
|
---|
| 120 | bool ?!=?(const string &, const string &);
|
---|
| 121 | bool ?>? (const string &, const string &);
|
---|
| 122 | bool ?>=?(const string &, const string &);
|
---|
| 123 | bool ?<=?(const string &, const string &);
|
---|
| 124 | bool ?<? (const string &, const string &);
|
---|
| 125 |
|
---|
| 126 | int cmp (const string &, const char*);
|
---|
| 127 | bool ?==?(const string &, const char*);
|
---|
| 128 | bool ?!=?(const string &, const char*);
|
---|
| 129 | bool ?>? (const string &, const char*);
|
---|
| 130 | bool ?>=?(const string &, const char*);
|
---|
| 131 | bool ?<=?(const string &, const char*);
|
---|
| 132 | bool ?<? (const string &, const char*);
|
---|
| 133 |
|
---|
| 134 | int cmp (const char*, const string &);
|
---|
| 135 | bool ?==?(const char*, const string &);
|
---|
| 136 | bool ?!=?(const char*, const string &);
|
---|
| 137 | bool ?>? (const char*, const string &);
|
---|
| 138 | bool ?>=?(const char*, const string &);
|
---|
| 139 | bool ?<=?(const char*, const string &);
|
---|
| 140 | bool ?<? (const char*, const string &);
|
---|
| 141 |
|
---|
[f450f2f] | 142 |
|
---|
| 143 | // Slicing
|
---|
| 144 | string ?()( string & this, size_t start, size_t end ); // TODO const?
|
---|
| 145 | string ?()( string & this, size_t start);
|
---|
| 146 |
|
---|
| 147 | // String search
|
---|
[6264087] | 148 | bool contains(const string & s, char ch); // single character
|
---|
[f450f2f] | 149 |
|
---|
[6264087] | 150 | int find(const string & s, char search);
|
---|
| 151 | int find(const string & s, const string & search);
|
---|
| 152 | int find(const string & s, const char * search);
|
---|
| 153 | int find(const string & s, const char * search, size_t searchsize);
|
---|
[f450f2f] | 154 |
|
---|
[6264087] | 155 | int findFrom(const string & s, size_t fromPos, char search);
|
---|
| 156 | int findFrom(const string & s, size_t fromPos, const string & search);
|
---|
| 157 | int findFrom(const string & s, size_t fromPos, const char * search);
|
---|
| 158 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize);
|
---|
[08ed947] | 159 |
|
---|
[6264087] | 160 | bool includes(const string & s, const string & search);
|
---|
| 161 | bool includes(const string & s, const char * search);
|
---|
| 162 | bool includes(const string & s, const char * search, size_t searchsize);
|
---|
[f450f2f] | 163 |
|
---|
[6264087] | 164 | bool startsWith(const string & s, const string & prefix);
|
---|
| 165 | bool startsWith(const string & s, const char * prefix);
|
---|
| 166 | bool startsWith(const string & s, const char * prefix, size_t prefixsize);
|
---|
[f450f2f] | 167 |
|
---|
[6264087] | 168 | bool endsWith(const string & s, const string & suffix);
|
---|
| 169 | bool endsWith(const string & s, const char * suffix);
|
---|
| 170 | bool endsWith(const string & s, const char * suffix, size_t suffixsize);
|
---|
[f450f2f] | 171 |
|
---|
| 172 | // Modifiers
|
---|
[6264087] | 173 | void padStart(string & s, size_t n);
|
---|
| 174 | void padStart(string & s, size_t n, char padding);
|
---|
| 175 | void padEnd(string & s, size_t n);
|
---|
| 176 | void padEnd(string & s, size_t n, char padding);
|
---|
[f450f2f] | 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | struct charclass {
|
---|
| 181 | charclass_res * inner;
|
---|
| 182 | };
|
---|
| 183 |
|
---|
| 184 | void ?{}( charclass & ) = void;
|
---|
| 185 | void ?{}( charclass &, charclass) = void;
|
---|
| 186 | charclass ?=?( charclass &, charclass) = void;
|
---|
| 187 |
|
---|
| 188 | void ?{}( charclass &, const string & chars);
|
---|
| 189 | void ?{}( charclass &, const char * chars );
|
---|
| 190 | void ?{}( charclass &, const char * chars, size_t charssize );
|
---|
| 191 | void ^?{}( charclass & );
|
---|
| 192 |
|
---|
[6264087] | 193 | int include(const string & s, const charclass & mask);
|
---|
[f450f2f] | 194 |
|
---|
[6264087] | 195 | int exclude(const string & s, const charclass & mask);
|
---|
[f450f2f] | 196 |
|
---|
| 197 | /*
|
---|
| 198 | What to do with?
|
---|
[6264087] | 199 | StrRet include(string & s, const charclass & mask);
|
---|
| 200 | StrRet exclude(string & s, const charclass & mask);
|
---|
[f450f2f] | 201 | */
|
---|