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