ref: 67ec1a16297664c91a52f7a4380f3b7f2a1c66fc
parent: b525ad28eda5b9ac0f6f64d7dfeec42ae9cb6cf0
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Feb 22 10:15:22 EST 2015
Change fmt() to return a slice, not a size.
--- a/libstd/execvp.myr
+++ b/libstd/execvp.myr
@@ -12,7 +12,7 @@
;;
const execvp = {cmd, args
- var paths, cmdlen
+ var paths, cmd
var buf : byte[512]
match strfind(cmd, "/")
@@ -21,8 +21,8 @@
| `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 execvpe = {cmd, args, env
- var paths, cmdlen
+ var paths, cmd
var buf : byte[512]
match strfind(cmd, "/")
@@ -39,8 +39,8 @@
| `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)
;;
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -30,10 +30,11 @@
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 @@
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 @@
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 @@
n += encode(buf[n:], c)
;;
;;
- -> n
+ -> buf[:n]
}
const strfmt = {buf, str, padto, padfill
--- /dev/null
+++ b/libstd/test/fmt.myr
@@ -1,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")
+}
+