summaryrefslogtreecommitdiff
path: root/chapter25/src/main/smp/sys/aryu8.smp
diff options
context:
space:
mode:
Diffstat (limited to 'chapter25/src/main/smp/sys/aryu8.smp')
-rw-r--r--chapter25/src/main/smp/sys/aryu8.smp38
1 files changed, 30 insertions, 8 deletions
diff --git a/chapter25/src/main/smp/sys/aryu8.smp b/chapter25/src/main/smp/sys/aryu8.smp
index 0ce4bd23..186a45a3 100644
--- a/chapter25/src/main/smp/sys/aryu8.smp
+++ b/chapter25/src/main/smp/sys/aryu8.smp
@@ -5,9 +5,7 @@ struct aryu8 {
u32 !len = 0;
u8[] !buf;
- new aryu8 = { u8[] b ->
- buf = b;
- };
+ new aryu8 = { -> buf = new u8[1]; };
// Grow as needed
val _grow = { int len2 ->
@@ -15,7 +13,7 @@ struct aryu8 {
int newlen = buf# * 2;
while( len+len2 >= newlen ) newlen *= 2;
u8[] !buf2 = new u8[newlen];
- Ary.copy(buf2,0,buf,0,buf#);
+ ary.copy(buf2,0,buf,0,buf#);
buf = buf2;
self;
};
@@ -29,10 +27,10 @@ struct aryu8 {
};
// Add all of a plain array to the aryu8
- val addAry = { u8[~] ary ->
- _grow(ary#);
- Ary.copy(buf,len,ary,0,ary#);
- len += ary#;
+ val addAry = { u8[~] xs ->
+ _grow(xs#);
+ ary.copy(buf,len,xs,0,xs#);
+ len += xs#;
self;
};
@@ -64,3 +62,27 @@ struct aryu8 {
libc.write(fd,ptr,len);
};
};
+
+
+// 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;
+ while( x < str# && str[x] <= ' ' ) x++;
+ if( x == str# ) return 0;
+ bool neg = false;
+ if( str[x]=='-' ) { neg=true; x++; }
+ i64 n = 0;
+ while( x < str# && '0' <= str[x] <= '9' )
+ n = n*10 + str[x++]-'0';
+ return neg ? -n : n;
+};
+
+// Zero-terminate string for libc strings. No error checking, just copies and
+// appends a zero.
+{ u8[~] -> u8[] } str0 = { u8[~] str ->
+ u8[] !strz = new u8[str#+1];
+ ary.copy(strz,0,str,0,str#);
+ strz[str#] = '\0';
+ strz;
+};