summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-07-01 21:36:02 -0700
committerOri Bernstein <ori@eigenstate.org>2015-07-01 21:36:02 -0700
commitbd225cf9a9ec5a2d3bd35c067d011729d8fb9ae0 (patch)
tree33cac68e81a4e34f64cf472baffa1793fb0b4008 /libstd
parentaa419bde150ce4827ec127288876ce2d0a191629 (diff)
downloadmc-bd225cf9a9ec5a2d3bd35c067d011729d8fb9ae0.tar.gz
Add format funcs.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/bigint.myr20
-rw-r--r--libstd/bld.sub1
-rw-r--r--libstd/fmtfuncs.myr54
3 files changed, 55 insertions, 20 deletions
diff --git a/libstd/bigint.myr b/libstd/bigint.myr
index 5fd9966..719fd69 100644
--- a/libstd/bigint.myr
+++ b/libstd/bigint.myr
@@ -27,7 +27,6 @@ pkg std =
const bigmove : (d : bigint#, s : bigint# -> bigint#)
const bigparse : (s : byte[:] -> option(bigint#))
const bigclear : (a : bigint# -> bigint#)
- const bigfmt : (a : bigint#, base : int -> byte[:])
const bigbfmt : (b : byte[:], a : bigint#, base : int -> size)
/*
const bigtoint : (a : bigint# -> @a::(numeric,integral))
@@ -117,25 +116,6 @@ const bigclear = {v
-> v
}
-const bigfmt = {a, base
- var buf
- var n
-
- /*
- allocate a buffer guaranteed to be big enough.
- that's
- 2 + floor(nbits/(log_2(10)))
- or
- 2 + a.dig.len * 32/3.32...
- or
- 2 + a.dig.len * 10
- plus one for the - sign.
- */
- buf = slalloc(3 + a.dig.len * 10)
- n = bigbfmt(buf, a, base)
- -> buf[:n]
-}
-
/* for now, just dump out something for debugging... */
const bigbfmt = {buf, x, base
const digitchars = [
diff --git a/libstd/bld.sub b/libstd/bld.sub
index 8158ce3..b84be64 100644
--- a/libstd/bld.sub
+++ b/libstd/bld.sub
@@ -40,6 +40,7 @@ lib std {inc=.} =
fltbits.myr
fltfmt.myr
fmt.myr
+ fmtfuncs.myr
getcwd.myr
getint.myr
hashfuncs.myr
diff --git a/libstd/fmtfuncs.myr b/libstd/fmtfuncs.myr
new file mode 100644
index 0000000..22e1e8a
--- /dev/null
+++ b/libstd/fmtfuncs.myr
@@ -0,0 +1,54 @@
+use "alloc.use"
+use "bigint.use"
+use "bitset.use"
+use "fmt.use"
+use "introspect.use"
+use "strbuf.use"
+use "varargs.use"
+
+pkg std =
+;;
+
+const __init__ = {
+ var bigint : bigint#
+ var bitset : bitset#
+
+ fmtinstall(typeof(bigint), bigfmt, [][:])
+ fmtinstall(typeof(bitset), bsfmt, [][:])
+}
+
+const bigfmt = {sb, ap, opts
+ var a, n, buf
+
+ a = vanext(ap)
+ /*
+ allocate a buffer guaranteed to be big enough.
+ that's
+ 2 + floor(nbits/(log_2(10)))
+ or
+ 2 + a.dig.len * 32/3.32...
+ or
+ 2 + a.dig.len * 10
+ plus one for the - sign.
+ */
+ buf = slalloc(3 + a.dig.len * 10)
+ n = bigbfmt(buf, a, 10)
+ sbputs(sb, buf[:n])
+ slfree(buf)
+}
+
+const bsfmt = {sb, ap, opts
+ var i, bs
+
+ bs = vanext(ap)
+ sbputs(sb, "bitset{")
+ for i = 0; i < bsmax(bs); i++
+ if bshas(bs, i)
+ if i != 0
+ sbfmt(sb, ", ")
+ ;;
+ sbfmt(sb, "{}", i)
+ ;;
+ ;;
+ sbputs(sb, "}")
+}