summaryrefslogtreecommitdiff
path: root/chapter25/src/main/smp/sys/Scan.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-12 11:20:14 -0700
committerCliff Click <cliffc@acm.org>2026-08-12 11:20:14 -0700
commit1cb8279a64f4f79615ac8358927b11237df29bf2 (patch)
treed057b4ee723a9d29e16f2c71959c0ae2773cdb63 /chapter25/src/main/smp/sys/Scan.smp
parent628452bb9f6137d433395fae5a9f5d780fb0f1e1 (diff)
downloadSimple-1cb8279a64f4f79615ac8358927b11237df29bf2.tar.gz
Simple-1cb8279a64f4f79615ac8358927b11237df29bf2.zip
Real argc/argv args
Beef up BubbleSort error handling
Diffstat (limited to 'chapter25/src/main/smp/sys/Scan.smp')
-rw-r--r--chapter25/src/main/smp/sys/Scan.smp8
1 files changed, 7 insertions, 1 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;
};