summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-02-22 15:15:22 -0800
committerOri Bernstein <ori@eigenstate.org>2015-02-22 15:15:22 -0800
commit64d76c27e6ffeb9fe652af8388470f7c9283e803 (patch)
tree5ba3f2134d5a44ea34a035d499c2ae1ebdd01bc5 /libstd
parentecefac44634dd5c7e9b7f17716644934b8b240f5 (diff)
downloadmc-64d76c27e6ffeb9fe652af8388470f7c9283e803.tar.gz
Change fmt() to return a slice, not a size.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/execvp.myr12
-rw-r--r--libstd/fmt.myr16
-rw-r--r--libstd/test/fmt.myr28
3 files changed, 40 insertions, 16 deletions
diff --git a/libstd/execvp.myr b/libstd/execvp.myr
index 8855af5..6939917 100644
--- a/libstd/execvp.myr
+++ b/libstd/execvp.myr
@@ -12,7 +12,7 @@ pkg std =
;;
const execvp = {cmd, args
- var paths, cmdlen
+ var paths, cmd
var buf : byte[512]
match strfind(cmd, "/")
@@ -21,8 +21,8 @@ const execvp = {cmd, args
| `None:
paths = getpaths()
for p in paths
- cmdlen = bfmt(buf[:], "%s/%s", p, cmd)
- execv(buf[:cmdlen], args)
+ cmd = bfmt(buf[:], "%s/%s", p, cmd)
+ execv(cmd, args)
;;
slfree(paths)
;;
@@ -30,7 +30,7 @@ const execvp = {cmd, args
}
const execvpe = {cmd, args, env
- var paths, cmdlen
+ var paths, cmd
var buf : byte[512]
match strfind(cmd, "/")
@@ -39,8 +39,8 @@ const execvpe = {cmd, args, env
| `None:
paths = getpaths()
for p in paths
- cmdlen = bfmt(buf[:], "%s/%s", p, cmd)
- execve(buf[:cmdlen], args, env)
+ cmd = bfmt(buf[:], "%s/%s", p, cmd)
+ execve(cmd, args, env)
;;
slfree(paths)
;;
diff --git a/libstd/fmt.myr b/libstd/fmt.myr
index ed2a61e..5504710 100644
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -30,10 +30,11 @@ pkg std =
const fput : (fd : fd, fmt : byte[:], args : ... -> size)
const putv : (fmt : byte[:], ap : valist -> size)
const fputv : (fd : fd, fmt : byte[:], ap : valist -> size)
+
const fmt : (fmt : byte[:], args : ... -> byte[:])
const fmtv : (fmt : byte[:], ap : valist -> byte[:])
- const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> size)
- const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> size)
+ const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> byte[:])
+ const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> byte[:])
$noret const fatal : (status : int, fmt : byte[:], args : ... -> void)
$noret const fatalv : (status : int, fmt : byte[:], ap : valist -> void)
@@ -56,11 +57,8 @@ const putv = {fmt, ap
as the source of the arguments */
const fputv = {fd, fmt, ap
var buf : byte[2048]
- var n
- n = bfmtv(buf[:], fmt, ap)
- write(fd, buf[:n])
- -> n
+ -> write(fd, bfmtv(buf[:], fmt, ap))
}
/* same as 'put', but exits the program after printing */
@@ -85,11 +83,9 @@ const fmt = {fmt, args
size needed. Takes a valist as it's last argument. */
const fmtv = {fmt, ap
var buf
- var sz
buf = slalloc(2048)
- sz = bfmtv(buf, fmt, ap)
- -> buf[:sz]
+ -> bfmtv(buf, fmt, ap)
}
/* formats a string of text as specified by 'fmt' into 'buf' */
@@ -281,7 +277,7 @@ const bfmtv = {buf, fmt, ap
n += encode(buf[n:], c)
;;
;;
- -> n
+ -> buf[:n]
}
const strfmt = {buf, str, padto, padfill
diff --git a/libstd/test/fmt.myr b/libstd/test/fmt.myr
new file mode 100644
index 0000000..c51c3cc
--- /dev/null
+++ b/libstd/test/fmt.myr
@@ -0,0 +1,28 @@
+use std
+
+const check = {expected, fmt, args : ...
+ var buf : byte[2048]
+ var sl, ap
+
+ ap = std.vastart(&args)
+ sl = std.bfmtv(buf[:], fmt, ap)
+ if !std.sleq(expected, sl)
+ std.fatal(1, "mismatched expected fmt \"%s\", expected \"%s\"\n", sl, expected)
+ ;;
+}
+
+const main = {
+ check(" abcd", "%10s", "abcd")
+ check("00000bdcae", "%010s", "bdcae")
+ check("abcdefghijkl", "%010s", "abcdefghijkl")
+ check("a", "%01s", "a")
+ check(" 10", "%10i", 10)
+ check("0000000010", "%010i", 10)
+ check("4294967295", "%010ui", -1)
+ check("-000000001", "%010i", -1)
+ check(" -1", "%10i", -1)
+ check("100000" , "%3i", 100000)
+ check("foobarbaz", "%sbar%s", "foo", "baz")
+ check("%sbarbaz", "%%sbar%s", "baz")
+}
+