- Timestamp:
- Feb 26, 2024, 3:53:42 AM (2 years ago)
- Branches:
- master
- Children:
- 3f9a8d0
- Parents:
- 0522ebe (diff), 022bce0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- libcfa/src
- Files:
-
- 10 edited
-
Exception.hfa (modified) (1 diff)
-
bits/signal.hfa (modified) (2 diffs)
-
collections/string.cfa (modified) (2 diffs)
-
collections/string.hfa (modified) (3 diffs)
-
collections/string_res.cfa (modified) (38 diffs)
-
collections/string_res.hfa (modified) (2 diffs)
-
fstream.cfa (modified) (9 diffs)
-
fstream.hfa (modified) (6 diffs)
-
iostream.cfa (modified) (16 diffs)
-
iostream.hfa (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/Exception.hfa
r0522ebe ra4da45e 2 2 3 3 // TEMPORARY 4 #define ExceptionDecl( name, fields... ) exception name{ fields }; __attribute__(( cfa_linkonce )) vtable( name ) name ## _vt 5 #define ExceptionInst( name, values... ) (name){ &name ## _vt, values } 4 #define ExceptionDecl( name, fields... ) exception name{ fields }; \ 5 __attribute__(( cfa_linkonce )) vtable( name ) name ## _vt 6 #define ExceptionArgs( name, args... ) &name ## _vt, args 7 #define ExceptionInst( name, args... ) (name){ ExceptionArgs( name, args ) } -
libcfa/src/bits/signal.hfa
r0522ebe ra4da45e 34 34 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; 35 35 sigemptyset( &act.sa_mask ); 36 sigaddset( &act.sa_mask, SIGALRM ); // disabled during signal handler36 sigaddset( &act.sa_mask, SIGALRM ); // disabled during signal handler 37 37 sigaddset( &act.sa_mask, SIGUSR1 ); 38 38 sigaddset( &act.sa_mask, SIGSEGV ); … … 40 40 sigaddset( &act.sa_mask, SIGILL ); 41 41 sigaddset( &act.sa_mask, SIGFPE ); 42 sigaddset( &act.sa_mask, SIGHUP ); // revert to default on second delivery42 sigaddset( &act.sa_mask, SIGHUP ); // revert to default on second delivery 43 43 sigaddset( &act.sa_mask, SIGTERM ); 44 44 sigaddset( &act.sa_mask, SIGINT ); -
libcfa/src/collections/string.cfa
r0522ebe ra4da45e 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 14 12:03:47202413 // Update Count : 2 4012 // Last Modified On : Wed Feb 7 21:17:06 2024 13 // Update Count : 259 14 14 // 15 15 … … 210 210 } 211 211 212 void ?|?( ifstream & in, string & s ) { 213 in | (*s.inner); 214 } 212 ifstream & ?|?( ifstream & is, _Istream_Squoted f ) { 213 _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } }; 214 return is | f2; 215 } // ?|? 215 216 216 217 ifstream & ?|?( ifstream & is, _Istream_Sstr f ) { 218 // _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr}; 217 219 _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f}; 218 220 return is | f2; 219 221 } // ?|? 220 221 void ?|?( ifstream & in, _Istream_Sstr f ) {222 (ifstream &)(in | f);223 }224 222 225 223 //////////////////////////////////////////////////////// -
libcfa/src/collections/string.hfa
r0522ebe ra4da45e 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 14 12:03:46202413 // Update Count : 8112 // Last Modified On : Tue Feb 6 20:59:18 2024 13 // Update Count : 118 14 14 // 15 15 … … 80 80 void ?|?(ofstream & out, const string & s); 81 81 ifstream & ?|?(ifstream & in, string & s); 82 void ?|?( ifstream & in, string & s );83 82 84 83 static inline { … … 96 95 void ?|?( ofstream & os, _Ostream_Manip(string) ); 97 96 97 struct _Istream_Swidth { 98 string & s; 99 inline _Istream_str_base; 100 }; // _Istream_Swidth 101 102 struct _Istream_Squoted { 103 _Istream_Swidth sstr; 104 }; // _Istream_Squoted 105 98 106 struct _Istream_Sstr { 99 107 string & s; 100 108 inline _Istream_str_base; 109 // _Istream_Swidth sstr; 101 110 }; // _Istream_Sstr 102 111 103 112 static inline { 104 113 // read width does not include null terminator 105 _Istream_S str wdi( unsigned int rwd, string & s ) { return (_Istream_Sstr)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; }114 _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s : s, { {.scanset : 0p}, .wd : rwd, {.flags.rwd : true} } }; } 106 115 _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { 107 return (_Istream_Sstr)@{ s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} }; 108 } 109 _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) { 110 fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; 111 } 112 _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : false}} }; } 113 _Istream_Sstr & incl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } 114 _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : true}} }; } 115 _Istream_Sstr & excl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } 116 _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ s, {{0p}, -1, {.flags.ignore : true}} }; } 117 _Istream_Sstr & ignore( _Istream_Sstr & fmt ) { fmt.flags.ignore = true; return fmt; } 116 // return (_Istream_Sstr)@{ { .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } } }; 117 return (_Istream_Sstr)@{ .s : s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } }; 118 } 119 _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) { 120 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f; 121 } 122 _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) { 123 return (_Istream_Squoted)@{ { .s : s, { {.delimiters : { Ldelimiter, Rdelimiter, '\0' }}, .wd : -1, {.flags.rwd : true} } } }; 124 } 125 _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { 126 f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; 127 return (_Istream_Squoted &)f; 128 } 129 // _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } } }; } 130 _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } }; } 131 _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; } 132 // _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } } }; } 133 _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } }; } 134 _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; } 135 // _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } } }; } 136 _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s : s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } }; } 137 _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } 138 _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; } 139 // _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; } 140 _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } 118 141 } // distribution 142 ifstream & ?|?( ifstream & is, _Istream_Squoted f ); 119 143 ifstream & ?|?( ifstream & is, _Istream_Sstr f ); 120 void ?|?( ifstream & is, _Istream_Sstr t ); 144 static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; } 121 145 122 146 // Concatenation -
libcfa/src/collections/string_res.cfa
r0522ebe ra4da45e 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 22 23:12:42 202413 // Update Count : 4312 // Last Modified On : Sat Feb 10 17:47:22 2024 13 // Update Count : 83 14 14 // 15 15 … … 25 25 26 26 #include <assert.h> 27 #include <complex.h> // creal, cimag27 #include <complex.h> // creal, cimag 28 28 29 29 //######################### VbyteHeap "header" ######################### … … 34 34 35 35 struct VbyteHeap { 36 int NoOfCompactions;// number of compactions of the byte area37 int NoOfExtensions;// number of extensions in the size of the byte area38 int NoOfReductions;// number of reductions in the size of the byte area39 40 int InitSize;// initial number of bytes in the byte-string area41 int CurrSize;// current number of bytes in the byte-string area42 char *StartVbyte;// pointer to the `st byte of the start of the byte-string area43 char *EndVbyte;// pointer to the next byte after the end of the currently used portion of byte-string area44 void *ExtVbyte;// pointer to the next byte after the end of the byte-string area45 46 HandleNode Header;// header node for handle list36 int NoOfCompactions; // number of compactions of the byte area 37 int NoOfExtensions; // number of extensions in the size of the byte area 38 int NoOfReductions; // number of reductions in the size of the byte area 39 40 int InitSize; // initial number of bytes in the byte-string area 41 int CurrSize; // current number of bytes in the byte-string area 42 char *StartVbyte; // pointer to the `st byte of the start of the byte-string area 43 char *EndVbyte; // pointer to the next byte after the end of the currently used portion of byte-string area 44 void *ExtVbyte; // pointer to the next byte after the end of the byte-string area 45 46 HandleNode Header; // header node for handle list 47 47 }; // VbyteHeap 48 48 49 50 static void compaction( VbyteHeap & ); // compaction of the byte area51 static void garbage( VbyteHeap &, int ); // garbage collect the byte area52 static void extend( VbyteHeap &, int ); // extend the size of the byte area53 static void reduce( VbyteHeap &, int ); // reduce the size of the byte area49 50 static void compaction( VbyteHeap & ); // compaction of the byte area 51 static void garbage( VbyteHeap &, int ); // garbage collect the byte area 52 static void extend( VbyteHeap &, int ); // extend the size of the byte area 53 static void reduce( VbyteHeap &, int ); // reduce the size of the byte area 54 54 55 55 static void ?{}( VbyteHeap &, size_t = 1000 ); 56 56 static void ^?{}( VbyteHeap & ); 57 57 58 static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes58 static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes 59 59 static char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap 60 60 static char *VbyteTryAdjustLast( VbyteHeap &, int ); … … 62 62 static void AddThisAfter( HandleNode &, HandleNode & ); 63 63 static void DeleteNode( HandleNode & ); 64 static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle64 static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle 65 65 66 66 … … 69 69 static void ?{}( VbyteHeap & s, size_t Size ) with(s) { 70 70 #ifdef VbyteDebug 71 serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;72 #endif // VbyteDebug 73 NoOfCompactions = NoOfExtensions = NoOfReductions = 0;74 InitSize = CurrSize = Size;75 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);76 ExtVbyte = (void *)( StartVbyte + CurrSize );77 Header.flink = Header.blink = &Header;78 Header.ulink = &s;79 #ifdef VbyteDebug 80 HeaderPtr = &Header;81 serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;71 serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size; 72 #endif // VbyteDebug 73 NoOfCompactions = NoOfExtensions = NoOfReductions = 0; 74 InitSize = CurrSize = Size; 75 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 76 ExtVbyte = (void *)( StartVbyte + CurrSize ); 77 Header.flink = Header.blink = &Header; 78 Header.ulink = &s; 79 #ifdef VbyteDebug 80 HeaderPtr = &Header; 81 serr | "exit:VbyteHeap::VbyteHeap, s:" | &s; 82 82 #endif // VbyteDebug 83 83 } // VbyteHeap … … 87 87 88 88 static void ^?{}( VbyteHeap & s ) with(s) { 89 free( StartVbyte );89 free( StartVbyte ); 90 90 } // ~VbyteHeap 91 91 … … 99 99 static void ?{}( HandleNode & s ) with(s) { 100 100 #ifdef VbyteDebug 101 serr | "enter:HandleNode::HandleNode, s:" | &s;102 #endif // VbyteDebug 103 s = 0;104 lnth = 0;105 #ifdef VbyteDebug 106 serr | "exit:HandleNode::HandleNode, s:" | &s;101 serr | "enter:HandleNode::HandleNode, s:" | &s; 102 #endif // VbyteDebug 103 s = 0; 104 lnth = 0; 105 #ifdef VbyteDebug 106 serr | "exit:HandleNode::HandleNode, s:" | &s; 107 107 #endif // VbyteDebug 108 108 } // HandleNode … … 114 114 static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) { 115 115 #ifdef VbyteDebug 116 serr | "enter:HandleNode::HandleNode, s:" | &s;117 #endif // VbyteDebug 118 s = 0;119 lnth = 0;120 ulink = &vh;121 AddThisAfter( s, *vh.Header.blink );122 #ifdef VbyteDebug 123 serr | "exit:HandleNode::HandleNode, s:" | &s;116 serr | "enter:HandleNode::HandleNode, s:" | &s; 117 #endif // VbyteDebug 118 s = 0; 119 lnth = 0; 120 ulink = &vh; 121 AddThisAfter( s, *vh.Header.blink ); 122 #ifdef VbyteDebug 123 serr | "exit:HandleNode::HandleNode, s:" | &s; 124 124 #endif // VbyteDebug 125 125 } // HandleNode … … 131 131 static void ^?{}( HandleNode & s ) with(s) { 132 132 #ifdef VbyteDebug 133 serr | "enter:HandleNode::~HandleNode, s:" | & s;134 {135 serr | nlOff;136 serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";137 for ( i; lnth ) {138 serr | s[i];139 } // for140 serr | "\" flink:" | flink | " blink:" | blink | nl;141 serr | nlOn;142 }143 #endif // VbyteDebug 144 DeleteNode( s );133 serr | "enter:HandleNode::~HandleNode, s:" | & s; 134 { 135 serr | nlOff; 136 serr | " lnth:" | lnth | " s:" | (void *)s | ",\""; 137 for ( i; lnth ) { 138 serr | s[i]; 139 } // for 140 serr | "\" flink:" | flink | " blink:" | blink | nl; 141 serr | nlOn; 142 } 143 #endif // VbyteDebug 144 DeleteNode( s ); 145 145 } // ~HandleNode 146 146 … … 148 148 //######################### String Sharing Context ######################### 149 149 150 static string_sharectx * ambient_string_sharectx; // fickle top of stack150 static string_sharectx * ambient_string_sharectx; // fickle top of stack 151 151 static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack 152 152 153 153 void ?{}( string_sharectx & s, StringSharectx_Mode mode ) with( s ) { 154 (older){ ambient_string_sharectx };155 if ( mode == NEW_SHARING ) {156 (activeHeap){ new( (size_t) 1000 ) };157 } else {158 verify( mode == NO_SHARING );159 (activeHeap){ 0p };160 }161 ambient_string_sharectx = & s;154 (older){ ambient_string_sharectx }; 155 if ( mode == NEW_SHARING ) { 156 (activeHeap){ new( (size_t) 1000 ) }; 157 } else { 158 verify( mode == NO_SHARING ); 159 (activeHeap){ 0p }; 160 } 161 ambient_string_sharectx = & s; 162 162 } 163 163 164 164 void ^?{}( string_sharectx & s ) with( s ) { 165 if ( activeHeap ) delete( activeHeap );166 167 // unlink s from older-list starting from ambient_string_sharectx168 // usually, s==ambient_string_sharectx and the loop runs zero times169 string_sharectx *& c = ambient_string_sharectx;170 while ( c != &s ) &c = &c->older;// find s171 c = s.older;// unlink165 if ( activeHeap ) delete( activeHeap ); 166 167 // unlink s from older-list starting from ambient_string_sharectx 168 // usually, s==ambient_string_sharectx and the loop runs zero times 169 string_sharectx *& c = ambient_string_sharectx; 170 while ( c != &s ) &c = &c->older; // find s 171 c = s.older; // unlink 172 172 } 173 173 … … 176 176 177 177 VbyteHeap * DEBUG_string_heap() { 178 assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );179 return ambient_string_sharectx->activeHeap;178 assert( ambient_string_sharectx->activeHeap && "No sharing context is active" ); 179 return ambient_string_sharectx->activeHeap; 180 180 } 181 181 182 182 size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) { 183 return ((char *)heap->ExtVbyte) - heap->EndVbyte;183 return ((char *)heap->ExtVbyte) - heap->EndVbyte; 184 184 } 185 185 186 186 size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) { 187 return heap->CurrSize;187 return heap->CurrSize; 188 188 } 189 189 190 190 const char * DEBUG_string_heap_start( VbyteHeap * heap ) { 191 return heap->StartVbyte;191 return heap->StartVbyte; 192 192 } 193 193 194 194 // Returns the size of the string in bytes 195 195 size_t size(const string_res & s) with(s) { 196 return Handle.lnth;196 return Handle.lnth; 197 197 } 198 198 … … 201 201 // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0. 202 202 out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl; 203 return out;203 return out; 204 204 } 205 205 … … 210 210 // Input operator 211 211 ifstream & ?|?(ifstream & in, string_res & s) { 212 // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing.213 // If s is a substring of something larger, simple assignment takes care of that case correctly.214 // But directly reading a variable amount of text into the middle of a larger context is not practical.215 string_res temp;216 217 // Read in chunks. Often, one chunk is enough. Keep the string that accumulates chunks last in the heap,218 // so available room is rest of heap. When a chunk fills the heap, force growth then take the next chunk.219 for (bool cont = true; cont; ) {220 cont = false;221 222 // Append dummy content to temp, forcing expansion when applicable (occurs always on subsequent loops)223 // length 2 ensures room for at least one real char, plus scanf/pipe-cstr's null terminator224 temp += "--";225 assert( temp.Handle.ulink->EndVbyte == temp.Handle.s + temp.Handle.lnth );// last in heap226 227 // reset, to overwrite the appended "--"228 temp.Handle.lnth -= 2;229 temp.Handle.ulink->EndVbyte -= 2;230 231 // rest of heap is available to read into232 int lenReadable = (char *)temp.Handle.ulink->ExtVbyte - temp.Handle.ulink->EndVbyte;233 assert (lenReadable >= 2);234 235 // get bytes236 try {212 // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing. 213 // If s is a substring of something larger, simple assignment takes care of that case correctly. 214 // But directly reading a variable amount of text into the middle of a larger context is not practical. 215 string_res temp; 216 217 // Read in chunks. Often, one chunk is enough. Keep the string that accumulates chunks last in the heap, 218 // so available room is rest of heap. When a chunk fills the heap, force growth then take the next chunk. 219 for (bool cont = true; cont; ) { 220 cont = false; 221 222 // Append dummy content to temp, forcing expansion when applicable (occurs always on subsequent loops) 223 // length 2 ensures room for at least one real char, plus scanf/pipe-cstr's null terminator 224 temp += "--"; 225 assert( temp.Handle.ulink->EndVbyte == temp.Handle.s + temp.Handle.lnth ); // last in heap 226 227 // reset, to overwrite the appended "--" 228 temp.Handle.lnth -= 2; 229 temp.Handle.ulink->EndVbyte -= 2; 230 231 // rest of heap is available to read into 232 int lenReadable = (char *)temp.Handle.ulink->ExtVbyte - temp.Handle.ulink->EndVbyte; 233 assert (lenReadable >= 2); 234 235 // get bytes 236 try { 237 237 *(temp.Handle.ulink->EndVbyte) = '\0'; // pre-assign empty cstring 238 in | wdi( lenReadable, temp.Handle.ulink->EndVbyte );239 } catch (cstring_length *) {240 cont = true;241 }242 int lenWasRead = strlen(temp.Handle.ulink->EndVbyte);243 244 // update metadata245 temp.Handle.lnth += lenWasRead;246 temp.Handle.ulink->EndVbyte += lenWasRead;247 }238 in | wdi( lenReadable, temp.Handle.ulink->EndVbyte ); 239 } catch (cstring_length *) { 240 cont = true; 241 } 242 int lenWasRead = strlen(temp.Handle.ulink->EndVbyte); 243 244 // update metadata 245 temp.Handle.lnth += lenWasRead; 246 temp.Handle.ulink->EndVbyte += lenWasRead; 247 } 248 248 249 249 if ( temp.Handle.lnth > 0 ) s = temp; 250 return in; 251 } 252 253 void ?|?( ifstream & in, string_res & s ) { 254 (ifstream &)(in | s); 250 return in; 251 } 252 253 ifstream & ?|?( ifstream & is, _Istream_Rquoted f ) with( f.rstr ) { 254 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 255 int args; 256 fini: { 257 char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' }; 258 int len = -1; // may not be set in fmt 259 args = fmt( is, rfmt, &len ); // remove leading whitespace and quote 260 if ( eof( is ) || len == -1 ) break fini; 261 262 // Change the remainder of the read into a getline by reseting the closing delimiter. 263 if ( delimiters[1] != '\0' ) { 264 delimiters[0] = delimiters[1]; 265 delimiters[1] = '\0'; 266 } // if 267 flags.delimiter = true; 268 return is | *(_Istream_Rstr *)&f; 269 } // fini 270 // read failed => no pattern match => set string to null 271 if ( ! flags.ignore && s != 0p && args == 0 ) s[0] = '\0'; 272 if ( args == 1 && eof( is ) ) { // data but scan ended at EOF 273 clear( is ); // => reset EOF => detect again on next read 274 } // if 275 return is; 255 276 } 256 277 … … 295 316 } // ?|? 296 317 297 void ?|?( ifstream & in, _Istream_Rstr f ) {298 (ifstream &)(in | f);299 }300 301 302 318 // Empty constructor 303 319 void ?{}(string_res & s) with(s) { 304 if( ambient_string_sharectx->activeHeap ) {305 (Handle){ * ambient_string_sharectx->activeHeap };306 (shareEditSet_owns_ulink){ false };307 verify( Handle.s == 0p && Handle.lnth == 0 );308 } else {309 (Handle){ * new( (size_t) 10 ) }; // TODO: can I lazily avoid allocating for empty string310 (shareEditSet_owns_ulink){ true };311 Handle.s = Handle.ulink->StartVbyte;312 verify( Handle.lnth == 0 );313 }314 s.shareEditSet_prev = &s;315 s.shareEditSet_next = &s;316 }320 if( ambient_string_sharectx->activeHeap ) { 321 (Handle){ * ambient_string_sharectx->activeHeap }; 322 (shareEditSet_owns_ulink){ false }; 323 verify( Handle.s == 0p && Handle.lnth == 0 ); 324 } else { 325 (Handle){ * new( (size_t) 10 ) }; // TODO: can I lazily avoid allocating for empty string 326 (shareEditSet_owns_ulink){ true }; 327 Handle.s = Handle.ulink->StartVbyte; 328 verify( Handle.lnth == 0 ); 329 } 330 s.shareEditSet_prev = &s; 331 s.shareEditSet_next = &s; 332 } 317 333 318 334 static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) { 319 if( ambient_string_sharectx->activeHeap ) {320 (Handle){ * ambient_string_sharectx->activeHeap };321 (shareEditSet_owns_ulink){ false };322 } else {323 (Handle){ * new( rhslnth ) };324 (shareEditSet_owns_ulink){ true };325 }326 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);327 Handle.lnth = rhslnth;328 memmove( Handle.s, rhs, rhslnth );329 s.shareEditSet_prev = &s;330 s.shareEditSet_next = &s;335 if( ambient_string_sharectx->activeHeap ) { 336 (Handle){ * ambient_string_sharectx->activeHeap }; 337 (shareEditSet_owns_ulink){ false }; 338 } else { 339 (Handle){ * new( rhslnth ) }; 340 (shareEditSet_owns_ulink){ true }; 341 } 342 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); 343 Handle.lnth = rhslnth; 344 memmove( Handle.s, rhs, rhslnth ); 345 s.shareEditSet_prev = &s; 346 s.shareEditSet_next = &s; 331 347 } 332 348 333 349 // Constructor from a raw buffer and size 334 350 void ?{}(string_res & s, const char * rhs, size_t rhslnth) with(s) { 335 eagerCopyCtorHelper(s, rhs, rhslnth);351 eagerCopyCtorHelper(s, rhs, rhslnth); 336 352 } 337 353 338 354 void ?{}( string_res & s, ssize_t rhs ) { 339 char buf[64];340 int len;341 snprintf( buf, sizeof(buf)-1, "%zd%n", rhs, &len );342 ( s ){ buf, len };355 char buf[64]; 356 int len; 357 snprintf( buf, sizeof(buf)-1, "%zd%n", rhs, &len ); 358 ( s ){ buf, len }; 343 359 } 344 360 void ?{}( string_res & s, size_t rhs ) { 345 char buf[64];346 int len;347 snprintf( buf, sizeof(buf)-1, "%zu%n", rhs, &len );348 ( s ){ buf, len };361 char buf[64]; 362 int len; 363 snprintf( buf, sizeof(buf)-1, "%zu%n", rhs, &len ); 364 ( s ){ buf, len }; 349 365 } 350 366 void ?{}( string_res & s, double rhs ) { 351 char buf[64];352 int len;353 snprintf( buf, sizeof(buf)-1, "%g%n", rhs, &len );354 ( s ){ buf, len };367 char buf[64]; 368 int len; 369 snprintf( buf, sizeof(buf)-1, "%g%n", rhs, &len ); 370 ( s ){ buf, len }; 355 371 } 356 372 void ?{}( string_res & s, long double rhs ) { 357 char buf[64];358 int len;359 snprintf( buf, sizeof(buf)-1, "%Lg%n", rhs, &len );360 ( s ){ buf, len };373 char buf[64]; 374 int len; 375 snprintf( buf, sizeof(buf)-1, "%Lg%n", rhs, &len ); 376 ( s ){ buf, len }; 361 377 } 362 378 void ?{}( string_res & s, double _Complex rhs ) { 363 char buf[64];364 int len;365 snprintf( buf, sizeof(buf)-1, "%g+%gi%n", creal( rhs ), cimag( rhs ), &len );366 ( s ){ buf, len };379 char buf[64]; 380 int len; 381 snprintf( buf, sizeof(buf)-1, "%g+%gi%n", creal( rhs ), cimag( rhs ), &len ); 382 ( s ){ buf, len }; 367 383 } 368 384 void ?{}( string_res & s, long double _Complex rhs ) { 369 char buf[64];370 int len;371 snprintf( buf, sizeof(buf)-1, "%Lg+%Lgi%n", creall( rhs ), cimagl( rhs ), &len );372 ( s ){ buf, len };385 char buf[64]; 386 int len; 387 snprintf( buf, sizeof(buf)-1, "%Lg+%Lgi%n", creall( rhs ), cimagl( rhs ), &len ); 388 ( s ){ buf, len }; 373 389 } 374 390 375 391 // private ctor (not in header): use specified heap (ignore ambient) and copy chars in 376 392 void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) { 377 (Handle){ heap };378 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);379 Handle.lnth = rhslnth;380 (s.shareEditSet_owns_ulink){ false };381 memmove( Handle.s, rhs, rhslnth );382 s.shareEditSet_prev = &s;383 s.shareEditSet_next = &s;393 (Handle){ heap }; 394 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); 395 Handle.lnth = rhslnth; 396 (s.shareEditSet_owns_ulink){ false }; 397 memmove( Handle.s, rhs, rhslnth ); 398 s.shareEditSet_prev = &s; 399 s.shareEditSet_next = &s; 384 400 } 385 401 … … 387 403 // General copy constructor 388 404 void ?{}(string_res & s, const string_res & s2, StrResInitMode mode, size_t start, size_t len ) { 389 390 size_t end = start + len; 391 verify( start <= end && end <= s2.Handle.lnth ); 392 393 if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) { 394 // crossing heaps (including private): copy eagerly 395 eagerCopyCtorHelper(s, s2.Handle.s + start, end - start); 396 verify(s.shareEditSet_prev == &s); 397 verify(s.shareEditSet_next == &s); 398 } else { 399 (s.Handle){}; 400 s.Handle.s = s2.Handle.s + start; 401 s.Handle.lnth = end - start; 402 s.Handle.ulink = s2.Handle.ulink; 403 404 AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle 405 // ^ bug? skip others at early point in string 406 407 if (mode == COPY_VALUE) { 408 verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap); 409 // requested logical copy in same heap: defer copy until write 410 411 (s.shareEditSet_owns_ulink){ false }; 412 413 // make s alone in its shareEditSet 414 s.shareEditSet_prev = &s; 415 s.shareEditSet_next = &s; 416 } else { 417 verify( mode == SHARE_EDITS ); 418 // sharing edits with source forces same heap as source (ignore context) 419 420 (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink }; 421 422 // s2 is logically const but not implementation const 423 string_res & s2mod = (string_res &) s2; 424 425 // insert s after s2 on shareEditSet 426 s.shareEditSet_next = s2mod.shareEditSet_next; 427 s.shareEditSet_prev = &s2mod; 428 s.shareEditSet_next->shareEditSet_prev = &s; 429 s.shareEditSet_prev->shareEditSet_next = &s; 430 } 431 } 405 size_t end = start + len; 406 verify( start <= end && end <= s2.Handle.lnth ); 407 408 if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) { 409 // crossing heaps (including private): copy eagerly 410 eagerCopyCtorHelper(s, s2.Handle.s + start, end - start); 411 verify(s.shareEditSet_prev == &s); 412 verify(s.shareEditSet_next == &s); 413 } else { 414 (s.Handle){}; 415 s.Handle.s = s2.Handle.s + start; 416 s.Handle.lnth = end - start; 417 s.Handle.ulink = s2.Handle.ulink; 418 419 AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle 420 // ^ bug? skip others at early point in string 421 422 if (mode == COPY_VALUE) { 423 verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap); 424 // requested logical copy in same heap: defer copy until write 425 426 (s.shareEditSet_owns_ulink){ false }; 427 428 // make s alone in its shareEditSet 429 s.shareEditSet_prev = &s; 430 s.shareEditSet_next = &s; 431 } else { 432 verify( mode == SHARE_EDITS ); 433 // sharing edits with source forces same heap as source (ignore context) 434 435 (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink }; 436 437 // s2 is logically const but not implementation const 438 string_res & s2mod = (string_res &) s2; 439 440 // insert s after s2 on shareEditSet 441 s.shareEditSet_next = s2mod.shareEditSet_next; 442 s.shareEditSet_prev = &s2mod; 443 s.shareEditSet_next->shareEditSet_prev = &s; 444 s.shareEditSet_prev->shareEditSet_next = &s; 445 } 446 } 432 447 } 433 448 434 449 static void assignEditSet(string_res & s, string_res * shareEditSetStartPeer, string_res * shareEditSetEndPeer, 435 char * resultSesStart,436 size_t resultSesLnth,437 HandleNode * resultPadPosition, size_t bsize ) {438 439 char * beforeBegin = shareEditSetStartPeer->Handle.s;440 size_t beforeLen = s.Handle.s - beforeBegin;441 442 char * afterBegin = s.Handle.s + s.Handle.lnth;443 size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin;444 445 size_t oldLnth = s.Handle.lnth;446 447 s.Handle.s = resultSesStart + beforeLen;448 s.Handle.lnth = bsize;449 if (resultPadPosition)450 MoveThisAfter( s.Handle, *resultPadPosition );451 452 // adjust all substring string and handle locations, and check if any substring strings are outside the new base string453 char *limit = resultSesStart + resultSesLnth;454 for ( string_res * p = s.shareEditSet_next; p != &s; p = p->shareEditSet_next ) {455 verify (p->Handle.s >= beforeBegin);456 if ( p->Handle.s >= afterBegin ) {457 verify ( p->Handle.s <= afterBegin + afterLen );458 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );459 // p starts after the edit460 // take start and end as end-anchored461 size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s;462 p->Handle.s = limit - startOffsetFromEnd;463 // p->Handle.lnth unaffected464 } else if ( p->Handle.s <= beforeBegin + beforeLen ) {465 // p starts before, or at the start of, the edit466 if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) {467 // p ends before the edit468 // take end as start-anchored too469 // p->Handle.lnth unaffected470 } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) {471 // p ends during the edit; p does not include the last character replaced472 // clip end of p to end at start of edit473 p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin );474 } else {475 // p ends after the edit476 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );477 // take end as end-anchored478 // stretch-shrink p according to the edit479 p->Handle.lnth += s.Handle.lnth;480 p->Handle.lnth -= oldLnth;481 }482 // take start as start-anchored483 size_t startOffsetFromStart = p->Handle.s - beforeBegin;484 p->Handle.s = resultSesStart + startOffsetFromStart;485 } else {486 verify ( p->Handle.s < afterBegin );487 // p starts during the edit488 verify( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen );489 if ( p->Handle.s + p->Handle.lnth < afterBegin ) {490 // p ends during the edit; p does not include the last character replaced491 // set p to empty string at start of edit492 p->Handle.s = s.Handle.s;493 p->Handle.lnth = 0;494 } else {495 // p includes the end of the edit496 // clip start of p to start at end of edit497 int charsToClip = afterBegin - p->Handle.s;498 p->Handle.s = s.Handle.s + s.Handle.lnth;499 p->Handle.lnth -= charsToClip;500 }501 }502 if (resultPadPosition)503 MoveThisAfter( p->Handle, *resultPadPosition ); // move substring handle to maintain sorted order by string position504 }450 char * resultSesStart, 451 size_t resultSesLnth, 452 HandleNode * resultPadPosition, size_t bsize ) { 453 454 char * beforeBegin = shareEditSetStartPeer->Handle.s; 455 size_t beforeLen = s.Handle.s - beforeBegin; 456 457 char * afterBegin = s.Handle.s + s.Handle.lnth; 458 size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin; 459 460 size_t oldLnth = s.Handle.lnth; 461 462 s.Handle.s = resultSesStart + beforeLen; 463 s.Handle.lnth = bsize; 464 if (resultPadPosition) 465 MoveThisAfter( s.Handle, *resultPadPosition ); 466 467 // adjust all substring string and handle locations, and check if any substring strings are outside the new base string 468 char *limit = resultSesStart + resultSesLnth; 469 for ( string_res * p = s.shareEditSet_next; p != &s; p = p->shareEditSet_next ) { 470 verify (p->Handle.s >= beforeBegin); 471 if ( p->Handle.s >= afterBegin ) { 472 verify ( p->Handle.s <= afterBegin + afterLen ); 473 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen ); 474 // p starts after the edit 475 // take start and end as end-anchored 476 size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s; 477 p->Handle.s = limit - startOffsetFromEnd; 478 // p->Handle.lnth unaffected 479 } else if ( p->Handle.s <= beforeBegin + beforeLen ) { 480 // p starts before, or at the start of, the edit 481 if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) { 482 // p ends before the edit 483 // take end as start-anchored too 484 // p->Handle.lnth unaffected 485 } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) { 486 // p ends during the edit; p does not include the last character replaced 487 // clip end of p to end at start of edit 488 p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin ); 489 } else { 490 // p ends after the edit 491 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen ); 492 // take end as end-anchored 493 // stretch-shrink p according to the edit 494 p->Handle.lnth += s.Handle.lnth; 495 p->Handle.lnth -= oldLnth; 496 } 497 // take start as start-anchored 498 size_t startOffsetFromStart = p->Handle.s - beforeBegin; 499 p->Handle.s = resultSesStart + startOffsetFromStart; 500 } else { 501 verify ( p->Handle.s < afterBegin ); 502 // p starts during the edit 503 verify( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen ); 504 if ( p->Handle.s + p->Handle.lnth < afterBegin ) { 505 // p ends during the edit; p does not include the last character replaced 506 // set p to empty string at start of edit 507 p->Handle.s = s.Handle.s; 508 p->Handle.lnth = 0; 509 } else { 510 // p includes the end of the edit 511 // clip start of p to start at end of edit 512 int charsToClip = afterBegin - p->Handle.s; 513 p->Handle.s = s.Handle.s + s.Handle.lnth; 514 p->Handle.lnth -= charsToClip; 515 } 516 } 517 if (resultPadPosition) 518 MoveThisAfter( p->Handle, *resultPadPosition ); // move substring handle to maintain sorted order by string position 519 } 505 520 } 506 521 507 522 // traverse the share-edit set (SES) to recover the range of a base string to which `s` belongs 508 523 static void locateInShareEditSet( string_res & s, string_res *& shareEditSetStartPeer, string_res *& shareEditSetEndPeer ) { 509 shareEditSetStartPeer = & s;510 shareEditSetEndPeer = & s;511 for (string_res * editPeer = s.shareEditSet_next; editPeer != &s; editPeer = editPeer->shareEditSet_next) {512 if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) {513 shareEditSetStartPeer = editPeer;514 }515 if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) {516 shareEditSetEndPeer = editPeer;517 }518 }524 shareEditSetStartPeer = & s; 525 shareEditSetEndPeer = & s; 526 for (string_res * editPeer = s.shareEditSet_next; editPeer != &s; editPeer = editPeer->shareEditSet_next) { 527 if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) { 528 shareEditSetStartPeer = editPeer; 529 } 530 if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) { 531 shareEditSetEndPeer = editPeer; 532 } 533 } 519 534 } 520 535 521 536 static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) { 522 523 string_res * shareEditSetStartPeer; 524 string_res * shareEditSetEndPeer; 525 locateInShareEditSet( s, shareEditSetStartPeer, shareEditSetEndPeer ); 526 527 verify( shareEditSetEndPeer->Handle.s >= shareEditSetStartPeer->Handle.s ); 528 size_t origEditSetLength = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - shareEditSetStartPeer->Handle.s; 529 verify( origEditSetLength >= s.Handle.lnth ); 530 531 if ( s.shareEditSet_owns_ulink ) { // assigning to private context 532 // ok to overwrite old value within LHS 533 char * prefixStartOrig = shareEditSetStartPeer->Handle.s; 534 int prefixLen = s.Handle.s - prefixStartOrig; 535 char * suffixStartOrig = s.Handle.s + s.Handle.lnth; 536 int suffixLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - suffixStartOrig; 537 538 int delta = bsize - s.Handle.lnth; 539 if ( char * oldBytes = VbyteTryAdjustLast( *s.Handle.ulink, delta ) ) { 540 // growing: copy from old to new 541 char * dest = VbyteAlloc( *s.Handle.ulink, origEditSetLength + delta ); 542 char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen); 543 destCursor += prefixLen; memcpy(destCursor, buffer , bsize ); 544 destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen); 545 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 546 dest, 547 origEditSetLength + delta, 548 0p, bsize); 549 free( oldBytes ); 550 } else { 551 // room is already allocated in-place: bubble suffix and overwite middle 552 memmove( suffixStartOrig + delta, suffixStartOrig, suffixLen ); 553 memcpy( s.Handle.s, buffer, bsize ); 554 555 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 556 shareEditSetStartPeer->Handle.s, 557 origEditSetLength + delta, 558 0p, bsize); 559 } 560 561 } else if ( // assigning to shared context 562 s.Handle.lnth == origEditSetLength && // overwriting entire run of SES 563 & valSrc && // sourcing from a managed string 564 valSrc.Handle.ulink == s.Handle.ulink ) { // sourcing from same heap 565 566 // SES's result will only use characters from the source string => reuse source 567 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 568 valSrc.Handle.s, 569 valSrc.Handle.lnth, 570 &((string_res&)valSrc).Handle, bsize); 571 572 } else { 573 // overwriting a proper substring of some string: mash characters from old and new together (copy on write) 574 // OR we are importing characters: need to copy eagerly (can't refer to source) 575 576 // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer 577 // `s` occurs in the middle of it, to be replaced 578 // build up the new text in `pasting` 579 580 string_res pasting = { 581 * s.Handle.ulink, // maintain same heap, regardless of context 582 shareEditSetStartPeer->Handle.s, // start of SES 583 s.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before s 584 append( pasting, 585 buffer, // start of replacement for s 586 bsize ); // length of replacement for s 587 append( pasting, 588 s.Handle.s + s.Handle.lnth, // start of SES after s 589 shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - 590 (s.Handle.s + s.Handle.lnth) ); // length of SES, after s 591 592 // The above string building can trigger compaction. 593 // The reference points (that are arguments of the string building) may move during that building. 594 // From s point on, they are stable. 595 596 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 597 pasting.Handle.s, 598 pasting.Handle.lnth, 599 &pasting.Handle, bsize); 600 } 601 602 return s; 537 string_res * shareEditSetStartPeer; 538 string_res * shareEditSetEndPeer; 539 locateInShareEditSet( s, shareEditSetStartPeer, shareEditSetEndPeer ); 540 541 verify( shareEditSetEndPeer->Handle.s >= shareEditSetStartPeer->Handle.s ); 542 size_t origEditSetLength = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - shareEditSetStartPeer->Handle.s; 543 verify( origEditSetLength >= s.Handle.lnth ); 544 545 if ( s.shareEditSet_owns_ulink ) { // assigning to private context 546 // ok to overwrite old value within LHS 547 char * prefixStartOrig = shareEditSetStartPeer->Handle.s; 548 int prefixLen = s.Handle.s - prefixStartOrig; 549 char * suffixStartOrig = s.Handle.s + s.Handle.lnth; 550 int suffixLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - suffixStartOrig; 551 552 int delta = bsize - s.Handle.lnth; 553 if ( char * oldBytes = VbyteTryAdjustLast( *s.Handle.ulink, delta ) ) { 554 // growing: copy from old to new 555 char * dest = VbyteAlloc( *s.Handle.ulink, origEditSetLength + delta ); 556 char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen); 557 destCursor += prefixLen; memcpy(destCursor, buffer , bsize ); 558 destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen); 559 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 560 dest, 561 origEditSetLength + delta, 562 0p, bsize); 563 free( oldBytes ); 564 } else { 565 // room is already allocated in-place: bubble suffix and overwite middle 566 memmove( suffixStartOrig + delta, suffixStartOrig, suffixLen ); 567 memcpy( s.Handle.s, buffer, bsize ); 568 569 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 570 shareEditSetStartPeer->Handle.s, 571 origEditSetLength + delta, 572 0p, bsize); 573 } 574 575 } else if ( // assigning to shared context 576 s.Handle.lnth == origEditSetLength && // overwriting entire run of SES 577 & valSrc && // sourcing from a managed string 578 valSrc.Handle.ulink == s.Handle.ulink ) { // sourcing from same heap 579 580 // SES's result will only use characters from the source string => reuse source 581 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 582 valSrc.Handle.s, 583 valSrc.Handle.lnth, 584 &((string_res&)valSrc).Handle, bsize); 585 586 } else { 587 // overwriting a proper substring of some string: mash characters from old and new together (copy on write) 588 // OR we are importing characters: need to copy eagerly (can't refer to source) 589 590 // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer 591 // `s` occurs in the middle of it, to be replaced 592 // build up the new text in `pasting` 593 594 string_res pasting = { 595 * s.Handle.ulink, // maintain same heap, regardless of context 596 shareEditSetStartPeer->Handle.s, // start of SES 597 s.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before s 598 append( pasting, 599 buffer, // start of replacement for s 600 bsize ); // length of replacement for s 601 append( pasting, 602 s.Handle.s + s.Handle.lnth, // start of SES after s 603 shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - 604 (s.Handle.s + s.Handle.lnth) ); // length of SES, after s 605 606 // The above string building can trigger compaction. 607 // The reference points (that are arguments of the string building) may move during that building. 608 // From s point on, they are stable. 609 610 assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, 611 pasting.Handle.s, 612 pasting.Handle.lnth, 613 &pasting.Handle, bsize); 614 } 615 616 return s; 603 617 } 604 618 605 619 string_res & assign(string_res & s, const string_res & src, size_t maxlen) { 606 return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p);620 return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p); 607 621 } 608 622 609 623 string_res & assign(string_res & s, const char * buffer, size_t bsize) { 610 return assign_(s, buffer, bsize, *0p);624 return assign_(s, buffer, bsize, *0p); 611 625 } 612 626 613 627 string_res & ?=?(string_res & s, char c) { 614 return assign(s, &c, 1);628 return assign(s, &c, 1); 615 629 } 616 630 617 631 string_res & ?=?( string_res & s, ssize_t rhs ) { 618 string_res rhs2 = rhs;619 s = rhs2;620 return s;632 string_res rhs2 = rhs; 633 s = rhs2; 634 return s; 621 635 } 622 636 string_res & ?=?( string_res & s, size_t rhs ) { 623 string_res rhs2 = rhs;624 s = rhs2;625 return s;637 string_res rhs2 = rhs; 638 s = rhs2; 639 return s; 626 640 } 627 641 string_res & ?=?( string_res & s, double rhs ) { 628 string_res rhs2 = rhs;629 s = rhs2;630 return s;642 string_res rhs2 = rhs; 643 s = rhs2; 644 return s; 631 645 } 632 646 string_res & ?=?( string_res & s, long double rhs ) { 633 string_res rhs2 = rhs;634 s = rhs2;635 return s;647 string_res rhs2 = rhs; 648 s = rhs2; 649 return s; 636 650 } 637 651 string_res & ?=?( string_res & s, double _Complex rhs ) { 638 string_res rhs2 = rhs;639 s = rhs2;640 return s;652 string_res rhs2 = rhs; 653 s = rhs2; 654 return s; 641 655 } 642 656 string_res & ?=?( string_res & s, long double _Complex rhs ) { 643 string_res rhs2 = rhs;644 s = rhs2;645 return s;657 string_res rhs2 = rhs; 658 s = rhs2; 659 return s; 646 660 } 647 661 648 662 // Copy assignment operator 649 663 string_res & ?=?(string_res & s, const string_res & rhs) with( s ) { 650 return assign_(s, rhs.Handle.s, rhs.Handle.lnth, rhs);664 return assign_(s, rhs.Handle.s, rhs.Handle.lnth, rhs); 651 665 } 652 666 653 667 string_res & ?=?(string_res & s, string_res & rhs) with( s ) { 654 const string_res & rhs2 = rhs;655 return s = rhs2;668 const string_res & rhs2 = rhs; 669 return s = rhs2; 656 670 } 657 671 … … 659 673 // Destructor 660 674 void ^?{}(string_res & s) with(s) { 661 // much delegated to implied ^VbyteSM662 663 // sever s from its share-edit peers, if any (four no-ops when already solo)664 s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next;665 s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev;666 // s.shareEditSet_next = &s;667 // s.shareEditSet_prev = &s;668 669 if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out670 delete( s.Handle.ulink );671 }675 // much delegated to implied ^VbyteSM 676 677 // sever s from its share-edit peers, if any (four no-ops when already solo) 678 s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next; 679 s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev; 680 // s.shareEditSet_next = &s; 681 // s.shareEditSet_prev = &s; 682 683 if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out 684 delete( s.Handle.ulink ); 685 } 672 686 } 673 687 … … 677 691 // offset from the start of the string. 678 692 char ?[?](const string_res & s, size_t index) with(s) { 679 //TODO: Check if index is valid (no exceptions yet)680 return Handle.s[index];693 //TODO: Check if index is valid (no exceptions yet) 694 return Handle.s[index]; 681 695 } 682 696 683 697 void assignAt(const string_res & s, size_t index, char val) { 684 // caution: not tested (not reachable by string-api-coverage interface)685 // equivalent form at string level is `s[index] = val`,686 // which uses the overload that returns a length-1 string687 string_res editZone = { s, SHARE_EDITS, index, 1 };688 assign(editZone, &val, 1);698 // caution: not tested (not reachable by string-api-coverage interface) 699 // equivalent form at string level is `s[index] = val`, 700 // which uses the overload that returns a length-1 string 701 string_res editZone = { s, SHARE_EDITS, index, 1 }; 702 assign(editZone, &val, 1); 689 703 } 690 704 … … 694 708 695 709 void append(string_res & str1, const char * buffer, size_t bsize) { 696 size_t clnth = str1.Handle.lnth + bsize;697 if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?698 // no-op699 } else {// must copy some text700 if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?701 VbyteAlloc( *str1.Handle.ulink, bsize );// create room for 2nd part at the end of string area702 } else {// copy the two parts703 char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth );704 char * str1oldBuf = str1.Handle.s;// must read after VbyteAlloc call in case it gs's705 str1.Handle.s = str1newBuf;706 memcpy( str1.Handle.s, str1oldBuf, str1.Handle.lnth );707 } // if708 memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize );709 } // if710 str1.Handle.lnth = clnth;710 size_t clnth = str1.Handle.lnth + bsize; 711 if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ? 712 // no-op 713 } else { // must copy some text 714 if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ? 715 VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area 716 } else { // copy the two parts 717 char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth ); 718 char * str1oldBuf = str1.Handle.s; // must read after VbyteAlloc call in case it gs's 719 str1.Handle.s = str1newBuf; 720 memcpy( str1.Handle.s, str1oldBuf, str1.Handle.lnth ); 721 } // if 722 memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize ); 723 } // if 724 str1.Handle.lnth = clnth; 711 725 } 712 726 713 727 void ?+=?(string_res & str1, const string_res & str2) { 714 append( str1, str2.Handle.s, str2.Handle.lnth );728 append( str1, str2.Handle.s, str2.Handle.lnth ); 715 729 } 716 730 717 731 void append(string_res & str1, const string_res & str2, size_t maxlen) { 718 append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) );732 append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) ); 719 733 } 720 734 721 735 void ?+=?(string_res & s, char c) { 722 append( s, & c, 1 );736 append( s, & c, 1 ); 723 737 } 724 738 void ?+=?(string_res & s, const char * c) { 725 append( s, c, strlen(c) );739 append( s, c, strlen(c) ); 726 740 } 727 741 … … 730 744 731 745 void ?*=?(string_res & s, size_t factor) { 732 string_res s2 = { s, COPY_VALUE };733 s = "";734 for (factor) s += s2;746 string_res s2 = { s, COPY_VALUE }; 747 s = ""; 748 for (factor) s += s2; 735 749 } 736 750 … … 739 753 740 754 int strcmp(const string_res & s1, const string_res & s2) { 741 // return 0;742 int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth));743 if (ans1 != 0) return ans1;744 return s1.Handle.lnth - s2.Handle.lnth;755 // return 0; 756 int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth)); 757 if (ans1 != 0) return ans1; 758 return s1.Handle.lnth - s2.Handle.lnth; 745 759 } 746 760 … … 753 767 754 768 int strcmp (const string_res & s1, const char * s2) { 755 string_res s2x = s2;756 return strcmp(s1, s2x);769 string_res s2x = s2; 770 return strcmp(s1, s2x); 757 771 } 758 772 … … 765 779 766 780 int strcmp (const char * s1, const string_res & s2) { 767 string_res s1x = s1;768 return strcmp(s1x, s2);781 string_res s1x = s1; 782 return strcmp(s1x, s2); 769 783 } 770 784 … … 777 791 778 792 779 780 793 ////////////////////////////////////////////////////////// 781 794 // Search 782 795 783 796 bool contains(const string_res & s, char ch) { 784 for ( i; size(s) ) {785 if (s[i] == ch) return true;786 }787 return false;797 for ( i; size(s) ) { 798 if (s[i] == ch) return true; 799 } 800 return false; 788 801 } 789 802 790 803 int find(const string_res & s, char search) { 791 return findFrom(s, 0, search);804 return findFrom(s, 0, search); 792 805 } 793 806 794 807 int findFrom(const string_res & s, size_t fromPos, char search) { 795 // FIXME: This paricular overload (find of single char) is optimized to use memchr.796 // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match.797 char * searchFrom = s.Handle.s + fromPos;798 size_t searchLnth = s.Handle.lnth - fromPos;799 int searchVal = search;800 char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth);801 if (foundAt == 0p) return s.Handle.lnth;802 else return foundAt - s.Handle.s;808 // FIXME: This paricular overload (find of single char) is optimized to use memchr. 809 // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match. 810 char * searchFrom = s.Handle.s + fromPos; 811 size_t searchLnth = s.Handle.lnth - fromPos; 812 int searchVal = search; 813 char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth); 814 if (foundAt == 0p) return s.Handle.lnth; 815 else return foundAt - s.Handle.s; 803 816 } 804 817 805 818 int find(const string_res & s, const string_res & search) { 806 return findFrom(s, 0, search);819 return findFrom(s, 0, search); 807 820 } 808 821 809 822 int findFrom(const string_res & s, size_t fromPos, const string_res & search) { 810 return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth);823 return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth); 811 824 } 812 825 813 826 int find(const string_res & s, const char * search) { 814 return findFrom(s, 0, search);827 return findFrom(s, 0, search); 815 828 } 816 829 int findFrom(const string_res & s, size_t fromPos, const char * search) { 817 return findFrom(s, fromPos, search, strlen(search));830 return findFrom(s, fromPos, search, strlen(search)); 818 831 } 819 832 820 833 int find(const string_res & s, const char * search, size_t searchsize) { 821 return findFrom(s, 0, search, searchsize);834 return findFrom(s, 0, search, searchsize); 822 835 } 823 836 824 837 int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize) { 825 826 /* Remaining implementations essentially ported from Sunjay's work */ 827 828 829 // FIXME: This is a naive algorithm. We probably want to switch to someting 830 // like Boyer-Moore in the future. 831 // https://en.wikipedia.org/wiki/String_searching_algorithm 832 833 // Always find the empty string 834 if (searchsize == 0) { 835 return 0; 836 } 837 838 for ( i; fromPos ~ s.Handle.lnth ) { 839 size_t remaining = s.Handle.lnth - i; 840 // Never going to find the search string if the remaining string is 841 // smaller than search 842 if (remaining < searchsize) { 843 break; 844 } 845 846 bool matched = true; 847 for ( j; searchsize ) { 848 if (search[j] != s.Handle.s[i + j]) { 849 matched = false; 850 break; 851 } 852 } 853 if (matched) { 854 return i; 855 } 856 } 857 858 return s.Handle.lnth; 838 /* Remaining implementations essentially ported from Sunjay's work */ 839 840 // FIXME: This is a naive algorithm. We probably want to switch to someting 841 // like Boyer-Moore in the future. 842 // https://en.wikipedia.org/wiki/String_searching_algorithm 843 844 // Always find the empty string 845 if (searchsize == 0) { 846 return 0; 847 } 848 849 for ( i; fromPos ~ s.Handle.lnth ) { 850 size_t remaining = s.Handle.lnth - i; 851 // Never going to find the search string if the remaining string is 852 // smaller than search 853 if (remaining < searchsize) { 854 break; 855 } 856 857 bool matched = true; 858 for ( j; searchsize ) { 859 if (search[j] != s.Handle.s[i + j]) { 860 matched = false; 861 break; 862 } 863 } 864 if (matched) { 865 return i; 866 } 867 } 868 return s.Handle.lnth; 859 869 } 860 870 861 871 bool includes(const string_res & s, const string_res & search) { 862 return includes(s, search.Handle.s, search.Handle.lnth);872 return includes(s, search.Handle.s, search.Handle.lnth); 863 873 } 864 874 865 875 bool includes(const string_res & s, const char * search) { 866 return includes(s, search, strlen(search));876 return includes(s, search, strlen(search)); 867 877 } 868 878 869 879 bool includes(const string_res & s, const char * search, size_t searchsize) { 870 return find(s, search, searchsize) < s.Handle.lnth;880 return find(s, search, searchsize) < s.Handle.lnth; 871 881 } 872 882 873 883 bool startsWith(const string_res & s, const string_res & prefix) { 874 return startsWith(s, prefix.Handle.s, prefix.Handle.lnth);884 return startsWith(s, prefix.Handle.s, prefix.Handle.lnth); 875 885 } 876 886 877 887 bool startsWith(const string_res & s, const char * prefix) { 878 return startsWith(s, prefix, strlen(prefix));888 return startsWith(s, prefix, strlen(prefix)); 879 889 } 880 890 881 891 bool startsWith(const string_res & s, const char * prefix, size_t prefixsize) { 882 if (s.Handle.lnth < prefixsize) {883 return false;884 }885 return memcmp(s.Handle.s, prefix, prefixsize) == 0;892 if (s.Handle.lnth < prefixsize) { 893 return false; 894 } 895 return memcmp(s.Handle.s, prefix, prefixsize) == 0; 886 896 } 887 897 888 898 bool endsWith(const string_res & s, const string_res & suffix) { 889 return endsWith(s, suffix.Handle.s, suffix.Handle.lnth);899 return endsWith(s, suffix.Handle.s, suffix.Handle.lnth); 890 900 } 891 901 892 902 bool endsWith(const string_res & s, const char * suffix) { 893 return endsWith(s, suffix, strlen(suffix));903 return endsWith(s, suffix, strlen(suffix)); 894 904 } 895 905 896 906 bool endsWith(const string_res & s, const char * suffix, size_t suffixsize) { 897 if (s.Handle.lnth < suffixsize) { 898 return false; 899 } 900 // Amount to offset the bytes pointer so that we are comparing the end of s 901 // to suffix. s.bytes + offset should be the first byte to compare against suffix 902 size_t offset = s.Handle.lnth - suffixsize; 903 return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0; 904 } 905 906 /* Back to Mike's work */ 907 907 if (s.Handle.lnth < suffixsize) { 908 return false; 909 } 910 // Amount to offset the bytes pointer so that we are comparing the end of s 911 // to suffix. s.bytes + offset should be the first byte to compare against suffix 912 size_t offset = s.Handle.lnth - suffixsize; 913 return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0; 914 } 915 916 /* Back to Mike's work */ 908 917 909 918 /////////////////////////////////////////////////////////////////////////// … … 911 920 912 921 void ?{}( charclass_res & s, const string_res & chars) { 913 (s){ chars.Handle.s, chars.Handle.lnth };922 (s){ chars.Handle.s, chars.Handle.lnth }; 914 923 } 915 924 916 925 void ?{}( charclass_res & s, const char * chars ) { 917 (s){ chars, strlen(chars) };926 (s){ chars, strlen(chars) }; 918 927 } 919 928 920 929 void ?{}( charclass_res & s, const char * chars, size_t charssize ) { 921 (s.chars){ chars, charssize };922 // now sort it ?930 (s.chars){ chars, charssize }; 931 // now sort it ? 923 932 } 924 933 925 934 void ^?{}( charclass_res & s ) { 926 ^(s.chars){};935 ^(s.chars){}; 927 936 } 928 937 929 938 static bool test( const charclass_res & mask, char c ) { 930 // instead, use sorted char list?931 return contains( mask.chars, c );939 // instead, use sorted char list? 940 return contains( mask.chars, c ); 932 941 } 933 942 934 943 int exclude(const string_res & s, const charclass_res & mask) { 935 for ( i; size(s) ) {936 if ( test(mask, s[i]) ) return i;937 }938 return size(s);944 for ( i; size(s) ) { 945 if ( test(mask, s[i]) ) return i; 946 } 947 return size(s); 939 948 } 940 949 941 950 int include(const string_res & s, const charclass_res & mask) { 942 for ( i; size(s) ) {943 if ( ! test(mask, s[i]) ) return i;944 }945 return size(s);951 for ( i; size(s) ) { 952 if ( ! test(mask, s[i]) ) return i; 953 } 954 return size(s); 946 955 } 947 956 … … 953 962 static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) { 954 963 #ifdef VbyteDebug 955 serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;956 #endif // VbyteDebug 957 // 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).958 verify( n.ulink != 0p );959 verify( s.ulink == n.ulink );960 flink = n.flink;961 blink = &n;962 n.flink->blink = &s;963 n.flink = &s;964 #ifdef VbyteDebug 965 {964 serr | "enter:AddThisAfter, s:" | &s | " n:" | &n; 965 #endif // VbyteDebug 966 // 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). 967 verify( n.ulink != 0p ); 968 verify( s.ulink == n.ulink ); 969 flink = n.flink; 970 blink = &n; 971 n.flink->blink = &s; 972 n.flink = &s; 973 #ifdef VbyteDebug 974 { 966 975 serr | "HandleList:"; 967 976 serr | nlOff; … … 974 983 } // for 975 984 serr | nlOn; 976 }977 serr | "exit:AddThisAfter";985 } 986 serr | "exit:AddThisAfter"; 978 987 #endif // VbyteDebug 979 988 } // AddThisAfter … … 984 993 static void DeleteNode( HandleNode & s ) with(s) { 985 994 #ifdef VbyteDebug 986 serr | "enter:DeleteNode, s:" | &s;987 #endif // VbyteDebug 988 flink->blink = blink;989 blink->flink = flink;990 #ifdef VbyteDebug 991 serr | "exit:DeleteNode";995 serr | "enter:DeleteNode, s:" | &s; 996 #endif // VbyteDebug 997 flink->blink = blink; 998 blink->flink = flink; 999 #ifdef VbyteDebug 1000 serr | "exit:DeleteNode"; 992 1001 #endif // VbyteDebug 993 1002 } // DeleteNode … … 999 1008 static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) { 1000 1009 #ifdef VbyteDebug 1001 serr | "enter:VbyteAlloc, size:" | size;1002 #endif // VbyteDebug 1003 uintptr_t NoBytes;1004 char *r;1005 1006 NoBytes = ( uintptr_t )EndVbyte + size;1007 if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ?1010 serr | "enter:VbyteAlloc, size:" | size; 1011 #endif // VbyteDebug 1012 uintptr_t NoBytes; 1013 char *r; 1014 1015 NoBytes = ( uintptr_t )EndVbyte + size; 1016 if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ? 1008 1017 garbage( s, size ); // firer up the garbage collector 1009 1018 verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte && "garbage run did not free up required space" ); 1010 } // if1011 r = EndVbyte;1012 EndVbyte += size;1013 #ifdef VbyteDebug 1014 serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;1015 #endif // VbyteDebug 1016 return r;1019 } // if 1020 r = EndVbyte; 1021 EndVbyte += size; 1022 #ifdef VbyteDebug 1023 serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte; 1024 #endif // VbyteDebug 1025 return r; 1017 1026 } // VbyteAlloc 1018 1027 … … 1027 1036 1028 1037 static char * VbyteTryAdjustLast( VbyteHeap & s, int delta ) with(s) { 1029 if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) {1030 // room available1031 EndVbyte += delta;1032 return 0p;1033 }1034 1035 char *oldBytes = StartVbyte;1036 1037 NoOfExtensions += 1;1038 CurrSize *= 2;1039 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);1040 ExtVbyte = StartVbyte + CurrSize;1041 1042 return oldBytes;1038 if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) { 1039 // room available 1040 EndVbyte += delta; 1041 return 0p; 1042 } 1043 1044 char *oldBytes = StartVbyte; 1045 1046 NoOfExtensions += 1; 1047 CurrSize *= 2; 1048 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 1049 ExtVbyte = StartVbyte + CurrSize; 1050 1051 return oldBytes; 1043 1052 } 1044 1053 … … 1049 1058 static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) { 1050 1059 #ifdef VbyteDebug 1051 serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;1052 #endif // VbyteDebug 1053 verify( h.ulink != 0p );1054 verify( s.ulink == h.ulink );1055 if ( s < h.s ) { // check argument values1060 serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h; 1061 #endif // VbyteDebug 1062 verify( h.ulink != 0p ); 1063 verify( s.ulink == h.ulink ); 1064 if ( s < h.s ) { // check argument values 1056 1065 // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:" 1057 // | ( h->s ) | " and keep handles in ascending order";1066 // | ( h->s ) | " and keep handles in ascending order"; 1058 1067 // exit(-1 ); 1059 1068 verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order"); 1060 } // if1061 1062 HandleNode *i;1063 for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h1064 if ( & s != i->blink ) {1069 } // if 1070 1071 HandleNode *i; 1072 for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h 1073 if ( & s != i->blink ) { 1065 1074 DeleteNode( s ); 1066 1075 AddThisAfter( s, *i->blink ); 1067 } // if1068 #ifdef VbyteDebug 1069 {1070 serr | "HandleList:";1071 serr | nlOff;1072 for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {1073 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";1074 for ( i; n->lnth ) {1075 serr | n->s[i];1076 } // for1077 serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;1078 } // for1079 serr | nlOn;1080 }1081 serr | "exit:MoveThisAfter";1076 } // if 1077 #ifdef VbyteDebug 1078 { 1079 serr | "HandleList:"; 1080 serr | nlOff; 1081 for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) { 1082 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; 1083 for ( i; n->lnth ) { 1084 serr | n->s[i]; 1085 } // for 1086 serr | "\" flink:" | n->flink | " blink:" | n->blink | nl; 1087 } // for 1088 serr | nlOn; 1089 } 1090 serr | "exit:MoveThisAfter"; 1082 1091 #endif // VbyteDebug 1083 1092 } // MoveThisAfter 1084 1085 1086 1087 1093 1088 1094 … … 1097 1103 int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) { 1098 1104 #ifdef VbyteDebug 1099 serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth; 1100 #endif // VbyteDebug 1101 int cmp; 1102 1103 CharZip: for ( int i = 0; ; i += 1 ) { 1104 if ( i == Src2Lnth - 1 ) { 1105 for ( ; ; i += 1 ) { 1105 serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth; 1106 #endif // VbyteDebug 1107 int cmp; 1108 1109 CharZip: for ( int i = 0; ; i += 1 ) { 1110 if ( i == Src2Lnth - 1 ) { 1111 for ( ; ; i += 1 ) { 1112 if ( i == Src1Lnth - 1 ) { 1113 cmp = 0; 1114 break CharZip; 1115 } // exit 1116 if ( Src1[Src1Start + i] != ' ') { 1117 // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?) 1118 cmp = 1; 1119 break CharZip; 1120 } // exit 1121 } // for 1122 } // exit 1106 1123 if ( i == Src1Lnth - 1 ) { 1107 cmp = 0; 1108 break CharZip; 1124 for ( ; ; i += 1 ) { 1125 if ( i == Src2Lnth - 1 ) { 1126 cmp = 0; 1127 break CharZip; 1128 } // exit 1129 if ( Src2[Src2Start + i] != ' ') { 1130 cmp = -1; 1131 break CharZip; 1132 } // exit 1133 } // for 1109 1134 } // exit 1110 if ( Src1[Src1Start + i] != ' ') { 1111 // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?) 1112 cmp = 1; 1113 break CharZip; 1135 if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) { 1136 cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1; 1137 break CharZip; 1114 1138 } // exit 1115 } // for 1116 } // exit 1117 if ( i == Src1Lnth - 1 ) { 1118 for ( ; ; i += 1 ) { 1119 if ( i == Src2Lnth - 1 ) { 1120 cmp = 0; 1121 break CharZip; 1122 } // exit 1123 if ( Src2[Src2Start + i] != ' ') { 1124 cmp = -1; 1125 break CharZip; 1126 } // exit 1127 } // for 1128 } // exit 1129 if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) { 1130 cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1; 1131 break CharZip; 1132 } // exit 1133 } // for 1134 #ifdef VbyteDebug 1135 serr | "exit:ByteCmp, cmp:" | cmp; 1136 #endif // VbyteDebug 1137 return cmp; 1139 } // for 1140 #ifdef VbyteDebug 1141 serr | "exit:ByteCmp, cmp:" | cmp; 1142 #endif // VbyteDebug 1143 return cmp; 1138 1144 } // ByteCmp 1139 1145 … … 1145 1151 1146 1152 void compaction(VbyteHeap & s) with(s) { 1147 HandleNode *h;1148 char *obase, *nbase, *limit;1149 1150 NoOfCompactions += 1;1151 EndVbyte = StartVbyte;1152 h = Header.flink; // ignore header node1153 for () {1153 HandleNode *h; 1154 char *obase, *nbase, *limit; 1155 1156 NoOfCompactions += 1; 1157 EndVbyte = StartVbyte; 1158 h = Header.flink; // ignore header node 1159 for () { 1154 1160 memmove( EndVbyte, h->s, h->lnth ); 1155 1161 obase = h->s; … … 1169 1175 } // for 1170 1176 if ( h == &Header ) break; // end of header list ? 1171 } // for1177 } // for 1172 1178 } // compaction 1173 1179 1174 1180 1175 1181 static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage) 1176 // probably an unreasonable default, but need to assess early-round tests on changing it1182 // probably an unreasonable default, but need to assess early-round tests on changing it 1177 1183 1178 1184 void TUNING_set_string_heap_liveness_threshold( double val ) { 1179 heap_expansion_freespace_threshold = 1.0 - val;1185 heap_expansion_freespace_threshold = 1.0 - val; 1180 1186 } 1181 1187 … … 1186 1192 void garbage(VbyteHeap & s, int minreq ) with(s) { 1187 1193 #ifdef VbyteDebug 1188 serr | "enter:garbage";1189 {1194 serr | "enter:garbage"; 1195 { 1190 1196 serr | "HandleList:"; 1191 1197 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { … … 1198 1204 serr | "\" flink:" | n->flink | " blink:" | n->blink; 1199 1205 } // for 1200 }1201 #endif // VbyteDebug 1202 int AmountUsed, AmountFree;1203 1204 AmountUsed = 0;1205 for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used1206 } 1207 #endif // VbyteDebug 1208 int AmountUsed, AmountFree; 1209 1210 AmountUsed = 0; 1211 for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used 1206 1212 AmountUsed += i->lnth; 1207 } // for1208 AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;1209 1210 if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request1213 } // for 1214 AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed; 1215 1216 if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request 1211 1217 1212 1218 extend( s, max( CurrSize, minreq ) ); // extend the heap 1213 1219 1214 // Peter says, "This needs work before it should be used."1215 // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ?1216 // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory1217 1218 // `extend` implies a `compaction` during the copy1219 1220 } else {1221 compaction(s); // in-place1222 }// if1223 #ifdef VbyteDebug 1224 {1220 // Peter says, "This needs work before it should be used." 1221 // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ? 1222 // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory 1223 1224 // `extend` implies a `compaction` during the copy 1225 1226 } else { 1227 compaction(s); // in-place 1228 }// if 1229 #ifdef VbyteDebug 1230 { 1225 1231 serr | "HandleList:"; 1226 1232 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { … … 1233 1239 serr | "\" flink:" | n->flink | " blink:" | n->blink; 1234 1240 } // for 1235 }1236 serr | "exit:garbage";1241 } 1242 serr | "exit:garbage"; 1237 1243 #endif // VbyteDebug 1238 1244 } // garbage … … 1247 1253 void extend( VbyteHeap & s, int size ) with (s) { 1248 1254 #ifdef VbyteDebug 1249 serr | "enter:extend, size:" | size;1250 #endif // VbyteDebug 1251 char *OldStartVbyte;1252 1253 NoOfExtensions += 1;1254 OldStartVbyte = StartVbyte; // save previous byte area1255 1256 CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size1257 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);1258 ExtVbyte = (void *)( StartVbyte + CurrSize );1259 compaction(s); // copy from old heap to new & adjust pointers to new heap1260 free( OldStartVbyte ); // release old heap1261 #ifdef VbyteDebug 1262 serr | "exit:extend, CurrSize:" | CurrSize;1255 serr | "enter:extend, size:" | size; 1256 #endif // VbyteDebug 1257 char *OldStartVbyte; 1258 1259 NoOfExtensions += 1; 1260 OldStartVbyte = StartVbyte; // save previous byte area 1261 1262 CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size 1263 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 1264 ExtVbyte = (void *)( StartVbyte + CurrSize ); 1265 compaction(s); // copy from old heap to new & adjust pointers to new heap 1266 free( OldStartVbyte ); // release old heap 1267 #ifdef VbyteDebug 1268 serr | "exit:extend, CurrSize:" | CurrSize; 1263 1269 #endif // VbyteDebug 1264 1270 } // extend … … 1272 1278 void VbyteHeap::reduce( int size ) { 1273 1279 #ifdef VbyteDebug 1274 serr | "enter:reduce, size:" | size;1275 #endif // VbyteDebug 1276 char *OldStartVbyte;1277 1278 NoOfReductions += 1;1279 OldStartVbyte = StartVbyte; // save previous byte area1280 1281 CurrSize -= size;1282 StartVbyte = EndVbyte = new char[CurrSize];1283 ExtVbyte = (void *)( StartVbyte + CurrSize );1284 compaction(); // copy from old heap to new & adjust pointers to new heap1285 delete OldStartVbyte; // release old heap1286 #ifdef VbyteDebug 1287 serr | "exit:reduce, CurrSize:" | CurrSize;1280 serr | "enter:reduce, size:" | size; 1281 #endif // VbyteDebug 1282 char *OldStartVbyte; 1283 1284 NoOfReductions += 1; 1285 OldStartVbyte = StartVbyte; // save previous byte area 1286 1287 CurrSize -= size; 1288 StartVbyte = EndVbyte = new char[CurrSize]; 1289 ExtVbyte = (void *)( StartVbyte + CurrSize ); 1290 compaction(); // copy from old heap to new & adjust pointers to new heap 1291 delete OldStartVbyte; // release old heap 1292 #ifdef VbyteDebug 1293 !serr | "exit:reduce, CurrSize:" | CurrSize; 1288 1294 #endif // VbyteDebug 1289 1295 } // reduce -
libcfa/src/collections/string_res.hfa
r0522ebe ra4da45e 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jan 4 11:28:06202413 // Update Count : 2712 // Last Modified On : Wed Feb 7 21:24:40 2024 13 // Update Count : 59 14 14 // 15 15 … … 123 123 void ?|?(ofstream & out, const string_res & s); 124 124 ifstream & ?|?(ifstream & in, string_res & s); 125 void ?|?( ifstream & in, string_res & s ); 125 126 struct _Istream_Rwidth { 127 string_res * s; 128 inline _Istream_str_base; 129 }; // _Istream_Rwidth 130 131 struct _Istream_Rquoted { 132 // string_res * s; 133 // inline _Istream_str_base; 134 _Istream_Rwidth rstr; 135 }; // _Istream_Rquoted 126 136 127 137 struct _Istream_Rstr { 128 138 string_res * s; 129 139 inline _Istream_str_base; 140 // _Istream_Rwidth rstr; 130 141 }; // _Istream_Rstr 131 142 132 143 static inline { 133 144 // read width does not include null terminator 134 _Istream_R str wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, rwd, {.flags.rwd : true}} }; }145 _Istream_Rwidth wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rwidth)@{ .s : &s, { {.scanset : 0p}, .wd : rwd, {.flags.rwd : true} } }; } 135 146 _Istream_Rstr getline( string_res & s, const char delimiter = '\n' ) { 136 return (_Istream_Rstr)@{ &s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} }; 137 } 138 _Istream_Rstr & getline( _Istream_Rstr & fmt, const char delimiter = '\n' ) { 139 fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; 140 } 141 _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : false}} }; } 142 _Istream_Rstr & incl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } 143 _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : true}} }; } 144 _Istream_Rstr & excl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } 145 _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, -1, {.flags.ignore : true}} }; } 146 _Istream_Rstr & ignore( _Istream_Rstr & fmt ) { fmt.flags.ignore = true; return fmt; } 147 // return (_Istream_Rstr)@{ { .s : &s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } } }; 148 return (_Istream_Rstr)@{ .s : &s, { {.delimiters : { delimiter, '\0' } }, .wd : -1, {.flags.delimiter : true} } }; 149 } 150 _Istream_Rstr & getline( _Istream_Rwidth & f, const char delimiter = '\n' ) { 151 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Rstr &)f; 152 } 153 _Istream_Rquoted quoted( string_res & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) { 154 return (_Istream_Rquoted)@{ { .s : &s, { {.delimiters : { Ldelimiter, Rdelimiter, '\0' }}, .wd : -1, {.flags.rwd : true} } } }; 155 } 156 _Istream_Rquoted & quoted( _Istream_Rwidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { 157 f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; 158 return (_Istream_Rquoted &)f; 159 } 160 _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ .s : &s, { {.scanset : scanset}, .wd : -1, {.flags.inex : false} } }; } 161 _Istream_Rstr & incl( const char scanset[], _Istream_Rwidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Rstr &)f; } 162 _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ .s : &s, { {.scanset : scanset}, .wd : -1, {.flags.inex : true} } }; } 163 _Istream_Rstr & excl( const char scanset[], _Istream_Rwidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Rstr &)f; } 164 _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ .s : &s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } }; } 165 _Istream_Rstr & ignore( _Istream_Rwidth & f ) { f.flags.ignore = true; return (_Istream_Rstr &)f; } 166 _Istream_Rquoted & ignore( _Istream_Rquoted & f ) { f.rstr.flags.ignore = true; return (_Istream_Rquoted &)f; } 167 _Istream_Rstr & ignore( _Istream_Rstr & f ) { f.flags.ignore = true; return (_Istream_Rstr &)f; } 147 168 } // distribution 169 ifstream & ?|?( ifstream & is, _Istream_Rquoted f ); 148 170 ifstream & ?|?( ifstream & is, _Istream_Rstr f ); 149 void ?|?( ifstream & is, _Istream_Rstr t ); 171 static inline ifstream & ?|?( ifstream & is, _Istream_Rwidth f ) { return is | *(_Istream_Rstr *)&f; } 150 172 151 173 // Concatenation -
libcfa/src/fstream.cfa
r0522ebe ra4da45e 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 28 09:56:08202413 // Update Count : 5 5412 // Last Modified On : Sun Feb 11 20:55:45 2024 13 // Update Count : 580 14 14 // 15 15 … … 121 121 } // for 122 122 if ( file == 0p ) { 123 throw (open_failure){ os };123 throwResume (open_failure){ os }; 124 124 // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 125 125 } // if … … 241 241 } // for 242 242 if ( file == 0p ) { 243 throw (open_failure){ is };243 throwResume (open_failure){ is }; 244 244 // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 245 245 } // if … … 295 295 va_start( args, format ); 296 296 297 int nargs ;297 int nargs, tmperrno; 298 298 for () { // no check for EINTR limit waiting for keyboard input 299 errno = 0;300 299 disable_interrupts(); 300 errno = 0; 301 301 nargs = vfscanf( (FILE *)(is.file$), format, args ); 302 tmperrno = errno; 302 303 enable_interrupts(); 303 304 if ( nargs != EOF || errno != EINTR ) break; // timer interrupt ? … … 308 309 } // if 309 310 } // if 311 if ( tmperrno == ERANGE ) throwResume ExceptionInst( data_range ); 310 312 va_end( args ); 311 313 return nargs; … … 318 320 // *********************************** exceptions *********************************** 319 321 320 321 static vtable(open_failure) open_failure_vt;322 322 323 323 // exception I/O constructors … … 335 335 336 336 337 static vtable(close_failure) close_failure_vt;338 339 337 // exception I/O constructors 340 338 void ?{}( close_failure & ex, ofstream & ostream ) with( ex ) { … … 351 349 352 350 353 static vtable(write_failure) write_failure_vt;354 355 351 // exception I/O constructors 356 352 void ?{}( write_failure & ex, ofstream & ostream ) with( ex ) { … … 367 363 368 364 369 static vtable(read_failure) read_failure_vt;370 371 365 // exception I/O constructors 372 366 void ?{}( read_failure & ex, ofstream & ostream ) with( ex ) { -
libcfa/src/fstream.hfa
r0522ebe ra4da45e 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Oct 18 20:30:12 202313 // Update Count : 2 6112 // Last Modified On : Sun Feb 11 20:35:00 2024 13 // Update Count : 274 14 14 // 15 15 … … 139 139 140 140 141 exception open_failure { 141 ExceptionDecl( open_failure, 142 142 union { 143 143 ofstream * ostream; … … 146 146 // TEMPORARY: need polymorphic exceptions 147 147 int tag; // 1 => ostream; 0 => istream 148 };148 ); 149 149 150 150 void ?{}( open_failure & this, ofstream & ); 151 151 void ?{}( open_failure & this, ifstream & ); 152 152 153 exception close_failure { 153 ExceptionDecl( close_failure, 154 154 union { 155 155 ofstream * ostream; … … 158 158 // TEMPORARY: need polymorphic exceptions 159 159 int tag; // 1 => ostream; 0 => istream 160 };160 ); 161 161 162 162 void ?{}( close_failure & this, ofstream & ); 163 163 void ?{}( close_failure & this, ifstream & ); 164 164 165 exception write_failure { 165 ExceptionDecl( write_failure, 166 166 union { 167 167 ofstream * ostream; … … 170 170 // TEMPORARY: need polymorphic exceptions 171 171 int tag; // 1 => ostream; 0 => istream 172 };172 ); 173 173 174 174 void ?{}( write_failure & this, ofstream & ); 175 175 void ?{}( write_failure & this, ifstream & ); 176 176 177 exception read_failure { 177 ExceptionDecl( read_failure, 178 178 union { 179 179 ofstream * ostream; … … 182 182 // TEMPORARY: need polymorphic exceptions 183 183 int tag; // 1 => ostream; 0 => istream 184 };184 ); 185 185 186 186 void ?{}( read_failure & this, ofstream & ); -
libcfa/src/iostream.cfa
r0522ebe ra4da45e 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 28 11:58:54202413 // Update Count : 19 1712 // Last Modified On : Mon Feb 12 09:26:05 2024 13 // Update Count : 1966 14 14 // 15 15 … … 774 774 forall( istype & | basic_istream( istype ) ) { 775 775 istype & ?|?( istype & is, bool & b ) { 776 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 776 777 char val[6]; 777 778 int args = fmt( is, "%5s", val ); 778 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );779 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 779 780 if ( strcmp( val, "true" ) == 0 ) b = true; 780 781 else if ( strcmp( val, "false" ) == 0 ) b = false; … … 787 788 788 789 istype & ?|?( istype & is, char & c ) { 790 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 789 791 char temp; 790 792 for () { 791 793 int args = fmt( is, "%c", &temp ); 792 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );794 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 793 795 // do not overwrite parameter with newline unless appropriate 794 796 if ( temp != '\n' || getANL$( is ) ) { c = temp; break; } … … 799 801 800 802 istype & ?|?( istype & is, signed char & sc ) { 803 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 801 804 int args = fmt( is, "%hhi", &sc ); 802 if ( args != -1 && args !=1 ) throwResume ExceptionInst( missing_data );805 if ( args != 1 ) throwResume ExceptionInst( missing_data ); 803 806 return is; 804 807 } // ?|? 805 808 806 809 istype & ?|?( istype & is, unsigned char & usc ) { 810 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 807 811 int args = fmt( is, "%hhi", &usc ); 808 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );812 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 809 813 return is; 810 814 } // ?|? 811 815 812 816 istype & ?|?( istype & is, short int & si ) { 817 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 813 818 int args = fmt( is, "%hi", &si ); 814 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );819 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 815 820 return is; 816 821 } // ?|? 817 822 818 823 istype & ?|?( istype & is, unsigned short int & usi ) { 824 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 819 825 int args = fmt( is, "%hi", &usi ); 820 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );826 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 821 827 return is; 822 828 } // ?|? 823 829 824 830 istype & ?|?( istype & is, int & i ) { 831 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 825 832 int args = fmt( is, "%i", &i ); 826 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );833 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 827 834 return is; 828 835 } // ?|? 829 836 830 837 istype & ?|?( istype & is, unsigned int & ui ) { 838 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 831 839 int args = fmt( is, "%i", &ui ); 832 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );840 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 833 841 return is; 834 842 } // ?|? 835 843 836 844 istype & ?|?( istype & is, long int & li ) { 845 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 837 846 int args = fmt( is, "%li", &li ); 838 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );847 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 839 848 return is; 840 849 } // ?|? 841 850 842 851 istype & ?|?( istype & is, unsigned long int & ulli ) { 852 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 843 853 int args = fmt( is, "%li", &ulli ); 844 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );854 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 845 855 return is; 846 856 } // ?|? 847 857 848 858 istype & ?|?( istype & is, long long int & lli ) { 859 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 849 860 int args = fmt( is, "%lli", &lli ); 850 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );861 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 851 862 return is; 852 863 } // ?|? 853 864 854 865 istype & ?|?( istype & is, unsigned long long int & ulli ) { 866 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 855 867 int args = fmt( is, "%lli", &ulli ); 856 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );868 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 857 869 return is; 858 870 } // ?|? … … 881 893 882 894 istype & ?|?( istype & is, float & f ) { 895 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 883 896 int args = fmt( is, "%f", &f ); 884 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );897 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 885 898 return is; 886 899 } // ?|? 887 900 888 901 istype & ?|?( istype & is, double & d ) { 902 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 889 903 int args = fmt( is, "%lf", &d ); 890 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );904 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 891 905 return is; 892 906 } // ?|? 893 907 894 908 istype & ?|?( istype & is, long double & ld ) { 909 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 895 910 int args = fmt( is, "%Lf", &ld ); 896 if ( args != -1&& args != 1 ) throwResume ExceptionInst( missing_data );911 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 897 912 return is; 898 913 } // ?|? 899 914 900 915 istype & ?|?( istype & is, float _Complex & fc ) { 916 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 901 917 float re, im; 902 918 int args = fmt( is, "%f%fi", &re, &im ); 903 if ( args != -1&& args != 2 ) throwResume ExceptionInst( missing_data );919 if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); 904 920 fc = re + im * _Complex_I; 905 921 return is; … … 907 923 908 924 istype & ?|?( istype & is, double _Complex & dc ) { 925 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 909 926 double re, im; 910 927 int args = fmt( is, "%lf%lfi", &re, &im ); 911 if ( args != -1&& args != 2 ) throwResume ExceptionInst( missing_data );928 if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); 912 929 dc = re + im * _Complex_I; 913 930 return is; … … 915 932 916 933 istype & ?|?( istype & is, long double _Complex & ldc ) { 934 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 917 935 long double re, im; 918 936 int args = fmt( is, "%Lf%Lfi", &re, &im ); 919 if ( args != -1&& args != 2 ) throwResume ExceptionInst( missing_data );937 if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); 920 938 ldc = re + im * _Complex_I; 921 939 return is; … … 923 941 924 942 istype & ?|?( istype & is, const char fmt[] ) { 943 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 925 944 size_t len = strlen( fmt ); 926 char fmt 2[len + 16];927 strcpy( fmt 2, fmt ); // copy format and add %n928 strcpy( &fmt 2[len], "%n" );945 char fmtstr[len + 16]; 946 strcpy( fmtstr, fmt ); // copy format and add %n 947 strcpy( &fmtstr[len], "%n" ); 929 948 int len2 = -1; 930 int args = fmt( is, fmt2, &len2 );931 if ( args != -1&& len2 == -1 ) throwResume ExceptionInst( missing_data );949 fmt( is, fmtstr, &len2 ); 950 if ( ! eof( is ) && len2 == -1 ) throwResume ExceptionInst( missing_data ); 932 951 return is; 933 952 } // ?|? … … 963 982 forall( istype & | basic_istream( istype ) ) { 964 983 istype & ?|?( istype & is, _Istream_Cskip f ) { 965 // printf( "skip %s %d\n", f.scanset, f.wd);984 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 966 985 if ( f.scanset ) { 967 986 int nscanset = strlen(f.scanset); … … 971 990 strcpy( &fmtstr[pos], f.scanset ); pos += nscanset; 972 991 strcpy( &fmtstr[pos], "]" ); 973 fmt( is, fmtstr, "" ); // skip scanset 992 fmt( is, fmtstr, "" ); // skip scanset, zero or more 974 993 } else { 975 994 char ch; 976 // fprintf( stderr, "skip " );977 995 for ( f.wd ) { // skip N characters 978 if ( eof( is ) ) break; 979 fmt( is, "%c", &ch ); 980 // fprintf( stderr, "`%c' ", ch ); 996 int args = fmt( is, "%c", &ch ); 997 if ( args != 1 ) throwResume ExceptionInst( missing_data ); 981 998 } // for 982 999 } // if … … 985 1002 986 1003 istype & ?|?( istype & is, _Istream_Cquoted f ) with( f.cstr ) { 1004 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 987 1005 int args; 988 1006 fini: { 989 args = fmt( is, "%*[ \f\n\r\t\v]" ); // remove leading whitespace 990 if ( eof( is ) ) break fini; 991 char rfmt[4] = { delimiters[0], '%', 'n', '\0' }; 992 int len = 0; // may not be set in fmt 993 args = fmt( is, rfmt, &len ); // remove leading quote 994 if ( len == 0 || eof( is ) ) break fini; 1007 char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' }; 1008 int len = -1; // may not be set in fmt 1009 args = fmt( is, rfmt, &len ); // remove leading whitespace and quote 1010 if ( eof( is ) || len == -1 ) break fini; 995 1011 996 1012 // Change the remainder of the read into a getline by reseting the closing delimiter. … … 1011 1027 1012 1028 istype & ?|?( istype & is, _Istream_Cstr f ) with( f.cstr ) { 1029 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 1013 1030 const char * scanset; 1014 1031 size_t nscanset = 0; … … 1041 1058 if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1042 1059 else args = fmt( is, fmtstr, s, &len ); 1043 // fprintf( stderr, "cstr %s %d %d %d %s\n", fmtstr, args, len, wd, s);1060 // fprintf( stderr, "cstr %s %d %d %d\n", fmtstr, args, len, f.cstr.wd ); 1044 1061 if ( check && len >= rwd && ! eof( is ) ) { // might not fit 1045 1062 char peek; 1046 1063 fmt( is, "%c", &peek ); // check for whitespace terminator 1047 1064 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1048 if ( ! eof( is ) ) { 1065 if ( ! eof( is ) ) { // can only fail at eof 1049 1066 ungetc( is, peek ); 1050 1067 if ( ! isspace( peek ) ) throwResume ExceptionInst( cstring_length ); … … 1052 1069 } // if 1053 1070 // FIX ME: CFA strings need to be modified to NOT change the argument for this case, then this can be removed. 1054 if ( ! flags.ignore && args == 0 ) s[0]= '\0'; // read failed => no pattern match => set string to null 1071 //fprintf( stderr, "cstr %d %d %d %d '%s'\n", flags.ignore, args, len, eof( is ), s ); 1072 //if ( ! flags.ignore && args == 0 ) s[0]= '\0'; // read failed => no pattern match => set string to null 1055 1073 } else { 1056 1074 if ( flags.delimiter ) { // getline 1057 1075 int len = 0; // may not be set in fmt 1058 if ( delimiters[2] != '\0' ) { // read single character ?1076 if ( delimiters[2] != '\0' ) { // (quoted) read single character ? 1059 1077 sprintf( &fmtstr[pos], "c%%n" ); 1060 1078 } else { … … 1063 1081 if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1064 1082 else args = fmt( is, fmtstr, s, &len ); 1083 1065 1084 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1066 1085 char peek; … … 1096 1115 } // if 1097 1116 if ( args == 1 && eof( is ) ) { // data but scan ended at EOF 1098 // fprintf( stderr, "clear\n" );1099 1117 clear( is ); // => reset EOF => detect again on next read 1100 1118 } // if -
libcfa/src/iostream.hfa
r0522ebe ra4da45e 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 28 11:56:29202413 // Update Count : 7 3312 // Last Modified On : Tue Feb 6 18:35:54 2024 13 // Update Count : 743 14 14 // 15 15 … … 370 370 // *********************************** exceptions *********************************** 371 371 372 ExceptionDecl( cstring_length ); 373 ExceptionDecl( missing_data ); 372 ExceptionDecl( missing_data ); // read finds no appropriate data 373 ExceptionDecl( cstring_length ); // character string size exceeded 374 ExceptionDecl( data_range ); // value too large for numerical type 374 375 375 376 // *********************************** manipulators *********************************** … … 406 407 char * s; 407 408 inline _Istream_str_base; 408 }; // _Istream_C str409 }; // _Istream_Cwidth 409 410 410 411 // Restrict nesting of input manipulators to those combinations that make sense. 412 413 struct _Istream_Cquoted { 414 _Istream_Cwidth cstr; 415 }; // _Istream_Cquoted 411 416 412 417 struct _Istream_Cstr { … … 414 419 }; // _Istream_Cstr 415 420 416 struct _Istream_Cquoted {417 _Istream_Cwidth cstr;418 }; // _Istream_Cquoted419 420 421 static inline { 421 422 // width must include room for null terminator, (gcc) scanf does not allow a 0 width => wd > 1 (1 char and null) and rd > 0 (1 char); 422 423 _Istream_Cwidth wdi( unsigned int wd, char s[] ) { 423 if ( wd <= 1 ) throw (cstring_length){ &cstring_length_vt }; // minimum 1 character and null terminator424 if ( wd <= 1 ) throwResume ExceptionInst( cstring_length ); // minimum 1 character and null terminator 424 425 return (_Istream_Cwidth)@{ .s : s, { {.scanset : 0p}, .wd : wd, {.all : 0} } }; 425 426 } 426 427 _Istream_Cwidth wdi( unsigned int wd, unsigned int rwd, char s[] ) { 427 if ( wd <= 1 || wd <= rwd ) throw (cstring_length){ &cstring_length_vt }; // minimum 1 character, null terminator, plus subset428 if ( wd <= 1 || wd <= rwd ) throwResume ExceptionInst( cstring_length ); // minimum 1 character, null terminator, plus subset 428 429 return (_Istream_Cwidth)@{ .s : s, { {.scanset : 0p}, .wd : rwd, {.flags.rwd : true} } }; 430 } 431 _Istream_Cstr & getline( _Istream_Cwidth & f, const char delimiter = '\n' ) { 432 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Cstr &)f; 429 433 } 430 434 _Istream_Cquoted quoted( char & ch, const char Ldelimiter = '\'', const char Rdelimiter = '\0' ) { … … 435 439 return (_Istream_Cquoted &)f; 436 440 } 437 _Istream_Cstr & getline( _Istream_Cwidth & f, const char delimiter = '\n' ) {438 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Cstr &)f;439 }440 441 _Istream_Cstr & incl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Cstr &)f; } 441 442 _Istream_Cstr & excl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Cstr &)f; } 442 _Istream_Cstr ignore( const char s[] ) { return (_Istream_C width)@{ .s : (char *)s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } }; }443 _Istream_Cstr ignore( const char s[] ) { return (_Istream_Cstr)@{ { .s : (char *)s, { {.scanset : 0p}, .wd : -1, {.flags.ignore : true} } } }; } 443 444 _Istream_Cstr & ignore( _Istream_Cwidth & f ) { f.flags.ignore = true; return (_Istream_Cstr &)f; } 444 445 _Istream_Cquoted & ignore( _Istream_Cquoted & f ) { f.cstr.flags.ignore = true; return (_Istream_Cquoted &)f; } … … 450 451 istype & ?|?( istype & is, _Istream_Cquoted f ); 451 452 istype & ?|?( istype & is, _Istream_Cstr f ); 452 static inline { 453 istype & ?|?( istype & is, _Istream_Cwidth f ) { return is | *(_Istream_Cstr *)&f; } 454 } // distribution 453 static inline istype & ?|?( istype & is, _Istream_Cwidth f ) { return is | *(_Istream_Cstr *)&f; } 455 454 } // distribution 456 455
Note:
See TracChangeset
for help on using the changeset viewer.