commit 7f2ac4036a96529a1ab643bd6b25ec71cff3ac3a
parent 9291f1fafde7577fda4558f850ac44a1e725fb4b
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 5 Dec 2022 19:48:02 -0500
sys/io: add ability to print/spit null-terminated strings
Diffstat:
5 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/fs/doc/io.txt b/fs/doc/io.txt
@@ -70,6 +70,9 @@ convenience words:
:puts ( str hdl -- )
Write str to hdl.
+:putz ( zstr hdl -- )
+ Write null-terminated string zstr to hdl.
+
:putback ( c hdl -- )
Set the putback value to c.
@@ -175,6 +178,7 @@ these letters are supported (others produce an error):
w - 2b hexadecimal
b - 1b hexadecimal
s - string
+ z - null-terminated string
c - char
When you want to quickly printf to the console, there's the .f" shortcut, which
diff --git a/fs/lib/fmt.fs b/fs/lib/fmt.fs
@@ -28,6 +28,7 @@ struct+[ IO
'x' of = V1 :.x endof
'd' of = V1 :. endof
's' of = V1 :puts endof
+ 'z' of = V1 :putz endof
'c' of = V1 :putc endof
abort" unsupported fmt argument"
endcase
diff --git a/fs/sys/io.fs b/fs/sys/io.fs
@@ -18,6 +18,8 @@ struct+[ IO
create _buf 1 allot
: :putc ( c hdl -- ) swap _buf c! _buf 1 rot :writebuf not if _ioerr then ;
: :puts ( str hdl -- ) swap c@+ rot :write ;
+ : :putz ( zstr hdl -- )
+ over 0 swap $100 [c]? dup 0< if _ioerr then ( zstr hdl len ) swap :write ;
create _buf( $100 allot
here value _)buf
diff --git a/fs/tests/lib/all.fs b/fs/tests/lib/all.fs
@@ -8,3 +8,4 @@ f<< /tests/lib/arena.fs
f<< /tests/lib/math.fs
f<< /tests/lib/stack.fs
f<< /tests/lib/tree.fs
+f<< /tests/lib/fmt.fs
diff --git a/fs/tests/lib/fmt.fs b/fs/tests/lib/fmt.fs
@@ -0,0 +1,12 @@
+?f<< /tests/harness.fs
+?f<< /lib/fmt.fs
+testbegin
+\ Testing lib/fmt
+: _printf StdOut IO :printf ;
+10 11 12 13 S" foo %b bar %w baz %x bleh %d" capture _printf
+S" foo 0d bar 000c baz 0000000b bleh 10" #s=
+
+create s0 ," null-terminated\0"
+s0 S" hello" 'X' S" foo %c bar %s baz %z" capture _printf
+S" foo X bar hello baz null-terminated" #s=
+testend