summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-06 21:38:10 -0700
committerOri Bernstein <ori@eigenstate.org>2015-06-06 21:38:10 -0700
commit4f57d78598daddaab2b204fe418cf217b541b70e (patch)
tree6fb3ad20d5371d239a89fe21ff59c6d40fea1176 /libstd
parent8fef0e65ac05920b30d13ef4816dd166c3c83c47 (diff)
downloadmc-4f57d78598daddaab2b204fe418cf217b541b70e.tar.gz
Convert fltfmt to use a strbuf.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/fltfmt.myr78
-rw-r--r--libstd/fmt.myr14
-rw-r--r--libstd/fmt2.myr7
-rw-r--r--libstd/strbuf.myr1
4 files changed, 54 insertions, 46 deletions
diff --git a/libstd/fltfmt.myr b/libstd/fltfmt.myr
index 9fe07cd..395ade4 100644
--- a/libstd/fltfmt.myr
+++ b/libstd/fltfmt.myr
@@ -1,43 +1,48 @@
+use "alloc.use"
use "bigint.use"
-use "types.use"
-use "fltbits.use"
+use "die.use"
use "extremum.use"
-use "utf.use"
-use "alloc.use"
+use "fltbits.use"
use "slpush.use"
-use "die.use"
+use "strbuf.use"
+use "types.use"
+use "utf.use"
pkg std =
- pkglocal const flt64bfmt : (buf : byte[:], val : flt64, mode : int, precision : int -> size)
- pkglocal const flt32bfmt : (buf : byte[:], val : flt32, mode : int, precision : int -> size)
+ pkglocal const MNormal = 0
+ pkglocal const MAbsolute = 1
+ pkglocal const MRelative = 2
+
+ pkglocal const flt64bfmt : (sb : strbuf#, val : flt64, mode : int, precision : int -> void)
+ pkglocal const flt32bfmt : (sb : strbuf#, val : flt32, mode : int, precision : int -> void)
;;
const Dblbias = 1023
const Fltbias = 127
-const flt64bfmt = {buf, val, mode, precision
+const flt64bfmt = {sb, val, mode, precision
var isneg, exp, mant
(isneg, mant, exp) = flt64explode(val)
- -> dragon4(buf, isneg, mant, (exp - 52) castto(int64), Dblbias, mode, precision)
+ dragon4(sb, isneg, mant, (exp - 52) castto(int64), Dblbias, mode, precision)
}
-const flt32bfmt = {buf, val, mode, precision
+const flt32bfmt = {sb, val, mode, precision
var isneg, exp, mant
(isneg, mant, exp) = flt32explode(val)
- -> dragon4(buf, isneg, mant castto(int64), (exp - 52) castto(int64), Fltbias, mode, precision)
+ dragon4(sb, isneg, mant castto(int64), (exp - 52) castto(int64), Fltbias, mode, precision)
}
/*
-buf: output buffer
+sb: output buffer
e: exponent
p: precision
f: mantissa
flting value: x = f^(e - p)
*/
-const dragon4 = {buf, isneg, f, e, p, mode, cutoff
+const dragon4 = {sb, isneg, f, e, p, mode, cutoff
var r, s, t, u, v, y
var udig
var mm, mp /* margins above and below */
@@ -49,13 +54,10 @@ const dragon4 = {buf, isneg, f, e, p, mode, cutoff
/* if we have zero for the mantissa, we can return early */
n = 0
if isneg
- n += encode(buf, '-')
+ sbputs(sb, "-")
;;
if f == 0
- n += encode(buf[n:], '0')
- n += encode(buf[n:], '.')
- n += encode(buf[n:], '0')
- -> n
+ sbputs(sb, "0.0")
;;
/* initialize */
@@ -112,13 +114,14 @@ const dragon4 = {buf, isneg, f, e, p, mode, cutoff
bigfree(u)
;;
;;
- if mode == 0
- cutoff = k - buf.len - 1
+ if mode == MNormal
+ cutoff = k
else
- if mode == 2
- cutoff += k
+ if mode == MRelative
+ cutoff += k - 1
;;
- a = cutoff - k
+ /* common between relative and absolute */
+ a = cutoff - k - 1
y = bigdup(s)
if a < 0
for i = 0; i < a; i++
@@ -156,8 +159,7 @@ const dragon4 = {buf, isneg, f, e, p, mode, cutoff
;;
if k <= 0
- n += encode(buf[n:], '0')
- n += encode(buf[n:], '.')
+ sbputs(sb, "0.")
;;
while true
k--
@@ -193,22 +195,22 @@ const dragon4 = {buf, isneg, f, e, p, mode, cutoff
if low || high || k == cutoff
break
;;
- n += format(buf[n:], lowdig(u), k)
+ format(sb, lowdig(u), k)
bigfree(u)
;;
/* format the last digit */
udig = lowdig(u)
if low && !high
- n += format(buf[n:], udig, k)
+ format(sb, udig, k)
elif high && !low
- n += format(buf[n:], udig + 1, k)
+ format(sb, udig + 1, k)
else
bigmuli(r, 2)
match bigcmp(r, s)
- | `Before: n += format(buf[n:], udig, k)
- | `Equal: n += format(buf[n:], udig, k)
- | `After: n += format(buf[n:], udig + 1, k)
+ | `Before: format(sb, udig, k)
+ | `Equal: format(sb, udig, k)
+ | `After: format(sb, udig + 1, k)
;;
;;
-> n
@@ -221,19 +223,17 @@ const lowdig = {u
-> 0
}
-const format = {buf, d, k
+const format = {sb, d, k
const dig = "0123456789"
- var n, i
+ var i
- n = 0
if k < 0
- for i = 0; i < -k; i++
- n += encode(buf[n:], '0')
+ for i = 0; i < -k - 1; i++
+ sbputs(sb, "0")
;;
;;
- buf[n++] = dig[d]
+ sbputb(sb, dig[d])
if k == 0
- n += encode(buf[n:], '.')
+ sbputs(sb, ".")
;;
- -> n
}
diff --git a/libstd/fmt.myr b/libstd/fmt.myr
index 37ae650..7b0b19f 100644
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -5,6 +5,7 @@ use "die.use"
use "extremum.use"
use "fltfmt.use"
use "introspect.use"
+use "strbuf.use"
use "syswrap-ss.use"
use "syswrap.use"
use "types.use"
@@ -169,6 +170,7 @@ const bfmtv = {buf, fmt, ap
var p_val : byte#
var c_val : char
var f_val : flt64, F_val : flt32
+ var sb
n = 0
while fmt.len != 0
@@ -221,11 +223,17 @@ const bfmtv = {buf, fmt, ap
| 'f':
(f_val, ap) = vanext(ap)
b = buf[n:]
- /* FIXME(ori): bug, b[n:].len fails since b[n:] isn't an lval */
- n += flt64bfmt(buf[n:], f_val, 0, b.len)
+ sb = mkbufsb(b)
+ flt64bfmt(sb, f_val, 0, b.len)
+ n += sb.len
+ strfmt(buf[n:], sbfin(sb), 0, ' ')
| 'F':
(F_val, ap) = vanext(ap)
- n += flt64bfmt(buf[n:], F_val castto(flt64), 0, b.len)
+ 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
diff --git a/libstd/fmt2.myr b/libstd/fmt2.myr
index 081fb93..b97052e 100644
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -187,10 +187,11 @@ const fallbackfmt = {sb, params, tyenc, ap
(l_val, ap) = vanext(ap)
intfmt(sb, intparams(params), false, ul_val)
| `Tyflt32:
- (l_val, ap) = vanext(ap)
- sbputs(sb, "flt32")
+ (f32_val, ap) = vanext(ap)
+ flt32bfmt(sb, f32_val, MNormal, 0)
| `Tyflt64:
- sbputs(sb, "flt64")
+ (f64_val, ap) = vanext(ap)
+ flt64bfmt(sb, f64_val, MNormal, 0)
| `Tyvalist:
sbputs(sb, "...")
diff --git a/libstd/strbuf.myr b/libstd/strbuf.myr
index 47de02e..097d1ce 100644
--- a/libstd/strbuf.myr
+++ b/libstd/strbuf.myr
@@ -4,7 +4,6 @@ use "extremum.use"
use "slcp.use"
use "types.use"
use "utf.use"
-use "fmt.use"
pkg std =
type strbuf = struct