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/aryi64.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/aryi64.smp')
| -rw-r--r-- | src/main/smp/sys/aryi64.smp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/main/smp/sys/aryi64.smp b/src/main/smp/sys/aryi64.smp index 98b72605..b9f1fbed 100644 --- a/src/main/smp/sys/aryi64.smp +++ b/src/main/smp/sys/aryi64.smp @@ -9,36 +9,36 @@ struct aryi64 { }; // 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. i64[] !buf2 = new i64[newlen]; - for( int i=0; i<buf#; i++ ) + for( u32 i=0; i<len; i++ ) buf2[i] = buf[i]; buf = buf2; self; }; - // Add a i64 to aryu8 + // Add an i64 to aryi64 val add = { i64 c -> _grow(1).buf[len++] = c; self; }; - val get = { int index -> return buf[index]; }; + val get = { u32 index -> return buf[index]; }; // Write the bracketed array-list form to aryu8. val str = { -> // TODO: clunky default constructor syntax var str = new aryu8().add('['); - for( int i=0; i<len; i++ ) + for( u32 i=0; i<len; i++ ) str.num(buf[i]).add(',').add(' '); if( len ) str.undo(2); return str.add(']'); }; - val swap = { int i, int j -> + val swap = { u32 i, u32 j -> i64 tmp = buf[i]; buf[i] = buf[j]; buf[j] = tmp; @@ -75,8 +75,11 @@ val parseAryI64 = { u8[~] str -> return s.x == s.buf# ? nums : null; }; -val copy = { i64[] dst, int doff, i64[~] src, int soff, int len -> - for( int i=0; i<len; i++ ) +val copy = { i64[] dst, u32 doff, i64[~] src, u32 soff, u32 len -> + if( doff > dst# || soff > src# ) return dst; + if( len > dst# - doff ) len = dst# - doff; + if( len > src# - soff ) len = src# - soff; + for( u32 i=0; i<len; i++ ) dst[i+doff] = src[i+soff]; return dst; }; |
