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_res -- variable-length, mutable run of text, with resource 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 Aug 12 15:45:47 2023
|
---|
13 | // Update Count : 2
|
---|
14 | //
|
---|
15 |
|
---|
16 | #pragma once
|
---|
17 |
|
---|
18 | #include <fstream.hfa>
|
---|
19 | #include <string.h> // e.g. strlen
|
---|
20 |
|
---|
21 |
|
---|
22 | //######################### HandleNode #########################
|
---|
23 | //private
|
---|
24 |
|
---|
25 | struct VbyteHeap;
|
---|
26 |
|
---|
27 | struct HandleNode {
|
---|
28 | HandleNode *flink; // forward link
|
---|
29 | HandleNode *blink; // backward link
|
---|
30 | VbyteHeap *ulink; // upward link
|
---|
31 |
|
---|
32 | char *s; // pointer to byte string
|
---|
33 | unsigned int lnth; // length of byte string
|
---|
34 | }; // HandleNode
|
---|
35 |
|
---|
36 | VbyteHeap * DEBUG_string_heap();
|
---|
37 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap );
|
---|
38 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap );
|
---|
39 | const char * DEBUG_string_heap_start( VbyteHeap * heap );
|
---|
40 |
|
---|
41 | void TUNING_set_string_heap_liveness_threshold( double val );
|
---|
42 |
|
---|
43 | //######################### String #########################
|
---|
44 |
|
---|
45 | // A dynamically-sized string
|
---|
46 | struct string_res {
|
---|
47 | HandleNode Handle; // chars, start, end, global neighbours
|
---|
48 | bool shareEditSet_owns_ulink;
|
---|
49 | string_res * shareEditSet_prev;
|
---|
50 | string_res * shareEditSet_next;
|
---|
51 | };
|
---|
52 |
|
---|
53 |
|
---|
54 | //######################### charclass_res #########################
|
---|
55 |
|
---|
56 | struct charclass_res {
|
---|
57 | string_res chars;
|
---|
58 | };
|
---|
59 |
|
---|
60 | void ?{}( charclass_res & ) = void;
|
---|
61 | void ?{}( charclass_res &, charclass_res) = void;
|
---|
62 | charclass_res ?=?( charclass_res &, charclass_res) = void;
|
---|
63 | void ?{}( charclass_res &, const string_res & chars);
|
---|
64 | void ?{}( charclass_res &, const char * chars );
|
---|
65 | void ?{}( charclass_res &, const char * chars, size_t charssize );
|
---|
66 | void ^?{}( charclass_res & );
|
---|
67 |
|
---|
68 |
|
---|
69 | //######################### String #########################
|
---|
70 |
|
---|
71 | // Getters
|
---|
72 | size_t size(const string_res &s);
|
---|
73 |
|
---|
74 | // Constructors, Assignment Operators, Destructor
|
---|
75 | void ?{}(string_res &s); // empty string
|
---|
76 | void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
|
---|
77 | static inline void ?{}(string_res &s, const char* rhs) { // copy from string literal (NULL-terminated)
|
---|
78 | (s){ rhs, strlen(rhs) };
|
---|
79 | }
|
---|
80 |
|
---|
81 | void ?{}(string_res &s, const string_res & s2) = void;
|
---|
82 | void ?{}(string_res &s, string_res & s2) = void;
|
---|
83 |
|
---|
84 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS };
|
---|
85 | void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end );
|
---|
86 | static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) {
|
---|
87 | ?{}( s, src, mode, 0, size(src));
|
---|
88 | }
|
---|
89 |
|
---|
90 | string_res & assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
|
---|
91 | static inline string_res & ?=?(string_res &s, const char* other) { // copy from string literal (NULL-terminated)
|
---|
92 | return assign(s, other, strlen(other));
|
---|
93 | }
|
---|
94 | string_res & ?=?(string_res &s, const string_res &other);
|
---|
95 | string_res & ?=?(string_res &s, string_res &other);
|
---|
96 | string_res & ?=?(string_res &s, char other);
|
---|
97 |
|
---|
98 | void ^?{}(string_res &s);
|
---|
99 |
|
---|
100 | // IO Operator
|
---|
101 | ofstream & ?|?(ofstream &out, const string_res &s);
|
---|
102 | void ?|?(ofstream &out, const string_res &s);
|
---|
103 | ifstream & ?|?(ifstream &in, string_res &s);
|
---|
104 | void ?|?( ifstream & in, string_res & this );
|
---|
105 |
|
---|
106 | struct _Istream_Rstr {
|
---|
107 | string_res * s;
|
---|
108 | inline _Istream_str_base;
|
---|
109 | }; // _Istream_Rstr
|
---|
110 |
|
---|
111 | static inline {
|
---|
112 | // read width does not include null terminator
|
---|
113 | _Istream_Rstr wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, rwd, {.flags.rwd : true}} }; }
|
---|
114 | _Istream_Rstr getline( string_res & s, const char delimiter = '\n' ) {
|
---|
115 | return (_Istream_Rstr)@{ &s, {{.delimiter : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
|
---|
116 | }
|
---|
117 | _Istream_Rstr & getline( _Istream_Rstr & fmt, const char delimiter = '\n' ) {
|
---|
118 | fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
|
---|
119 | }
|
---|
120 | _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : false}} }; }
|
---|
121 | _Istream_Rstr & incl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
|
---|
122 | _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : true}} }; }
|
---|
123 | _Istream_Rstr & excl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
|
---|
124 | _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, -1, {.flags.ignore : true}} }; }
|
---|
125 | _Istream_Rstr & ignore( _Istream_Rstr & fmt ) { fmt.flags.ignore = true; return fmt; }
|
---|
126 | } // distribution
|
---|
127 | ifstream & ?|?( ifstream & is, _Istream_Rstr f );
|
---|
128 | void ?|?( ifstream & is, _Istream_Rstr t );
|
---|
129 |
|
---|
130 | // Concatenation
|
---|
131 | void append(string_res &s, const char* buffer, size_t bsize);
|
---|
132 | void ?+=?(string_res &s, char other); // append a character
|
---|
133 | void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string
|
---|
134 | static inline void ?+=?(string_res &s, const char* other) {
|
---|
135 | append( s, other, strlen(other) );
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Character access
|
---|
139 | void assignAt(const string_res &s, size_t index, char val);
|
---|
140 | char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
|
---|
141 | //char codePointAt(const string_res &s, size_t index); // revisit under Unicode
|
---|
142 |
|
---|
143 | // Comparisons
|
---|
144 | int cmp (const string_res &, const string_res &);
|
---|
145 | bool ?==?(const string_res &, const string_res &);
|
---|
146 | bool ?!=?(const string_res &, const string_res &);
|
---|
147 | bool ?>? (const string_res &, const string_res &);
|
---|
148 | bool ?>=?(const string_res &, const string_res &);
|
---|
149 | bool ?<=?(const string_res &, const string_res &);
|
---|
150 | bool ?<? (const string_res &, const string_res &);
|
---|
151 |
|
---|
152 | int cmp (const string_res &, const char*);
|
---|
153 | bool ?==?(const string_res &, const char*);
|
---|
154 | bool ?!=?(const string_res &, const char*);
|
---|
155 | bool ?>? (const string_res &, const char*);
|
---|
156 | bool ?>=?(const string_res &, const char*);
|
---|
157 | bool ?<=?(const string_res &, const char*);
|
---|
158 | bool ?<? (const string_res &, const char*);
|
---|
159 |
|
---|
160 | int cmp (const char*, const string_res &);
|
---|
161 | bool ?==?(const char*, const string_res &);
|
---|
162 | bool ?!=?(const char*, const string_res &);
|
---|
163 | bool ?>? (const char*, const string_res &);
|
---|
164 | bool ?>=?(const char*, const string_res &);
|
---|
165 | bool ?<=?(const char*, const string_res &);
|
---|
166 | bool ?<? (const char*, const string_res &);
|
---|
167 |
|
---|
168 | // String search
|
---|
169 | bool contains(const string_res &s, char ch); // single character
|
---|
170 |
|
---|
171 | int find(const string_res &s, char search);
|
---|
172 | int find(const string_res &s, const string_res &search);
|
---|
173 | int find(const string_res &s, const char* search);
|
---|
174 | int find(const string_res &s, const char* search, size_t searchsize);
|
---|
175 |
|
---|
176 | int findFrom(const string_res &s, size_t fromPos, char search);
|
---|
177 | int findFrom(const string_res &s, size_t fromPos, const string_res &search);
|
---|
178 | int findFrom(const string_res &s, size_t fromPos, const char* search);
|
---|
179 | int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize);
|
---|
180 |
|
---|
181 | bool includes(const string_res &s, const string_res &search);
|
---|
182 | bool includes(const string_res &s, const char* search);
|
---|
183 | bool includes(const string_res &s, const char* search, size_t searchsize);
|
---|
184 |
|
---|
185 | bool startsWith(const string_res &s, const string_res &prefix);
|
---|
186 | bool startsWith(const string_res &s, const char* prefix);
|
---|
187 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize);
|
---|
188 |
|
---|
189 | bool endsWith(const string_res &s, const string_res &suffix);
|
---|
190 | bool endsWith(const string_res &s, const char* suffix);
|
---|
191 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize);
|
---|
192 |
|
---|
193 | int include(const string_res &s, const charclass_res &mask);
|
---|
194 | int exclude(const string_res &s, const charclass_res &mask);
|
---|
195 |
|
---|
196 | // Modifiers
|
---|
197 | void padStart(string_res &s, size_t n);
|
---|
198 | void padStart(string_res &s, size_t n, char padding);
|
---|
199 | void padEnd(string_res &s, size_t n);
|
---|
200 | void padEnd(string_res &s, size_t n, char padding);
|
---|
201 |
|
---|