[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 |
---|
[6264087] | 118 | bool ?==?(const string & s, const string & other); |
---|
| 119 | bool ?!=?(const string & s, const string & other); |
---|
| 120 | bool ?==?(const string & s, const char * other); |
---|
| 121 | bool ?!=?(const string & s, const char * other); |
---|
[f450f2f] | 122 | |
---|
| 123 | // Slicing |
---|
| 124 | string ?()( string & this, size_t start, size_t end ); // TODO const? |
---|
| 125 | string ?()( string & this, size_t start); |
---|
| 126 | |
---|
| 127 | // String search |
---|
[6264087] | 128 | bool contains(const string & s, char ch); // single character |
---|
[f450f2f] | 129 | |
---|
[6264087] | 130 | int find(const string & s, char search); |
---|
| 131 | int find(const string & s, const string & search); |
---|
| 132 | int find(const string & s, const char * search); |
---|
| 133 | int find(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 134 | |
---|
[6264087] | 135 | int findFrom(const string & s, size_t fromPos, char search); |
---|
| 136 | int findFrom(const string & s, size_t fromPos, const string & search); |
---|
| 137 | int findFrom(const string & s, size_t fromPos, const char * search); |
---|
| 138 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize); |
---|
[08ed947] | 139 | |
---|
[6264087] | 140 | bool includes(const string & s, const string & search); |
---|
| 141 | bool includes(const string & s, const char * search); |
---|
| 142 | bool includes(const string & s, const char * search, size_t searchsize); |
---|
[f450f2f] | 143 | |
---|
[6264087] | 144 | bool startsWith(const string & s, const string & prefix); |
---|
| 145 | bool startsWith(const string & s, const char * prefix); |
---|
| 146 | bool startsWith(const string & s, const char * prefix, size_t prefixsize); |
---|
[f450f2f] | 147 | |
---|
[6264087] | 148 | bool endsWith(const string & s, const string & suffix); |
---|
| 149 | bool endsWith(const string & s, const char * suffix); |
---|
| 150 | bool endsWith(const string & s, const char * suffix, size_t suffixsize); |
---|
[f450f2f] | 151 | |
---|
| 152 | // Modifiers |
---|
[6264087] | 153 | void padStart(string & s, size_t n); |
---|
| 154 | void padStart(string & s, size_t n, char padding); |
---|
| 155 | void padEnd(string & s, size_t n); |
---|
| 156 | void padEnd(string & s, size_t n, char padding); |
---|
[f450f2f] | 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | struct charclass { |
---|
| 161 | charclass_res * inner; |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | void ?{}( charclass & ) = void; |
---|
| 165 | void ?{}( charclass &, charclass) = void; |
---|
| 166 | charclass ?=?( charclass &, charclass) = void; |
---|
| 167 | |
---|
| 168 | void ?{}( charclass &, const string & chars); |
---|
| 169 | void ?{}( charclass &, const char * chars ); |
---|
| 170 | void ?{}( charclass &, const char * chars, size_t charssize ); |
---|
| 171 | void ^?{}( charclass & ); |
---|
| 172 | |
---|
[6264087] | 173 | int include(const string & s, const charclass & mask); |
---|
[f450f2f] | 174 | |
---|
[6264087] | 175 | int exclude(const string & s, const charclass & mask); |
---|
[f450f2f] | 176 | |
---|
| 177 | /* |
---|
| 178 | What to do with? |
---|
[6264087] | 179 | StrRet include(string & s, const charclass & mask); |
---|
| 180 | StrRet exclude(string & s, const charclass & mask); |
---|
[f450f2f] | 181 | */ |
---|