[PATCH 4/5] Split out intfmt to a separate file.
[Thread Prev] | [Thread Next]
- Subject: [PATCH 4/5] Split out intfmt to a separate file.
- From: "S. Gilles" <sgilles@xxxxxxxxxxxx>
- Reply-to: myrddin-dev@xxxxxxxxxxxxxx
- Date: Mon, 9 Sep 2019 08:38:28 -0400
- To: "myrddin-dev" <myrddin-dev@xxxxxxxxxxxxxx>
- Cc: "S. Gilles" <sgilles@xxxxxxxxxxxx>
This prepares for intfmt being used within fltfmt for the exponent in scientific notation. --- lib/std/bld.sub | 1 + lib/std/fmt.myr | 61 +---------------------------------- lib/std/intfmt.myr | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 60 deletions(-) create mode 100644 lib/std/intfmt.myr diff --git a/lib/std/bld.sub b/lib/std/bld.sub index 093e2429..4ab703c5 100644 --- a/lib/std/bld.sub +++ b/lib/std/bld.sub @@ -38,6 +38,7 @@ lib std = hasprefix.myr hassuffix.myr htab.myr + intfmt.myr intparse.myr introspect.myr ipparse.myr diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr index 5dc3ba37..b981a74f 100644 --- a/lib/std/fmt.myr +++ b/lib/std/fmt.myr @@ -7,6 +7,7 @@ use "fltfmt" use "hashfuncs" use "hasprefix" use "htab" +use "intfmt" use "intparse" use "introspect" use "memops" @@ -477,12 +478,6 @@ const fltparams = {params -> fp } -type intparams = struct - base : size - padto : size - padfill : char -;; - const intparams = {params var ip : intparams @@ -572,60 +567,6 @@ const isprint = {b -> b >= (' ' : byte) && b < ('~' : byte) } -const digitchars = "0123456789abcdef" -const intfmt = {sb, opts, signed, bits : uint64, nbits - var isneg - var sval, val - var b : byte[64] - var i, j, npad - var base - - base = (opts.base : uint64) - if signed && bits >= 1 << (nbits - 1) - sval = -(bits : int64) - val = (sval : uint64) - isneg = true - - /* if its negative after inverting, we have int64 min */ - if sval < 0 - std.sbputs(sb, "-9223372036854775808") - -> void - ;; - else - val = (bits : uint64) - val &= ~0 >> nbits - isneg = false - ;; - - i = 0 - if val == 0 - b[0] = ('0' : byte) - i++ - ;; - while val != 0 - b[i] = digitchars[val % base] - val /= base - i++ - ;; - - npad = clamp(opts.padto - i, 0, opts.padto) - if isneg - npad-- - ;; - if opts.padfill == '0' && isneg - sbputb(sb, ('-' : byte)) - ;; - for j = 0; j < npad; j++ - sbputc(sb, opts.padfill) - ;; - if opts.padfill != '0' && isneg - sbputb(sb, ('-' : byte)) - ;; - for j = i; j != 0; j-- - sbputb(sb, b[j - 1]) - ;; -} - /* would use std.get(), but that's a dependency loop */ const getint = {s, msg match std.intparse(s) diff --git a/lib/std/intfmt.myr b/lib/std/intfmt.myr new file mode 100644 index 00000000..7b9cfdfd --- /dev/null +++ b/lib/std/intfmt.myr @@ -0,0 +1,79 @@ +use "die" +use "extremum" +use "intparse" +use "option" +use "strbuf" +use "striter" +use "types" + +pkg std = + pkglocal type intparams = struct + base : size + padto : size + padfill : char + ;; + + pkglocal const intfmt : (sb : strbuf#, opts : intparams, signed : bool, bits : uint64, nbits : uint64 -> void) +;; + +const digitchars = "0123456789abcdef" +const intfmt = {sb : strbuf#, opts : intparams, signed : bool, bits : uint64, nbits : uint64 + var isneg + var sval, val + var b : byte[64] + var i, j, npad + var base + + base = (opts.base : uint64) + if signed && bits >= 1 << (nbits - 1) + sval = -(bits : int64) + val = (sval : uint64) + isneg = true + + /* if its negative after inverting, we have int64 min */ + if sval < 0 + std.sbputs(sb, "-9223372036854775808") + -> void + ;; + else + val = (bits : uint64) + val &= ~0 >> nbits + isneg = false + ;; + + i = 0 + if val == 0 + b[0] = ('0' : byte) + i++ + ;; + while val != 0 + b[i] = digitchars[val % base] + val /= base + i++ + ;; + + npad = clamp(opts.padto - i, 0, opts.padto) + if isneg + npad-- + ;; + if opts.padfill == '0' && isneg + sbputb(sb, ('-' : byte)) + ;; + for j = 0; j < npad; j++ + sbputc(sb, opts.padfill) + ;; + if opts.padfill != '0' && isneg + sbputb(sb, ('-' : byte)) + ;; + for j = i; j != 0; j-- + sbputb(sb, b[j - 1]) + ;; +} + +/* would use std.get(), but that's a dependency loop */ +const getint = {s, msg + match std.intparse(s) + | `Some w: -> w; + | `None: die(msg) + ;; +} -- 2.23.0
[PATCH 0/5] Allow printing floating point in scientific notation | "S. Gilles" <sgilles@xxxxxxxxxxxx> |
- Prev by Date: [PATCH 3/5] Specify sigfigs (cutoff + Relative mode) for fltXY formatting
- Next by Date: [PATCH 5/5] Allow printing fltXY in scientific notation
- Previous by thread: [PATCH 3/5] Specify sigfigs (cutoff + Relative mode) for fltXY formatting
- Next by thread: [PATCH 5/5] Allow printing fltXY in scientific notation
- Index(es):