| [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
 | 
|---|
 | 11 | // Last Modified By : Michael L. Brooks
 | 
|---|
 | 12 | // Last Modified On : Fri Sep 03 11:00:00 2021
 | 
|---|
 | 13 | // Update Count     : 1
 | 
|---|
 | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "string_res.hfa"
 | 
|---|
 | 17 | #include <stdlib.hfa>  // e.g. malloc
 | 
|---|
 | 18 | #include <string.h>    // e.g. strlen
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | //######################### VbyteHeap "header" #########################
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | 
 | 
|---|
| [6cc87c0] | 23 | 
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | // DON'T COMMIT:
 | 
|---|
 | 30 | // #define VbyteDebug
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | 
 | 
|---|
| [f450f2f] | 36 | #ifdef VbyteDebug
 | 
|---|
| [6cc87c0] | 37 | HandleNode *HeaderPtr;
 | 
|---|
| [f450f2f] | 38 | #endif // VbyteDebug
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | struct VbyteHeap {
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |     int NoOfCompactions;                                // number of compactions of the byte area
 | 
|---|
 | 43 |     int NoOfExtensions;                                 // number of extensions in the size of the byte area
 | 
|---|
 | 44 |     int NoOfReductions;                                 // number of reductions in the size of the byte area
 | 
|---|
 | 45 |     
 | 
|---|
 | 46 |     int InitSize;                                       // initial number of bytes in the byte-string area
 | 
|---|
 | 47 |     int CurrSize;                                       // current number of bytes in the byte-string area
 | 
|---|
 | 48 |     char *StartVbyte;                                   // pointer to the `st byte of the start of the byte-string area
 | 
|---|
 | 49 |     char *EndVbyte;                                     // pointer to the next byte after the end of the currently used portion of byte-string area
 | 
|---|
 | 50 |     void *ExtVbyte;                                     // pointer to the next byte after the end of the byte-string area
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |     HandleNode Header;                                  // header node for handle list
 | 
|---|
 | 53 | }; // VbyteHeap
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |     
 | 
|---|
 | 56 | static inline void compaction( VbyteHeap & );                           // compaction of the byte area
 | 
|---|
 | 57 | static inline void garbage( VbyteHeap & );                              // garbage collect the byte area
 | 
|---|
 | 58 | static inline void extend( VbyteHeap &, int );                  // extend the size of the byte area
 | 
|---|
 | 59 | static inline void reduce( VbyteHeap &, int );                  // reduce the size of the byte area
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | static inline void ?{}( VbyteHeap &, int = 1000 );
 | 
|---|
 | 62 | static inline void ^?{}( VbyteHeap & );
 | 
|---|
 | 63 | static inline void ByteCopy( VbyteHeap &, char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another
 | 
|---|
 | 64 | static inline int ByteCmp( VbyteHeap &, char *, int, int, char *, int, int );   // compare 2 blocks of bytes
 | 
|---|
 | 65 | static inline char *VbyteAlloc( VbyteHeap &, int );                     // allocate a block bytes in the heap
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | static inline void AddThisAfter( HandleNode &, HandleNode & );
 | 
|---|
 | 69 | static inline void DeleteNode( HandleNode & );
 | 
|---|
 | 70 | static inline void MoveThisAfter( HandleNode &, const HandleNode & );           // move current handle after parameter handle
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | // Allocate the storage for the variable sized area and intialize the heap variables.
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | static inline void ?{}( VbyteHeap & this, int Size ) with(this) {
 | 
|---|
 | 76 | #ifdef VbyteDebug
 | 
|---|
 | 77 |     serr | "enter:VbyteHeap::VbyteHeap, this:" | &this | " Size:" | Size;
 | 
|---|
 | 78 | #endif // VbyteDebug
 | 
|---|
 | 79 |     NoOfCompactions = NoOfExtensions = NoOfReductions = 0;
 | 
|---|
 | 80 |     InitSize = CurrSize = Size;
 | 
|---|
 | 81 |     StartVbyte = EndVbyte = alloc(CurrSize);
 | 
|---|
 | 82 |     ExtVbyte = (void *)( StartVbyte + CurrSize );
 | 
|---|
 | 83 |     Header.flink = Header.blink = &Header;
 | 
|---|
 | 84 | #ifdef VbyteDebug
 | 
|---|
 | 85 |     HeaderPtr = &Header;
 | 
|---|
 | 86 |     serr | "exit:VbyteHeap::VbyteHeap, this:" | &this;
 | 
|---|
 | 87 | #endif // VbyteDebug
 | 
|---|
 | 88 | } // VbyteHeap
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | // Release the dynamically allocated storage for the byte area.
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 | static inline void ^?{}( VbyteHeap & this ) with(this) {
 | 
|---|
 | 94 |     free( StartVbyte );
 | 
|---|
 | 95 | } // ~VbyteHeap
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | //######################### HandleNode #########################
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | // Create a handle node. The handle is not linked into the handle list.  This is the responsibilitiy of the handle
 | 
|---|
 | 102 | // creator.
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | void ?{}( HandleNode & this ) with(this) {
 | 
|---|
 | 105 | #ifdef VbyteDebug
 | 
|---|
 | 106 |     serr | "enter:HandleNode::HandleNode, this:" | &this;
 | 
|---|
 | 107 | #endif // VbyteDebug
 | 
|---|
 | 108 |     s = 0;
 | 
|---|
 | 109 |     lnth = 0;
 | 
|---|
 | 110 | #ifdef VbyteDebug
 | 
|---|
 | 111 |     serr | "exit:HandleNode::HandleNode, this:" | &this;
 | 
|---|
 | 112 | #endif // VbyteDebug
 | 
|---|
 | 113 | } // HandleNode
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | // Create a handle node. The handle is linked into the handle list at the end. This means that this handle will NOT be
 | 
|---|
 | 116 | // in order by string address, but this is not a problem because a string with length zero does nothing during garbage
 | 
|---|
 | 117 | // collection.
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | void ?{}( HandleNode & this, VbyteHeap & vh ) with(this) {
 | 
|---|
 | 120 | #ifdef VbyteDebug
 | 
|---|
 | 121 |     serr | "enter:HandleNode::HandleNode, this:" | &this;
 | 
|---|
 | 122 | #endif // VbyteDebug
 | 
|---|
 | 123 |     s = 0;
 | 
|---|
 | 124 |     lnth = 0;
 | 
|---|
 | 125 |     AddThisAfter( this, *vh.Header.blink );
 | 
|---|
 | 126 | #ifdef VbyteDebug
 | 
|---|
 | 127 |     serr | "exit:HandleNode::HandleNode, this:" | &this;
 | 
|---|
 | 128 | #endif // VbyteDebug
 | 
|---|
 | 129 | } // HandleNode
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | // Delete a node from the handle list by unchaining it from the list. If the handle node was allocated dynamically, it
 | 
|---|
 | 133 | // is the responsibility of the creator to destroy it.
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | void ^?{}( HandleNode & this ) with(this) {
 | 
|---|
 | 136 | #ifdef VbyteDebug
 | 
|---|
 | 137 |     serr | "enter:HandleNode::~HandleNode, this:" | & this;
 | 
|---|
 | 138 |     {
 | 
|---|
 | 139 |         serr | nlOff;
 | 
|---|
 | 140 |         serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";
 | 
|---|
 | 141 |         for ( int i = 0; i < lnth; i += 1 ) {
 | 
|---|
 | 142 |             serr | s[i];
 | 
|---|
 | 143 |         } // for
 | 
|---|
 | 144 |         serr | "\" flink:" | flink | " blink:" | blink | nl;
 | 
|---|
 | 145 |         serr | nlOn;
 | 
|---|
 | 146 |     }
 | 
|---|
 | 147 | #endif // VbyteDebug
 | 
|---|
 | 148 |     DeleteNode( this );
 | 
|---|
 | 149 | } // ~HandleNode
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 | //######################### String Resource #########################
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | VbyteHeap HeapArea;
 | 
|---|
 | 155 | 
 | 
|---|
| [6cc87c0] | 156 | VbyteHeap * DEBUG_string_heap = & HeapArea;
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) {
 | 
|---|
 | 159 |     return ((char*)heap->ExtVbyte) - heap->EndVbyte;
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
 | 
|---|
 | 163 |     return heap->StartVbyte;
 | 
|---|
 | 164 | }
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 | 
 | 
|---|
| [f450f2f] | 167 | // Returns the size of the string in bytes
 | 
|---|
 | 168 | size_t size(const string_res &s) with(s) {
 | 
|---|
 | 169 |     return Handle.lnth;
 | 
|---|
 | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | // Output operator
 | 
|---|
 | 173 | ofstream & ?|?(ofstream &out, const string_res &s) {
 | 
|---|
 | 174 |     // Store auto-newline state so it can be restored
 | 
|---|
 | 175 |     bool anl = getANL$(out);
 | 
|---|
 | 176 |     nlOff(out);
 | 
|---|
 | 177 |     for (size_t i = 0; i < s.Handle.lnth; i++) {
 | 
|---|
 | 178 |         out | s[i];
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 |     out | sep;
 | 
|---|
 | 181 |     // Re-apply newlines after done, for chaining version
 | 
|---|
 | 182 |     if (anl) nlOn(out);
 | 
|---|
 | 183 |     return out;
 | 
|---|
 | 184 | }
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 | void ?|?(ofstream &out, const string_res &s) {
 | 
|---|
 | 187 |     // Store auto-newline state so it can be restored
 | 
|---|
 | 188 |     bool anl = getANL$(out);
 | 
|---|
 | 189 |     nlOff(out);
 | 
|---|
 | 190 |     for (size_t i = 0; i < s.Handle.lnth; i++) {
 | 
|---|
 | 191 |         // Need to re-apply on the last output operator, for whole-statement version
 | 
|---|
 | 192 |         if (anl && i == s.Handle.lnth-1) nlOn(out);
 | 
|---|
 | 193 |         out | s[i];
 | 
|---|
 | 194 |     }
 | 
|---|
 | 195 |     return out;
 | 
|---|
 | 196 | }
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 | // Empty constructor
 | 
|---|
 | 199 | void ?{}(string_res &s) with(s) {
 | 
|---|
 | 200 |     (Handle){ HeapArea };
 | 
|---|
 | 201 |     s.shareEditSet_prev = &s;
 | 
|---|
 | 202 |     s.shareEditSet_next = &s;
 | 
|---|
 | 203 | }
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 | // Constructor from a raw buffer and size
 | 
|---|
 | 206 | void ?{}(string_res &s, const char* rhs, size_t rhslnth) with(s) {
 | 
|---|
 | 207 |     (Handle){ HeapArea };
 | 
|---|
 | 208 |     Handle.s = VbyteAlloc(HeapArea, rhslnth);
 | 
|---|
 | 209 |     Handle.lnth = rhslnth;
 | 
|---|
 | 210 |     for ( int i = 0; i < rhslnth; i += 1 ) {            // copy characters
 | 
|---|
 | 211 |         Handle.s[i] = rhs[i];
 | 
|---|
 | 212 |     } // for
 | 
|---|
 | 213 |     s.shareEditSet_prev = &s;
 | 
|---|
 | 214 |     s.shareEditSet_next = &s;
 | 
|---|
 | 215 | }
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 | // String literal constructor
 | 
|---|
 | 218 | void ?{}(string_res &s, const char* rhs) {
 | 
|---|
 | 219 |     (s){ rhs, strlen(rhs) };
 | 
|---|
 | 220 | }
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 | // General copy constructor
 | 
|---|
 | 223 | void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) {
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 |     (s.Handle){ HeapArea };
 | 
|---|
 | 226 |     s.Handle.s = s2.Handle.s + start;
 | 
|---|
 | 227 |     s.Handle.lnth = end - start;
 | 
|---|
 | 228 |     MoveThisAfter(s.Handle, s2.Handle );                        // insert this handle after rhs handle
 | 
|---|
 | 229 |     // ^ bug?  skip others at early point in string
 | 
|---|
 | 230 |     
 | 
|---|
 | 231 |     if (mode == COPY_VALUE) {
 | 
|---|
 | 232 |         // make s alone in its shareEditSet
 | 
|---|
 | 233 |         s.shareEditSet_prev = &s;
 | 
|---|
 | 234 |         s.shareEditSet_next = &s;
 | 
|---|
 | 235 |     } else {
 | 
|---|
 | 236 |         assert( mode == SHARE_EDITS );
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |         // s2 is logically const but not implementation const
 | 
|---|
 | 239 |         string_res & s2mod = (string_res &) s2;
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 |         // insert s after s2 on shareEditSet
 | 
|---|
 | 242 |         s.shareEditSet_next = s2mod.shareEditSet_next;
 | 
|---|
 | 243 |         s.shareEditSet_prev = &s2mod;
 | 
|---|
 | 244 |         s.shareEditSet_next->shareEditSet_prev = &s;
 | 
|---|
 | 245 |         s.shareEditSet_prev->shareEditSet_next = &s;
 | 
|---|
 | 246 |     }
 | 
|---|
 | 247 | }
 | 
|---|
 | 248 | 
 | 
|---|
| [d8d512e] | 249 | void assign(string_res &this, const char* buffer, size_t bsize) {
 | 
|---|
| [f450f2f] | 250 | 
 | 
|---|
| [6cc87c0] | 251 |     // traverse the incumbent share-edit set (SES) to recover the range of a base string to which `this` belongs
 | 
|---|
 | 252 |     string_res * shareEditSetStartPeer = & this;
 | 
|---|
 | 253 |     string_res * shareEditSetEndPeer = & this;
 | 
|---|
| [f450f2f] | 254 |     for (string_res * editPeer = this.shareEditSet_next; editPeer != &this; editPeer = editPeer->shareEditSet_next) {
 | 
|---|
| [6cc87c0] | 255 |         if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) {
 | 
|---|
 | 256 |             shareEditSetStartPeer = editPeer;
 | 
|---|
 | 257 |         }
 | 
|---|
 | 258 |         if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) {
 | 
|---|
 | 259 |             shareEditSetEndPeer = editPeer;
 | 
|---|
 | 260 |         }
 | 
|---|
| [f450f2f] | 261 |     }
 | 
|---|
 | 262 | 
 | 
|---|
| [6cc87c0] | 263 |     // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer
 | 
|---|
 | 264 |     // `this` occurs in the middle of it, to be replaced
 | 
|---|
 | 265 |     // build up the new text in `pasting`
 | 
|---|
 | 266 | 
 | 
|---|
 | 267 |     string_res pasting = {
 | 
|---|
 | 268 |         shareEditSetStartPeer->Handle.s,                   // start of SES
 | 
|---|
 | 269 |         this.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before this
 | 
|---|
 | 270 |     append( pasting,
 | 
|---|
 | 271 |         buffer,                                            // start of replacement for this
 | 
|---|
 | 272 |         bsize );                                           // length of replacement for this
 | 
|---|
 | 273 |     append( pasting,
 | 
|---|
 | 274 |         this.Handle.s + this.Handle.lnth,                  // start of SES after this
 | 
|---|
 | 275 |         shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth -
 | 
|---|
 | 276 |         (this.Handle.s + this.Handle.lnth) );              // length of SES, after this
 | 
|---|
 | 277 | 
 | 
|---|
 | 278 |     // The above string building can trigger compaction.
 | 
|---|
 | 279 |     // The reference points (that are arguments of the string building) may move during that building.
 | 
|---|
 | 280 |     // From this point on, they are stable.
 | 
|---|
 | 281 |     // So now, capture their values for use in the overlap cases, below.
 | 
|---|
 | 282 |     // Do not factor these definitions with the arguments used above.
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 |     char * beforeBegin = shareEditSetStartPeer->Handle.s;
 | 
|---|
 | 285 |     size_t beforeLen = this.Handle.s - beforeBegin;
 | 
|---|
| [f450f2f] | 286 | 
 | 
|---|
| [6cc87c0] | 287 |     char * afterBegin = this.Handle.s + this.Handle.lnth;
 | 
|---|
 | 288 |     size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin;
 | 
|---|
| [f450f2f] | 289 | 
 | 
|---|
 | 290 |     size_t oldLnth = this.Handle.lnth;
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 |     this.Handle.s = pasting.Handle.s + beforeLen;
 | 
|---|
| [d8d512e] | 293 |     this.Handle.lnth = bsize;
 | 
|---|
| [f450f2f] | 294 |     MoveThisAfter( this.Handle, pasting.Handle );
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 |     // adjust all substring string and handle locations, and check if any substring strings are outside the new base string
 | 
|---|
 | 297 |     char *limit = pasting.Handle.s + pasting.Handle.lnth;
 | 
|---|
 | 298 |     for (string_res * p = this.shareEditSet_next; p != &this; p = p->shareEditSet_next) {
 | 
|---|
 | 299 |         assert (p->Handle.s >= beforeBegin);
 | 
|---|
| [6cc87c0] | 300 |         if ( p->Handle.s >= afterBegin ) {
 | 
|---|
 | 301 |             assert ( p->Handle.s <= afterBegin + afterLen );
 | 
|---|
 | 302 |             assert ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
 | 
|---|
 | 303 |             // p starts after the edit
 | 
|---|
 | 304 |             // take start and end as end-anchored
 | 
|---|
 | 305 |             size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s;
 | 
|---|
 | 306 |             p->Handle.s = limit - startOffsetFromEnd;
 | 
|---|
 | 307 |             // p->Handle.lnth unaffected
 | 
|---|
 | 308 |         } else if ( p->Handle.s <= beforeBegin + beforeLen ) {
 | 
|---|
 | 309 |             // p starts before, or at the start of, the edit
 | 
|---|
 | 310 |             if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) {
 | 
|---|
| [f450f2f] | 311 |                 // p ends before the edit
 | 
|---|
 | 312 |                 // take end as start-anchored too
 | 
|---|
 | 313 |                 // p->Handle.lnth unaffected
 | 
|---|
 | 314 |             } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
 | 
|---|
| [6cc87c0] | 315 |                 // p ends during the edit; p does not include the last character replaced
 | 
|---|
| [f450f2f] | 316 |                 // clip end of p to end at start of edit
 | 
|---|
 | 317 |                 p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin );
 | 
|---|
 | 318 |             } else {
 | 
|---|
 | 319 |                 // p ends after the edit
 | 
|---|
 | 320 |                 assert ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
 | 
|---|
 | 321 |                 // take end as end-anchored
 | 
|---|
 | 322 |                 // stretch-shrink p according to the edit
 | 
|---|
 | 323 |                 p->Handle.lnth += this.Handle.lnth;
 | 
|---|
 | 324 |                 p->Handle.lnth -= oldLnth;
 | 
|---|
 | 325 |             }
 | 
|---|
 | 326 |             // take start as start-anchored
 | 
|---|
 | 327 |             size_t startOffsetFromStart = p->Handle.s - beforeBegin;
 | 
|---|
 | 328 |             p->Handle.s = pasting.Handle.s + startOffsetFromStart;
 | 
|---|
| [6cc87c0] | 329 |         } else {
 | 
|---|
 | 330 |             assert ( p->Handle.s < afterBegin );
 | 
|---|
| [f450f2f] | 331 |             // p starts during the edit
 | 
|---|
 | 332 |             assert( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen );
 | 
|---|
 | 333 |             if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
 | 
|---|
| [6cc87c0] | 334 |                 // p ends during the edit; p does not include the last character replaced
 | 
|---|
| [f450f2f] | 335 |                 // set p to empty string at start of edit
 | 
|---|
 | 336 |                 p->Handle.s = this.Handle.s;
 | 
|---|
 | 337 |                 p->Handle.lnth = 0;
 | 
|---|
 | 338 |             } else {
 | 
|---|
| [6cc87c0] | 339 |                 // p includes the end of the edit
 | 
|---|
| [f450f2f] | 340 |                 // clip start of p to start at end of edit
 | 
|---|
| [6cc87c0] | 341 |                 int charsToClip = afterBegin - p->Handle.s;
 | 
|---|
| [f450f2f] | 342 |                 p->Handle.s = this.Handle.s + this.Handle.lnth;
 | 
|---|
| [6cc87c0] | 343 |                 p->Handle.lnth -= charsToClip;
 | 
|---|
| [f450f2f] | 344 |             }
 | 
|---|
 | 345 |         }
 | 
|---|
 | 346 |         MoveThisAfter( p->Handle, pasting.Handle );     // move substring handle to maintain sorted order by string position
 | 
|---|
 | 347 |     }
 | 
|---|
 | 348 | }
 | 
|---|
 | 349 | 
 | 
|---|
| [d8d512e] | 350 | void ?=?(string_res &s, const char* other) {
 | 
|---|
 | 351 |     assign(s, other, strlen(other));
 | 
|---|
 | 352 | }
 | 
|---|
 | 353 | 
 | 
|---|
 | 354 | void ?=?(string_res &s, char other) {
 | 
|---|
 | 355 |     assign(s, &other, 1);
 | 
|---|
 | 356 | }
 | 
|---|
 | 357 | 
 | 
|---|
 | 358 | // Copy assignment operator
 | 
|---|
 | 359 | void ?=?(string_res & this, const string_res & rhs) with( this ) {
 | 
|---|
 | 360 |     assign(this, rhs.Handle.s, rhs.Handle.lnth);
 | 
|---|
 | 361 | }
 | 
|---|
 | 362 | 
 | 
|---|
| [f450f2f] | 363 | void ?=?(string_res & this, string_res & rhs) with( this ) {
 | 
|---|
 | 364 |     const string_res & rhs2 = rhs;
 | 
|---|
 | 365 |     this = rhs2;
 | 
|---|
 | 366 | }
 | 
|---|
 | 367 | 
 | 
|---|
 | 368 | 
 | 
|---|
 | 369 | // Destructor
 | 
|---|
 | 370 | void ^?{}(string_res &s) with(s) {
 | 
|---|
 | 371 |     // much delegated to implied ^VbyteSM
 | 
|---|
 | 372 | 
 | 
|---|
 | 373 |     // sever s from its share-edit peers, if any (four no-ops when already solo)
 | 
|---|
 | 374 |     s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next;
 | 
|---|
 | 375 |     s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev;
 | 
|---|
 | 376 |     s.shareEditSet_next = &s;
 | 
|---|
 | 377 |     s.shareEditSet_prev = &s;
 | 
|---|
 | 378 | }
 | 
|---|
 | 379 | 
 | 
|---|
 | 380 | 
 | 
|---|
 | 381 | // Returns the character at the given index
 | 
|---|
 | 382 | // With unicode support, this may be different from just the byte at the given
 | 
|---|
 | 383 | // offset from the start of the string.
 | 
|---|
 | 384 | char ?[?](const string_res &s, size_t index) with(s) {
 | 
|---|
 | 385 |     //TODO: Check if index is valid (no exceptions yet)
 | 
|---|
 | 386 |     return Handle.s[index];
 | 
|---|
 | 387 | }
 | 
|---|
 | 388 | 
 | 
|---|
| [d8d512e] | 389 | 
 | 
|---|
| [f450f2f] | 390 | ///////////////////////////////////////////////////////////////////
 | 
|---|
| [d8d512e] | 391 | // Concatenation
 | 
|---|
| [f450f2f] | 392 | 
 | 
|---|
| [d8d512e] | 393 | void append(string_res &str1, const char * buffer, size_t bsize) {
 | 
|---|
 | 394 |     size_t clnth = size(str1) + bsize;
 | 
|---|
 | 395 |     if ( str1.Handle.s + size(str1) == buffer ) { // already juxtapose ?
 | 
|---|
 | 396 |         // no-op
 | 
|---|
| [f450f2f] | 397 |     } else {                                            // must copy some text
 | 
|---|
 | 398 |         if ( str1.Handle.s + size(str1) == VbyteAlloc(HeapArea, 0) ) { // str1 at end of string area ?
 | 
|---|
| [d8d512e] | 399 |             VbyteAlloc(HeapArea, bsize); // create room for 2nd part at the end of string area
 | 
|---|
| [f450f2f] | 400 |         } else {                                        // copy the two parts
 | 
|---|
 | 401 |             char * str1oldBuf = str1.Handle.s;
 | 
|---|
 | 402 |             str1.Handle.s = VbyteAlloc( HeapArea, clnth );
 | 
|---|
 | 403 |             ByteCopy( HeapArea, str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth);
 | 
|---|
 | 404 |         } // if
 | 
|---|
| [d8d512e] | 405 |         ByteCopy( HeapArea, str1.Handle.s, str1.Handle.lnth, bsize, (char*)buffer, 0, (int)bsize);
 | 
|---|
| [f450f2f] | 406 |         //       VbyteHeap & this, char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth 
 | 
|---|
 | 407 |     } // if
 | 
|---|
 | 408 |     str1.Handle.lnth = clnth;
 | 
|---|
 | 409 | }
 | 
|---|
 | 410 | 
 | 
|---|
 | 411 | void ?+=?(string_res &str1, const string_res &str2) {
 | 
|---|
| [d8d512e] | 412 |     append( str1, str2.Handle.s, str2.Handle.lnth );
 | 
|---|
| [f450f2f] | 413 | }
 | 
|---|
 | 414 | 
 | 
|---|
 | 415 | void ?+=?(string_res &s, char other) {
 | 
|---|
| [d8d512e] | 416 |     append( s, &other, 1 );
 | 
|---|
| [f450f2f] | 417 | }
 | 
|---|
 | 418 | 
 | 
|---|
 | 419 | void ?+=?(string_res &s, const char* other) {
 | 
|---|
| [d8d512e] | 420 |     append( s, other, strlen(other) );
 | 
|---|
| [f450f2f] | 421 | }
 | 
|---|
 | 422 | 
 | 
|---|
 | 423 | 
 | 
|---|
 | 424 | 
 | 
|---|
 | 425 | 
 | 
|---|
 | 426 | //////////////////////////////////////////////////////////
 | 
|---|
 | 427 | // Comparisons
 | 
|---|
 | 428 | 
 | 
|---|
 | 429 | 
 | 
|---|
 | 430 | bool ?==?(const string_res &s1, const string_res &s2) {
 | 
|---|
 | 431 |     return ByteCmp( HeapArea, s1.Handle.s, 0, s1.Handle.lnth, s2.Handle.s, 0, s2.Handle.lnth) == 0;
 | 
|---|
 | 432 | }
 | 
|---|
 | 433 | 
 | 
|---|
 | 434 | bool ?!=?(const string_res &s1, const string_res &s2) {
 | 
|---|
 | 435 |     return !(s1 == s2);
 | 
|---|
 | 436 | }
 | 
|---|
 | 437 | bool ?==?(const string_res &s, const char* other) {
 | 
|---|
 | 438 |     string_res sother = other;
 | 
|---|
 | 439 |     return s == sother;
 | 
|---|
 | 440 | }
 | 
|---|
 | 441 | bool ?!=?(const string_res &s, const char* other) {
 | 
|---|
 | 442 |     return !(s == other);
 | 
|---|
 | 443 | }
 | 
|---|
 | 444 | 
 | 
|---|
 | 445 | 
 | 
|---|
 | 446 | //////////////////////////////////////////////////////////
 | 
|---|
 | 447 | // Search
 | 
|---|
 | 448 | 
 | 
|---|
 | 449 | bool contains(const string_res &s, char ch) {
 | 
|---|
 | 450 |     for (i; size(s)) {
 | 
|---|
 | 451 |         if (s[i] == ch) return true;
 | 
|---|
 | 452 |     }
 | 
|---|
 | 453 |     return false;
 | 
|---|
 | 454 | }
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 | int find(const string_res &s, char search) {
 | 
|---|
 | 457 |     for (i; size(s)) {
 | 
|---|
 | 458 |         if (s[i] == search) return i;
 | 
|---|
 | 459 |     }
 | 
|---|
 | 460 |     return size(s);
 | 
|---|
 | 461 | }
 | 
|---|
 | 462 | 
 | 
|---|
 | 463 |     /* Remaining implementations essentially ported from Sunjay's work */
 | 
|---|
 | 464 | 
 | 
|---|
 | 465 | int find(const string_res &s, const string_res &search) {
 | 
|---|
 | 466 |     return find(s, search.Handle.s, search.Handle.lnth);
 | 
|---|
 | 467 | }
 | 
|---|
 | 468 | 
 | 
|---|
 | 469 | int find(const string_res &s, const char* search) {
 | 
|---|
 | 470 |     return find(s, search, strlen(search));
 | 
|---|
 | 471 | }
 | 
|---|
 | 472 | 
 | 
|---|
 | 473 | int find(const string_res &s, const char* search, size_t searchsize) {
 | 
|---|
 | 474 |     // FIXME: This is a naive algorithm. We probably want to switch to someting
 | 
|---|
 | 475 |     // like Boyer-Moore in the future.
 | 
|---|
 | 476 |     // https://en.wikipedia.org/wiki/String_searching_algorithm
 | 
|---|
 | 477 | 
 | 
|---|
 | 478 |     // Always find the empty string
 | 
|---|
 | 479 |     if (searchsize == 0) {
 | 
|---|
 | 480 |         return 0;
 | 
|---|
 | 481 |     }
 | 
|---|
 | 482 | 
 | 
|---|
 | 483 |     for (size_t i = 0; i < s.Handle.lnth; i++) {
 | 
|---|
 | 484 |         size_t remaining = s.Handle.lnth - i;
 | 
|---|
 | 485 |         // Never going to find the search string if the remaining string is
 | 
|---|
 | 486 |         // smaller than search
 | 
|---|
 | 487 |         if (remaining < searchsize) {
 | 
|---|
 | 488 |             break;
 | 
|---|
 | 489 |         }
 | 
|---|
 | 490 | 
 | 
|---|
 | 491 |         bool matched = true;
 | 
|---|
 | 492 |         for (size_t j = 0; j < searchsize; j++) {
 | 
|---|
 | 493 |             if (search[j] != s.Handle.s[i + j]) {
 | 
|---|
 | 494 |                 matched = false;
 | 
|---|
 | 495 |                 break;
 | 
|---|
 | 496 |             }
 | 
|---|
 | 497 |         }
 | 
|---|
 | 498 |         if (matched) {
 | 
|---|
 | 499 |             return i;
 | 
|---|
 | 500 |         }
 | 
|---|
 | 501 |     }
 | 
|---|
 | 502 | 
 | 
|---|
 | 503 |     return s.Handle.lnth;
 | 
|---|
 | 504 | }
 | 
|---|
 | 505 | 
 | 
|---|
 | 506 | bool includes(const string_res &s, const string_res &search) {
 | 
|---|
 | 507 |     return includes(s, search.Handle.s, search.Handle.lnth);
 | 
|---|
 | 508 | }
 | 
|---|
 | 509 | 
 | 
|---|
 | 510 | bool includes(const string_res &s, const char* search) {
 | 
|---|
 | 511 |     return includes(s, search, strlen(search));
 | 
|---|
 | 512 | }
 | 
|---|
 | 513 | 
 | 
|---|
 | 514 | bool includes(const string_res &s, const char* search, size_t searchsize) {
 | 
|---|
 | 515 |     return find(s, search, searchsize) < s.Handle.lnth;
 | 
|---|
 | 516 | }
 | 
|---|
 | 517 | 
 | 
|---|
 | 518 | bool startsWith(const string_res &s, const string_res &prefix) {
 | 
|---|
 | 519 |     return startsWith(s, prefix.Handle.s, prefix.Handle.lnth);
 | 
|---|
 | 520 | }
 | 
|---|
 | 521 | 
 | 
|---|
 | 522 | bool startsWith(const string_res &s, const char* prefix) {
 | 
|---|
 | 523 |     return startsWith(s, prefix, strlen(prefix));
 | 
|---|
 | 524 | }
 | 
|---|
 | 525 | 
 | 
|---|
 | 526 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize) {
 | 
|---|
 | 527 |     if (s.Handle.lnth < prefixsize) {
 | 
|---|
 | 528 |         return false;
 | 
|---|
 | 529 |     }
 | 
|---|
 | 530 |     return memcmp(s.Handle.s, prefix, prefixsize) == 0;
 | 
|---|
 | 531 | }
 | 
|---|
 | 532 | 
 | 
|---|
 | 533 | bool endsWith(const string_res &s, const string_res &suffix) {
 | 
|---|
 | 534 |     return endsWith(s, suffix.Handle.s, suffix.Handle.lnth);
 | 
|---|
 | 535 | }
 | 
|---|
 | 536 | 
 | 
|---|
 | 537 | bool endsWith(const string_res &s, const char* suffix) {
 | 
|---|
 | 538 |     return endsWith(s, suffix, strlen(suffix));
 | 
|---|
 | 539 | }
 | 
|---|
 | 540 | 
 | 
|---|
 | 541 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize) {
 | 
|---|
 | 542 |     if (s.Handle.lnth < suffixsize) {
 | 
|---|
 | 543 |         return false;
 | 
|---|
 | 544 |     }
 | 
|---|
 | 545 |     // Amount to offset the bytes pointer so that we are comparing the end of s
 | 
|---|
 | 546 |     // to suffix. s.bytes + offset should be the first byte to compare against suffix
 | 
|---|
 | 547 |     size_t offset = s.Handle.lnth - suffixsize;
 | 
|---|
 | 548 |     return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0;
 | 
|---|
 | 549 | }
 | 
|---|
 | 550 | 
 | 
|---|
 | 551 |     /* Back to Mike's work */
 | 
|---|
 | 552 | 
 | 
|---|
 | 553 | 
 | 
|---|
 | 554 | ///////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 555 | // charclass, include, exclude
 | 
|---|
 | 556 | 
 | 
|---|
 | 557 | void ?{}( charclass_res & this, const string_res & chars) {
 | 
|---|
 | 558 |     (this){ chars.Handle.s, chars.Handle.lnth };
 | 
|---|
 | 559 | }
 | 
|---|
 | 560 | 
 | 
|---|
 | 561 | void ?{}( charclass_res & this, const char * chars ) {
 | 
|---|
 | 562 |     (this){ chars, strlen(chars) };
 | 
|---|
 | 563 | }
 | 
|---|
 | 564 | 
 | 
|---|
 | 565 | void ?{}( charclass_res & this, const char * chars, size_t charssize ) {
 | 
|---|
 | 566 |     (this.chars){ chars, charssize };
 | 
|---|
 | 567 |     // now sort it ?
 | 
|---|
 | 568 | }
 | 
|---|
 | 569 | 
 | 
|---|
 | 570 | void ^?{}( charclass_res & this ) {
 | 
|---|
 | 571 |     ^(this.chars){};
 | 
|---|
 | 572 | }
 | 
|---|
 | 573 | 
 | 
|---|
 | 574 | static bool test( const charclass_res & mask, char c ) {
 | 
|---|
 | 575 |     // instead, use sorted char list?
 | 
|---|
 | 576 |     return contains( mask.chars, c );
 | 
|---|
 | 577 | }
 | 
|---|
 | 578 | 
 | 
|---|
 | 579 | int exclude(const string_res &s, const charclass_res &mask) {
 | 
|---|
 | 580 |     for (int i = 0; i < size(s); i++) {
 | 
|---|
 | 581 |         if ( test(mask, s[i]) ) return i;
 | 
|---|
 | 582 |     }
 | 
|---|
 | 583 |     return size(s);
 | 
|---|
 | 584 | }
 | 
|---|
 | 585 | 
 | 
|---|
 | 586 | int include(const string_res &s, const charclass_res &mask) {
 | 
|---|
 | 587 |     for (int i = 0; i < size(s); i++) {
 | 
|---|
 | 588 |         if ( ! test(mask, s[i]) ) return i;
 | 
|---|
 | 589 |     }
 | 
|---|
 | 590 |     return size(s);
 | 
|---|
 | 591 | }
 | 
|---|
 | 592 | 
 | 
|---|
 | 593 | //######################### VbyteHeap "implementation" #########################
 | 
|---|
 | 594 | 
 | 
|---|
 | 595 | 
 | 
|---|
 | 596 | // Add a new HandleNode node n after the current HandleNode node.
 | 
|---|
 | 597 | 
 | 
|---|
 | 598 | static inline void AddThisAfter( HandleNode & this, HandleNode & n ) with(this) {
 | 
|---|
 | 599 | #ifdef VbyteDebug
 | 
|---|
 | 600 |     serr | "enter:AddThisAfter, this:" | &this | " n:" | &n;
 | 
|---|
 | 601 | #endif // VbyteDebug
 | 
|---|
 | 602 |     flink = n.flink;
 | 
|---|
 | 603 |     blink = &n;
 | 
|---|
 | 604 |     n.flink->blink = &this;
 | 
|---|
 | 605 |     n.flink = &this;
 | 
|---|
 | 606 | #ifdef VbyteDebug
 | 
|---|
 | 607 |     {
 | 
|---|
 | 608 |                 serr | "HandleList:";
 | 
|---|
 | 609 |                 serr | nlOff;
 | 
|---|
 | 610 |                 for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) {
 | 
|---|
 | 611 |                         serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\"";
 | 
|---|
 | 612 |                         for ( int i = 0; i < ni->lnth; i += 1 ) {
 | 
|---|
 | 613 |                                 serr | ni->s[i];
 | 
|---|
 | 614 |                         } // for
 | 
|---|
 | 615 |                         serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl;
 | 
|---|
 | 616 |                 } // for
 | 
|---|
 | 617 |                 serr | nlOn;
 | 
|---|
 | 618 |     }
 | 
|---|
 | 619 |     serr | "exit:AddThisAfter";
 | 
|---|
 | 620 | #endif // VbyteDebug
 | 
|---|
 | 621 | } // AddThisAfter
 | 
|---|
 | 622 | 
 | 
|---|
 | 623 | 
 | 
|---|
 | 624 | // Delete the current HandleNode node.
 | 
|---|
 | 625 | 
 | 
|---|
 | 626 | static inline void DeleteNode( HandleNode & this ) with(this) {
 | 
|---|
 | 627 | #ifdef VbyteDebug
 | 
|---|
 | 628 |     serr | "enter:DeleteNode, this:" | &this;
 | 
|---|
 | 629 | #endif // VbyteDebug
 | 
|---|
 | 630 |     flink->blink = blink;
 | 
|---|
 | 631 |     blink->flink = flink;
 | 
|---|
 | 632 | #ifdef VbyteDebug
 | 
|---|
 | 633 |     serr | "exit:DeleteNode";
 | 
|---|
 | 634 | #endif // VbyteDebug
 | 
|---|
 | 635 | } //  DeleteNode
 | 
|---|
 | 636 | 
 | 
|---|
 | 637 | 
 | 
|---|
 | 638 | 
 | 
|---|
 | 639 | // Allocates specified storage for a string from byte-string area. If not enough space remains to perform the
 | 
|---|
 | 640 | // allocation, the garbage collection routine is called and a second attempt is made to allocate the space. If the
 | 
|---|
 | 641 | // second attempt fails, a further attempt is made to create a new, larger byte-string area.
 | 
|---|
 | 642 | 
 | 
|---|
 | 643 | static inline char * VbyteAlloc( VbyteHeap & this, int size ) with(this) {
 | 
|---|
 | 644 | #ifdef VbyteDebug
 | 
|---|
 | 645 |     serr | "enter:VbyteAlloc, size:" | size;
 | 
|---|
 | 646 | #endif // VbyteDebug
 | 
|---|
 | 647 |     uintptr_t NoBytes;
 | 
|---|
 | 648 |     char *r;
 | 
|---|
 | 649 | 
 | 
|---|
 | 650 |     NoBytes = ( uintptr_t )EndVbyte + size;
 | 
|---|
 | 651 |     if ( NoBytes > ( uintptr_t )ExtVbyte ) {            // enough room for new byte-string ?
 | 
|---|
 | 652 |                 garbage( this );                                        // firer up the garbage collector
 | 
|---|
 | 653 |                 NoBytes = ( uintptr_t )EndVbyte + size;         // try again
 | 
|---|
 | 654 |                 if ( NoBytes > ( uintptr_t )ExtVbyte ) {        // enough room for new byte-string ?
 | 
|---|
 | 655 | assert( 0 && "need to implement actual growth" );
 | 
|---|
 | 656 |                         // extend( size );                              // extend the byte-string area
 | 
|---|
 | 657 |                 } // if
 | 
|---|
 | 658 |     } // if
 | 
|---|
 | 659 |     r = EndVbyte;
 | 
|---|
 | 660 |     EndVbyte += size;
 | 
|---|
 | 661 | #ifdef VbyteDebug
 | 
|---|
 | 662 |     serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
 | 
|---|
 | 663 | #endif // VbyteDebug
 | 
|---|
 | 664 |     return r;
 | 
|---|
 | 665 | } // VbyteAlloc
 | 
|---|
 | 666 | 
 | 
|---|
 | 667 | 
 | 
|---|
 | 668 | // Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by
 | 
|---|
 | 669 | // the address in the byte string area.
 | 
|---|
 | 670 | 
 | 
|---|
 | 671 | static inline void MoveThisAfter( HandleNode & this, const HandleNode  & h ) with(this) {
 | 
|---|
 | 672 | #ifdef VbyteDebug
 | 
|---|
 | 673 |     serr | "enter:MoveThisAfter, this:" | & this | " h:" | & h;
 | 
|---|
 | 674 | #endif // VbyteDebug
 | 
|---|
 | 675 |     if ( s < h.s ) {                                    // check argument values
 | 
|---|
 | 676 |                 // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:"
 | 
|---|
 | 677 |                 //      | ( h->s ) | " and keep handles in ascending order";
 | 
|---|
 | 678 |                 // exit(-1 );
 | 
|---|
 | 679 |                 assert( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order");
 | 
|---|
 | 680 |     } // if
 | 
|---|
 | 681 | 
 | 
|---|
 | 682 |     HandleNode *i;
 | 
|---|
 | 683 |     for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h
 | 
|---|
 | 684 |     if ( & this != i->blink ) {
 | 
|---|
 | 685 |                 DeleteNode( this );
 | 
|---|
 | 686 |                 AddThisAfter( this, *i->blink );
 | 
|---|
 | 687 |     } // if
 | 
|---|
 | 688 | #ifdef VbyteDebug
 | 
|---|
 | 689 |     {
 | 
|---|
 | 690 |         serr | "HandleList:";
 | 
|---|
 | 691 |         serr | nlOff;
 | 
|---|
 | 692 |         for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {
 | 
|---|
 | 693 |             serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
 | 
|---|
 | 694 |             for ( int i = 0; i < n->lnth; i += 1 ) {
 | 
|---|
 | 695 |                 serr | n->s[i];
 | 
|---|
 | 696 |             } // for
 | 
|---|
| [6cc87c0] | 697 |             serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;
 | 
|---|
| [f450f2f] | 698 |         } // for
 | 
|---|
 | 699 |         serr | nlOn;
 | 
|---|
 | 700 |     }
 | 
|---|
| [6cc87c0] | 701 |     serr | "exit:MoveThisAfter";
 | 
|---|
| [f450f2f] | 702 | #endif // VbyteDebug
 | 
|---|
 | 703 | } // MoveThisAfter
 | 
|---|
 | 704 | 
 | 
|---|
 | 705 | 
 | 
|---|
 | 706 | 
 | 
|---|
 | 707 | 
 | 
|---|
 | 708 | 
 | 
|---|
 | 709 | //######################### VbyteHeap #########################
 | 
|---|
 | 710 | 
 | 
|---|
 | 711 | // Move characters from one location in the byte-string area to another. The routine handles the following situations:
 | 
|---|
 | 712 | //
 | 
|---|
 | 713 | // if the |Src| > |Dst| => truncate
 | 
|---|
 | 714 | // if the |Dst| > |Src| => pad Dst with blanks
 | 
|---|
 | 715 | 
 | 
|---|
 | 716 | void ByteCopy( VbyteHeap & this, char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth ) {
 | 
|---|
 | 717 |     for ( int i = 0; i < DstLnth; i += 1 ) {
 | 
|---|
 | 718 |       if ( i == SrcLnth ) {                             // |Dst| > |Src|
 | 
|---|
 | 719 |             for ( ; i < DstLnth; i += 1 ) {             // pad Dst with blanks
 | 
|---|
 | 720 |                 Dst[DstStart + i] = ' ';
 | 
|---|
 | 721 |             } // for
 | 
|---|
 | 722 |             break;
 | 
|---|
 | 723 |         } // exit
 | 
|---|
 | 724 |         Dst[DstStart + i] = Src[SrcStart + i];
 | 
|---|
 | 725 |     } // for
 | 
|---|
 | 726 | } // ByteCopy
 | 
|---|
 | 727 | 
 | 
|---|
 | 728 | // Compare two byte strings in the byte-string area. The routine returns the following values:
 | 
|---|
 | 729 | //
 | 
|---|
 | 730 | // 1 => Src1-byte-string > Src2-byte-string
 | 
|---|
 | 731 | // 0 => Src1-byte-string = Src2-byte-string
 | 
|---|
 | 732 | // -1 => Src1-byte-string < Src2-byte-string
 | 
|---|
 | 733 | 
 | 
|---|
 | 734 | int ByteCmp( VbyteHeap & this, char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth )  with(this) {
 | 
|---|
 | 735 | #ifdef VbyteDebug
 | 
|---|
 | 736 |     serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
 | 
|---|
 | 737 | #endif // VbyteDebug
 | 
|---|
 | 738 |     int cmp;
 | 
|---|
 | 739 | 
 | 
|---|
 | 740 |     CharZip: for ( int i = 0; ; i += 1 ) {
 | 
|---|
 | 741 |         if ( i == Src2Lnth - 1 ) {
 | 
|---|
 | 742 |             for ( ; ; i += 1 ) {
 | 
|---|
 | 743 |                 if ( i == Src1Lnth - 1 ) {
 | 
|---|
 | 744 |                     cmp = 0;
 | 
|---|
 | 745 |                     break CharZip;
 | 
|---|
 | 746 |                 } // exit
 | 
|---|
 | 747 |                 if ( Src1[Src1Start + i] != ' ') {
 | 
|---|
 | 748 |                         // SUSPECTED BUG:  this could be be why Peter got the bug report about == " "  (why is this case here at all?)
 | 
|---|
 | 749 |                     cmp = 1;
 | 
|---|
 | 750 |                     break CharZip;
 | 
|---|
 | 751 |                 } // exit
 | 
|---|
 | 752 |             } // for
 | 
|---|
 | 753 |         } // exit
 | 
|---|
 | 754 |         if ( i == Src1Lnth - 1 ) {
 | 
|---|
 | 755 |             for ( ; ; i += 1 ) {
 | 
|---|
 | 756 |                 if ( i == Src2Lnth - 1 ) {
 | 
|---|
 | 757 |                     cmp = 0;
 | 
|---|
 | 758 |                     break CharZip;
 | 
|---|
 | 759 |                 } // exit
 | 
|---|
 | 760 |                 if ( Src2[Src2Start + i] != ' ') {
 | 
|---|
 | 761 |                     cmp = -1;
 | 
|---|
 | 762 |                     break CharZip;
 | 
|---|
 | 763 |                 } // exit
 | 
|---|
 | 764 |             } // for
 | 
|---|
 | 765 |         } // exit
 | 
|---|
 | 766 |       if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) {
 | 
|---|
 | 767 |             cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1;
 | 
|---|
 | 768 |             break CharZip;
 | 
|---|
 | 769 |         } // exit
 | 
|---|
 | 770 |     } // for
 | 
|---|
 | 771 | #ifdef VbyteDebug
 | 
|---|
 | 772 |     serr | "exit:ByteCmp, cmp:" | cmp;
 | 
|---|
 | 773 | #endif // VbyteDebug
 | 
|---|
 | 774 |     return cmp;
 | 
|---|
 | 775 | } // ByteCmp
 | 
|---|
 | 776 | 
 | 
|---|
 | 777 | 
 | 
|---|
 | 778 | // The compaction moves all of the byte strings currently in use to the beginning of the byte-string area and modifies
 | 
|---|
 | 779 | // the handles to reflect the new positions of the byte strings. Compaction assumes that the handle list is in ascending
 | 
|---|
 | 780 | // order by pointers into the byte-string area.  The strings associated with substrings do not have to be moved because
 | 
|---|
 | 781 | // the containing string has been moved. Hence, they only require that their string pointers be adjusted.
 | 
|---|
 | 782 | 
 | 
|---|
 | 783 | void compaction(VbyteHeap & this) with(this) {
 | 
|---|
 | 784 |     HandleNode *h;
 | 
|---|
 | 785 |     char *obase, *nbase, *limit;
 | 
|---|
 | 786 |     
 | 
|---|
 | 787 |     NoOfCompactions += 1;
 | 
|---|
 | 788 |     EndVbyte = StartVbyte;
 | 
|---|
 | 789 |     h = Header.flink;                                   // ignore header node
 | 
|---|
 | 790 |     for (;;) {
 | 
|---|
 | 791 |                 ByteCopy( this, EndVbyte, 0, h->lnth, h->s, 0, h->lnth );
 | 
|---|
 | 792 |                 obase = h->s;
 | 
|---|
 | 793 |                 h->s = EndVbyte;
 | 
|---|
 | 794 |                 nbase = h->s;
 | 
|---|
 | 795 |                 EndVbyte += h->lnth;
 | 
|---|
 | 796 |                 limit = obase + h->lnth;
 | 
|---|
 | 797 |                 h = h->flink;
 | 
|---|
 | 798 |                 
 | 
|---|
 | 799 |                 // check if any substrings are allocated within a string
 | 
|---|
 | 800 |                 
 | 
|---|
 | 801 |                 for (;;) {
 | 
|---|
 | 802 |                         if ( h == &Header ) break;                      // end of header list ?
 | 
|---|
 | 803 |                         if ( h->s >= limit ) break;                     // outside of current string ?
 | 
|---|
 | 804 |                         h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
 | 
|---|
 | 805 |                         h = h->flink;
 | 
|---|
 | 806 |                 } // for
 | 
|---|
 | 807 |                 if ( h == &Header ) break;                      // end of header list ?
 | 
|---|
 | 808 |     } // for
 | 
|---|
 | 809 | } // compaction
 | 
|---|
 | 810 | 
 | 
|---|
 | 811 | 
 | 
|---|
 | 812 | // Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of
 | 
|---|
 | 813 | // the heap.  The heap is then compacted in the existing heap or into the newly allocated heap.
 | 
|---|
 | 814 | 
 | 
|---|
 | 815 | void garbage(VbyteHeap & this ) with(this) {
 | 
|---|
 | 816 | #ifdef VbyteDebug
 | 
|---|
 | 817 |     serr | "enter:garbage";
 | 
|---|
 | 818 |     {
 | 
|---|
 | 819 |                 serr | "HandleList:";
 | 
|---|
 | 820 |                 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
 | 
|---|
 | 821 |                         serr | nlOff;
 | 
|---|
 | 822 |                         serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
 | 
|---|
 | 823 |                         for ( int i = 0; i < n->lnth; i += 1 ) {
 | 
|---|
 | 824 |                                 serr | n->s[i];
 | 
|---|
 | 825 |                         } // for
 | 
|---|
 | 826 |                         serr | nlOn;
 | 
|---|
 | 827 |                         serr | "\" flink:" | n->flink | " blink:" | n->blink;
 | 
|---|
 | 828 |                 } // for
 | 
|---|
 | 829 |     }
 | 
|---|
 | 830 | #endif // VbyteDebug
 | 
|---|
 | 831 |     int AmountUsed, AmountFree;
 | 
|---|
 | 832 | 
 | 
|---|
 | 833 |     AmountUsed = 0;
 | 
|---|
 | 834 |     for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used
 | 
|---|
 | 835 |                 AmountUsed += i->lnth;
 | 
|---|
 | 836 |     } // for
 | 
|---|
 | 837 |     AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;
 | 
|---|
 | 838 |     
 | 
|---|
 | 839 |     if ( AmountFree < ( int )( CurrSize * 0.1 )) {      // free space less than 10% ?
 | 
|---|
 | 840 | 
 | 
|---|
 | 841 | assert( 0 && "need to implement actual growth" );
 | 
|---|
 | 842 | //              extend( CurrSize );                             // extend the heap
 | 
|---|
 | 843 | 
 | 
|---|
 | 844 |                         //  Peter says, "This needs work before it should be used."
 | 
|---|
 | 845 |                         //  } else if ( AmountFree > CurrSize / 2 ) {           // free space greater than 3 times the initial allocation ? 
 | 
|---|
 | 846 |                         //              reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory
 | 
|---|
 | 847 | 
 | 
|---|
 | 848 |     } // if
 | 
|---|
 | 849 |     compaction(this);                                   // compact the byte area, in the same or new heap area
 | 
|---|
 | 850 | #ifdef VbyteDebug
 | 
|---|
 | 851 |     {
 | 
|---|
 | 852 |                 serr | "HandleList:";
 | 
|---|
 | 853 |                 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
 | 
|---|
 | 854 |                         serr | nlOff;
 | 
|---|
 | 855 |                         serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
 | 
|---|
 | 856 |                         for ( int i = 0; i < n->lnth; i += 1 ) {
 | 
|---|
 | 857 |                                 serr | n->s[i];
 | 
|---|
 | 858 |                         } // for
 | 
|---|
 | 859 |                         serr | nlOn;
 | 
|---|
 | 860 |                         serr | "\" flink:" | n->flink | " blink:" | n->blink;
 | 
|---|
 | 861 |                 } // for
 | 
|---|
 | 862 |     }
 | 
|---|
 | 863 |     serr | "exit:garbage";
 | 
|---|
 | 864 | #endif // VbyteDebug
 | 
|---|
 | 865 | } // garbage
 | 
|---|
 | 866 | 
 | 
|---|
 | 867 | #undef VbyteDebug
 | 
|---|
 | 868 | 
 | 
|---|
 | 869 | //WIP
 | 
|---|
 | 870 | #if 0
 | 
|---|
 | 871 | 
 | 
|---|
 | 872 | 
 | 
|---|
 | 873 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
 | 
|---|
 | 874 | // area is deleted.
 | 
|---|
 | 875 | 
 | 
|---|
 | 876 | void VbyteHeap::extend( int size ) {
 | 
|---|
 | 877 | #ifdef VbyteDebug
 | 
|---|
 | 878 |     serr | "enter:extend, size:" | size;
 | 
|---|
 | 879 | #endif // VbyteDebug
 | 
|---|
 | 880 |     char *OldStartVbyte;
 | 
|---|
 | 881 | 
 | 
|---|
 | 882 |     NoOfExtensions += 1;
 | 
|---|
 | 883 |     OldStartVbyte = StartVbyte;                         // save previous byte area
 | 
|---|
 | 884 |     
 | 
|---|
 | 885 |     CurrSize += size > InitSize ? size : InitSize;      // minimum extension, initial size
 | 
|---|
 | 886 |     StartVbyte = EndVbyte = new char[CurrSize];
 | 
|---|
 | 887 |     ExtVbyte = (void *)( StartVbyte + CurrSize );
 | 
|---|
 | 888 |     compaction();                                       // copy from old heap to new & adjust pointers to new heap
 | 
|---|
 | 889 |     delete OldStartVbyte;                               // release old heap
 | 
|---|
 | 890 | #ifdef VbyteDebug
 | 
|---|
 | 891 |     serr | "exit:extend, CurrSize:" | CurrSize;
 | 
|---|
 | 892 | #endif // VbyteDebug
 | 
|---|
 | 893 | } // extend
 | 
|---|
 | 894 | 
 | 
|---|
 | 895 | 
 | 
|---|
 | 896 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
 | 
|---|
 | 897 | // area is deleted.
 | 
|---|
 | 898 | 
 | 
|---|
 | 899 | void VbyteHeap::reduce( int size ) {
 | 
|---|
 | 900 | #ifdef VbyteDebug
 | 
|---|
 | 901 |     serr | "enter:reduce, size:" | size;
 | 
|---|
 | 902 | #endif // VbyteDebug
 | 
|---|
 | 903 |     char *OldStartVbyte;
 | 
|---|
 | 904 | 
 | 
|---|
 | 905 |     NoOfReductions += 1;
 | 
|---|
 | 906 |     OldStartVbyte = StartVbyte;                         // save previous byte area
 | 
|---|
 | 907 |     
 | 
|---|
 | 908 |     CurrSize -= size;
 | 
|---|
 | 909 |     StartVbyte = EndVbyte = new char[CurrSize];
 | 
|---|
 | 910 |     ExtVbyte = (void *)( StartVbyte + CurrSize );
 | 
|---|
 | 911 |     compaction();                                       // copy from old heap to new & adjust pointers to new heap
 | 
|---|
 | 912 |     delete  OldStartVbyte;                              // release old heap
 | 
|---|
 | 913 | #ifdef VbyteDebug
 | 
|---|
 | 914 |     serr | "exit:reduce, CurrSize:" | CurrSize;
 | 
|---|
 | 915 | #endif // VbyteDebug
 | 
|---|
 | 916 | } // reduce
 | 
|---|
 | 917 | 
 | 
|---|
 | 918 | 
 | 
|---|
 | 919 | #endif
 | 
|---|