diff options
| author | Cliff Click <cliffc@acm.org> | 2026-08-10 11:11:18 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-08 14:10:02 -0800 |
| commit | 44505aa5b75b1046f13f2bc2a52fe0b16d975838 (patch) | |
| tree | cc558485c807ac3842cd73943cf5e294c4a307de /src/main/smp/sys/aryu8.smp | |
| parent | 2560096fb4636263798ae56c88687973c72cdf6e (diff) | |
| download | Simple-linear-chapter30.tar.gz Simple-linear-chapter30.zip | |
Chapter 30: Text Formatting and Diagnosticslinear-chapter30
Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com>
Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com>
Co-authored-by: Symious <14933944+symious@users.noreply.github.com>
Co-authored-by: Jun Kawasaki <jun784@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/main/smp/sys/aryu8.smp')
| -rw-r--r-- | src/main/smp/sys/aryu8.smp | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/src/main/smp/sys/aryu8.smp b/src/main/smp/sys/aryu8.smp index 21cf920b..18543f13 100644 --- a/src/main/smp/sys/aryu8.smp +++ b/src/main/smp/sys/aryu8.smp @@ -7,12 +7,12 @@ struct aryu8 { new aryu8 = { -> buf = new u8[1]; }; // Grow as needed - val _grow = { int len2 -> - if( len+len2 < buf# ) return self; - int newlen = buf# * 2; - while( len+len2 >= newlen ) newlen *= 2; + val _grow = { u32 len2 -> + if( len+len2 <= buf# ) return self; + u32 newlen = ary.capacity(buf#,len+len2); + if( !newlen ) libc._exit(1); // Length exceeds the array representation. u8[] !buf2 = new u8[newlen]; - ary.copy(buf2,0,buf,0,buf#); + ary.copy(buf2,0,buf,0,len); buf = buf2; self; }; @@ -27,38 +27,28 @@ struct aryu8 { // Add all of a plain array to the aryu8 val addAry = { u8[~] xs -> - _grow(xs#); - ary.copy(buf,len,xs,0,xs#); - len += xs#; + u32 n=xs#; + _grow(n); + ary.copy(buf,len,xs,0,n); + len += n; self; }; // Remove x bytes from the tail. - val undo = { int x -> + val undo = { u32 x -> len = len >= x ? len-x : 0; self; }; // Print ASCII number into aryu8 val num = { i64 x -> - if( !x ) { add('0'); return self; } - if( x < 0 ) { add('-'); x = -x; } - i64 ten = 1, log = 0; - while( ten <= x ) { ten *= 10; log++; } - for( int i=0; i<log; i++ ) { - ten /= 10; - i64 q = x/ten; - u8 digit = q+'0'; - add(digit); - x -= q*ten; - } + text.integer(self,x,0,0,-1); self; }; - // Write to fd, returns the number of bytes written (len). + // Write all bytes to fd; return len, or -1 with errno set on failure. val write = { i32 fd -> - i64 ptr = buf; // Cast buffer pointer to i64 for native C call - libc.write(fd,ptr,len); + io.writeOff(fd,buf,0,len); }; }; @@ -66,7 +56,7 @@ struct aryu8 { // Parse one integer, ignore trailing data; returns 0 with an empty string. // If you want a more robust parsing, use the `scan` class val atoi = { u8[~] str -> - int x = 0; + u32 x = 0; while( x < str# && str[x] <= ' ' ) x++; if( x == str# ) return 0; bool neg = false; @@ -77,11 +67,12 @@ val atoi = { u8[~] str -> return neg ? -n : n; }; -// Zero-terminate string for libc strings. No error checking, just copies and -// appends a zero. +// Zero-terminate a string for libc, reserving one array element for the zero. { u8[~] -> u8[] } str0 = { u8[~] str -> + if( str# == 4294967295 ) libc._exit(1); u8[] !strz = new u8[str#+1]; - ary.copy(strz,0,str,0,str#); + u32 len=str#; + ary.copy(strz,0,str,0,len); strz[str#] = '\0'; strz; }; |
