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 11:26:28 2023
|
---|
13 | // Update Count : 55
|
---|
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 |
|
---|
32 | // RAII, assignment
|
---|
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
|
---|
36 |
|
---|
37 | void ?{}(string & s, const string & s2);
|
---|
38 | void ?{}(string & s, string & s2);
|
---|
39 |
|
---|
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
|
---|
44 | //string ?=?( string &, string ) = void;
|
---|
45 | void ^?{}(string & s);
|
---|
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
|
---|
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 );
|
---|
59 |
|
---|
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 {
|
---|
75 | string & s;
|
---|
76 | inline _Istream_str_base;
|
---|
77 | }; // _Istream_Sstr
|
---|
78 |
|
---|
79 | static inline {
|
---|
80 | // read width does not include null terminator
|
---|
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}} };
|
---|
84 | }
|
---|
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;
|
---|
87 | }
|
---|
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; }
|
---|
94 | } // distribution
|
---|
95 | ifstream & ?|?( ifstream & is, _Istream_Sstr f );
|
---|
96 | void ?|?( ifstream & is, _Istream_Sstr t );
|
---|
97 |
|
---|
98 | // Concatenation
|
---|
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
|
---|
106 |
|
---|
107 | // Repetition
|
---|
108 | string ?*?(const string & s, size_t factor);
|
---|
109 | string ?*?(char c, size_t size);
|
---|
110 | string ?*?(const char *s, size_t size);
|
---|
111 |
|
---|
112 | // Character access
|
---|
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
|
---|
116 |
|
---|
117 | // Comparisons
|
---|
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 |
|
---|
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
|
---|
148 | bool contains(const string & s, char ch); // single character
|
---|
149 |
|
---|
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);
|
---|
154 |
|
---|
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);
|
---|
159 |
|
---|
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);
|
---|
163 |
|
---|
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);
|
---|
167 |
|
---|
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);
|
---|
171 |
|
---|
172 | // Modifiers
|
---|
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);
|
---|
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 |
|
---|
193 | int include(const string & s, const charclass & mask);
|
---|
194 |
|
---|
195 | int exclude(const string & s, const charclass & mask);
|
---|
196 |
|
---|
197 | /*
|
---|
198 | What to do with?
|
---|
199 | StrRet include(string & s, const charclass & mask);
|
---|
200 | StrRet exclude(string & s, const charclass & mask);
|
---|
201 | */
|
---|