ref: 8fef0e65ac05920b30d13ef4816dd166c3c83c47
parent: 4ca93982fdef1ddb426b5595f814f3099adfdc65
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jun 6 13:58:36 EDT 2015
Support formatting params for ints.
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -85,19 +85,31 @@
const f2sbfmtv = {sb, fmt, ap -> size
var c, params
+ var nfmt, nparams
+ nparams = ap.tc.nelt
+ nfmt = 0
while fmt.len != 0
(c, fmt) = striter(fmt)
match c
| '%':
+ nfmt++
+ if nfmt > nparams
+ die("too few params for fmt\n")
+ ;;
+
if fmt.len > 1 && fmt[0] == '{' castto(byte)
(params, fmt) = f2getparams(fmt[1:])
;;
ap = fallbackfmt(sb, params, vatype(ap), ap)
+
| chr:
sbputc(sb, chr)
;;
;;
+ if nfmt != nparams
+ die("too many params for fmt\n")
+ ;;
-> sb.len
}
@@ -136,44 +148,44 @@
sbputc(sb, c_val)
| `Tyint8:
(b_val, ap) = vanext(ap)
- intfmt(sb, params, true, b_val)
+ intfmt(sb, intparams(params), true, b_val)
| `Tyint16:
(w_val, ap) = vanext(ap)
- intfmt(sb, params, true, w_val)
+ intfmt(sb, intparams(params), true, w_val)
| `Tyint:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, true, i_val)
+ intfmt(sb, intparams(params), true, i_val)
| `Tyint32:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, true, i_val)
+ intfmt(sb, intparams(params), true, i_val)
| `Tyint64:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, true, l_val)
+ intfmt(sb, intparams(params), true, l_val)
| `Tylong:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, true, l_val)
+ intfmt(sb, intparams(params), true, l_val)
| `Tybyte:
(ub_val, ap) = vanext(ap)
- intfmt(sb, params, false, ub_val)
+ intfmt(sb, intparams(params), false, ub_val)
| `Tyuint8:
(ub_val, ap) = vanext(ap)
- intfmt(sb, params, false, ub_val)
+ intfmt(sb, intparams(params), false, ub_val)
| `Tyuint16:
(w_val, ap) = vanext(ap)
- intfmt(sb, params, false, uw_val)
+ intfmt(sb, intparams(params), false, uw_val)
| `Tyuint:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, false, ui_val)
+ intfmt(sb, intparams(params), false, ui_val)
| `Tyuint32:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, false, ui_val)
+ intfmt(sb, intparams(params), false, ui_val)
| `Tyuint64:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, false, ul_val)
+ intfmt(sb, intparams(params), false, ul_val)
| `Tyulong:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, false, ul_val)
+ intfmt(sb, intparams(params), false, ul_val)
| `Tyflt32:
(l_val, ap) = vanext(ap)
sbputs(sb, "flt32")
@@ -185,7 +197,10 @@
/* compound types */
| `Typtr desc:
(p_val, ap) = vanext(ap)
- intfmt(sb, params, false, p_val castto(intptr))
+ sbputs(sb, "0x")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
| `Tyslice desc:
match typedesc(desc)
| `Tybyte:
@@ -195,10 +210,14 @@
sbputs(sb, "slice[:]")
;;
| `Tyfunc tc:
- sbputs(sb, "func")
+ (p_val, ap) = 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:
sbputs(sb, "tuple")
@@ -212,6 +231,11 @@
-> ap
}
+type intparams = struct
+ base : size
+ padto : size
+ padfill : char
+;;
const f2getparams = {fmt
var i
@@ -226,6 +250,32 @@
-> (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',
@@ -236,12 +286,10 @@
var val
var b : char[32]
var i, j, npad
- var base, padto, padfill
+ var base
- i = 0
- base = 10
- padto = 0
- padfill = '0'
+ base = opts.base castto(uint64)
+ std.put("base = %l\n", base)
if signed && bits < 0
val = -bits castto(uint64)
isneg = true
@@ -251,6 +299,7 @@
isneg = false
;;
+ i = 0
if val == 0
b[0] = '0'
i++
@@ -261,17 +310,17 @@
i++
;;
- npad = clamp(padto - i, 0, padto)
+ npad = clamp(opts.padto - i, 0, opts.padto)
if isneg
npad--
;;
- if padfill == '0' && isneg
+ if opts.padfill == '0' && isneg
sbputc(sb, '-')
;;
for j = 0; j < npad; j++
- sbputc(sb, padfill)
+ sbputc(sb, opts.padfill)
;;
- if padfill != '0' && isneg
+ if opts.padfill != '0' && isneg
sbputc(sb, '-')
;;
for j = i; j != 0; j--
--- a/libstd/varargs.myr
+++ b/libstd/varargs.myr
@@ -8,6 +8,7 @@
const vastart : (args : ...# -> valist)
const vatype : (ap : valist -> byte[:])
+ const vaskip : (ap : valist -> byte[:])
generic vanext : (ap : valist -> (@a, valist))
;;