| [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_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
|
|---|
| [7d25f44] | 11 | // Last Modified By : Peter A. Buhr
|
|---|
| [ed5023d1] | 12 | // Last Modified On : Sun Apr 6 07:38:02 2025
|
|---|
| 13 | // Update Count : 111
|
|---|
| [f450f2f] | 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "string_res.hfa"
|
|---|
| [0f781fb8] | 17 | #include "string_sharectx.hfa"
|
|---|
| [08ed947] | 18 | #include "stdlib.hfa"
|
|---|
| [d32679d5] | 19 | #include <ctype.h>
|
|---|
| [08ed947] | 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)
|
|---|
| [681e12f] | 24 | #define TEMP_ALLOC(T, n) (( T * ) malloc( n * sizeof( T ) ))
|
|---|
| [0f781fb8] | 25 |
|
|---|
| [218096f] | 26 | #include <assert.h>
|
|---|
| [211def2] | 27 | #include <complex.h> // creal, cimag
|
|---|
| [f450f2f] | 28 |
|
|---|
| 29 | //######################### VbyteHeap "header" #########################
|
|---|
| 30 |
|
|---|
| 31 | #ifdef VbyteDebug
|
|---|
| [6cc87c0] | 32 | HandleNode *HeaderPtr;
|
|---|
| [f450f2f] | 33 | #endif // VbyteDebug
|
|---|
| 34 |
|
|---|
| 35 | struct VbyteHeap {
|
|---|
| [211def2] | 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
|
|---|
| [f450f2f] | 47 | }; // VbyteHeap
|
|---|
| 48 |
|
|---|
| [211def2] | 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
|
|---|
| [f450f2f] | 54 |
|
|---|
| [4e8df745] | 55 | static void ?{}( VbyteHeap &, size_t = 1000 );
|
|---|
| 56 | static void ^?{}( VbyteHeap & );
|
|---|
| [94647b0b] | 57 |
|
|---|
| [211def2] | 58 | static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes
|
|---|
| [4e8df745] | 59 | static char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap
|
|---|
| 60 | static char *VbyteTryAdjustLast( VbyteHeap &, int );
|
|---|
| [f450f2f] | 61 |
|
|---|
| [4e8df745] | 62 | static void AddThisAfter( HandleNode &, HandleNode & );
|
|---|
| 63 | static void DeleteNode( HandleNode & );
|
|---|
| [211def2] | 64 | static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle
|
|---|
| [f450f2f] | 65 |
|
|---|
| 66 |
|
|---|
| 67 | // Allocate the storage for the variable sized area and intialize the heap variables.
|
|---|
| 68 |
|
|---|
| [681e12f] | 69 | static void ?{}( VbyteHeap & s, size_t Size ) with(s) {
|
|---|
| [f450f2f] | 70 | #ifdef VbyteDebug
|
|---|
| [211def2] | 71 | serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;
|
|---|
| [f450f2f] | 72 | #endif // VbyteDebug
|
|---|
| [211def2] | 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;
|
|---|
| [f450f2f] | 79 | #ifdef VbyteDebug
|
|---|
| [211def2] | 80 | HeaderPtr = &Header;
|
|---|
| 81 | serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;
|
|---|
| [f450f2f] | 82 | #endif // VbyteDebug
|
|---|
| 83 | } // VbyteHeap
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | // Release the dynamically allocated storage for the byte area.
|
|---|
| 87 |
|
|---|
| [681e12f] | 88 | static void ^?{}( VbyteHeap & s ) with(s) {
|
|---|
| [211def2] | 89 | free( StartVbyte );
|
|---|
| [f450f2f] | 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 |
|
|---|
| [681e12f] | 99 | static void ?{}( HandleNode & s ) with(s) {
|
|---|
| [f450f2f] | 100 | #ifdef VbyteDebug
|
|---|
| [211def2] | 101 | serr | "enter:HandleNode::HandleNode, s:" | &s;
|
|---|
| [f450f2f] | 102 | #endif // VbyteDebug
|
|---|
| [211def2] | 103 | s = 0;
|
|---|
| 104 | lnth = 0;
|
|---|
| [f450f2f] | 105 | #ifdef VbyteDebug
|
|---|
| [211def2] | 106 | serr | "exit:HandleNode::HandleNode, s:" | &s;
|
|---|
| [f450f2f] | 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 |
|
|---|
| [681e12f] | 114 | static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) {
|
|---|
| [f450f2f] | 115 | #ifdef VbyteDebug
|
|---|
| [211def2] | 116 | serr | "enter:HandleNode::HandleNode, s:" | &s;
|
|---|
| [f450f2f] | 117 | #endif // VbyteDebug
|
|---|
| [211def2] | 118 | s = 0;
|
|---|
| 119 | lnth = 0;
|
|---|
| 120 | ulink = &vh;
|
|---|
| 121 | AddThisAfter( s, *vh.Header.blink );
|
|---|
| [f450f2f] | 122 | #ifdef VbyteDebug
|
|---|
| [211def2] | 123 | serr | "exit:HandleNode::HandleNode, s:" | &s;
|
|---|
| [f450f2f] | 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 |
|
|---|
| [681e12f] | 131 | static void ^?{}( HandleNode & s ) with(s) {
|
|---|
| [f450f2f] | 132 | #ifdef VbyteDebug
|
|---|
| [211def2] | 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 | }
|
|---|
| [f450f2f] | 143 | #endif // VbyteDebug
|
|---|
| [211def2] | 144 | DeleteNode( s );
|
|---|
| [f450f2f] | 145 | } // ~HandleNode
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| [0f781fb8] | 148 | //######################### String Sharing Context #########################
|
|---|
| [f450f2f] | 149 |
|
|---|
| [211def2] | 150 | static string_sharectx * ambient_string_sharectx; // fickle top of stack
|
|---|
| [0f781fb8] | 151 | static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack
|
|---|
| [f450f2f] | 152 |
|
|---|
| [681e12f] | 153 | void ?{}( string_sharectx & s, StringSharectx_Mode mode ) with( s ) {
|
|---|
| [211def2] | 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;
|
|---|
| [0f781fb8] | 162 | }
|
|---|
| 163 |
|
|---|
| [681e12f] | 164 | void ^?{}( string_sharectx & s ) with( s ) {
|
|---|
| [211def2] | 165 | if ( activeHeap ) delete( activeHeap );
|
|---|
| [0f781fb8] | 166 |
|
|---|
| [211def2] | 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
|
|---|
| [0f781fb8] | 172 | }
|
|---|
| 173 |
|
|---|
| 174 | //######################### String Resource #########################
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | VbyteHeap * DEBUG_string_heap() {
|
|---|
| [211def2] | 178 | assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
|
|---|
| 179 | return ambient_string_sharectx->activeHeap;
|
|---|
| [0f781fb8] | 180 | }
|
|---|
| [6cc87c0] | 181 |
|
|---|
| 182 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) {
|
|---|
| [211def2] | 183 | return ((char *)heap->ExtVbyte) - heap->EndVbyte;
|
|---|
| [6cc87c0] | 184 | }
|
|---|
| 185 |
|
|---|
| [7b0e8b7] | 186 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) {
|
|---|
| [211def2] | 187 | return heap->CurrSize;
|
|---|
| [7b0e8b7] | 188 | }
|
|---|
| 189 |
|
|---|
| [6cc87c0] | 190 | const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
|
|---|
| [211def2] | 191 | return heap->StartVbyte;
|
|---|
| [6cc87c0] | 192 | }
|
|---|
| 193 |
|
|---|
| [f450f2f] | 194 | // Returns the size of the string in bytes
|
|---|
| [ee70ff5] | 195 | size_t len(const string_res & s) with(s) {
|
|---|
| [211def2] | 196 | return Handle.lnth;
|
|---|
| [f450f2f] | 197 | }
|
|---|
| 198 |
|
|---|
| 199 | // Output operator
|
|---|
| [681e12f] | 200 | ofstream & ?|?(ofstream & out, const string_res & s) {
|
|---|
| [9ca5e56] | 201 | // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0.
|
|---|
| [fbe3f03] | 202 | return out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl;
|
|---|
| [f450f2f] | 203 | }
|
|---|
| 204 |
|
|---|
| [681e12f] | 205 | void ?|?(ofstream & out, const string_res & s) {
|
|---|
| [9ca5e56] | 206 | (ofstream &)(out | s); ends( out );
|
|---|
| [f450f2f] | 207 | }
|
|---|
| 208 |
|
|---|
| [d32679d5] | 209 | // Input operator
|
|---|
| [681e12f] | 210 | ifstream & ?|?(ifstream & in, string_res & s) {
|
|---|
| [211def2] | 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 {
|
|---|
| [37ceccb] | 236 | *(temp.Handle.ulink->EndVbyte) = '\0'; // pre-assign empty cstring
|
|---|
| [211def2] | 237 | in | wdi( lenReadable, temp.Handle.ulink->EndVbyte );
|
|---|
| 238 | } catch (cstring_length *) {
|
|---|
| 239 | cont = true;
|
|---|
| 240 | }
|
|---|
| 241 | int lenWasRead = strlen(temp.Handle.ulink->EndVbyte);
|
|---|
| [d32679d5] | 242 |
|
|---|
| [211def2] | 243 | // update metadata
|
|---|
| 244 | temp.Handle.lnth += lenWasRead;
|
|---|
| 245 | temp.Handle.ulink->EndVbyte += lenWasRead;
|
|---|
| 246 | }
|
|---|
| [d32679d5] | 247 |
|
|---|
| [37ceccb] | 248 | if ( temp.Handle.lnth > 0 ) s = temp;
|
|---|
| [211def2] | 249 | return in;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | ifstream & ?|?( ifstream & is, _Istream_Rquoted f ) with( f.rstr ) {
|
|---|
| [3ac5fd8] | 253 | if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
|
|---|
| [211def2] | 254 | int args;
|
|---|
| 255 | fini: {
|
|---|
| [714e206] | 256 | char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' };
|
|---|
| [ee70ff5] | 257 | int l = -1; // may not be set in fmt
|
|---|
| 258 | args = fmt( is, rfmt, &l ); // remove leading whitespace and quote
|
|---|
| 259 | if ( eof( is ) || l == -1 ) break fini;
|
|---|
| [211def2] | 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;
|
|---|
| [ff56dd2e] | 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 |
|
|---|
| [5764204] | 285 | _Istream_Cwidth cf = { cstr, (_Istream_str_base)f };
|
|---|
| [ff56dd2e] | 286 | if ( ! cf.flags.rwd ) cf.wd = wd;
|
|---|
| 287 |
|
|---|
| 288 | cstr[wd] = '\0'; // guard null terminate string
|
|---|
| 289 | try {
|
|---|
| [37ceccb] | 290 | cstr[0] = '\0'; // pre-assign as empty cstring
|
|---|
| [ff56dd2e] | 291 | is | cf;
|
|---|
| 292 | } catch( cstring_length * ) {
|
|---|
| 293 | cont = true;
|
|---|
| 294 | } finally {
|
|---|
| [681e12f] | 295 | if ( ! cf.flags.ignore // ok to initialize string
|
|---|
| 296 | // && cstr[0] != '\0' // something was read
|
|---|
| 297 | ) {
|
|---|
| [37ceccb] | 298 | *(f.s) = cstr;
|
|---|
| 299 | }
|
|---|
| [ff56dd2e] | 300 | } // try
|
|---|
| 301 | for ( ; cont; ) { // overflow read ?
|
|---|
| 302 | cont = false;
|
|---|
| 303 | try {
|
|---|
| [37ceccb] | 304 | cstr[0] = '\0'; // pre-assign as empty cstring
|
|---|
| [ff56dd2e] | 305 | is | cf;
|
|---|
| 306 | } catch( cstring_length * ) {
|
|---|
| 307 | cont = true; // continue not allowed
|
|---|
| 308 | } finally {
|
|---|
| [681e12f] | 309 | if ( ! cf.flags.ignore && cstr[0] != '\0' ) { // something was read
|
|---|
| [37ceccb] | 310 | *(f.s) += cstr; // build string chunk at a time
|
|---|
| 311 | }
|
|---|
| [ff56dd2e] | 312 | } // try
|
|---|
| 313 | } // for
|
|---|
| 314 | return is;
|
|---|
| 315 | } // ?|?
|
|---|
| 316 |
|
|---|
| [f450f2f] | 317 | // Empty constructor
|
|---|
| [681e12f] | 318 | void ?{}(string_res & s) with(s) {
|
|---|
| [211def2] | 319 | if( ambient_string_sharectx->activeHeap ) {
|
|---|
| 320 | (Handle){ * ambient_string_sharectx->activeHeap };
|
|---|
| [8c2723f] | 321 | (shareSet_owns_ulink){ false };
|
|---|
| [211def2] | 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
|
|---|
| [8c2723f] | 325 | (shareSet_owns_ulink){ true };
|
|---|
| [211def2] | 326 | Handle.s = Handle.ulink->StartVbyte;
|
|---|
| 327 | verify( Handle.lnth == 0 );
|
|---|
| 328 | }
|
|---|
| [8c2723f] | 329 | s.shareSet_prev = &s;
|
|---|
| 330 | s.shareSet_next = &s;
|
|---|
| [211def2] | 331 | }
|
|---|
| [f450f2f] | 332 |
|
|---|
| [06280ad] | 333 | static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) {
|
|---|
| [211def2] | 334 | if( ambient_string_sharectx->activeHeap ) {
|
|---|
| 335 | (Handle){ * ambient_string_sharectx->activeHeap };
|
|---|
| [8c2723f] | 336 | (shareSet_owns_ulink){ false };
|
|---|
| [211def2] | 337 | } else {
|
|---|
| 338 | (Handle){ * new( rhslnth ) };
|
|---|
| [8c2723f] | 339 | (shareSet_owns_ulink){ true };
|
|---|
| [211def2] | 340 | }
|
|---|
| 341 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
|
|---|
| 342 | Handle.lnth = rhslnth;
|
|---|
| 343 | memmove( Handle.s, rhs, rhslnth );
|
|---|
| [8c2723f] | 344 | s.shareSet_prev = &s;
|
|---|
| 345 | s.shareSet_next = &s;
|
|---|
| [f450f2f] | 346 | }
|
|---|
| 347 |
|
|---|
| [4b3b352] | 348 | // Constructor from a raw buffer and size
|
|---|
| [06280ad] | 349 | void ?{}(string_res & s, const char * rhs, size_t rhslnth) with(s) {
|
|---|
| [211def2] | 350 | eagerCopyCtorHelper(s, rhs, rhslnth);
|
|---|
| [4b3b352] | 351 | }
|
|---|
| 352 |
|
|---|
| [f2898df] | 353 | void ?{}( string_res & s, ssize_t rhs ) {
|
|---|
| [211def2] | 354 | char buf[64];
|
|---|
| [ee70ff5] | 355 | int l;
|
|---|
| 356 | snprintf( buf, sizeof(buf)-1, "%zd%n", rhs, &l );
|
|---|
| 357 | ( s ){ buf, l };
|
|---|
| [f2898df] | 358 | }
|
|---|
| 359 | void ?{}( string_res & s, size_t rhs ) {
|
|---|
| [211def2] | 360 | char buf[64];
|
|---|
| [ee70ff5] | 361 | int l;
|
|---|
| 362 | snprintf( buf, sizeof(buf)-1, "%zu%n", rhs, &l );
|
|---|
| 363 | ( s ){ buf, l };
|
|---|
| [f2898df] | 364 | }
|
|---|
| 365 | void ?{}( string_res & s, double rhs ) {
|
|---|
| [211def2] | 366 | char buf[64];
|
|---|
| [ee70ff5] | 367 | int l;
|
|---|
| 368 | snprintf( buf, sizeof(buf)-1, "%g%n", rhs, &l );
|
|---|
| 369 | ( s ){ buf, l };
|
|---|
| [f2898df] | 370 | }
|
|---|
| 371 | void ?{}( string_res & s, long double rhs ) {
|
|---|
| [211def2] | 372 | char buf[64];
|
|---|
| [ee70ff5] | 373 | int l;
|
|---|
| 374 | snprintf( buf, sizeof(buf)-1, "%Lg%n", rhs, &l );
|
|---|
| 375 | ( s ){ buf, l };
|
|---|
| [f2898df] | 376 | }
|
|---|
| 377 | void ?{}( string_res & s, double _Complex rhs ) {
|
|---|
| [211def2] | 378 | char buf[64];
|
|---|
| [ee70ff5] | 379 | int l;
|
|---|
| 380 | snprintf( buf, sizeof(buf)-1, "%g+%gi%n", creal( rhs ), cimag( rhs ), &l );
|
|---|
| 381 | ( s ){ buf, l };
|
|---|
| [f2898df] | 382 | }
|
|---|
| 383 | void ?{}( string_res & s, long double _Complex rhs ) {
|
|---|
| [211def2] | 384 | char buf[64];
|
|---|
| [ee70ff5] | 385 | int l;
|
|---|
| 386 | snprintf( buf, sizeof(buf)-1, "%Lg+%Lgi%n", creall( rhs ), cimagl( rhs ), &l );
|
|---|
| 387 | ( s ){ buf, l };
|
|---|
| [f2898df] | 388 | }
|
|---|
| 389 |
|
|---|
| [fe18b46] | 390 | // private ctor (not in header): use specified heap (ignore ambient) and copy chars in
|
|---|
| [06280ad] | 391 | void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) {
|
|---|
| [211def2] | 392 | (Handle){ heap };
|
|---|
| 393 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
|
|---|
| 394 | Handle.lnth = rhslnth;
|
|---|
| [8c2723f] | 395 | (s.shareSet_owns_ulink){ false };
|
|---|
| [211def2] | 396 | memmove( Handle.s, rhs, rhslnth );
|
|---|
| [8c2723f] | 397 | s.shareSet_prev = &s;
|
|---|
| 398 | s.shareSet_next = &s;
|
|---|
| [fe18b46] | 399 | }
|
|---|
| 400 |
|
|---|
| [e8b3717] | 401 |
|
|---|
| [f450f2f] | 402 | // General copy constructor
|
|---|
| [e8b3717] | 403 | void ?{}(string_res & s, const string_res & s2, StrResInitMode mode, size_t start, size_t len ) {
|
|---|
| [211def2] | 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);
|
|---|
| [8c2723f] | 410 | verify(s.shareSet_prev == &s);
|
|---|
| 411 | verify(s.shareSet_next == &s);
|
|---|
| [211def2] | 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 |
|
|---|
| [8c2723f] | 425 | (s.shareSet_owns_ulink){ false };
|
|---|
| [211def2] | 426 |
|
|---|
| [8c2723f] | 427 | // make s alone in its shareSet
|
|---|
| 428 | s.shareSet_prev = &s;
|
|---|
| 429 | s.shareSet_next = &s;
|
|---|
| [211def2] | 430 | } else {
|
|---|
| 431 | verify( mode == SHARE_EDITS );
|
|---|
| 432 | // sharing edits with source forces same heap as source (ignore context)
|
|---|
| 433 |
|
|---|
| [8c2723f] | 434 | (s.shareSet_owns_ulink){ s2.shareSet_owns_ulink };
|
|---|
| [211def2] | 435 |
|
|---|
| 436 | // s2 is logically const but not implementation const
|
|---|
| 437 | string_res & s2mod = (string_res &) s2;
|
|---|
| 438 |
|
|---|
| [8c2723f] | 439 | // insert s after s2 on shareSet
|
|---|
| 440 | s.shareSet_next = s2mod.shareSet_next;
|
|---|
| 441 | s.shareSet_prev = &s2mod;
|
|---|
| 442 | s.shareSet_next->shareSet_prev = &s;
|
|---|
| 443 | s.shareSet_prev->shareSet_next = &s;
|
|---|
| [211def2] | 444 | }
|
|---|
| 445 | }
|
|---|
| [4b3b352] | 446 | }
|
|---|
| [f450f2f] | 447 |
|
|---|
| [8c2723f] | 448 | static void assignEditSet(string_res & s, string_res * shareSetStartPeer, string_res * shareSetEndPeer,
|
|---|
| [211def2] | 449 | char * resultSesStart,
|
|---|
| 450 | size_t resultSesLnth,
|
|---|
| 451 | HandleNode * resultPadPosition, size_t bsize ) {
|
|---|
| 452 |
|
|---|
| [8c2723f] | 453 | char * beforeBegin = shareSetStartPeer->Handle.s;
|
|---|
| [211def2] | 454 | size_t beforeLen = s.Handle.s - beforeBegin;
|
|---|
| 455 |
|
|---|
| 456 | char * afterBegin = s.Handle.s + s.Handle.lnth;
|
|---|
| [8c2723f] | 457 | size_t afterLen = shareSetEndPeer->Handle.s + shareSetEndPeer->Handle.lnth - afterBegin;
|
|---|
| [211def2] | 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;
|
|---|
| [8c2723f] | 468 | for ( string_res * p = s.shareSet_next; p != &s; p = p->shareSet_next ) {
|
|---|
| [211def2] | 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 | }
|
|---|
| [4b3b352] | 519 | }
|
|---|
| 520 |
|
|---|
| [681e12f] | 521 | // traverse the share-edit set (SES) to recover the range of a base string to which `s` belongs
|
|---|
| [8c2723f] | 522 | static void locateInShareSet( string_res & s, string_res *& shareSetStartPeer, string_res *& shareSetEndPeer ) {
|
|---|
| 523 | shareSetStartPeer = & s;
|
|---|
| 524 | shareSetEndPeer = & s;
|
|---|
| 525 | for (string_res * editPeer = s.shareSet_next; editPeer != &s; editPeer = editPeer->shareSet_next) {
|
|---|
| 526 | if ( editPeer->Handle.s < shareSetStartPeer->Handle.s ) {
|
|---|
| 527 | shareSetStartPeer = editPeer;
|
|---|
| [211def2] | 528 | }
|
|---|
| [8c2723f] | 529 | if ( shareSetEndPeer->Handle.s + shareSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) {
|
|---|
| 530 | shareSetEndPeer = editPeer;
|
|---|
| [211def2] | 531 | }
|
|---|
| 532 | }
|
|---|
| [d32679d5] | 533 | }
|
|---|
| 534 |
|
|---|
| [06280ad] | 535 | static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) {
|
|---|
| [8c2723f] | 536 | string_res * shareSetStartPeer;
|
|---|
| 537 | string_res * shareSetEndPeer;
|
|---|
| 538 | locateInShareSet( s, shareSetStartPeer, shareSetEndPeer );
|
|---|
| [211def2] | 539 |
|
|---|
| [8c2723f] | 540 | verify( shareSetEndPeer->Handle.s >= shareSetStartPeer->Handle.s );
|
|---|
| 541 | size_t origEditSetLength = shareSetEndPeer->Handle.s + shareSetEndPeer->Handle.lnth - shareSetStartPeer->Handle.s;
|
|---|
| [211def2] | 542 | verify( origEditSetLength >= s.Handle.lnth );
|
|---|
| 543 |
|
|---|
| [8c2723f] | 544 | if ( s.shareSet_owns_ulink ) { // assigning to private context
|
|---|
| [211def2] | 545 | // ok to overwrite old value within LHS
|
|---|
| [8c2723f] | 546 | char * prefixStartOrig = shareSetStartPeer->Handle.s;
|
|---|
| [211def2] | 547 | int prefixLen = s.Handle.s - prefixStartOrig;
|
|---|
| 548 | char * suffixStartOrig = s.Handle.s + s.Handle.lnth;
|
|---|
| [8c2723f] | 549 | int suffixLen = shareSetEndPeer->Handle.s + shareSetEndPeer->Handle.lnth - suffixStartOrig;
|
|---|
| [211def2] | 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);
|
|---|
| [8c2723f] | 558 | assignEditSet(s, shareSetStartPeer, shareSetEndPeer,
|
|---|
| [211def2] | 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 |
|
|---|
| [8c2723f] | 568 | assignEditSet(s, shareSetStartPeer, shareSetEndPeer,
|
|---|
| 569 | shareSetStartPeer->Handle.s,
|
|---|
| [211def2] | 570 | origEditSetLength + delta,
|
|---|
| 571 | 0p, bsize);
|
|---|
| 572 | }
|
|---|
| [d32679d5] | 573 |
|
|---|
| [211def2] | 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
|
|---|
| [8c2723f] | 580 | assignEditSet(s, shareSetStartPeer, shareSetEndPeer,
|
|---|
| [211def2] | 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 |
|
|---|
| [8c2723f] | 589 | // full string is from start of shareSetStartPeer thru end of shareSetEndPeer
|
|---|
| [211def2] | 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
|
|---|
| [8c2723f] | 595 | shareSetStartPeer->Handle.s, // start of SES
|
|---|
| 596 | s.Handle.s - shareSetStartPeer->Handle.s }; // length of SES, before s
|
|---|
| [211def2] | 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
|
|---|
| [8c2723f] | 602 | shareSetEndPeer->Handle.s + shareSetEndPeer->Handle.lnth -
|
|---|
| [211def2] | 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 |
|
|---|
| [8c2723f] | 609 | assignEditSet(s, shareSetStartPeer, shareSetEndPeer,
|
|---|
| [211def2] | 610 | pasting.Handle.s,
|
|---|
| 611 | pasting.Handle.lnth,
|
|---|
| 612 | &pasting.Handle, bsize);
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | return s;
|
|---|
| [4b3b352] | 616 | }
|
|---|
| 617 |
|
|---|
| [e891349] | 618 | string_res & assign(string_res & s, const string_res & src, size_t maxlen) {
|
|---|
| [211def2] | 619 | return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p);
|
|---|
| [e891349] | 620 | }
|
|---|
| 621 |
|
|---|
| [06280ad] | 622 | string_res & assign(string_res & s, const char * buffer, size_t bsize) {
|
|---|
| [211def2] | 623 | return assign_(s, buffer, bsize, *0p);
|
|---|
| [f450f2f] | 624 | }
|
|---|
| 625 |
|
|---|
| [681e12f] | 626 | string_res & ?=?(string_res & s, char c) {
|
|---|
| [211def2] | 627 | return assign(s, &c, 1);
|
|---|
| [d8d512e] | 628 | }
|
|---|
| 629 |
|
|---|
| [f2898df] | 630 | string_res & ?=?( string_res & s, ssize_t rhs ) {
|
|---|
| [211def2] | 631 | string_res rhs2 = rhs;
|
|---|
| 632 | s = rhs2;
|
|---|
| 633 | return s;
|
|---|
| [f2898df] | 634 | }
|
|---|
| 635 | string_res & ?=?( string_res & s, size_t rhs ) {
|
|---|
| [211def2] | 636 | string_res rhs2 = rhs;
|
|---|
| 637 | s = rhs2;
|
|---|
| 638 | return s;
|
|---|
| [f2898df] | 639 | }
|
|---|
| 640 | string_res & ?=?( string_res & s, double rhs ) {
|
|---|
| [211def2] | 641 | string_res rhs2 = rhs;
|
|---|
| 642 | s = rhs2;
|
|---|
| 643 | return s;
|
|---|
| [f2898df] | 644 | }
|
|---|
| 645 | string_res & ?=?( string_res & s, long double rhs ) {
|
|---|
| [211def2] | 646 | string_res rhs2 = rhs;
|
|---|
| 647 | s = rhs2;
|
|---|
| 648 | return s;
|
|---|
| [f2898df] | 649 | }
|
|---|
| 650 | string_res & ?=?( string_res & s, double _Complex rhs ) {
|
|---|
| [211def2] | 651 | string_res rhs2 = rhs;
|
|---|
| 652 | s = rhs2;
|
|---|
| 653 | return s;
|
|---|
| [f2898df] | 654 | }
|
|---|
| 655 | string_res & ?=?( string_res & s, long double _Complex rhs ) {
|
|---|
| [211def2] | 656 | string_res rhs2 = rhs;
|
|---|
| 657 | s = rhs2;
|
|---|
| 658 | return s;
|
|---|
| [f2898df] | 659 | }
|
|---|
| 660 |
|
|---|
| [d8d512e] | 661 | // Copy assignment operator
|
|---|
| [681e12f] | 662 | string_res & ?=?(string_res & s, const string_res & rhs) with( s ) {
|
|---|
| [211def2] | 663 | return assign_(s, rhs.Handle.s, rhs.Handle.lnth, rhs);
|
|---|
| [d8d512e] | 664 | }
|
|---|
| 665 |
|
|---|
| [681e12f] | 666 | string_res & ?=?(string_res & s, string_res & rhs) with( s ) {
|
|---|
| [211def2] | 667 | const string_res & rhs2 = rhs;
|
|---|
| 668 | return s = rhs2;
|
|---|
| [f450f2f] | 669 | }
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 | // Destructor
|
|---|
| [681e12f] | 673 | void ^?{}(string_res & s) with(s) {
|
|---|
| [211def2] | 674 | // much delegated to implied ^VbyteSM
|
|---|
| [f450f2f] | 675 |
|
|---|
| [211def2] | 676 | // sever s from its share-edit peers, if any (four no-ops when already solo)
|
|---|
| [8c2723f] | 677 | s.shareSet_prev->shareSet_next = s.shareSet_next;
|
|---|
| 678 | s.shareSet_next->shareSet_prev = s.shareSet_prev;
|
|---|
| 679 | // s.shareSet_next = &s;
|
|---|
| 680 | // s.shareSet_prev = &s;
|
|---|
| [804bf677] | 681 |
|
|---|
| [8c2723f] | 682 | if (shareSet_owns_ulink && s.shareSet_next == &s) { // last one out
|
|---|
| [211def2] | 683 | delete( s.Handle.ulink );
|
|---|
| 684 | }
|
|---|
| [f450f2f] | 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.
|
|---|
| [681e12f] | 691 | char ?[?](const string_res & s, size_t index) with(s) {
|
|---|
| [211def2] | 692 | //TODO: Check if index is valid (no exceptions yet)
|
|---|
| 693 | return Handle.s[index];
|
|---|
| [f450f2f] | 694 | }
|
|---|
| 695 |
|
|---|
| [681e12f] | 696 | void assignAt(const string_res & s, size_t index, char val) {
|
|---|
| [211def2] | 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);
|
|---|
| [218096f] | 702 | }
|
|---|
| 703 |
|
|---|
| [d8d512e] | 704 |
|
|---|
| [f450f2f] | 705 | ///////////////////////////////////////////////////////////////////
|
|---|
| [d8d512e] | 706 | // Concatenation
|
|---|
| [f450f2f] | 707 |
|
|---|
| [681e12f] | 708 | void append(string_res & str1, const char * buffer, size_t bsize) {
|
|---|
| [211def2] | 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;
|
|---|
| [f450f2f] | 724 | }
|
|---|
| 725 |
|
|---|
| [ed5023d1] | 726 | void append( string_res & s, const string_res & s2, size_t maxlen ) {
|
|---|
| 727 | append( s, s2.Handle.s, min( s2.Handle.lnth, maxlen ) );
|
|---|
| [e891349] | 728 | }
|
|---|
| [f450f2f] | 729 |
|
|---|
| [38951c31] | 730 | ///////////////////////////////////////////////////////////////////
|
|---|
| 731 | // Repetition
|
|---|
| 732 |
|
|---|
| 733 | void ?*=?(string_res & s, size_t factor) {
|
|---|
| [211def2] | 734 | string_res s2 = { s, COPY_VALUE };
|
|---|
| 735 | s = "";
|
|---|
| 736 | for (factor) s += s2;
|
|---|
| [38951c31] | 737 | }
|
|---|
| 738 |
|
|---|
| [f450f2f] | 739 | //////////////////////////////////////////////////////////
|
|---|
| 740 | // Comparisons
|
|---|
| 741 |
|
|---|
| [ed5023d1] | 742 | int strcmp$( const char * s1, size_t l1, const char * s2, size_t l2 ) {
|
|---|
| 743 | int ret = memcmp( s1, s2, min( l1, l2 ) );
|
|---|
| 744 | if ( ret != 0 ) return ret;
|
|---|
| 745 | return l1 - l2;
|
|---|
| [f450f2f] | 746 | }
|
|---|
| 747 |
|
|---|
| 748 | //////////////////////////////////////////////////////////
|
|---|
| 749 | // Search
|
|---|
| 750 |
|
|---|
| [681e12f] | 751 | bool contains(const string_res & s, char ch) {
|
|---|
| [ee70ff5] | 752 | for ( i; len(s) ) {
|
|---|
| [211def2] | 753 | if (s[i] == ch) return true;
|
|---|
| 754 | }
|
|---|
| 755 | return false;
|
|---|
| [f450f2f] | 756 | }
|
|---|
| 757 |
|
|---|
| [681e12f] | 758 | int find(const string_res & s, char search) {
|
|---|
| [211def2] | 759 | return findFrom(s, 0, search);
|
|---|
| [f450f2f] | 760 | }
|
|---|
| 761 |
|
|---|
| [681e12f] | 762 | int findFrom(const string_res & s, size_t fromPos, char search) {
|
|---|
| [211def2] | 763 | // FIXME: This paricular overload (find of single char) is optimized to use memchr.
|
|---|
| 764 | // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match.
|
|---|
| 765 | char * searchFrom = s.Handle.s + fromPos;
|
|---|
| 766 | size_t searchLnth = s.Handle.lnth - fromPos;
|
|---|
| 767 | int searchVal = search;
|
|---|
| 768 | char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth);
|
|---|
| 769 | if (foundAt == 0p) return s.Handle.lnth;
|
|---|
| 770 | else return foundAt - s.Handle.s;
|
|---|
| [08ed947] | 771 | }
|
|---|
| [f450f2f] | 772 |
|
|---|
| [681e12f] | 773 | int find(const string_res & s, const string_res & search) {
|
|---|
| [211def2] | 774 | return findFrom(s, 0, search);
|
|---|
| [08ed947] | 775 | }
|
|---|
| 776 |
|
|---|
| [681e12f] | 777 | int findFrom(const string_res & s, size_t fromPos, const string_res & search) {
|
|---|
| [211def2] | 778 | return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth);
|
|---|
| [f450f2f] | 779 | }
|
|---|
| 780 |
|
|---|
| [06280ad] | 781 | int find(const string_res & s, const char * search) {
|
|---|
| [211def2] | 782 | return findFrom(s, 0, search);
|
|---|
| [08ed947] | 783 | }
|
|---|
| [06280ad] | 784 | int findFrom(const string_res & s, size_t fromPos, const char * search) {
|
|---|
| [211def2] | 785 | return findFrom(s, fromPos, search, strlen(search));
|
|---|
| [f450f2f] | 786 | }
|
|---|
| 787 |
|
|---|
| [06280ad] | 788 | int find(const string_res & s, const char * search, size_t searchsize) {
|
|---|
| [211def2] | 789 | return findFrom(s, 0, search, searchsize);
|
|---|
| [08ed947] | 790 | }
|
|---|
| 791 |
|
|---|
| [06280ad] | 792 | int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize) {
|
|---|
| [211def2] | 793 | /* Remaining implementations essentially ported from Sunjay's work */
|
|---|
| 794 |
|
|---|
| 795 | // FIXME: This is a naive algorithm. We probably want to switch to someting
|
|---|
| 796 | // like Boyer-Moore in the future.
|
|---|
| 797 | // https://en.wikipedia.org/wiki/String_searching_algorithm
|
|---|
| 798 |
|
|---|
| 799 | // Always find the empty string
|
|---|
| 800 | if (searchsize == 0) {
|
|---|
| 801 | return 0;
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | for ( i; fromPos ~ s.Handle.lnth ) {
|
|---|
| 805 | size_t remaining = s.Handle.lnth - i;
|
|---|
| 806 | // Never going to find the search string if the remaining string is
|
|---|
| 807 | // smaller than search
|
|---|
| 808 | if (remaining < searchsize) {
|
|---|
| 809 | break;
|
|---|
| 810 | }
|
|---|
| [08ed947] | 811 |
|
|---|
| [211def2] | 812 | bool matched = true;
|
|---|
| 813 | for ( j; searchsize ) {
|
|---|
| 814 | if (search[j] != s.Handle.s[i + j]) {
|
|---|
| 815 | matched = false;
|
|---|
| 816 | break;
|
|---|
| 817 | }
|
|---|
| 818 | }
|
|---|
| 819 | if (matched) {
|
|---|
| 820 | return i;
|
|---|
| 821 | }
|
|---|
| 822 | }
|
|---|
| 823 | return s.Handle.lnth;
|
|---|
| [f450f2f] | 824 | }
|
|---|
| 825 |
|
|---|
| [681e12f] | 826 | bool includes(const string_res & s, const string_res & search) {
|
|---|
| [211def2] | 827 | return includes(s, search.Handle.s, search.Handle.lnth);
|
|---|
| [f450f2f] | 828 | }
|
|---|
| 829 |
|
|---|
| [06280ad] | 830 | bool includes(const string_res & s, const char * search) {
|
|---|
| [211def2] | 831 | return includes(s, search, strlen(search));
|
|---|
| [f450f2f] | 832 | }
|
|---|
| 833 |
|
|---|
| [06280ad] | 834 | bool includes(const string_res & s, const char * search, size_t searchsize) {
|
|---|
| [211def2] | 835 | return find(s, search, searchsize) < s.Handle.lnth;
|
|---|
| [f450f2f] | 836 | }
|
|---|
| 837 |
|
|---|
| [681e12f] | 838 | bool startsWith(const string_res & s, const string_res & prefix) {
|
|---|
| [211def2] | 839 | return startsWith(s, prefix.Handle.s, prefix.Handle.lnth);
|
|---|
| [f450f2f] | 840 | }
|
|---|
| 841 |
|
|---|
| [06280ad] | 842 | bool startsWith(const string_res & s, const char * prefix) {
|
|---|
| [211def2] | 843 | return startsWith(s, prefix, strlen(prefix));
|
|---|
| [f450f2f] | 844 | }
|
|---|
| 845 |
|
|---|
| [06280ad] | 846 | bool startsWith(const string_res & s, const char * prefix, size_t prefixsize) {
|
|---|
| [211def2] | 847 | if (s.Handle.lnth < prefixsize) {
|
|---|
| 848 | return false;
|
|---|
| 849 | }
|
|---|
| 850 | return memcmp(s.Handle.s, prefix, prefixsize) == 0;
|
|---|
| [f450f2f] | 851 | }
|
|---|
| 852 |
|
|---|
| [681e12f] | 853 | bool endsWith(const string_res & s, const string_res & suffix) {
|
|---|
| [211def2] | 854 | return endsWith(s, suffix.Handle.s, suffix.Handle.lnth);
|
|---|
| [f450f2f] | 855 | }
|
|---|
| 856 |
|
|---|
| [06280ad] | 857 | bool endsWith(const string_res & s, const char * suffix) {
|
|---|
| [211def2] | 858 | return endsWith(s, suffix, strlen(suffix));
|
|---|
| [f450f2f] | 859 | }
|
|---|
| 860 |
|
|---|
| [06280ad] | 861 | bool endsWith(const string_res & s, const char * suffix, size_t suffixsize) {
|
|---|
| [211def2] | 862 | if (s.Handle.lnth < suffixsize) {
|
|---|
| 863 | return false;
|
|---|
| 864 | }
|
|---|
| 865 | // Amount to offset the bytes pointer so that we are comparing the end of s
|
|---|
| 866 | // to suffix. s.bytes + offset should be the first byte to compare against suffix
|
|---|
| 867 | size_t offset = s.Handle.lnth - suffixsize;
|
|---|
| 868 | return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0;
|
|---|
| [f450f2f] | 869 | }
|
|---|
| 870 |
|
|---|
| [211def2] | 871 | /* Back to Mike's work */
|
|---|
| [f450f2f] | 872 |
|
|---|
| 873 | ///////////////////////////////////////////////////////////////////////////
|
|---|
| 874 | // charclass, include, exclude
|
|---|
| 875 |
|
|---|
| [681e12f] | 876 | void ?{}( charclass_res & s, const string_res & chars) {
|
|---|
| [211def2] | 877 | (s){ chars.Handle.s, chars.Handle.lnth };
|
|---|
| [f450f2f] | 878 | }
|
|---|
| 879 |
|
|---|
| [681e12f] | 880 | void ?{}( charclass_res & s, const char * chars ) {
|
|---|
| [211def2] | 881 | (s){ chars, strlen(chars) };
|
|---|
| [f450f2f] | 882 | }
|
|---|
| 883 |
|
|---|
| [681e12f] | 884 | void ?{}( charclass_res & s, const char * chars, size_t charssize ) {
|
|---|
| [211def2] | 885 | (s.chars){ chars, charssize };
|
|---|
| 886 | // now sort it ?
|
|---|
| [f450f2f] | 887 | }
|
|---|
| 888 |
|
|---|
| [681e12f] | 889 | void ^?{}( charclass_res & s ) {
|
|---|
| [211def2] | 890 | ^(s.chars){};
|
|---|
| [f450f2f] | 891 | }
|
|---|
| 892 |
|
|---|
| 893 | static bool test( const charclass_res & mask, char c ) {
|
|---|
| [211def2] | 894 | // instead, use sorted char list?
|
|---|
| 895 | return contains( mask.chars, c );
|
|---|
| [f450f2f] | 896 | }
|
|---|
| 897 |
|
|---|
| [681e12f] | 898 | int exclude(const string_res & s, const charclass_res & mask) {
|
|---|
| [ee70ff5] | 899 | for ( i; len(s) ) {
|
|---|
| [211def2] | 900 | if ( test(mask, s[i]) ) return i;
|
|---|
| 901 | }
|
|---|
| [ee70ff5] | 902 | return len(s);
|
|---|
| [f450f2f] | 903 | }
|
|---|
| 904 |
|
|---|
| [681e12f] | 905 | int include(const string_res & s, const charclass_res & mask) {
|
|---|
| [ee70ff5] | 906 | for ( i; len(s) ) {
|
|---|
| [211def2] | 907 | if ( ! test(mask, s[i]) ) return i;
|
|---|
| 908 | }
|
|---|
| [ee70ff5] | 909 | return len(s);
|
|---|
| [f450f2f] | 910 | }
|
|---|
| 911 |
|
|---|
| 912 | //######################### VbyteHeap "implementation" #########################
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 | // Add a new HandleNode node n after the current HandleNode node.
|
|---|
| 916 |
|
|---|
| [681e12f] | 917 | static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) {
|
|---|
| [f450f2f] | 918 | #ifdef VbyteDebug
|
|---|
| [211def2] | 919 | serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;
|
|---|
| [f450f2f] | 920 | #endif // VbyteDebug
|
|---|
| [211def2] | 921 | // 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).
|
|---|
| 922 | verify( n.ulink != 0p );
|
|---|
| 923 | verify( s.ulink == n.ulink );
|
|---|
| 924 | flink = n.flink;
|
|---|
| 925 | blink = &n;
|
|---|
| 926 | n.flink->blink = &s;
|
|---|
| 927 | n.flink = &s;
|
|---|
| [f450f2f] | 928 | #ifdef VbyteDebug
|
|---|
| [211def2] | 929 | {
|
|---|
| [f450f2f] | 930 | serr | "HandleList:";
|
|---|
| 931 | serr | nlOff;
|
|---|
| 932 | for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) {
|
|---|
| 933 | serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\"";
|
|---|
| [9ca5e56] | 934 | for ( i; ni->lnth ) {
|
|---|
| [f450f2f] | 935 | serr | ni->s[i];
|
|---|
| 936 | } // for
|
|---|
| 937 | serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl;
|
|---|
| 938 | } // for
|
|---|
| 939 | serr | nlOn;
|
|---|
| [211def2] | 940 | }
|
|---|
| 941 | serr | "exit:AddThisAfter";
|
|---|
| [f450f2f] | 942 | #endif // VbyteDebug
|
|---|
| 943 | } // AddThisAfter
|
|---|
| 944 |
|
|---|
| 945 |
|
|---|
| 946 | // Delete the current HandleNode node.
|
|---|
| 947 |
|
|---|
| [681e12f] | 948 | static void DeleteNode( HandleNode & s ) with(s) {
|
|---|
| [f450f2f] | 949 | #ifdef VbyteDebug
|
|---|
| [211def2] | 950 | serr | "enter:DeleteNode, s:" | &s;
|
|---|
| [f450f2f] | 951 | #endif // VbyteDebug
|
|---|
| [211def2] | 952 | flink->blink = blink;
|
|---|
| 953 | blink->flink = flink;
|
|---|
| [f450f2f] | 954 | #ifdef VbyteDebug
|
|---|
| [211def2] | 955 | serr | "exit:DeleteNode";
|
|---|
| [f450f2f] | 956 | #endif // VbyteDebug
|
|---|
| 957 | } // DeleteNode
|
|---|
| 958 |
|
|---|
| 959 |
|
|---|
| 960 | // Allocates specified storage for a string from byte-string area. If not enough space remains to perform the
|
|---|
| [6f7aff3] | 961 | // allocation, the garbage collection routine is called.
|
|---|
| [f450f2f] | 962 |
|
|---|
| [681e12f] | 963 | static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) {
|
|---|
| [f450f2f] | 964 | #ifdef VbyteDebug
|
|---|
| [211def2] | 965 | serr | "enter:VbyteAlloc, size:" | size;
|
|---|
| [f450f2f] | 966 | #endif // VbyteDebug
|
|---|
| [211def2] | 967 | uintptr_t NoBytes;
|
|---|
| 968 | char *r;
|
|---|
| [f450f2f] | 969 |
|
|---|
| [211def2] | 970 | NoBytes = ( uintptr_t )EndVbyte + size;
|
|---|
| 971 | if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ?
|
|---|
| [681e12f] | 972 | garbage( s, size ); // firer up the garbage collector
|
|---|
| [4e8df745] | 973 | verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte && "garbage run did not free up required space" );
|
|---|
| [211def2] | 974 | } // if
|
|---|
| 975 | r = EndVbyte;
|
|---|
| 976 | EndVbyte += size;
|
|---|
| [f450f2f] | 977 | #ifdef VbyteDebug
|
|---|
| [211def2] | 978 | serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
|
|---|
| [f450f2f] | 979 | #endif // VbyteDebug
|
|---|
| [211def2] | 980 | return r;
|
|---|
| [f450f2f] | 981 | } // VbyteAlloc
|
|---|
| 982 |
|
|---|
| 983 |
|
|---|
| [6f7aff3] | 984 | // Adjusts the last allocation in this heap by delta bytes, or resets this heap to be able to offer
|
|---|
| 985 | // new allocations of its original size + delta bytes. Positive delta means bigger;
|
|---|
| 986 | // negative means smaller. A null return indicates that the original heap location has room for
|
|---|
| 987 | // the requested growth. A non-null return indicates that copying to a new location is required
|
|---|
| 988 | // but has not been done; the returned value is the old heap storage location; `this` heap is
|
|---|
| 989 | // modified to reference the new location. In the copy-requred case, the caller should use
|
|---|
| 990 | // VbyteAlloc to claim the new space, while doing optimal copying from old to new, then free old.
|
|---|
| 991 |
|
|---|
| [681e12f] | 992 | static char * VbyteTryAdjustLast( VbyteHeap & s, int delta ) with(s) {
|
|---|
| [211def2] | 993 | if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) {
|
|---|
| 994 | // room available
|
|---|
| 995 | EndVbyte += delta;
|
|---|
| 996 | return 0p;
|
|---|
| 997 | }
|
|---|
| [6f7aff3] | 998 |
|
|---|
| [211def2] | 999 | char *oldBytes = StartVbyte;
|
|---|
| [6f7aff3] | 1000 |
|
|---|
| [211def2] | 1001 | NoOfExtensions += 1;
|
|---|
| 1002 | CurrSize *= 2;
|
|---|
| 1003 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
|
|---|
| 1004 | ExtVbyte = StartVbyte + CurrSize;
|
|---|
| [6f7aff3] | 1005 |
|
|---|
| [211def2] | 1006 | return oldBytes;
|
|---|
| [6f7aff3] | 1007 | }
|
|---|
| 1008 |
|
|---|
| 1009 |
|
|---|
| [f450f2f] | 1010 | // Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by
|
|---|
| 1011 | // the address in the byte string area.
|
|---|
| 1012 |
|
|---|
| [681e12f] | 1013 | static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) {
|
|---|
| [f450f2f] | 1014 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1015 | serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;
|
|---|
| [f450f2f] | 1016 | #endif // VbyteDebug
|
|---|
| [211def2] | 1017 | verify( h.ulink != 0p );
|
|---|
| 1018 | verify( s.ulink == h.ulink );
|
|---|
| 1019 | if ( s < h.s ) { // check argument values
|
|---|
| [f450f2f] | 1020 | // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:"
|
|---|
| [211def2] | 1021 | // | ( h->s ) | " and keep handles in ascending order";
|
|---|
| [f450f2f] | 1022 | // exit(-1 );
|
|---|
| [218096f] | 1023 | verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order");
|
|---|
| [211def2] | 1024 | } // if
|
|---|
| [f450f2f] | 1025 |
|
|---|
| [211def2] | 1026 | HandleNode *i;
|
|---|
| 1027 | for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h
|
|---|
| 1028 | if ( & s != i->blink ) {
|
|---|
| [681e12f] | 1029 | DeleteNode( s );
|
|---|
| 1030 | AddThisAfter( s, *i->blink );
|
|---|
| [211def2] | 1031 | } // if
|
|---|
| [f450f2f] | 1032 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1033 | {
|
|---|
| 1034 | serr | "HandleList:";
|
|---|
| 1035 | serr | nlOff;
|
|---|
| 1036 | for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {
|
|---|
| 1037 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
|---|
| 1038 | for ( i; n->lnth ) {
|
|---|
| 1039 | serr | n->s[i];
|
|---|
| 1040 | } // for
|
|---|
| 1041 | serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;
|
|---|
| 1042 | } // for
|
|---|
| 1043 | serr | nlOn;
|
|---|
| 1044 | }
|
|---|
| 1045 | serr | "exit:MoveThisAfter";
|
|---|
| [f450f2f] | 1046 | #endif // VbyteDebug
|
|---|
| 1047 | } // MoveThisAfter
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 | //######################### VbyteHeap #########################
|
|---|
| 1051 |
|
|---|
| 1052 | // Compare two byte strings in the byte-string area. The routine returns the following values:
|
|---|
| 1053 | //
|
|---|
| 1054 | // 1 => Src1-byte-string > Src2-byte-string
|
|---|
| 1055 | // 0 => Src1-byte-string = Src2-byte-string
|
|---|
| 1056 | // -1 => Src1-byte-string < Src2-byte-string
|
|---|
| 1057 |
|
|---|
| [0f781fb8] | 1058 | int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) {
|
|---|
| [f450f2f] | 1059 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1060 | serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
|
|---|
| [f450f2f] | 1061 | #endif // VbyteDebug
|
|---|
| [211def2] | 1062 | int cmp;
|
|---|
| 1063 |
|
|---|
| 1064 | CharZip: for ( int i = 0; ; i += 1 ) {
|
|---|
| 1065 | if ( i == Src2Lnth - 1 ) {
|
|---|
| 1066 | for ( ; ; i += 1 ) {
|
|---|
| 1067 | if ( i == Src1Lnth - 1 ) {
|
|---|
| 1068 | cmp = 0;
|
|---|
| 1069 | break CharZip;
|
|---|
| 1070 | } // exit
|
|---|
| 1071 | if ( Src1[Src1Start + i] != ' ') {
|
|---|
| 1072 | // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?)
|
|---|
| 1073 | cmp = 1;
|
|---|
| 1074 | break CharZip;
|
|---|
| 1075 | } // exit
|
|---|
| 1076 | } // for
|
|---|
| [f450f2f] | 1077 | } // exit
|
|---|
| [211def2] | 1078 | if ( i == Src1Lnth - 1 ) {
|
|---|
| 1079 | for ( ; ; i += 1 ) {
|
|---|
| 1080 | if ( i == Src2Lnth - 1 ) {
|
|---|
| 1081 | cmp = 0;
|
|---|
| 1082 | break CharZip;
|
|---|
| 1083 | } // exit
|
|---|
| 1084 | if ( Src2[Src2Start + i] != ' ') {
|
|---|
| 1085 | cmp = -1;
|
|---|
| 1086 | break CharZip;
|
|---|
| 1087 | } // exit
|
|---|
| 1088 | } // for
|
|---|
| [f450f2f] | 1089 | } // exit
|
|---|
| [211def2] | 1090 | if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) {
|
|---|
| 1091 | cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1;
|
|---|
| 1092 | break CharZip;
|
|---|
| [f450f2f] | 1093 | } // exit
|
|---|
| [211def2] | 1094 | } // for
|
|---|
| [f450f2f] | 1095 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1096 | serr | "exit:ByteCmp, cmp:" | cmp;
|
|---|
| [f450f2f] | 1097 | #endif // VbyteDebug
|
|---|
| [211def2] | 1098 | return cmp;
|
|---|
| [f450f2f] | 1099 | } // ByteCmp
|
|---|
| 1100 |
|
|---|
| 1101 |
|
|---|
| 1102 | // The compaction moves all of the byte strings currently in use to the beginning of the byte-string area and modifies
|
|---|
| 1103 | // the handles to reflect the new positions of the byte strings. Compaction assumes that the handle list is in ascending
|
|---|
| 1104 | // order by pointers into the byte-string area. The strings associated with substrings do not have to be moved because
|
|---|
| 1105 | // the containing string has been moved. Hence, they only require that their string pointers be adjusted.
|
|---|
| 1106 |
|
|---|
| [681e12f] | 1107 | void compaction(VbyteHeap & s) with(s) {
|
|---|
| [211def2] | 1108 | HandleNode *h;
|
|---|
| 1109 | char *obase, *nbase, *limit;
|
|---|
| 1110 |
|
|---|
| 1111 | NoOfCompactions += 1;
|
|---|
| 1112 | EndVbyte = StartVbyte;
|
|---|
| 1113 | h = Header.flink; // ignore header node
|
|---|
| 1114 | for () {
|
|---|
| [94647b0b] | 1115 | memmove( EndVbyte, h->s, h->lnth );
|
|---|
| [f450f2f] | 1116 | obase = h->s;
|
|---|
| 1117 | h->s = EndVbyte;
|
|---|
| 1118 | nbase = h->s;
|
|---|
| 1119 | EndVbyte += h->lnth;
|
|---|
| 1120 | limit = obase + h->lnth;
|
|---|
| 1121 | h = h->flink;
|
|---|
| 1122 |
|
|---|
| 1123 | // check if any substrings are allocated within a string
|
|---|
| 1124 |
|
|---|
| [9ca5e56] | 1125 | for () {
|
|---|
| [f450f2f] | 1126 | if ( h == &Header ) break; // end of header list ?
|
|---|
| 1127 | if ( h->s >= limit ) break; // outside of current string ?
|
|---|
| 1128 | h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
|
|---|
| 1129 | h = h->flink;
|
|---|
| 1130 | } // for
|
|---|
| 1131 | if ( h == &Header ) break; // end of header list ?
|
|---|
| [211def2] | 1132 | } // for
|
|---|
| [f450f2f] | 1133 | } // compaction
|
|---|
| 1134 |
|
|---|
| 1135 |
|
|---|
| [08ed947] | 1136 | static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage)
|
|---|
| [211def2] | 1137 | // probably an unreasonable default, but need to assess early-round tests on changing it
|
|---|
| [08ed947] | 1138 |
|
|---|
| 1139 | void TUNING_set_string_heap_liveness_threshold( double val ) {
|
|---|
| [211def2] | 1140 | heap_expansion_freespace_threshold = 1.0 - val;
|
|---|
| [08ed947] | 1141 | }
|
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| [f450f2f] | 1144 | // Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of
|
|---|
| 1145 | // the heap. The heap is then compacted in the existing heap or into the newly allocated heap.
|
|---|
| 1146 |
|
|---|
| [681e12f] | 1147 | void garbage(VbyteHeap & s, int minreq ) with(s) {
|
|---|
| [f450f2f] | 1148 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1149 | serr | "enter:garbage";
|
|---|
| 1150 | {
|
|---|
| [f450f2f] | 1151 | serr | "HandleList:";
|
|---|
| 1152 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
|
|---|
| 1153 | serr | nlOff;
|
|---|
| 1154 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
|---|
| [9ca5e56] | 1155 | for ( i; n->lnth ) {
|
|---|
| [f450f2f] | 1156 | serr | n->s[i];
|
|---|
| 1157 | } // for
|
|---|
| 1158 | serr | nlOn;
|
|---|
| 1159 | serr | "\" flink:" | n->flink | " blink:" | n->blink;
|
|---|
| 1160 | } // for
|
|---|
| [211def2] | 1161 | }
|
|---|
| [f450f2f] | 1162 | #endif // VbyteDebug
|
|---|
| [211def2] | 1163 | int AmountUsed, AmountFree;
|
|---|
| [f450f2f] | 1164 |
|
|---|
| [211def2] | 1165 | AmountUsed = 0;
|
|---|
| 1166 | for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used
|
|---|
| [f450f2f] | 1167 | AmountUsed += i->lnth;
|
|---|
| [211def2] | 1168 | } // for
|
|---|
| 1169 | AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;
|
|---|
| 1170 |
|
|---|
| 1171 | if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request
|
|---|
| [f450f2f] | 1172 |
|
|---|
| [681e12f] | 1173 | extend( s, max( CurrSize, minreq ) ); // extend the heap
|
|---|
| [f450f2f] | 1174 |
|
|---|
| [211def2] | 1175 | // Peter says, "This needs work before it should be used."
|
|---|
| 1176 | // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ?
|
|---|
| 1177 | // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory
|
|---|
| [f450f2f] | 1178 |
|
|---|
| [211def2] | 1179 | // `extend` implies a `compaction` during the copy
|
|---|
| [97c215f] | 1180 |
|
|---|
| [211def2] | 1181 | } else {
|
|---|
| 1182 | compaction(s); // in-place
|
|---|
| 1183 | }// if
|
|---|
| [f450f2f] | 1184 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1185 | {
|
|---|
| [f450f2f] | 1186 | serr | "HandleList:";
|
|---|
| 1187 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
|
|---|
| 1188 | serr | nlOff;
|
|---|
| 1189 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
|
|---|
| [9ca5e56] | 1190 | for ( i; n->lnth ) {
|
|---|
| [f450f2f] | 1191 | serr | n->s[i];
|
|---|
| 1192 | } // for
|
|---|
| 1193 | serr | nlOn;
|
|---|
| 1194 | serr | "\" flink:" | n->flink | " blink:" | n->blink;
|
|---|
| 1195 | } // for
|
|---|
| [211def2] | 1196 | }
|
|---|
| 1197 | serr | "exit:garbage";
|
|---|
| [f450f2f] | 1198 | #endif // VbyteDebug
|
|---|
| 1199 | } // garbage
|
|---|
| 1200 |
|
|---|
| 1201 | #undef VbyteDebug
|
|---|
| 1202 |
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
|
|---|
| 1206 | // area is deleted.
|
|---|
| 1207 |
|
|---|
| [681e12f] | 1208 | void extend( VbyteHeap & s, int size ) with (s) {
|
|---|
| [f450f2f] | 1209 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1210 | serr | "enter:extend, size:" | size;
|
|---|
| [f450f2f] | 1211 | #endif // VbyteDebug
|
|---|
| [211def2] | 1212 | char *OldStartVbyte;
|
|---|
| 1213 |
|
|---|
| 1214 | NoOfExtensions += 1;
|
|---|
| 1215 | OldStartVbyte = StartVbyte; // save previous byte area
|
|---|
| 1216 |
|
|---|
| 1217 | CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size
|
|---|
| 1218 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
|
|---|
| 1219 | ExtVbyte = (void *)( StartVbyte + CurrSize );
|
|---|
| 1220 | compaction(s); // copy from old heap to new & adjust pointers to new heap
|
|---|
| 1221 | free( OldStartVbyte ); // release old heap
|
|---|
| [f450f2f] | 1222 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1223 | serr | "exit:extend, CurrSize:" | CurrSize;
|
|---|
| [f450f2f] | 1224 | #endif // VbyteDebug
|
|---|
| 1225 | } // extend
|
|---|
| 1226 |
|
|---|
| [7b0e8b7] | 1227 | //WIP
|
|---|
| 1228 | #if 0
|
|---|
| [f450f2f] | 1229 |
|
|---|
| 1230 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
|
|---|
| 1231 | // area is deleted.
|
|---|
| 1232 |
|
|---|
| 1233 | void VbyteHeap::reduce( int size ) {
|
|---|
| 1234 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1235 | serr | "enter:reduce, size:" | size;
|
|---|
| [f450f2f] | 1236 | #endif // VbyteDebug
|
|---|
| [211def2] | 1237 | char *OldStartVbyte;
|
|---|
| 1238 |
|
|---|
| 1239 | NoOfReductions += 1;
|
|---|
| 1240 | OldStartVbyte = StartVbyte; // save previous byte area
|
|---|
| 1241 |
|
|---|
| 1242 | CurrSize -= size;
|
|---|
| 1243 | StartVbyte = EndVbyte = new char[CurrSize];
|
|---|
| 1244 | ExtVbyte = (void *)( StartVbyte + CurrSize );
|
|---|
| 1245 | compaction(); // copy from old heap to new & adjust pointers to new heap
|
|---|
| 1246 | delete OldStartVbyte; // release old heap
|
|---|
| [f450f2f] | 1247 | #ifdef VbyteDebug
|
|---|
| [211def2] | 1248 | !serr | "exit:reduce, CurrSize:" | CurrSize;
|
|---|
| [f450f2f] | 1249 | #endif // VbyteDebug
|
|---|
| 1250 | } // reduce
|
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 | #endif
|
|---|