diff options
Diffstat (limited to 'chapter25/src/main/smp')
| -rw-r--r-- | chapter25/src/main/smp/sys/Scan.smp | 8 | ||||
| -rw-r--r-- | chapter25/src/main/smp/sys/aryi64.smp | 13 |
2 files changed, 19 insertions, 2 deletions
diff --git a/chapter25/src/main/smp/sys/Scan.smp b/chapter25/src/main/smp/sys/Scan.smp index acf366f7..acf9fbb8 100644 --- a/chapter25/src/main/smp/sys/Scan.smp +++ b/chapter25/src/main/smp/sys/Scan.smp @@ -3,6 +3,7 @@ // Scanner; a simple string parser. struct Scan { int !x; // Cursor + bool !bad; // True after a malformed token u8[~] buf; // Immutable byte array, must be set in 2nd constructor new Scan = { u8[~] b -> @@ -19,6 +20,7 @@ struct Scan { // True if find required character, and skip it; else False. val match = { u8 c -> skip(); + if( x >= buf# ) return false; if( buf[x] != c ) return false; x++; return true; @@ -28,8 +30,12 @@ struct Scan { val number = { -> skip(); bool neg = match('-'); + if( x >= buf# || buf[x] < '0' || '9' < buf[x] ) { + bad = true; + return 0; + } i64 n = 0; - while( '0' <= buf[x] & buf[x] <= '9' ) + while( x < buf# && '0' <= buf[x] & buf[x] <= '9' ) n = n*10 + (buf[x++] - '0'); return neg ? -n : n; }; diff --git a/chapter25/src/main/smp/sys/aryi64.smp b/chapter25/src/main/smp/sys/aryi64.smp index 8a154f34..c2bc394f 100644 --- a/chapter25/src/main/smp/sys/aryi64.smp +++ b/chapter25/src/main/smp/sys/aryi64.smp @@ -36,6 +36,14 @@ struct aryi64 { str.undo(2).add(']'); }; + // Comma-separated form used by command-line examples. + val csv = { -> + var str = new aryu8(new u8[1]); + for( int i=0; i<len; i++ ) + str.num(buf[i]).add(',').add(' '); + str.undo(2).add('\n'); + }; + val swap = { int i, int j -> i64 tmp = buf[i]; buf[i] = buf[j]; @@ -62,9 +70,12 @@ val parseAryI64 = { u8[~] str -> // Parse number commas "123, " until something else while( s.x < s.buf# ) { nums.add(s.number()); + if( s.bad ) return null; if( !s.match(',') ) break; }; // Close the list - return s.match(']') ? nums : null; + if( !s.match(']') ) return null; + s.skip(); + return s.x == s.buf# ? nums : null; }; |
