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 : Mon Apr 15 21:56:27 2024
|
---|
13 | // Update Count : 85
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "string_res.hfa"
|
---|
17 | #include "string_sharectx.hfa"
|
---|
18 | #include "stdlib.hfa"
|
---|
19 | #include <ctype.h>
|
---|
20 |
|
---|
21 | // Workaround for observed performance penalty from calling CFA's alloc.
|
---|
22 | // Workaround is: EndVbyte = TEMP_ALLOC(char, CurrSize)
|
---|
23 | // Should be: EndVbyte = alloc(CurrSize)
|
---|
24 | #define TEMP_ALLOC(T, n) (( T * ) malloc( n * sizeof( T ) ))
|
---|
25 |
|
---|
26 | #include <assert.h>
|
---|
27 | #include <complex.h> // creal, cimag
|
---|
28 |
|
---|
29 | //######################### VbyteHeap "header" #########################
|
---|
30 |
|
---|
31 | #ifdef VbyteDebug
|
---|
32 | HandleNode *HeaderPtr;
|
---|
33 | #endif // VbyteDebug
|
---|
34 |
|
---|
35 | struct VbyteHeap {
|
---|
36 | int NoOfCompactions; // number of compactions of the byte area
|
---|
37 | int NoOfExtensions; // number of extensions in the size of the byte area
|
---|
38 | int NoOfReductions; // number of reductions in the size of the byte area
|
---|
39 |
|
---|
40 | int InitSize; // initial number of bytes in the byte-string area
|
---|
41 | int CurrSize; // current number of bytes in the byte-string area
|
---|
42 | char *StartVbyte; // pointer to the `st byte of the start of the byte-string area
|
---|
43 | char *EndVbyte; // pointer to the next byte after the end of the currently used portion of byte-string area
|
---|
44 | void *ExtVbyte; // pointer to the next byte after the end of the byte-string area
|
---|
45 |
|
---|
46 | HandleNode Header; // header node for handle list
|
---|
47 | }; // VbyteHeap
|
---|
48 |
|
---|
49 |
|
---|
50 | static void compaction( VbyteHeap & ); // compaction of the byte area
|
---|
51 | static void garbage( VbyteHeap &, int ); // garbage collect the byte area
|
---|
52 | static void extend( VbyteHeap &, int ); // extend the size of the byte area
|
---|
53 | static void reduce( VbyteHeap &, int ); // reduce the size of the byte area
|
---|
54 |
|
---|
55 | static void ?{}( VbyteHeap &, size_t = 1000 );
|
---|
56 | static void ^?{}( VbyteHeap & );
|
---|
57 |
|
---|
58 | static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes
|
---|
59 | static char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap
|
---|
60 | static char *VbyteTryAdjustLast( VbyteHeap &, int );
|
---|
61 |
|
---|
62 | static void AddThisAfter( HandleNode &, HandleNode & );
|
---|
63 | static void DeleteNode( HandleNode & );
|
---|
64 | static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle
|
---|
65 |
|
---|
66 |
|
---|
67 | // Allocate the storage for the variable sized area and intialize the heap variables.
|
---|
68 |
|
---|
69 | static void ?{}( VbyteHeap & s, size_t Size ) with(s) {
|
---|
70 | #ifdef VbyteDebug
|
---|
71 | serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;
|
---|
72 | #endif // VbyteDebug
|
---|
73 | NoOfCompactions = NoOfExtensions = NoOfReductions = 0;
|
---|
74 | InitSize = CurrSize = Size;
|
---|
75 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
|
---|
76 | ExtVbyte = (void *)( StartVbyte + CurrSize );
|
---|
77 | Header.flink = Header.blink = &Header;
|
---|
78 | Header.ulink = &s;
|
---|
79 | #ifdef VbyteDebug
|
---|
80 | HeaderPtr = &Header;
|
---|
81 | serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;
|
---|
82 | #endif // VbyteDebug
|
---|
83 | } // VbyteHeap
|
---|
84 |
|
---|
85 |
|
---|
86 | // Release the dynamically allocated storage for the byte area.
|
---|
87 |
|
---|
88 | static void ^?{}( VbyteHeap & s ) with(s) {
|
---|
89 | free( StartVbyte );
|
---|
90 | } // ~VbyteHeap
|
---|
91 |
|
---|
92 |
|
---|
93 | //######################### HandleNode #########################
|
---|
94 |
|
---|
95 |
|
---|
96 | // Create a handle node. The handle is not linked into the handle list. This is the responsibilitiy of the handle
|
---|
97 | // creator.
|
---|
98 |
|
---|
99 | static void ?{}( HandleNode & s ) with(s) {
|
---|
100 | #ifdef VbyteDebug
|
---|
101 | serr | "enter:HandleNode::HandleNode, s:" | &s;
|
---|
102 | #endif // VbyteDebug
|
---|
103 | s = 0;
|
---|
104 | lnth = 0;
|
---|
105 | #ifdef VbyteDebug
|
---|
106 | serr | "exit:HandleNode::HandleNode, s:" | &s;
|
---|
107 | #endif // VbyteDebug
|
---|
108 | } // HandleNode
|
---|
109 |
|
---|
110 | // Create a handle node. The handle is linked into the handle list at the end. This means that this handle will NOT be
|
---|
111 | // in order by string address, but this is not a problem because a string with length zero does nothing during garbage
|
---|
112 | // collection.
|
---|
113 |
|
---|
114 | static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) {
|
---|
115 | #ifdef VbyteDebug
|
---|
116 | serr | "enter:HandleNode::HandleNode, s:" | &s;
|
---|
117 | #endif // VbyteDebug
|
---|
118 | s = 0;
|
---|
119 | lnth = 0;
|
---|
120 | ulink = &vh;
|
---|
121 | AddThisAfter( s, *vh.Header.blink );
|
---|
122 | #ifdef VbyteDebug
|
---|
123 | serr | "exit:HandleNode::HandleNode, s:" | &s;
|
---|
124 | #endif // VbyteDebug
|
---|
125 | } // HandleNode
|
---|
126 |
|
---|
127 |
|
---|
128 | // Delete a node from the handle list by unchaining it from the list. If the handle node was allocated dynamically, it
|
---|
129 | // is the responsibility of the creator to destroy it.
|
---|
130 |
|
---|
131 | static void ^?{}( HandleNode & s ) with(s) {
|
---|
132 | #ifdef VbyteDebug
|
---|
133 | serr | "enter:HandleNode::~HandleNode, s:" | & s;
|
---|
134 | {
|
---|
135 | serr | nlOff;
|
---|
136 | serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";
|
---|
137 | for ( i; lnth ) {
|
---|
138 | serr | s[i];
|
---|
139 | } // for
|
---|
140 | serr | "\" flink:" | flink | " blink:" | blink | nl;
|
---|
141 | serr | nlOn;
|
---|
142 | }
|
---|
143 | #endif // VbyteDebug
|
---|
144 | DeleteNode( s );
|
---|
145 | } // ~HandleNode
|
---|
146 |
|
---|
147 |
|
---|
148 | //######################### String Sharing Context #########################
|
---|
149 |
|
---|
150 | static string_sharectx * ambient_string_sharectx; // fickle top of stack
|
---|
151 | static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack
|
---|
152 |
|
---|
153 | void ?{}( string_sharectx & s, StringSharectx_Mode mode ) with( s ) {
|
---|
154 | (older){ ambient_string_sharectx };
|
---|
155 | if ( mode == NEW_SHARING ) {
|
---|
156 | (activeHeap){ new( (size_t) 1000 ) };
|
---|
157 | } else {
|
---|
158 | verify( mode == NO_SHARING );
|
---|
159 | (activeHeap){ 0p };
|
---|
160 | }
|
---|
161 | ambient_string_sharectx = & s;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void ^?{}( string_sharectx & s ) with( s ) {
|
---|
165 | if ( activeHeap ) delete( activeHeap );
|
---|
166 |
|
---|
167 | // unlink s from older-list starting from ambient_string_sharectx
|
---|
168 | // usually, s==ambient_string_sharectx and the loop runs zero times
|
---|
169 | string_sharectx *& c = ambient_string_sharectx;
|
---|
170 | while ( c != &s ) &c = &c->older; // find s
|
---|
171 | c = s.older; // unlink
|
---|
172 | }
|
---|
173 |
|
---|
174 | //######################### String Resource #########################
|
---|
175 |
|
---|
176 |
|
---|
177 | VbyteHeap * DEBUG_string_heap() {
|
---|
178 | assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
|
---|
179 | return ambient_string_sharectx->activeHeap;
|
---|
180 | }
|
---|
181 |
|
---|
182 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) {
|
---|
183 | return ((char *)heap->ExtVbyte) - heap->EndVbyte;
|
---|
184 | }
|
---|
185 |
|
---|
186 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) {
|
---|
187 | return heap->CurrSize;
|
---|
188 | }
|
---|
189 |
|
---|
190 | const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
|
---|
191 | return heap->StartVbyte;
|
---|
192 | }
|
---|
193 |
|
---|
194 | // Returns the size of the string in bytes
|
---|
195 | size_t size(const string_res & s) with(s) {
|
---|
196 | return Handle.lnth;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Output operator
|
---|
200 | ofstream & ?|?(ofstream & out, const string_res & s) {
|
---|
201 | // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0.
|
---|
202 | return out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl;
|
---|
203 | }
|
---|
204 |
|
---|
205 | void ?|?(ofstream & out, const string_res & s) {
|
---|
206 | (ofstream &)(out | s); ends( out );
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Input operator
|
---|
210 | ifstream & ?|?(ifstream & in, string_res & s) {
|
---|
211 | // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing.
|
---|
212 | // If s is a substring of something larger, simple assignment takes care of that case correctly.
|
---|
213 | // But directly reading a variable amount of text into the middle of a larger context is not practical.
|
---|
214 | string_res temp;
|
---|
215 |
|
---|
216 | // Read in chunks. Often, one chunk is enough. Keep the string that accumulates chunks last in the heap,
|
---|
217 | // so available room is rest of heap. When a chunk fills the heap, force growth then take the next chunk.
|
---|
218 | for (bool cont = true; cont; ) {
|
---|
219 | cont = false;
|
---|
220 |
|
---|
221 | // Append dummy content to temp, forcing expansion when applicable (occurs always on subsequent loops)
|
---|
222 | // length 2 ensures room for at least one real char, plus scanf/pipe-cstr's null terminator
|
---|
223 | temp += "--";
|
---|
224 | assert( temp.Handle.ulink->EndVbyte == temp.Handle.s + temp.Handle.lnth ); // last in heap
|
---|
225 |
|
---|
226 | // reset, to overwrite the appended "--"
|
---|
227 | temp.Handle.lnth -= 2;
|
---|
228 | temp.Handle.ulink->EndVbyte -= 2;
|
---|
229 |
|
---|
230 | // rest of heap is available to read into
|
---|
231 | int lenReadable = (char *)temp.Handle.ulink->ExtVbyte - temp.Handle.ulink->EndVbyte;
|
---|
232 | assert (lenReadable >= 2);
|
---|
233 |
|
---|
234 | // get bytes
|
---|
235 | try {
|
---|
236 | *(temp.Handle.ulink->EndVbyte) = '\0'; // pre-assign empty cstring
|
---|
237 | in | wdi( lenReadable, temp.Handle.ulink->EndVbyte );
|
---|
238 | } catch (cstring_length *) {
|
---|
239 | cont = true;
|
---|
240 | }
|
---|
241 | int lenWasRead = strlen(temp.Handle.ulink->EndVbyte);
|
---|
242 |
|
---|
243 | // update metadata
|
---|
244 | temp.Handle.lnth += lenWasRead;
|
---|
245 | temp.Handle.ulink->EndVbyte += lenWasRead;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if ( temp.Handle.lnth > 0 ) s = temp;
|
---|
249 | return in;
|
---|
250 | }
|
---|
251 |
|
---|
252 | ifstream & ?|?( ifstream & is, _Istream_Rquoted f ) with( f.rstr ) {
|
---|
253 | if ( eof( is ) ) throwResume ExceptionInst( missing_data );
|
---|
254 | int args;
|
---|
255 | fini: {
|
---|
256 | char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' };
|
---|
257 | int len = -1; // may not be set in fmt
|
---|
258 | args = fmt( is, rfmt, &len ); // remove leading whitespace and quote
|
---|
259 | if ( eof( is ) || len == -1 ) break fini;
|
---|
260 |
|
---|
261 | // Change the remainder of the read into a getline by reseting the closing delimiter.
|
---|
262 | if ( delimiters[1] != '\0' ) {
|
---|
263 | delimiters[0] = delimiters[1];
|
---|
264 | delimiters[1] = '\0';
|
---|
265 | } // if
|
---|
266 | flags.delimiter = true;
|
---|
267 | return is | *(_Istream_Rstr *)&f;
|
---|
268 | } // fini
|
---|
269 | // read failed => no pattern match => set string to null
|
---|
270 | if ( ! flags.ignore && s != 0p && args == 0 ) s[0] = '\0';
|
---|
271 | if ( args == 1 && eof( is ) ) { // data but scan ended at EOF
|
---|
272 | clear( is ); // => reset EOF => detect again on next read
|
---|
273 | } // if
|
---|
274 | return is;
|
---|
275 | }
|
---|
276 |
|
---|
277 | ifstream & ?|?( ifstream & is, _Istream_Rstr f ) {
|
---|
278 | // .---------------,
|
---|
279 | // | | | | |...|0|0| null terminator and guard if missing
|
---|
280 | // `---------------'
|
---|
281 | enum { gwd = 128 + 1, wd = gwd - 1 }; // guard and unguard width
|
---|
282 | char cstr[gwd]; // read in chunks
|
---|
283 | bool cont = false;
|
---|
284 |
|
---|
285 | _Istream_Cwidth cf = { cstr, (_Istream_str_base)f };
|
---|
286 | if ( ! cf.flags.rwd ) cf.wd = wd;
|
---|
287 |
|
---|
288 | cstr[wd] = '\0'; // guard null terminate string
|
---|
289 | try {
|
---|
290 | cstr[0] = '\0'; // pre-assign as empty cstring
|
---|
291 | is | cf;
|
---|
292 | } catch( cstring_length * ) {
|
---|
293 | cont = true;
|
---|
294 | } finally {
|
---|
295 | if ( ! cf.flags.ignore // ok to initialize string
|
---|
296 | // && cstr[0] != '\0' // something was read
|
---|
297 | ) {
|
---|
298 | *(f.s) = cstr;
|
---|
299 | }
|
---|
300 | } // try
|
---|
301 | for ( ; cont; ) { // overflow read ?
|
---|
302 | cont = false;
|
---|
303 | try {
|
---|
304 | cstr[0] = '\0'; // pre-assign as empty cstring
|
---|
305 | is | cf;
|
---|
306 | } catch( cstring_length * ) {
|
---|
307 | cont = true; // continue not allowed
|
---|
308 | } finally {
|
---|
309 | if ( ! cf.flags.ignore && cstr[0] != '\0' ) { // something was read
|
---|
310 | *(f.s) += cstr; // build string chunk at a time
|
---|
311 | }
|
---|
312 | } // try
|
---|
313 | } // for
|
---|
314 | return is;
|
---|
315 | } // ?|?
|
---|
316 |
|
---|
317 | // Empty constructor
|
---|
318 | void ?{}(string_res & s) with(s) {
|
---|
319 | if( ambient_string_sharectx->activeHeap ) {
|
---|
320 | (Handle){ * ambient_string_sharectx->activeHeap };
|
---|
321 | (shareEditSet_owns_ulink){ false };
|
---|
322 | verify( Handle.s == 0p && Handle.lnth == 0 );
|
---|
323 | } else {
|
---|
324 | (Handle){ * new( (size_t) 10 ) }; // TODO: can I lazily avoid allocating for empty string
|
---|
325 | (shareEditSet_owns_ulink){ true };
|
---|
326 | Handle.s = Handle.ulink->StartVbyte;
|
---|
327 | verify( Handle.lnth == 0 );
|
---|
328 | }
|
---|
329 | s.shareEditSet_prev = &s;
|
---|
330 | s.shareEditSet_next = &s;
|
---|
331 | }
|
---|
332 |
|
---|
333 | static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) {
|
---|
334 | if( ambient_string_sharectx->activeHeap ) {
|
---|
335 | (Handle){ * ambient_string_sharectx->activeHeap };
|
---|
336 | (shareEditSet_owns_ulink){ false };
|
---|
337 | } else {
|
---|
338 | (Handle){ * new( rhslnth ) };
|
---|
339 | (shareEditSet_owns_ulink){ true };
|
---|
340 | }
|
---|
341 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
|
---|
342 | Handle.lnth = rhslnth;
|
---|
343 | memmove( Handle.s, rhs, rhslnth );
|
---|
344 | s.shareEditSet_prev = &s;
|
---|
345 | s.shareEditSet_next = &s;
|
---|
346 | }
|
---|
347 |
|
---|
348 | // Constructor from a raw buffer and size
|
---|
349 | void ?{}(string_res & s, const char * rhs, size_t rhslnth) with(s) {
|
---|
350 | eagerCopyCtorHelper(s, rhs, rhslnth);
|
---|
351 | }
|
---|
352 |
|
---|
353 | void ?{}( string_res & s, ssize_t rhs ) {
|
---|
354 | char buf[64];
|
---|
355 | int len;
|
---|
356 | snprintf( buf, sizeof(buf)-1, "%zd%n", rhs, &len );
|
---|
357 | ( s ){ buf, len };
|
---|
358 | }
|
---|
359 | void ?{}( string_res & s, size_t rhs ) {
|
---|
360 | char buf[64];
|
---|
361 | int len;
|
---|
362 | snprintf( buf, sizeof(buf)-1, "%zu%n", rhs, &len );
|
---|
363 | ( s ){ buf, len };
|
---|
364 | }
|
---|
365 | void ?{}( string_res & s, double rhs ) {
|
---|
366 | char buf[64];
|
---|
367 | int len;
|
---|
368 | snprintf( buf, sizeof(buf)-1, "%g%n", rhs, &len );
|
---|
369 | ( s ){ buf, len };
|
---|
370 | }
|
---|
371 | void ?{}( string_res & s, long double rhs ) {
|
---|
372 | char buf[64];
|
---|
373 | int len;
|
---|
374 | snprintf( buf, sizeof(buf)-1, "%Lg%n", rhs, &len );
|
---|
375 | ( s ){ buf, len };
|
---|
376 | }
|
---|
377 | void ?{}( string_res & s, double _Complex rhs ) {
|
---|
378 | char buf[64];
|
---|
379 | int len;
|
---|
380 | snprintf( buf, sizeof(buf)-1, "%g+%gi%n", creal( rhs ), cimag( rhs ), &len );
|
---|
381 | ( s ){ buf, len };
|
---|
382 | }
|
---|
383 | void ?{}( string_res & s, long double _Complex rhs ) {
|
---|
384 | char buf[64];
|
---|
385 | int len;
|
---|
386 | snprintf( buf, sizeof(buf)-1, "%Lg+%Lgi%n", creall( rhs ), cimagl( rhs ), &len );
|
---|
387 | ( s ){ buf, len };
|
---|
388 | }
|
---|
389 |
|
---|
390 | // private ctor (not in header): use specified heap (ignore ambient) and copy chars in
|
---|
391 | void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) {
|
---|
392 | (Handle){ heap };
|
---|
393 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
|
---|
394 | Handle.lnth = rhslnth;
|
---|
395 | (s.shareEditSet_owns_ulink){ false };
|
---|
396 | memmove( Handle.s, rhs, rhslnth );
|
---|
397 | s.shareEditSet_prev = &s;
|
---|
398 | s.shareEditSet_next = &s;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | // General copy constructor
|
---|
403 | void ?{}(string_res & s, const string_res & s2, StrResInitMode mode, size_t start, size_t len ) {
|
---|
404 | size_t end = start + len;
|
---|
405 | verify( start <= end && end <= s2.Handle.lnth );
|
---|
406 |
|
---|
407 | if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) {
|
---|
408 | // crossing heaps (including private): copy eagerly
|
---|
409 | eagerCopyCtorHelper(s, s2.Handle.s + start, end - start);
|
---|
410 | verify(s.shareEditSet_prev == &s);
|
---|
411 | verify(s.shareEditSet_next == &s);
|
---|
412 | } else {
|
---|
413 | (s.Handle){};
|
---|
414 | s.Handle.s = s2.Handle.s + start;
|
---|
415 | s.Handle.lnth = end - start;
|
---|
416 | s.Handle.ulink = s2.Handle.ulink;
|
---|
417 |
|
---|
418 | AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle
|
---|
419 | // ^ bug? skip others at early point in string
|
---|
420 |
|
---|
421 | if (mode == COPY_VALUE) {
|
---|
422 | verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap);
|
---|
423 | // requested logical copy in same heap: defer copy until write
|
---|
424 |
|
---|
425 | (s.shareEditSet_owns_ulink){ false };
|
---|
426 |
|
---|
427 | // make s alone in its shareEditSet
|
---|
428 | s.shareEditSet_prev = &s;
|
---|
429 | s.shareEditSet_next = &s;
|
---|
430 | } else {
|
---|
431 | verify( mode == SHARE_EDITS );
|
---|
432 | // sharing edits with source forces same heap as source (ignore context)
|
---|
433 |
|
---|
434 | (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink };
|
---|
435 |
|
---|
436 | // s2 is logically const but not implementation const
|
---|
437 | string_res & s2mod = (string_res &) s2;
|
---|
438 |
|
---|
439 | // insert s after s2 on shareEditSet
|
---|
440 | s.shareEditSet_next = s2mod.shareEditSet_next;
|
---|
441 | s.shareEditSet_prev = &s2mod;
|
---|
442 | s.shareEditSet_next->shareEditSet_prev = &s;
|
---|
443 | s.shareEditSet_prev->shareEditSet_next = &s;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | static void assignEditSet(string_res & s, string_res * shareEditSetStartPeer, string_res * shareEditSetEndPeer,
|
---|
449 | char * resultSesStart,
|
---|
450 | size_t resultSesLnth,
|
---|
451 | HandleNode * resultPadPosition, size_t bsize ) {
|
---|
452 |
|
---|
453 | char * beforeBegin = shareEditSetStartPeer->Handle.s;
|
---|
454 | size_t beforeLen = s.Handle.s - beforeBegin;
|
---|
455 |
|
---|
456 | char * afterBegin = s.Handle.s + s.Handle.lnth;
|
---|
457 | size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin;
|
---|
458 |
|
---|
459 | size_t oldLnth = s.Handle.lnth;
|
---|
460 |
|
---|
461 | s.Handle.s = resultSesStart + beforeLen;
|
---|
462 | s.Handle.lnth = bsize;
|
---|
463 | if (resultPadPosition)
|
---|
464 | MoveThisAfter( s.Handle, *resultPadPosition );
|
---|
465 |
|
---|
466 | // adjust all substring string and handle locations, and check if any substring strings are outside the new base string
|
---|
467 | char *limit = resultSesStart + resultSesLnth;
|
---|
468 | for ( string_res * p = s.shareEditSet_next; p != &s; p = p->shareEditSet_next ) {
|
---|
469 | verify (p->Handle.s >= beforeBegin);
|
---|
470 | if ( p->Handle.s >= afterBegin ) {
|
---|
471 | verify ( p->Handle.s <= afterBegin + afterLen );
|
---|
472 | verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
|
---|
473 | // p starts after the edit
|
---|
474 | // take start and end as end-anchored
|
---|
475 | size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s;
|
---|
476 | p->Handle.s = limit - startOffsetFromEnd;
|
---|
477 | // p->Handle.lnth unaffected
|
---|
478 | } else if ( p->Handle.s <= beforeBegin + beforeLen ) {
|
---|
479 | // p starts before, or at the start of, the edit
|
---|
480 | if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) {
|
---|
481 | // p ends before the edit
|
---|
482 | // take end as start-anchored too
|
---|
483 | // p->Handle.lnth unaffected
|
---|
484 | } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
|
---|
485 | // p ends during the edit; p does not include the last character replaced
|
---|
486 | // clip end of p to end at start of edit
|
---|
487 | p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin );
|
---|
488 | } else {
|
---|
489 | // p ends after the edit
|
---|
490 | verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
|
---|
491 | // take end as end-anchored
|
---|
492 | // stretch-shrink p according to the edit
|
---|
493 | p->Handle.lnth += s.Handle.lnth;
|
---|
494 | p->Handle.lnth -= oldLnth;
|
---|
495 | }
|
---|
496 | // take start as start-anchored
|
---|
497 | size_t startOffsetFromStart = p->Handle.s - beforeBegin;
|
---|
498 | p->Handle.s = resultSesStart + startOffsetFromStart;
|
---|
499 | } else {
|
---|
500 | verify ( p->Handle.s < afterBegin );
|
---|
501 | // p starts during the edit
|
---|
502 | verify( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen );
|
---|
503 | if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
|
---|
504 | // p ends during the edit; p does not include the last character replaced
|
---|
505 | // set p to empty string at start of edit
|
---|
506 | p->Handle.s = s.Handle.s;
|
---|
507 | p->Handle.lnth = 0;
|
---|
508 | } else {
|
---|
509 | // p includes the end of the edit
|
---|
510 | // clip start of p to start at end of edit
|
---|
511 | int charsToClip = afterBegin - p->Handle.s;
|
---|
512 | p->Handle.s = s.Handle.s + s.Handle.lnth;
|
---|
513 | p->Handle.lnth -= charsToClip;
|
---|
514 | }
|
---|
515 | }
|
---|
516 | if (resultPadPosition)
|
---|
517 | MoveThisAfter( p->Handle, *resultPadPosition ); // move substring handle to maintain sorted order by string position
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | // traverse the share-edit set (SES) to recover the range of a base string to which `s` belongs
|
---|
522 | static void locateInShareEditSet( string_res & s, string_res *& shareEditSetStartPeer, string_res *& shareEditSetEndPeer ) {
|
---|
523 | shareEditSetStartPeer = & s;
|
---|
524 | shareEditSetEndPeer = & s;
|
---|
525 | for (string_res * editPeer = s.shareEditSet_next; editPeer != &s; editPeer = editPeer->shareEditSet_next) {
|
---|
526 | if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) {
|
---|
527 | shareEditSetStartPeer = editPeer;
|
---|
528 | }
|
---|
529 | if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) {
|
---|
530 | shareEditSetEndPeer = editPeer;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) {
|
---|
536 | string_res * shareEditSetStartPeer;
|
---|
537 | string_res * shareEditSetEndPeer;
|
---|
538 | locateInShareEditSet( s, shareEditSetStartPeer, shareEditSetEndPeer );
|
---|
539 |
|
---|
540 | verify( shareEditSetEndPeer->Handle.s >= shareEditSetStartPeer->Handle.s );
|
---|
541 | size_t origEditSetLength = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - shareEditSetStartPeer->Handle.s;
|
---|
542 | verify( origEditSetLength >= s.Handle.lnth );
|
---|
543 |
|
---|
544 | if ( s.shareEditSet_owns_ulink ) { // assigning to private context
|
---|
545 | // ok to overwrite old value within LHS
|
---|
546 | char * prefixStartOrig = shareEditSetStartPeer->Handle.s;
|
---|
547 | int prefixLen = s.Handle.s - prefixStartOrig;
|
---|
548 | char * suffixStartOrig = s.Handle.s + s.Handle.lnth;
|
---|
549 | int suffixLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - suffixStartOrig;
|
---|
550 |
|
---|
551 | int delta = bsize - s.Handle.lnth;
|
---|
552 | if ( char * oldBytes = VbyteTryAdjustLast( *s.Handle.ulink, delta ) ) {
|
---|
553 | // growing: copy from old to new
|
---|
554 | char * dest = VbyteAlloc( *s.Handle.ulink, origEditSetLength + delta );
|
---|
555 | char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen);
|
---|
556 | destCursor += prefixLen; memcpy(destCursor, buffer , bsize );
|
---|
557 | destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen);
|
---|
558 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer,
|
---|
559 | dest,
|
---|
560 | origEditSetLength + delta,
|
---|
561 | 0p, bsize);
|
---|
562 | free( oldBytes );
|
---|
563 | } else {
|
---|
564 | // room is already allocated in-place: bubble suffix and overwite middle
|
---|
565 | memmove( suffixStartOrig + delta, suffixStartOrig, suffixLen );
|
---|
566 | memcpy( s.Handle.s, buffer, bsize );
|
---|
567 |
|
---|
568 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer,
|
---|
569 | shareEditSetStartPeer->Handle.s,
|
---|
570 | origEditSetLength + delta,
|
---|
571 | 0p, bsize);
|
---|
572 | }
|
---|
573 |
|
---|
574 | } else if ( // assigning to shared context
|
---|
575 | s.Handle.lnth == origEditSetLength && // overwriting entire run of SES
|
---|
576 | & valSrc && // sourcing from a managed string
|
---|
577 | valSrc.Handle.ulink == s.Handle.ulink ) { // sourcing from same heap
|
---|
578 |
|
---|
579 | // SES's result will only use characters from the source string => reuse source
|
---|
580 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer,
|
---|
581 | valSrc.Handle.s,
|
---|
582 | valSrc.Handle.lnth,
|
---|
583 | &((string_res&)valSrc).Handle, bsize);
|
---|
584 |
|
---|
585 | } else {
|
---|
586 | // overwriting a proper substring of some string: mash characters from old and new together (copy on write)
|
---|
587 | // OR we are importing characters: need to copy eagerly (can't refer to source)
|
---|
588 |
|
---|
589 | // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer
|
---|
590 | // `s` occurs in the middle of it, to be replaced
|
---|
591 | // build up the new text in `pasting`
|
---|
592 |
|
---|
593 | string_res pasting = {
|
---|
594 | * s.Handle.ulink, // maintain same heap, regardless of context
|
---|
595 | shareEditSetStartPeer->Handle.s, // start of SES
|
---|
596 | s.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before s
|
---|
597 | append( pasting,
|
---|
598 | buffer, // start of replacement for s
|
---|
599 | bsize ); // length of replacement for s
|
---|
600 | append( pasting,
|
---|
601 | s.Handle.s + s.Handle.lnth, // start of SES after s
|
---|
602 | shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth -
|
---|
603 | (s.Handle.s + s.Handle.lnth) ); // length of SES, after s
|
---|
604 |
|
---|
605 | // The above string building can trigger compaction.
|
---|
606 | // The reference points (that are arguments of the string building) may move during that building.
|
---|
607 | // From s point on, they are stable.
|
---|
608 |
|
---|
609 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer,
|
---|
610 | pasting.Handle.s,
|
---|
611 | pasting.Handle.lnth,
|
---|
612 | &pasting.Handle, bsize);
|
---|
613 | }
|
---|
614 |
|
---|
615 | return s;
|
---|
616 | }
|
---|
617 |
|
---|
618 | string_res & assign(string_res & s, const string_res & src, size_t maxlen) {
|
---|
619 | return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p);
|
---|
620 | }
|
---|
621 |
|
---|
622 | string_res & assign(string_res & s, const char * buffer, size_t bsize) {
|
---|
623 | return assign_(s, buffer, bsize, *0p);
|
---|
624 | }
|
---|
625 |
|
---|
626 | string_res & ?=?(string_res & s, char c) {
|
---|
627 | return assign(s, &c, 1);
|
---|
628 | }
|
---|
629 |
|
---|
630 | string_res & ?=?( string_res & s, ssize_t rhs ) {
|
---|
631 | string_res rhs2 = rhs;
|
---|
632 | s = rhs2;
|
---|
633 | return s;
|
---|
634 | }
|
---|
635 | string_res & ?=?( string_res & s, size_t rhs ) {
|
---|
636 | string_res rhs2 = rhs;
|
---|
637 | s = rhs2;
|
---|
638 | return s;
|
---|
639 | }
|
---|
640 | string_res & ?=?( string_res & s, double rhs ) {
|
---|
641 | string_res rhs2 = rhs;
|
---|
642 | s = rhs2;
|
---|
643 | return s;
|
---|
644 | }
|
---|
645 | string_res & ?=?( string_res & s, long double rhs ) {
|
---|
646 | string_res rhs2 = rhs;
|
---|
647 | s = rhs2;
|
---|
648 | return s;
|
---|
649 | }
|
---|
650 | string_res & ?=?( string_res & s, double _Complex rhs ) {
|
---|
651 | string_res rhs2 = rhs;
|
---|
652 | s = rhs2;
|
---|
653 | return s;
|
---|
654 | }
|
---|
655 | string_res & ?=?( string_res & s, long double _Complex rhs ) {
|
---|
656 | string_res rhs2 = rhs;
|
---|
657 | s = rhs2;
|
---|
658 | return s;
|
---|
659 | }
|
---|
660 |
|
---|
661 | // Copy assignment operator
|
---|
662 | string_res & ?=?(string_res & s, const string_res & rhs) with( s ) {
|
---|
663 | return assign_(s, rhs.Handle.s, rhs.Handle.lnth, rhs);
|
---|
664 | }
|
---|
665 |
|
---|
666 | string_res & ?=?(string_res & s, string_res & rhs) with( s ) {
|
---|
667 | const string_res & rhs2 = rhs;
|
---|
668 | return s = rhs2;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | // Destructor
|
---|
673 | void ^?{}(string_res & s) with(s) {
|
---|
674 | // much delegated to implied ^VbyteSM
|
---|
675 |
|
---|
676 | // sever s from its share-edit peers, if any (four no-ops when already solo)
|
---|
677 | s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next;
|
---|
678 | s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev;
|
---|
679 | // s.shareEditSet_next = &s;
|
---|
680 | // s.shareEditSet_prev = &s;
|
---|
681 |
|
---|
682 | if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out
|
---|
683 | delete( s.Handle.ulink );
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | // Returns the character at the given index
|
---|
689 | // With unicode support, this may be different from just the byte at the given
|
---|
690 | // offset from the start of the string.
|
---|
691 | char ?[?](const string_res & s, size_t index) with(s) {
|
---|
692 | //TODO: Check if index is valid (no exceptions yet)
|
---|
693 | return Handle.s[index];
|
---|
694 | }
|
---|
695 |
|
---|
696 | void assignAt(const string_res & s, size_t index, char val) {
|
---|
697 | // caution: not tested (not reachable by string-api-coverage interface)
|
---|
698 | // equivalent form at string level is `s[index] = val`,
|
---|
699 | // which uses the overload that returns a length-1 string
|
---|
700 | string_res editZone = { s, SHARE_EDITS, index, 1 };
|
---|
701 | assign(editZone, &val, 1);
|
---|
702 | }
|
---|
703 |
|
---|
704 |
|
---|
705 | ///////////////////////////////////////////////////////////////////
|
---|
706 | // Concatenation
|
---|
707 |
|
---|
708 | void append(string_res & str1, const char * buffer, size_t bsize) {
|
---|
709 | size_t clnth = str1.Handle.lnth + bsize;
|
---|
710 | if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?
|
---|
711 | // no-op
|
---|
712 | } else { // must copy some text
|
---|
713 | if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
|
---|
714 | VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area
|
---|
715 | } else { // copy the two parts
|
---|
716 | char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth );
|
---|
717 | char * str1oldBuf = str1.Handle.s; // must read after VbyteAlloc call in case it gs's
|
---|
718 | str1.Handle.s = str1newBuf;
|
---|
719 | memcpy( str1.Handle.s, str1oldBuf, str1.Handle.lnth );
|
---|
720 | } // if
|
---|
721 | memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize );
|
---|
722 | } // if
|
---|
723 | str1.Handle.lnth = clnth;
|
---|
724 | }
|
---|
725 |
|
---|
726 | void ?+=?(string_res & str1, const string_res & str2) {
|
---|
727 | append( str1, str2.Handle.s, str2.Handle.lnth );
|
---|
728 | }
|
---|
729 |
|
---|
730 | void append(string_res & str1, const string_res & str2, size_t maxlen) {
|
---|
731 | append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) );
|
---|
732 | }
|
---|
733 |
|
---|
734 | void ?+=?(string_res & s, char c) {
|
---|
735 | append( s, & c, 1 );
|
---|
736 | }
|
---|
737 | void ?+=?(string_res & s, const char * c) {
|
---|
738 | append( s, c, strlen(c) );
|
---|
739 | }
|
---|
740 |
|
---|
741 | ///////////////////////////////////////////////////////////////////
|
---|
742 | // Repetition
|
---|
743 |
|
---|
744 | void ?*=?(string_res & s, size_t factor) {
|
---|
745 | string_res s2 = { s, COPY_VALUE };
|
---|
746 | s = "";
|
---|
747 | for (factor) s += s2;
|
---|
748 | }
|
---|
749 |
|
---|
750 | //////////////////////////////////////////////////////////
|
---|
751 | // Comparisons
|
---|
752 |
|
---|
753 | int strcmp(const string_res & s1, const string_res & s2) {
|
---|
754 | // return 0;
|
---|
755 | int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth));
|
---|
756 | if (ans1 != 0) return ans1;
|
---|
757 | return s1.Handle.lnth - s2.Handle.lnth;
|
---|
758 | }
|
---|
759 |
|
---|
760 | bool ?==?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) == 0; }
|
---|
761 | bool ?!=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) != 0; }
|
---|
762 | bool ?>? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) > 0; }
|
---|
763 | bool ?>=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) >= 0; }
|
---|
764 | bool ?<=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) <= 0; }
|
---|
765 | bool ?<? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) < 0; }
|
---|
766 |
|
---|
767 | int strcmp (const string_res & s1, const char * s2) {
|
---|
768 | string_res s2x = s2;
|
---|
769 | return strcmp(s1, s2x);
|
---|
770 | }
|
---|
771 |
|
---|
772 | bool ?==?(const string_res & s1, const char * s2) { return strcmp(s1, s2) == 0; }
|
---|
773 | bool ?!=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) != 0; }
|
---|
774 | bool ?>? (const string_res & s1, const char * s2) { return strcmp(s1, s2) > 0; }
|
---|
775 | bool ?>=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) >= 0; }
|
---|
776 | bool ?<=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) <= 0; }
|
---|
777 | bool ?<? (const string_res & s1, const char * s2) { return strcmp(s1, s2) < 0; }
|
---|
778 |
|
---|
779 | int strcmp (const char * s1, const string_res & s2) {
|
---|
780 | string_res s1x = s1;
|
---|
781 | return strcmp(s1x, s2);
|
---|
782 | }
|
---|
783 |
|
---|
784 | bool ?==?(const char * s1, const string_res & s2) { return strcmp(s1, s2) == 0; }
|
---|
785 | bool ?!=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) != 0; }
|
---|
786 | bool ?>? (const char * s1, const string_res & s2) { return strcmp(s1, s2) > 0; }
|
---|
787 | bool ?>=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) >= 0; }
|
---|
788 | bool ?<=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) <= 0; }
|
---|
789 | bool ?<? (const char * s1, const string_res & s2) { return strcmp(s1, s2) < 0; }
|
---|
790 |
|
---|
791 |
|
---|
792 | //////////////////////////////////////////////////////////
|
---|
793 | // Search
|
---|
794 |
|
---|
795 | bool contains(const string_res & s, char ch) {
|
---|
796 | for ( i; size(s) ) {
|
---|
797 | if (s[i] == ch) return true;
|
---|
798 | }
|
---|
799 | return false;
|
---|
800 | }
|
---|
801 |
|
---|
802 | int find(const string_res & s, char search) {
|
---|
803 | return findFrom(s, 0, search);
|
---|
804 | }
|
---|
805 |
|
---|
806 | int findFrom(const string_res & s, size_t fromPos, char search) {
|
---|
807 | // FIXME: This paricular overload (find of single char) is optimized to use memchr.
|
---|
808 | // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match.
|
---|
809 | char * searchFrom = s.Handle.s + fromPos;
|
---|
810 | size_t searchLnth = s.Handle.lnth - fromPos;
|
---|
811 | int searchVal = search;
|
---|
812 | char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth);
|
---|
813 | if (foundAt == 0p) return s.Handle.lnth;
|
---|
814 | else return foundAt - s.Handle.s;
|
---|
815 | }
|
---|
816 |
|
---|
817 | int find(const string_res & s, const string_res & search) {
|
---|
818 | return findFrom(s, 0, search);
|
---|
819 | }
|
---|
820 |
|
---|
821 | int findFrom(const string_res & s, size_t fromPos, const string_res & search) {
|
---|
822 | return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth);
|
---|
823 | }
|
---|
824 |
|
---|
825 | int find(const string_res & s, const char * search) {
|
---|
826 | return findFrom(s, 0, search);
|
---|
827 | }
|
---|
828 | int findFrom(const string_res & s, size_t fromPos, const char * search) {
|
---|
829 | return findFrom(s, fromPos, search, strlen(search));
|
---|
830 | }
|
---|
831 |
|
---|
832 | int find(const string_res & s, const char * search, size_t searchsize) {
|
---|
833 | return findFrom(s, 0, search, searchsize);
|
---|
834 | }
|
---|
835 |
|
---|
836 | int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize) {
|
---|
837 | /* Remaining implementations essentially ported from Sunjay's work */
|
---|
838 |
|
---|
839 | // FIXME: This is a naive algorithm. We probably want to switch to someting
|
---|
840 | // like Boyer-Moore in the future.
|
---|
841 | // https://en.wikipedia.org/wiki/String_searching_algorithm
|
---|
842 |
|
---|
843 | // Always find the empty string
|
---|
844 | if (searchsize == 0) {
|
---|
845 | return 0;
|
---|
846 | }
|
---|
847 |
|
---|
848 | for ( i; fromPos ~ s.Handle.lnth ) {
|
---|
849 | size_t remaining = s.Handle.lnth - i;
|
---|
850 | // Never going to find the search string if the remaining string is
|
---|
851 | // smaller than search
|
---|
852 | if (remaining < searchsize) {
|
---|
853 | break;
|
---|
854 | }
|
---|
855 |
|
---|
856 | bool matched = true;
|
---|
857 | for ( j; searchsize ) {
|
---|
858 | if (search[j] != s.Handle.s[i + j]) {
|
---|
859 | matched = false;
|
---|
860 | break;
|
---|
861 | }
|
---|
862 | }
|
---|
863 | if (matched) {
|
---|
864 | return i;
|
---|
865 | }
|
---|
866 | }
|
---|
867 | return s.Handle.lnth;
|
---|
868 | }
|
---|
869 |
|
---|
870 | bool includes(const string_res & s, const string_res & search) {
|
---|
871 | return includes(s, search.Handle.s, search.Handle.lnth);
|
---|
872 | }
|
---|
873 |
|
---|
874 | bool includes(const string_res & s, const char * search) {
|
---|
875 | return includes(s, search, strlen(search));
|
---|
876 | }
|
---|
877 |
|
---|
878 | bool includes(const string_res & s, const char * search, size_t searchsize) {
|
---|
879 | return find(s, search, searchsize) < s.Handle.lnth;
|
---|
880 | }
|
---|
881 |
|
---|
882 | bool startsWith(const string_res & s, const string_res & prefix) {
|
---|
883 | return startsWith(s, prefix.Handle.s, prefix.Handle.lnth);
|
---|
884 | }
|
---|
885 |
|
---|
886 | bool startsWith(const string_res & s, const char * prefix) {
|
---|
887 | return startsWith(s, prefix, strlen(prefix));
|
---|
888 | }
|
---|
889 |
|
---|
890 | bool startsWith(const string_res & s, const char * prefix, size_t prefixsize) {
|
---|
891 | if (s.Handle.lnth < prefixsize) {
|
---|
892 | return false;
|
---|
893 | }
|
---|
894 | return memcmp(s.Handle.s, prefix, prefixsize) == 0;
|
---|
895 | }
|
---|
896 |
|
---|
897 | bool endsWith(const string_res & s, const string_res & suffix) {
|
---|
898 | return endsWith(s, suffix.Handle.s, suffix.Handle.lnth);
|
---|
899 | }
|
---|
900 |
|
---|
901 | bool endsWith(const string_res & s, const char * suffix) {
|
---|
902 | return endsWith(s, suffix, strlen(suffix));
|
---|
903 | }
|
---|
904 |
|
---|
905 | bool endsWith(const string_res & s, const char * suffix, size_t suffixsize) {
|
---|
906 | if (s.Handle.lnth < suffixsize) {
|
---|
907 | return false;
|
---|
908 | }
|
---|
909 | // Amount to offset the bytes pointer so that we are comparing the end of s
|
---|
910 | // to suffix. s.bytes + offset should be the first byte to compare against suffix
|
---|
911 | size_t offset = s.Handle.lnth - suffixsize;
|
---|
912 | return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /* Back to Mike's work */
|
---|
916 |
|
---|
917 | ///////////////////////////////////////////////////////////////////////////
|
---|
918 | // charclass, include, exclude
|
---|
919 |
|
---|
920 | void ?{}( charclass_res & s, const string_res & chars) {
|
---|
921 | (s){ chars.Handle.s, chars.Handle.lnth };
|
---|
922 | }
|
---|
923 |
|
---|
924 | void ?{}( charclass_res & s, const char * chars ) {
|
---|
925 | (s){ chars, strlen(chars) };
|
---|
926 | }
|
---|
927 |
|
---|
928 | void ?{}( charclass_res & s, const char * chars, size_t charssize ) {
|
---|
929 | (s.chars){ chars, charssize };
|
---|
930 | // now sort it ?
|
---|
931 | }
|
---|
932 |
|
---|
933 | void ^?{}( charclass_res & s ) {
|
---|
934 | ^(s.chars){};
|
---|
935 | }
|
---|
936 |
|
---|
937 | static bool test( const charclass_res & mask, char c ) {
|
---|
938 | // instead, use sorted char list?
|
---|
939 | return contains( mask.chars, c );
|
---|
940 | }
|
---|
941 |
|
---|
942 | int exclude(const string_res & s, const charclass_res & mask) {
|
---|
943 | for ( i; size(s) ) {
|
---|
944 | if ( test(mask, s[i]) ) return i;
|
---|
945 | }
|
---|
946 | return size(s);
|
---|
947 | }
|
---|
948 |
|
---|
949 | int include(const string_res & s, const charclass_res & mask) {
|
---|
950 | for ( i; size(s) ) {
|
---|
951 | if ( ! test(mask, s[i]) ) return i;
|
---|
952 | }
|
---|
953 | return size(s);
|
---|
954 | }
|
---|
955 |
|
---|
956 | //######################### VbyteHeap "implementation" #########################
|
---|
957 |
|
---|
958 |
|
---|
959 | // Add a new HandleNode node n after the current HandleNode node.
|
---|
960 |
|
---|
961 | static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) {
|
---|
962 | #ifdef VbyteDebug
|
---|
963 | serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;
|
---|
964 | #endif // VbyteDebug
|
---|
965 | // Performance note: we are on the critical path here. MB has ensured that the verifies don't contribute to runtime (are compiled away, like they're supposed to be).
|
---|
966 | verify( n.ulink != 0p );
|
---|
967 | verify( s.ulink == n.ulink );
|
---|
968 | flink = n.flink;
|
---|
969 | blink = &n;
|
---|
970 | n.flink->blink = &s;
|
---|
971 | n.flink = &s;
|
---|
972 | #ifdef VbyteDebug
|
---|
973 | {
|
---|
974 | serr | "HandleList:";
|
---|
975 | serr | nlOff;
|
---|
976 | for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) {
|
---|
977 | serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\"";
|
---|
978 | for ( i; ni->lnth ) {
|
---|
979 | serr | ni->s[i];
|
---|
980 | } // for
|
---|
981 | serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl;
|
---|
982 | } // for
|
---|
983 | serr | nlOn;
|
---|
984 | }
|
---|
985 | serr | "exit:AddThisAfter";
|
---|
986 | #endif // VbyteDebug
|
---|
987 | } // AddThisAfter
|
---|
988 |
|
---|
989 |
|
---|
990 | // Delete the current HandleNode node.
|
---|
991 |
|
---|
992 | static void DeleteNode( HandleNode & s ) with(s) {
|
---|
993 | #ifdef VbyteDebug
|
---|
994 | serr | "enter:DeleteNode, s:" | &s;
|
---|
995 | #endif // VbyteDebug
|
---|
996 | flink->blink = blink;
|
---|
997 | blink->flink = flink;
|
---|
998 | #ifdef VbyteDebug
|
---|
999 | serr | "exit:DeleteNode";
|
---|
1000 | #endif // VbyteDebug
|
---|
1001 | } // DeleteNode
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | // Allocates specified storage for a string from byte-string area. If not enough space remains to perform the
|
---|
1005 | // allocation, the garbage collection routine is called.
|
---|
1006 |
|
---|
1007 | static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) {
|
---|
1008 | #ifdef VbyteDebug
|
---|
1009 | serr | "enter:VbyteAlloc, size:" | size;
|
---|
1010 | #endif // VbyteDebug
|
---|
1011 | uintptr_t NoBytes;
|
---|
1012 | char *r;
|
---|
1013 |
|
---|
1014 | NoBytes = ( uintptr_t )EndVbyte + size;
|
---|
1015 | if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ?
|
---|
1016 | garbage( s, size ); // firer up the garbage collector
|
---|
1017 | verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte && "garbage run did not free up required space" );
|
---|
1018 | } // if
|
---|
1019 | r = EndVbyte;
|
---|
1020 | EndVbyte += size;
|
---|
1021 | #ifdef VbyteDebug
|
---|
1022 | serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
|
---|
1023 | #endif // VbyteDebug
|
---|
1024 | return r;
|
---|
1025 | } // VbyteAlloc
|
---|
1026 |
|
---|
1027 |
|
---|
1028 | // Adjusts the last allocation in this heap by delta bytes, or resets this heap to be able to offer
|
---|
1029 | // new allocations of its original size + delta bytes. Positive delta means bigger;
|
---|
1030 | // negative means smaller. A null return indicates that the original heap location has room for
|
---|
1031 | // the requested growth. A non-null return indicates that copying to a new location is required
|
---|
1032 | // but has not been done; the returned value is the old heap storage location; `this` heap is
|
---|
1033 | // modified to reference the new location. In the copy-requred case, the caller should use
|
---|
1034 | // VbyteAlloc to claim the new space, while doing optimal copying from old to new, then free old.
|
---|
1035 |
|
---|
1036 | static char * VbyteTryAdjustLast( VbyteHeap & s, int delta ) with(s) {
|
---|
1037 | if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) {
|
---|
1038 | // room available
|
---|
1039 | EndVbyte += delta;
|
---|
1040 | return 0p;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | char *oldBytes = StartVbyte;
|
---|
1044 |
|
---|
1045 | NoOfExtensions += 1;
|
---|
1046 | CurrSize *= 2;
|
---|
1047 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
|
---|
1048 | ExtVbyte = StartVbyte + CurrSize;
|
---|
1049 |
|
---|
1050 | return oldBytes;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 |
|
---|
1054 | // Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by
|
---|
1055 | // the address in the byte string area.
|
---|
1056 |
|
---|
1057 | static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) {
|
---|
1058 | #ifdef VbyteDebug
|
---|
1059 | serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;
|
---|
1060 | #endif // VbyteDebug
|
---|
1061 | verify( h.ulink != 0p );
|
---|
1062 | verify( s.ulink == h.ulink );
|
---|
1063 | if ( s < h.s ) { // check argument values
|
---|
1064 | // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:"
|
---|
1065 | // | ( h->s ) | " and keep handles in ascending order";
|
---|
1066 | // exit(-1 );
|
---|
1067 | verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order");
|
---|
1068 | } // if
|
---|
1069 |
|
---|
1070 | HandleNode *i;
|
---|
1071 | for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h
|
---|
1072 | if ( & s != i->blink ) {
|
---|
1073 | DeleteNode( s );
|
---|
1074 | AddThisAfter( s, *i->blink );
|
---|
1075 | } // if
|
---|
1076 | #ifdef VbyteDebug
|
---|
1077 | {
|
---|
1078 | serr | "HandleList:";
|
---|
1079 | serr | nlOff;
|
---|
1080 | for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {
|
---|
1081 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
---|
1082 | for ( i; n->lnth ) {
|
---|
1083 | serr | n->s[i];
|
---|
1084 | } // for
|
---|
1085 | serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;
|
---|
1086 | } // for
|
---|
1087 | serr | nlOn;
|
---|
1088 | }
|
---|
1089 | serr | "exit:MoveThisAfter";
|
---|
1090 | #endif // VbyteDebug
|
---|
1091 | } // MoveThisAfter
|
---|
1092 |
|
---|
1093 |
|
---|
1094 | //######################### VbyteHeap #########################
|
---|
1095 |
|
---|
1096 | // Compare two byte strings in the byte-string area. The routine returns the following values:
|
---|
1097 | //
|
---|
1098 | // 1 => Src1-byte-string > Src2-byte-string
|
---|
1099 | // 0 => Src1-byte-string = Src2-byte-string
|
---|
1100 | // -1 => Src1-byte-string < Src2-byte-string
|
---|
1101 |
|
---|
1102 | int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) {
|
---|
1103 | #ifdef VbyteDebug
|
---|
1104 | serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
|
---|
1105 | #endif // VbyteDebug
|
---|
1106 | int cmp;
|
---|
1107 |
|
---|
1108 | CharZip: for ( int i = 0; ; i += 1 ) {
|
---|
1109 | if ( i == Src2Lnth - 1 ) {
|
---|
1110 | for ( ; ; i += 1 ) {
|
---|
1111 | if ( i == Src1Lnth - 1 ) {
|
---|
1112 | cmp = 0;
|
---|
1113 | break CharZip;
|
---|
1114 | } // exit
|
---|
1115 | if ( Src1[Src1Start + i] != ' ') {
|
---|
1116 | // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?)
|
---|
1117 | cmp = 1;
|
---|
1118 | break CharZip;
|
---|
1119 | } // exit
|
---|
1120 | } // for
|
---|
1121 | } // exit
|
---|
1122 | if ( i == Src1Lnth - 1 ) {
|
---|
1123 | for ( ; ; i += 1 ) {
|
---|
1124 | if ( i == Src2Lnth - 1 ) {
|
---|
1125 | cmp = 0;
|
---|
1126 | break CharZip;
|
---|
1127 | } // exit
|
---|
1128 | if ( Src2[Src2Start + i] != ' ') {
|
---|
1129 | cmp = -1;
|
---|
1130 | break CharZip;
|
---|
1131 | } // exit
|
---|
1132 | } // for
|
---|
1133 | } // exit
|
---|
1134 | if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) {
|
---|
1135 | cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1;
|
---|
1136 | break CharZip;
|
---|
1137 | } // exit
|
---|
1138 | } // for
|
---|
1139 | #ifdef VbyteDebug
|
---|
1140 | serr | "exit:ByteCmp, cmp:" | cmp;
|
---|
1141 | #endif // VbyteDebug
|
---|
1142 | return cmp;
|
---|
1143 | } // ByteCmp
|
---|
1144 |
|
---|
1145 |
|
---|
1146 | // The compaction moves all of the byte strings currently in use to the beginning of the byte-string area and modifies
|
---|
1147 | // the handles to reflect the new positions of the byte strings. Compaction assumes that the handle list is in ascending
|
---|
1148 | // order by pointers into the byte-string area. The strings associated with substrings do not have to be moved because
|
---|
1149 | // the containing string has been moved. Hence, they only require that their string pointers be adjusted.
|
---|
1150 |
|
---|
1151 | void compaction(VbyteHeap & s) with(s) {
|
---|
1152 | HandleNode *h;
|
---|
1153 | char *obase, *nbase, *limit;
|
---|
1154 |
|
---|
1155 | NoOfCompactions += 1;
|
---|
1156 | EndVbyte = StartVbyte;
|
---|
1157 | h = Header.flink; // ignore header node
|
---|
1158 | for () {
|
---|
1159 | memmove( EndVbyte, h->s, h->lnth );
|
---|
1160 | obase = h->s;
|
---|
1161 | h->s = EndVbyte;
|
---|
1162 | nbase = h->s;
|
---|
1163 | EndVbyte += h->lnth;
|
---|
1164 | limit = obase + h->lnth;
|
---|
1165 | h = h->flink;
|
---|
1166 |
|
---|
1167 | // check if any substrings are allocated within a string
|
---|
1168 |
|
---|
1169 | for () {
|
---|
1170 | if ( h == &Header ) break; // end of header list ?
|
---|
1171 | if ( h->s >= limit ) break; // outside of current string ?
|
---|
1172 | h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
|
---|
1173 | h = h->flink;
|
---|
1174 | } // for
|
---|
1175 | if ( h == &Header ) break; // end of header list ?
|
---|
1176 | } // for
|
---|
1177 | } // compaction
|
---|
1178 |
|
---|
1179 |
|
---|
1180 | static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage)
|
---|
1181 | // probably an unreasonable default, but need to assess early-round tests on changing it
|
---|
1182 |
|
---|
1183 | void TUNING_set_string_heap_liveness_threshold( double val ) {
|
---|
1184 | heap_expansion_freespace_threshold = 1.0 - val;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 |
|
---|
1188 | // Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of
|
---|
1189 | // the heap. The heap is then compacted in the existing heap or into the newly allocated heap.
|
---|
1190 |
|
---|
1191 | void garbage(VbyteHeap & s, int minreq ) with(s) {
|
---|
1192 | #ifdef VbyteDebug
|
---|
1193 | serr | "enter:garbage";
|
---|
1194 | {
|
---|
1195 | serr | "HandleList:";
|
---|
1196 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
|
---|
1197 | serr | nlOff;
|
---|
1198 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
---|
1199 | for ( i; n->lnth ) {
|
---|
1200 | serr | n->s[i];
|
---|
1201 | } // for
|
---|
1202 | serr | nlOn;
|
---|
1203 | serr | "\" flink:" | n->flink | " blink:" | n->blink;
|
---|
1204 | } // for
|
---|
1205 | }
|
---|
1206 | #endif // VbyteDebug
|
---|
1207 | int AmountUsed, AmountFree;
|
---|
1208 |
|
---|
1209 | AmountUsed = 0;
|
---|
1210 | for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used
|
---|
1211 | AmountUsed += i->lnth;
|
---|
1212 | } // for
|
---|
1213 | AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;
|
---|
1214 |
|
---|
1215 | if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request
|
---|
1216 |
|
---|
1217 | extend( s, max( CurrSize, minreq ) ); // extend the heap
|
---|
1218 |
|
---|
1219 | // Peter says, "This needs work before it should be used."
|
---|
1220 | // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ?
|
---|
1221 | // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory
|
---|
1222 |
|
---|
1223 | // `extend` implies a `compaction` during the copy
|
---|
1224 |
|
---|
1225 | } else {
|
---|
1226 | compaction(s); // in-place
|
---|
1227 | }// if
|
---|
1228 | #ifdef VbyteDebug
|
---|
1229 | {
|
---|
1230 | serr | "HandleList:";
|
---|
1231 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
|
---|
1232 | serr | nlOff;
|
---|
1233 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
---|
1234 | for ( i; n->lnth ) {
|
---|
1235 | serr | n->s[i];
|
---|
1236 | } // for
|
---|
1237 | serr | nlOn;
|
---|
1238 | serr | "\" flink:" | n->flink | " blink:" | n->blink;
|
---|
1239 | } // for
|
---|
1240 | }
|
---|
1241 | serr | "exit:garbage";
|
---|
1242 | #endif // VbyteDebug
|
---|
1243 | } // garbage
|
---|
1244 |
|
---|
1245 | #undef VbyteDebug
|
---|
1246 |
|
---|
1247 |
|
---|
1248 |
|
---|
1249 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
|
---|
1250 | // area is deleted.
|
---|
1251 |
|
---|
1252 | void extend( VbyteHeap & s, int size ) with (s) {
|
---|
1253 | #ifdef VbyteDebug
|
---|
1254 | serr | "enter:extend, size:" | size;
|
---|
1255 | #endif // VbyteDebug
|
---|
1256 | char *OldStartVbyte;
|
---|
1257 |
|
---|
1258 | NoOfExtensions += 1;
|
---|
1259 | OldStartVbyte = StartVbyte; // save previous byte area
|
---|
1260 |
|
---|
1261 | CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size
|
---|
1262 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
|
---|
1263 | ExtVbyte = (void *)( StartVbyte + CurrSize );
|
---|
1264 | compaction(s); // copy from old heap to new & adjust pointers to new heap
|
---|
1265 | free( OldStartVbyte ); // release old heap
|
---|
1266 | #ifdef VbyteDebug
|
---|
1267 | serr | "exit:extend, CurrSize:" | CurrSize;
|
---|
1268 | #endif // VbyteDebug
|
---|
1269 | } // extend
|
---|
1270 |
|
---|
1271 | //WIP
|
---|
1272 | #if 0
|
---|
1273 |
|
---|
1274 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
|
---|
1275 | // area is deleted.
|
---|
1276 |
|
---|
1277 | void VbyteHeap::reduce( int size ) {
|
---|
1278 | #ifdef VbyteDebug
|
---|
1279 | serr | "enter:reduce, size:" | size;
|
---|
1280 | #endif // VbyteDebug
|
---|
1281 | char *OldStartVbyte;
|
---|
1282 |
|
---|
1283 | NoOfReductions += 1;
|
---|
1284 | OldStartVbyte = StartVbyte; // save previous byte area
|
---|
1285 |
|
---|
1286 | CurrSize -= size;
|
---|
1287 | StartVbyte = EndVbyte = new char[CurrSize];
|
---|
1288 | ExtVbyte = (void *)( StartVbyte + CurrSize );
|
---|
1289 | compaction(); // copy from old heap to new & adjust pointers to new heap
|
---|
1290 | delete OldStartVbyte; // release old heap
|
---|
1291 | #ifdef VbyteDebug
|
---|
1292 | !serr | "exit:reduce, CurrSize:" | CurrSize;
|
---|
1293 | #endif // VbyteDebug
|
---|
1294 | } // reduce
|
---|
1295 |
|
---|
1296 |
|
---|
1297 | #endif
|
---|