summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-12 12:46:50 -0400
committerOri Bernstein <ori@eigenstate.org>2015-06-12 14:01:54 -0400
commit21b91ad9fa7ae830272dc67fbe3150dc30921702 (patch)
treea674ea3e28f90ba8fa2f4521069a010b4742961e /libstd
parent96c8217e913d8d6de22bbb1aa3624c26e66838fc (diff)
downloadmc-21b91ad9fa7ae830272dc67fbe3150dc30921702.tar.gz
Move fmt to fmt2.
Shit seems to work.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/blat.myr1
-rw-r--r--libstd/bld.sub3
-rw-r--r--libstd/dial+plan9.myr10
-rw-r--r--libstd/dial+posixy.myr1
-rw-r--r--libstd/execvp.myr4
-rw-r--r--libstd/fmt.myr554
-rw-r--r--libstd/fmt2.myr412
-rw-r--r--libstd/htab.myr1
-rw-r--r--libstd/intparse.myr1
-rw-r--r--libstd/optparse.myr20
-rw-r--r--libstd/pathjoin.myr2
-rw-r--r--libstd/rand.myr1
-rw-r--r--libstd/resolve+posixy.myr1
-rw-r--r--libstd/search.myr1
-rw-r--r--libstd/slput.myr1
-rw-r--r--libstd/slurp.myr1
-rw-r--r--libstd/spork.myr2
-rw-r--r--libstd/strsplit.myr1
18 files changed, 344 insertions, 673 deletions
diff --git a/libstd/blat.myr b/libstd/blat.myr
index 724bc90..90c0f94 100644
--- a/libstd/blat.myr
+++ b/libstd/blat.myr
@@ -1,5 +1,4 @@
use "syswrap.use"
-use "fmt.use"
pkg std =
const blat : (path : byte[:], buf : byte[:], perm : int64 -> bool)
diff --git a/libstd/bld.sub b/libstd/bld.sub
index c8cf73c..8158ce3 100644
--- a/libstd/bld.sub
+++ b/libstd/bld.sub
@@ -39,8 +39,7 @@ lib std {inc=.} =
extremum.myr
fltbits.myr
fltfmt.myr
- fmt.myr
- fmt2.myr
+ fmt.myr
getcwd.myr
getint.myr
hashfuncs.myr
diff --git a/libstd/dial+plan9.myr b/libstd/dial+plan9.myr
index 4d95f28..35d97e9 100644
--- a/libstd/dial+plan9.myr
+++ b/libstd/dial+plan9.myr
@@ -40,10 +40,10 @@ const csdial = {netdir, proto, rem
var buf : byte[Maxpath]
/* Try using the connection server */
- dir = fmt("%s/cs", netdir)
+ dir = fmt("{}/cs", netdir)
csfd = open(dir, Ordwr)
if csfd < 0
- clone = fmt("%s/%s/clone", netdir, proto)
+ clone = fmt("{}/{}/clone", netdir, proto)
ret = call(clone, rem, netdir)
slfree(clone)
if ret == -1
@@ -54,7 +54,7 @@ const csdial = {netdir, proto, rem
;;
slfree(dir)
- csaddr = fmt("%s!%s", proto, rem)
+ csaddr = fmt("{}!{}", proto, rem)
if write(csfd, csaddr) < 0
close(csfd)
-> `Fail "couldn't blah cs"
@@ -106,13 +106,13 @@ const call = {clone, addr, netdir
if n < 0
goto cleanup
;;
- fput(cfd, "connect %s", addr)
+ fput(cfd, "connect {}", addr)
name = strstrip(namebuf[:n])
match strrfind(c, "/")
| `None: die("there should be a '/' here\n")
| `Some i: base = c[:i]
;;
- dpath = bfmt(databuf[:], "%s/%s/data", base, name)
+ dpath = bfmt(databuf[:], "{}/{}/data", base, name)
datafd = open(dpath, Ordwr)
:cleanup
close(cfd)
diff --git a/libstd/dial+posixy.myr b/libstd/dial+posixy.myr
index 77920b6..e63df76 100644
--- a/libstd/dial+posixy.myr
+++ b/libstd/dial+posixy.myr
@@ -4,7 +4,6 @@ use "alloc.use"
use "chartype.use"
use "die.use"
use "endian.use"
-use "fmt.use"
use "hasprefix.use"
use "intparse.use"
use "ipparse.use"
diff --git a/libstd/execvp.myr b/libstd/execvp.myr
index 4f70e63..38d9fc5 100644
--- a/libstd/execvp.myr
+++ b/libstd/execvp.myr
@@ -21,7 +21,7 @@ const execvp = {cmd, args
| `None:
paths = getpaths()
for p in paths
- binpath = bfmt(buf[:], "%s/%s", p, cmd)
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
execv(binpath, args)
;;
slfree(paths)
@@ -39,7 +39,7 @@ const execvpe = {cmd, args, env
| `None:
paths = getpaths()
for p in paths
- binpath = bfmt(buf[:], "%s/%s", p, cmd)
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
execve(binpath, args, env)
;;
slfree(paths)
diff --git a/libstd/fmt.myr b/libstd/fmt.myr
index f78023d..1df1253 100644
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -1,10 +1,12 @@
-use sys
use "alloc.use"
use "chartype.use"
use "die.use"
use "extremum.use"
use "fltfmt.use"
+use "htab.use"
+use "hashfuncs.use"
use "introspect.use"
+use "option.use"
use "strbuf.use"
use "syswrap-ss.use"
use "syswrap.use"
@@ -12,100 +14,348 @@ use "types.use"
use "utf.use"
use "varargs.use"
-/*
- printf-like functions. These use a different syntax from the C printf,
- as described below:
-
- %s - A string, ie, a utf8 encoded byte slice.
- %t - A boolean
- %b - A byte.
- %w - A 16 bit integer
- %i - A 32 bit integer
- %l - A 64 bit integer
- %z - A size
- %p - A pointer
- %c - A char
-*/
-
pkg std =
+ /* write to fd */
const put : (fmt : byte[:], args : ... -> size)
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 putv : (fmt : byte[:], ap : valist# -> size)
+ const fputv : (fd : fd, fmt : byte[:], ap : valist# -> size)
+ /* write to buffer */
const fmt : (fmt : byte[:], args : ... -> byte[:])
- const fmtv : (fmt : byte[:], ap : valist -> byte[:])
+ const fmtv : (fmt : byte[:], ap : valist# -> byte[:])
const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> byte[:])
- const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> byte[:])
+ const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist# -> byte[:])
+
+ /* write to strbuf */
+ const sbfmt : (buf : strbuf#, fmt : byte[:], args : ... -> size)
+ const sbfmtv : (buf : strbuf#, fmt : byte[:], ap : valist# -> size)
+
+ /* add a formatter function */
+ const fmtinstall : (ty : byte[:], \
+ fn : (sb : strbuf#, ap : valist#, opts : byte[:] -> void) \
+ -> void)
$noret const fatal : (fmt : byte[:], args : ... -> void)
- $noret const fatalv : (fmt : byte[:], ap : valist -> void)
+ $noret const fatalv : (fmt : byte[:], ap : valist# -> void)
;;
-/* Writes a string of text up to 2 kb in size to stdout */
-const put = {fmt, args
- -> fputv(1, fmt, vastart(&args))
+/* same as 'put', but exits the program after printing */
+const fatal = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ putv(fmt, &ap)
+ exit(1)
}
-const fput = {fd, fmt, args
- -> fputv(fd, fmt, vastart(&args))
+/* same as 'putv', but exits the program after printing */
+const fatalv = {fmt, ap
+ putv(fmt, ap)
+ exit(1)
+}
+
+var fmtmapinited : bool = false
+var fmtmap : htab(byte[:], (sb : strbuf#, ap : valist#, opts : byte[:] -> void))#
+
+const fmtinstall = {ty, fn
+ if !fmtmapinited
+ fmtmapinited = true
+ fmtmap = mkht(strhash, streq)
+ ;;
+ htput(fmtmap, ty, fn)
+}
+
+const put = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> fputv(1, fmt, &ap)
}
const putv = {fmt, ap
-> fputv(1, fmt, ap)
}
-/* Writes a string of text up to 2kb long to stdout, using a valist
- as the source of the arguments */
-const fputv = {fd, fmt, ap
- var buf : byte[2048]
-
- -> write(fd, bfmtv(buf[:], fmt, ap))
-}
+const fput = {fd, fmt, args
+ var ap
-/* same as 'put', but exits the program after printing */
-const fatal = {fmt, args
- putv(fmt, vastart(&args))
- exit(1)
+ ap = vastart(&args)
+ -> fputv(fd, fmt, &ap)
}
-/* same as 'putv', but exits the program after printing */
-const fatalv = {fmt, ap
- putv(fmt, ap)
- exit(1)
+const fputv = {fd, fmt, ap
+ var sb, s
+
+ sb = mksb()
+ sbfmtv(sb, fmt, ap)
+ s = sbfin(sb)
+ -> writeall(fd, s)
}
-/* formats a string, allocating the slice. FIXME: calculate the
- size needed. */
const fmt = {fmt, args
- -> fmtv(fmt, vastart(&args))
+ var ap
+
+ ap = vastart(&args)
+ -> fmtv(fmt, &ap)
}
-/* formats a string, allocating the slice. FIXME: calculate the
- size needed. Takes a valist as it's last argument. */
const fmtv = {fmt, ap
- var buf
+ var sb
- buf = slalloc(2048)
- -> bfmtv(buf, fmt, ap)
+ sb = mksb()
+ sbfmtv(sb, fmt, ap)
+ -> sbfin(sb)
}
-/* formats a string of text as specified by 'fmt' into 'buf' */
const bfmt = {buf, fmt, args
- -> bfmtv(buf, fmt, vastart(&args))
+ var ap
+
+ ap = vastart(&args)
+ -> bfmtv(buf, fmt, &ap)
+}
+
+const bfmtv = {buf, fmt, ap
+ var sb
+
+ sb = mkbufsb(buf)
+ sbfmtv(sb, fmt, ap)
+ -> sbfin(sb)
+}
+
+const sbfmt = {sb, fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> sbfmtv(sb, fmt, &ap)
+}
+
+const sbfmtv = {sb, fmt, ap -> size
+ var c, params, ty
+ var nfmt, nparams
+
+ nparams = ap.tc.nelt
+ nfmt = 0
+ if !fmtmapinited
+ fmtmapinited = true
+ fmtmap = mkht(strhash, streq)
+ ;;
+ while fmt.len != 0
+ (c, fmt) = striter(fmt)
+ match c
+ | '{':
+ if decode(fmt) == '{'
+ (c, fmt) = striter(fmt)
+ sbputc(sb, '{')
+ else
+ (params, fmt) = getparams(fmt)
+ nfmt++
+ if nfmt > nparams
+ die("too few params for fmt\n")
+ ;;
+
+ ty = vatype(ap)
+ match htget(fmtmap, ty)
+ | `Some func:
+ func(sb, ap, params)
+ | `None:
+ fallbackfmt(sb, params, ty, ap)
+ ;;
+ ;;
+ | '}':
+ if decode(fmt) == '}'
+ sbputc(sb, '}')
+ ;;
+ | chr:
+ sbputc(sb, chr)
+ ;;
+:fmtdone
+ ;;
+ if nfmt != nparams
+ die("too many params for fmt\n")
+ ;;
+ -> sb.len
+}
+
+const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
+ /* value types */
+ var t_val : bool
+ var b_val : int8, ub_val : uint8
+ var w_val : int16, uw_val : uint16
+ var i_val : int32, ui_val : uint32
+ var l_val : int64, ul_val : uint64
+ var z_val : size
+ var p_val : byte#
+ var c_val : char
+ var s_val : byte[:]
+ var f32_val : flt32, f64_val : flt64
+ var i8 : int8, i16: int16, i32 : int32
+ var by : byte
+ var i : int, i64 : int64, l : long
+ var ui8 : int8, ui16: int16, ui32 : int32
+ var ui : int, ui64 : int64, ul : long
+
+ match typedesc(tyenc)
+ | `Tynone: /* nothing */
+ /* atomic types */
+ | `Tyvoid:
+ sbputs(sb, "void")
+ | `Tybool:
+ t_val = vanext(ap)
+ if t_val
+ sbputs(sb ,"true")
+ else
+ sbputs(sb, "false")
+ ;;
+ | `Tychar:
+ c_val = vanext(ap)
+ sbputc(sb, c_val)
+ | `Tyint8:
+ b_val = vanext(ap)
+ intfmt(sb, intparams(params), true, b_val)
+ | `Tyint16:
+ w_val = vanext(ap)
+ intfmt(sb, intparams(params), true, w_val)
+ | `Tyint:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), true, i_val)
+ | `Tyint32:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), true, i_val)
+ | `Tyint64:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), true, l_val)
+ | `Tylong:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), true, l_val)
+
+ | `Tybyte:
+ ub_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ub_val)
+ | `Tyuint8:
+ ub_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ub_val)
+ | `Tyuint16:
+ w_val = vanext(ap)
+ intfmt(sb, intparams(params), false, uw_val)
+ | `Tyuint:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ui_val)
+ | `Tyuint32:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ui_val)
+ | `Tyuint64:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ul_val)
+ | `Tyulong:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ul_val)
+ | `Tyflt32:
+ f32_val = vanext(ap)
+ flt32bfmt(sb, f32_val, MNormal, 0)
+ | `Tyflt64:
+ f64_val = vanext(ap)
+ flt64bfmt(sb, f64_val, MNormal, 0)
+ | `Tyvalist:
+ sbputs(sb, "...")
+
+ /* compound types */
+ | `Typtr desc:
+ p_val = vanext(ap)
+ sbputs(sb, "0x")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
+ | `Tyslice desc:
+ match typedesc(desc)
+ | `Tybyte:
+ s_val = vanext(ap)
+ sbputs(sb, s_val)
+ | _:
+ sbputs(sb, "slice[:]")
+ ;;
+ | `Tyfunc tc:
+ p_val = vanext(ap)
+ sbputs(sb, "func{")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
+ sbputs(sb, "}")
+ | `Tyarray (sz, data):
+ sbputs(sb, "array")
+ /* aggregate types */
+ | `Tytuple typecursor:
+ vabytes(ap)
+ sbputs(sb, "tuple")
+ | `Tystruct namecursor:
+ vabytes(ap)
+ sbputs(sb, "struct")
+ | `Tyunion namecursor:
+ vabytes(ap)
+ sbputs(sb, "union")
+ | `Tyname (name, desc):
+ fallbackfmt(sb, params, desc, ap)
+ ;;
+}
+
+type intparams = struct
+ base : size
+ padto : size
+ padfill : char
+;;
+
+const getparams = {fmt
+ var i
+
+ for i = 0; i < fmt.len; i++
+ if fmt[i] == '}' castto(byte)
+ goto foundparams
+ ;;
+ ;;
+ die("invalid format string")
+:foundparams
+ -> (fmt[:i], fmt[i+1:])
+}
+
+const intparams = {params
+ var ip : intparams
+ var c
+
+ ip = [
+ .base = 10,
+ .padfill = ' ',
+ .padto = 0
+ ]
+
+ while params.len > 0
+ (c, params) = striter(params)
+ match c
+ | 'x': ip.base = 16
+ | '0': ip.padfill = '0'
+ | chr:
+ while isdigit(c)
+ ip.padto = ip.padto*10 + charval(c, 10)
+ (c, params) = striter(params)
+ ;;
+ ;;
+ ;;
+ -> ip
+
}
const digitchars = [
- '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
+ '0','1','2','3','4',
+ '5','6','7','8','9',
+ 'a','b','c','d','e','f'
]
-generic intfmt = {buf : byte[:], bits : @a::(integral,numeric), base, signed, padto, padfill
+generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
var isneg
var val
var b : char[32]
- var i, j, n, npad
+ var i, j, npad
+ var base
- n = 0
- i = 0
+ base = opts.base castto(uint64)
if signed && bits < 0
val = -bits castto(uint64)
isneg = true
@@ -115,6 +365,7 @@ generic intfmt = {buf : byte[:], bits : @a::(integral,numeric), base, signed, pa
isneg = false
;;
+ i = 0
if val == 0
b[0] = '0'
i++
@@ -125,192 +376,35 @@ generic intfmt = {buf : byte[:], bits : @a::(integral,numeric), base, signed, pa
i++
;;
- npad = clamp(padto - i, 0, padto)
- n = 0
+ npad = clamp(opts.padto - i, 0, opts.padto)
if isneg
npad--
;;
- if padfill == '0' && isneg && n < buf.len
- n += encode(buf[n:], '-')
+ if opts.padfill == '0' && isneg
+ sbputc(sb, '-')
;;
- for j = 0; j < min(npad, buf.len); j++
- if n >= buf.len
- break
- ;;
- n += encode(buf[n:], padfill)
+ for j = 0; j < npad; j++
+ sbputc(sb, opts.padfill)
;;
- if padfill != '0' && isneg && n < buf.len
- n += encode(buf[n:], '-')
+ if opts.padfill != '0' && isneg
+ sbputc(sb, '-')
;;
for j = i; j != 0; j--
- if n >= buf.len
- break
- ;;
- n += encode(buf[n:], b[j - 1])
+ sbputc(sb, b[j - 1])
;;
- -> n
}
-/* formats a string of text as specified by 'fmt' into 'buf',
- using a valist for the arguments */
-const bfmtv = {buf, fmt, ap
- var b
- var c
- var n
- var padto
- var base
- var signed, padfill
- var s_val : byte[:]
- var t_val : bool
- var b_val : int8, ub_val : uint8
- var w_val : int16, uw_val : uint16
- var i_val : int32, ui_val : uint32
- var l_val : int64, ul_val : uint64
- var z_val : size
- var p_val : byte#
- var c_val : char
- var f_val : flt64, F_val : flt32
- var sb
+const writeall = {fd, buf
+ var n, len
- n = 0
- while fmt.len != 0
- (c, fmt) = striter(fmt)
- if c == '%'
- base = 10
- padto = 0
- signed = true
- padfill = ' '
- (c, fmt) = striter(fmt)
- /* modifiers */
- while fmt.len > 0
- match c
- | 'x':
- (c, fmt) = striter(fmt)
- base = 16
- signed = false
-
- | 'u':
- (c, fmt) = striter(fmt)
- signed = false
- | '0':
- (c, fmt) = striter(fmt)
- padfill = '0'
- | _: /* nothing */
- ;;
- if isdigit(c) && padto == 0
- /*
- We can't get a 0 on the first iteration, since
- that was matched above. So, no special checks
- for nonzero on the first iteration.
- */
- padto = 0
- while isdigit(c)
- padto = padto*10 + charval(c, 10)
- (c, fmt) = striter(fmt)
- ;;
- else
- break
- ;;
- ;;
- /* format specifiers */
- match c
- | 's':
- s_val = vanext(&ap)
- n += strfmt(buf[n:], s_val, padto, padfill)
- | 't':
- t_val = vanext(&ap)
- n += boolfmt(buf[n:], t_val, padto, padfill)
- | 'f':
- f_val = vanext(&ap)
- b = buf[n:]
- sb = mkbufsb(b)
- flt64bfmt(sb, f_val, 0, b.len)
- n += sb.len
- strfmt(buf[n:], sbfin(sb), 0, ' ')
- | 'F':
- F_val = vanext(&ap)
- b = buf[n:]
- sb = mkbufsb(b)
- flt64bfmt(sb, F_val castto(flt64), 0, b.len)
- n += sb.len
- strfmt(buf[n:], sbfin(sb), 0, ' ')
- /* format integers */
- | 'b':
- if signed
- b_val = vanext(&ap)
- n += intfmt(buf[n:], b_val, base, signed, padto, padfill)
- else
- ub_val = vanext(&ap)
- n += intfmt(buf[n:], ub_val, base, signed, padto, padfill)
- ;;
- | 'w':
- if signed
- w_val = vanext(&ap)
- n += intfmt(buf[n:], w_val, base, signed, padto, padfill)
- else
- uw_val = vanext(&ap)
- n += intfmt(buf[n:], uw_val, base, signed, padto, padfill)
- ;;
- | 'i':
- if signed
- i_val = vanext(&ap)
- n += intfmt(buf[n:], i_val, base, signed, padto, padfill)
- else
- ui_val = vanext(&ap)
- n += intfmt(buf[n:], ui_val, base, signed, padto, padfill)
- ;;
- | 'l':
- if signed
- l_val = vanext(&ap)
- n += intfmt(buf[n:], l_val, base, signed, padto, padfill)
- else
- ul_val = vanext(&ap)
- n += intfmt(buf[n:], ul_val, base, signed, padto, padfill)
- ;;
-
- | 'z':
- z_val = vanext(&ap)
- n += intfmt(buf[n:], z_val castto(int64), base, signed, padto, padfill)
- | 'p':
- p_val = vanext(&ap)
- n += intfmt(buf[n:], p_val castto(int64), 16, false, padto, padfill)
- | 'c':
- c_val = vanext(&ap)
- n += encode(buf[n:], c_val)
- | '%':
- n += encode(buf[n:], '%')
- | _:
- die("Unknown format specifier\n")
- ;;
- else
- n += encode(buf[n:], c)
+ len = 0
+ while true
+ n = write(fd, buf)
+ if n <= 0 || n >= len
+ break
;;
+ len += n
;;
- -> buf[:n]
-}
-
-const strfmt = {buf, str, padto, padfill
- var i, n, npad
-
- n = 0
- npad = clamp(padto - str.len, 0, padto)
- for i = 0; i < padto - str.len; i++
- n += encode(buf[n:], padfill)
- ;;
- for i = 0; i < min(str.len, buf.len); i++
- buf[n++] = str[i]
- ;;
- -> n
-}
-
-const boolfmt = {buf, val, padto, padfill
- var s
-
- if val
- s = "true"
- else
- s = "false"
- ;;
- -> strfmt(buf, s, padto, padfill)
+ -> len
}
diff --git a/libstd/fmt2.myr b/libstd/fmt2.myr
deleted file mode 100644
index bc1de45..0000000
--- a/libstd/fmt2.myr
+++ /dev/null
@@ -1,412 +0,0 @@
-use "alloc.use"
-use "chartype.use"
-use "die.use"
-use "extremum.use"
-use "fltfmt.use"
-use "htab.use"
-use "hashfuncs.use"
-use "introspect.use"
-use "option.use"
-use "strbuf.use"
-use "syswrap-ss.use"
-use "syswrap.use"
-use "types.use"
-use "utf.use"
-use "varargs.use"
-
-use "fmt.use" /* FOR DEBUGGING */
-
-pkg std =
- /* write to fd */
- const f2put : (fmt : byte[:], args : ... -> size)
- const f2fput : (fd : fd, fmt : byte[:], args : ... -> size)
- const f2putv : (fmt : byte[:], ap : valist# -> size)
- const f2fputv : (fd : fd, fmt : byte[:], ap : valist# -> size)
-
- /* write to buffer */
- const f2fmt : (fmt : byte[:], args : ... -> byte[:])
- const f2fmtv : (fmt : byte[:], ap : valist# -> byte[:])
- const f2bfmt : (buf : byte[:], fmt : byte[:], args : ... -> byte[:])
- const f2bfmtv : (buf : byte[:], fmt : byte[:], ap : valist# -> byte[:])
-
- /* write to strbuf */
- const f2sbfmt : (buf : strbuf#, fmt : byte[:], args : ... -> size)
- const f2sbfmtv : (buf : strbuf#, fmt : byte[:], ap : valist# -> size)
-
- /* add a formatter function */
- const f2fmtinstall : (ty : byte[:], \
- fn : (sb : strbuf#, ap : valist#, opts : byte[:] -> void) \
- -> void)
-
- $noret const f2fatal : (fmt : byte[:], args : ... -> void)
- $noret const f2fatalv : (fmt : byte[:], ap : valist# -> void)
-;;
-
-/* same as 'put', but exits the program after printing */
-const f2fatal = {fmt, args
- var ap
-
- ap = vastart(&args)
- f2putv(fmt, &ap)
- exit(1)
-}
-
-/* same as 'putv', but exits the program after printing */
-const f2fatalv = {fmt, ap
- f2putv(fmt, ap)
- exit(1)
-}
-
-var fmtmapinited : bool = false
-var fmtmap : htab(byte[:], (sb : strbuf#, ap : valist#, opts : byte[:] -> void))#
-
-const f2fmtinstall = {ty, fn
- if !fmtmapinited
- fmtmapinited = true
- fmtmap = mkht(strhash, streq)
- ;;
- htput(fmtmap, ty, fn)
-}
-
-const f2put = {fmt, args
- var ap
-
- ap = vastart(&args)
- -> f2fputv(1, fmt, &ap)
-}
-
-const f2putv = {fmt, ap
- -> f2fputv(1, fmt, ap)
-}
-
-const f2fput = {fd, fmt, args
- var ap
-
- ap = vastart(&args)
- -> f2fputv(fd, fmt, &ap)
-}
-
-const f2fputv = {fd, fmt, ap
- var sb, s
-
- sb = mksb()
- f2sbfmtv(sb, fmt, ap)
- s = sbfin(sb)
- -> writeall(fd, s)
-}
-
-const f2fmt = {fmt, args
- var ap
-
- ap = vastart(&args)
- -> f2fmtv(fmt, &ap)
-}
-
-const f2fmtv = {fmt, ap
- var sb
-
- sb = mksb()
- f2sbfmtv(sb, fmt, ap)
- -> sbfin(sb)
-}
-
-const f2bfmt = {buf, fmt, args
- var ap
-
- ap = vastart(&args)
- -> f2bfmtv(buf, fmt, &ap)
-}
-
-const f2bfmtv = {buf, fmt, ap
- var sb
-
- sb = mkbufsb(buf)
- f2sbfmtv(sb, fmt, ap)
- -> sbfin(sb)
-}
-
-const f2sbfmt = {sb, fmt, args
- var ap
-
- ap = vastart(&args)
- -> f2sbfmtv(sb, fmt, &ap)
-}
-
-const f2sbfmtv = {sb, fmt, ap -> size
- var c, params, ty
- var nfmt, nparams
-
- nparams = ap.tc.nelt
- nfmt = 0
- if !fmtmapinited
- fmtmapinited = true
- fmtmap = mkht(strhash, streq)
- ;;
- while fmt.len != 0
- (c, fmt) = striter(fmt)
- match c
- | '{':
- if decode(fmt) == '{'
- (c, fmt) = striter(fmt)
- sbputc(sb, '{')
- else
- (params, fmt) = f2getparams(fmt)
- nfmt++
- if nfmt > nparams
- die("too few params for fmt\n")
- ;;
-
- ty = vatype(ap)
- match htget(fmtmap, ty)
- | `Some func:
- func(sb, ap, params)
- | `None:
- fallbackfmt(sb, params, ty, ap)
- ;;
- ;;
- | '}':
- if decode(fmt) == '}'
- sbputc(sb, '}')
- ;;
- | chr:
- sbputc(sb, chr)
- ;;
-:fmtdone
- ;;
- if nfmt != nparams
- die("too many params for fmt\n")
- ;;
- -> sb.len
-}
-
-const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
- /* value types */
- var t_val : bool
- var b_val : int8, ub_val : uint8
- var w_val : int16, uw_val : uint16
- var i_val : int32, ui_val : uint32
- var l_val : int64, ul_val : uint64
- var z_val : size
- var p_val : byte#
- var c_val : char
- var s_val : byte[:]
- var f32_val : flt32, f64_val : flt64
- var i8 : int8, i16: int16, i32 : int32
- var by : byte
- var i : int, i64 : int64, l : long
- var ui8 : int8, ui16: int16, ui32 : int32
- var ui : int, ui64 : int64, ul : long
-
- match typedesc(tyenc)
- | `Tynone: /* nothing */
- /* atomic types */
- | `Tyvoid:
- sbputs(sb, "void")
- | `Tybool:
- t_val = vanext(ap)
- if t_val
- sbputs(sb ,"true")
- else
- sbputs(sb, "false")
- ;;
- | `Tychar:
- c_val = vanext(ap)
- sbputc(sb, c_val)
- | `Tyint8:
- b_val = vanext(ap)
- intfmt(sb, intparams(params), true, b_val)
- | `Tyint16:
- w_val = vanext(ap)
- intfmt(sb, intparams(params), true, w_val)
- | `Tyint:
- i_val = vanext(ap)
- intfmt(sb, intparams(params), true, i_val)
- | `Tyint32:
- i_val = vanext(ap)
- intfmt(sb, intparams(params), true, i_val)
- | `Tyint64:
- l_val = vanext(ap)
- intfmt(sb, intparams(params), true, l_val)
- | `Tylong:
- l_val = vanext(ap)
- intfmt(sb, intparams(params), true, l_val)
-
- | `Tybyte:
- ub_val = vanext(ap)
- intfmt(sb, intparams(params), false, ub_val)
- | `Tyuint8:
- ub_val = vanext(ap)
- intfmt(sb, intparams(params), false, ub_val)
- | `Tyuint16:
- w_val = vanext(ap)
- intfmt(sb, intparams(params), false, uw_val)
- | `Tyuint:
- i_val = vanext(ap)
- intfmt(sb, intparams(params), false, ui_val)
- | `Tyuint32:
- i_val = vanext(ap)
- intfmt(sb, intparams(params), false, ui_val)
- | `Tyuint64:
- l_val = vanext(ap)
- intfmt(sb, intparams(params), false, ul_val)
- | `Tyulong:
- l_val = vanext(ap)
- intfmt(sb, intparams(params), false, ul_val)
- | `Tyflt32:
- f32_val = vanext(ap)
- flt32bfmt(sb, f32_val, MNormal, 0)
- | `Tyflt64:
- f64_val = vanext(ap)
- flt64bfmt(sb, f64_val, MNormal, 0)
- | `Tyvalist:
- sbputs(sb, "...")
-
- /* compound types */
- | `Typtr desc:
- p_val = vanext(ap)
- sbputs(sb, "0x")
- intfmt(sb, \
- [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
- false, p_val castto(intptr))
- | `Tyslice desc:
- match typedesc(desc)
- | `Tybyte:
- s_val = vanext(ap)
- sbputs(sb, s_val)
- | _:
- sbputs(sb, "slice[:]")
- ;;
- | `Tyfunc tc:
- p_val = vanext(ap)
- sbputs(sb, "func{")
- intfmt(sb, \
- [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
- false, p_val castto(intptr))
- sbputs(sb, "}")
- | `Tyarray (sz, data):
- sbputs(sb, "array")
- /* aggregate types */
- | `Tytuple typecursor:
- vabytes(ap)
- sbputs(sb, "tuple")
- | `Tystruct namecursor:
- vabytes(ap)
- sbputs(sb, "struct")
- | `Tyunion namecursor:
- vabytes(ap)
- sbputs(sb, "union")
- | `Tyname (name, desc):
- fallbackfmt(sb, params, desc, ap)
- ;;
-}
-
-type intparams = struct
- base : size
- padto : size
- padfill : char
-;;
-
-const f2getparams = {fmt
- var i
-
- for i = 0; i < fmt.len; i++
- if fmt[i] == '}' castto(byte)
- goto foundparams
- ;;
- ;;
- die("invalid format string")
-:foundparams
- -> (fmt[:i], fmt[i+1:])
-}
-
-const intparams = {params
- var ip : intparams
- var c
-
- ip = [
- .base = 10,
- .padfill = ' ',
- .padto = 0
- ]
-
- while params.len > 0
- (c, params) = striter(params)
- match c
- | 'x': ip.base = 16
- | '0': ip.padfill = '0'
- | chr:
- while isdigit(c)
- ip.padto = ip.padto*10 + charval(c, 10)
- (c, params) = striter(params)
- ;;
- ;;
- ;;
- -> ip
-
-}
-
-const digitchars = [
- '0','1','2','3','4',
- '5','6','7','8','9',
- 'a','b','c','d','e','f'
-]
-generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
- var isneg
- var val
- var b : char[32]
- var i, j, npad
- var base
-
- base = opts.base castto(uint64)
- if signed && bits < 0
- val = -bits castto(uint64)
- isneg = true
- else
- val = bits castto(uint64)
- val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
- isneg = false
- ;;
-
- i = 0
- if val == 0
- b[0] = '0'
- i++
- ;;
- while val != 0
- b[i] = digitchars[val % base]
- val /= base
- i++
- ;;
-
- npad = clamp(opts.padto - i, 0, opts.padto)
- if isneg
- npad--
- ;;
- if opts.padfill == '0' && isneg
- sbputc(sb, '-')
- ;;
- for j = 0; j < npad; j++
- sbputc(sb, opts.padfill)
- ;;
- if opts.padfill != '0' && isneg
- sbputc(sb, '-')
- ;;
- for j = i; j != 0; j--
- sbputc(sb, b[j - 1])
- ;;
-}
-
-const writeall = {fd, buf
- var n, len
-
- len = 0
- while true
- n = write(fd, buf)
- if n <= 0 || n >= len
- break
- ;;
- len += n
- ;;
- -> len
-}
-
diff --git a/libstd/htab.myr b/libstd/htab.myr
index c8cb694..e9880b9 100644
--- a/libstd/htab.myr
+++ b/libstd/htab.myr
@@ -1,7 +1,6 @@
use "alloc.use"
use "die.use"
use "extremum.use"
-use "fmt.use"
use "option.use"
use "types.use"
diff --git a/libstd/intparse.myr b/libstd/intparse.myr
index 685c6cd..4180f8c 100644
--- a/libstd/intparse.myr
+++ b/libstd/intparse.myr
@@ -1,6 +1,5 @@
use "chartype.use"
use "die.use"
-use "fmt.use"
use "hasprefix.use"
use "option.use"
use "types.use"
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index 888b72f..cd22801 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -1,7 +1,7 @@
use "alloc.use"
use "die.use"
use "extremum.use"
-use "fmt2.use"
+use "fmt.use"
use "option.use"
use "sleq.use"
use "slpush.use"
@@ -62,7 +62,7 @@ const optparse = {args, def
parsed.opts = slpush(parsed.opts, optnext(&ctx))
;;
if ctx.args.len < def.minargs
- f2put("error: expected at least {} args, got {}\n", def.minargs, ctx.args.len)
+ put("error: expected at least {} args, got {}\n", def.minargs, ctx.args.len)
optusage(ctx.optargs[0], ctx.optdef)
exit(1)
;;
@@ -97,7 +97,7 @@ const optnext = {ctx
optusage(ctx.optargs[0], ctx.optdef)
exit(0)
else
- f2fatal("unexpected argument '{}'\n", c)
+ fatal("unexpected argument '{}'\n", c)
;;
| `Some (true, needed):
/* -arg => '-a' 'rg' */
@@ -111,7 +111,7 @@ const optnext = {ctx
ctx.argidx++
next(ctx)
elif needed
- f2put("Expected argument for {}\n", c)
+ put("Expected argument for {}\n", c)
exit(1)
;;
| `Some (false, _):
@@ -162,15 +162,15 @@ const next = {ctx
}
const optusage = {prog, def
- std.f2put("usage: {} [-h?] ", prog)
+ std.put("usage: {} [-h?] ", prog)
for o in def.opts
- std.f2put("[-{}{}{}] ", o.opt, sep(o.arg), o.arg)
+ std.put("[-{}{}{}] ", o.opt, sep(o.arg), o.arg)
;;
- std.f2put("{}\n", def.argdesc)
- std.f2put("\t-h\tprint this help message\n")
- std.f2put("\t-?\tprint this help message\n")
+ std.put("{}\n", def.argdesc)
+ std.put("\t-h\tprint this help message\n")
+ std.put("\t-?\tprint this help message\n")
for o in def.opts
- std.f2put("\t-{}{}{}\t{}\n", o.opt, sep(o.arg), o.arg, o.desc)
+ std.put("\t-{}{}{}\t{}\n", o.opt, sep(o.arg), o.arg, o.desc)
;;
}
diff --git a/libstd/pathjoin.myr b/libstd/pathjoin.myr
index 3e05905..f9df8aa 100644
--- a/libstd/pathjoin.myr
+++ b/libstd/pathjoin.myr
@@ -92,7 +92,7 @@ const pathnorm = {p
;;
;;
s = strjoin(comps[i:], "/")
- ret = fmt("/%s", s)
+ ret = fmt("/{}", s)
slfree(s)
elif comps.len == 0
ret = sldup(".")
diff --git a/libstd/rand.myr b/libstd/rand.myr
index 06599d6..2e8c73e 100644
--- a/libstd/rand.myr
+++ b/libstd/rand.myr
@@ -1,5 +1,4 @@
use "die.use"
-use "fmt.use"
use "types.use"
use "alloc.use"
/*
diff --git a/libstd/resolve+posixy.myr b/libstd/resolve+posixy.myr
index 3a4fc42..e4cf555 100644
--- a/libstd/resolve+posixy.myr
+++ b/libstd/resolve+posixy.myr
@@ -5,7 +5,6 @@ use "die.use"
use "endian.use"
use "result.use"
use "extremum.use"
-use "fmt.use"
use "hashfuncs.use"
use "htab.use"
use "ipparse.use"
diff --git a/libstd/search.myr b/libstd/search.myr
index 26b9870..725e419 100644
--- a/libstd/search.myr
+++ b/libstd/search.myr
@@ -1,6 +1,5 @@
use "cmp.use"
use "option.use"
-use "fmt.use"
pkg std =
generic lsearch : (sl : @t[:], val : @t, cmp : (a : @t, b : @t -> order) -> option(@idx::(integral,numeric)))
diff --git a/libstd/slput.myr b/libstd/slput.myr
index c1a7d33..88dc875 100644
--- a/libstd/slput.myr
+++ b/libstd/slput.myr
@@ -1,7 +1,6 @@
use "types.use"
use "alloc.use"
use "die.use"
-use "fmt.use"
pkg std =
generic slput : (sl : @a[:], idx : size, elt : @a -> @a[:])
diff --git a/libstd/slurp.myr b/libstd/slurp.myr
index c4ebb2c..8636f40 100644
--- a/libstd/slurp.myr
+++ b/libstd/slurp.myr
@@ -2,7 +2,6 @@ use "alloc.use"
use "die.use"
use "result.use"
use "extremum.use"
-use "fmt.use"
use "syswrap.use"
use "types.use"
diff --git a/libstd/spork.myr b/libstd/spork.myr
index f728768..64e8fce 100644
--- a/libstd/spork.myr
+++ b/libstd/spork.myr
@@ -53,7 +53,7 @@ const sporkfd = {cmd, infd, outfd
close(infd)
close(outfd)
execvp(cmd[0], cmd) < 0
- fatal("failed to exec %s\n")
+ fatal("failed to exec {}\n", cmd[0])
/* parent */
else
-> `Ok pid
diff --git a/libstd/strsplit.myr b/libstd/strsplit.myr
index b09e8c8..e64d907 100644
--- a/libstd/strsplit.myr
+++ b/libstd/strsplit.myr
@@ -1,7 +1,6 @@
use "alloc.use"
use "die.use"
use "extremum.use"
-use "fmt.use"
use "option.use"
use "slpush.use"
use "strfind.use"