summaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-01-27 01:03:56 -0800
committerOri Bernstein <ori@eigenstate.org>2018-01-27 01:03:56 -0800
commitdbe9ae0f9dbed30d40952261384b4673766d83ca (patch)
tree962767ae0ae26d5da8ebdcfba5fab3b00ed53d94 /lib/std
parentff991b5eb1358c01af30fa167d82d0e367ac464a (diff)
downloadmc-dbe9ae0f9dbed30d40952261384b4673766d83ca.tar.gz
Optimize int formatting a bit.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fmt.myr70
1 files changed, 33 insertions, 37 deletions
diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr
index 005d1d3..663f0f9 100644
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -276,37 +276,37 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
sbputc(sb, val)
| `Tyint8:
var val : int8 = vanext(ap)
- intfmt(sb, intparams(params), true, val)
+ intfmt(sb, intparams(params), true, (val : uint64), 8)
| `Tyint16:
var val : int16 = vanext(ap)
- intfmt(sb, intparams(params), true, val)
+ intfmt(sb, intparams(params), true, (val : uint64), 16)
| `Tyint:
var val : int = vanext(ap)
- intfmt(sb, intparams(params), true, val)
+ intfmt(sb, intparams(params), true, (val : uint64), 32)
| `Tyint32:
var val : int32 = vanext(ap)
- intfmt(sb, intparams(params), true, val)
+ intfmt(sb, intparams(params), true, (val : uint64), 32)
| `Tyint64:
var val : int64 = vanext(ap)
- intfmt(sb, intparams(params), true, val)
+ intfmt(sb, intparams(params), true, (val : uint64), 64)
| `Tybyte:
var val : byte = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 8)
| `Tyuint8:
var val : uint8 = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 8)
| `Tyuint16:
var val : uint16 = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 16)
| `Tyuint:
var val : uint = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 32)
| `Tyuint32:
var val : uint32 = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 32)
| `Tyuint64:
var val : uint64 = vanext(ap)
- intfmt(sb, intparams(params), false, val)
+ intfmt(sb, intparams(params), false, (val : uint64), 64)
| `Tyflt32:
var val : flt32 = vanext(ap)
flt32bfmt(sb, val, MNormal, 0)
@@ -320,9 +320,9 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
| `Typtr desc:
var val : void# = vanext(ap)
sbputs(sb, "0x")
- intfmt(sb, \
- [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
- false, (val : intptr))
+ intfmt(sb,
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'],
+ false, (val : uint64), 64)
| `Tyslice desc:
match typedesc(desc)
| `Tybyte:
@@ -336,13 +336,13 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
| `Tyfunc tc:
var val : intptr[2] = vanext(ap)
sbputs(sb, "func{")
- intfmt(sb, \
- [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
- false, (val[0] : intptr))
+ intfmt(sb,
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'],
+ false, (val[0] : uint64), 0)
sbputs(sb, ", ")
- intfmt(sb, \
- [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
- false, (val[1] : intptr))
+ intfmt(sb,
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'],
+ false, (val[1] : uint64), 0)
sbputs(sb, "}")
| `Tyarray (sz, desc):
subap = vaenter(ap)
@@ -498,7 +498,7 @@ const strfmt = {sb, str, params
if esc
sbputs(sb, "\\x")
;;
- intfmt(sb, [.padto=2, .padfill='0', .base=16], false, b)
+ intfmt(sb, [.padto=2, .padfill='0', .base=16], false, (b : uint64), 8)
;;
elif esc
for b : str
@@ -516,13 +516,13 @@ const strfmt = {sb, str, params
| '\0': sbputs(sb, "\\0")
| _:
sbputs(sb, "\\x")
- intfmt(sb, [.padto=2, .padfill='0', .base=16], false, b)
+ intfmt(sb, [.padto=2, .padfill='0', .base=16], false, (b : uint64), 8)
;;
;;
;;
else
- for i = 0; i < w - strcellwidth(str); i++
- sbputc(sb, p)
+ for i = 0; i < w - strcellwidth(str); i++
+ sbputc(sb, p)
;;
sbputs(sb, str)
;;
@@ -532,20 +532,16 @@ const isprint = {b
-> b >= (' ' : byte) && b < ('~' : byte)
}
-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 @a
+const digitchars = "0123456789abcdef"
+const intfmt = {sb, opts, signed, bits : uint64, nbits
var isneg
var sval, val
- var b : char[32]
+ var b : byte[32]
var i, j, npad
var base
base = (opts.base : uint64)
- if signed && bits < 0
+ if signed && bits >= 1 << (nbits - 1)
sval = -(bits : int64)
val = (sval : uint64)
isneg = true
@@ -557,13 +553,13 @@ generic intfmt = {sb, opts, signed, bits : @a :: integral,numeric @a
;;
else
val = (bits : uint64)
- val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
+ val &= ~0 >> nbits
isneg = false
;;
i = 0
if val == 0
- b[0] = '0'
+ b[0] = ('0' : byte)
i++
;;
while val != 0
@@ -577,16 +573,16 @@ generic intfmt = {sb, opts, signed, bits : @a :: integral,numeric @a
npad--
;;
if opts.padfill == '0' && isneg
- sbputc(sb, '-')
+ sbputb(sb, ('-' : byte))
;;
for j = 0; j < npad; j++
sbputc(sb, opts.padfill)
;;
if opts.padfill != '0' && isneg
- sbputc(sb, '-')
+ sbputb(sb, ('-' : byte))
;;
for j = i; j != 0; j--
- sbputc(sb, b[j - 1])
+ sbputb(sb, b[j - 1])
;;
}