ref: dbe9ae0f9dbed30d40952261384b4673766d83ca
parent: ff991b5eb1358c01af30fa167d82d0e367ac464a
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jan 26 20:03:56 EST 2018
Optimize int formatting a bit.
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -276,37 +276,37 @@
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 @@
| `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 @@
| `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 @@
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 @@
| '\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 @@
-> 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 @@
;;
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 @@
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])
;;
}