Jump to content

Category talk:Wren-fmt: Difference between revisions

→‎Source code: Further tinkerings including some formatting options which were missing before.
(→‎Source code: Fixed Typo)
(→‎Source code: Further tinkerings including some formatting options which were missing before.)
Line 316:
// Convenience version of the above which uses double quotes as the embedding characters.
static q(v) { "\"%(v)\"" }
 
// Formats a number 'n' (using 'h' format) to a maximum precision of 14 decimal places.
// It then converts it to exponential format and formats the mantissa to 'p' decimal places.
// The result is then padded with spaces to a minimum width 'w'.
// Negative 'w' left justifies, non-negative 'w' right justifies.
static e(w, n, p) {
var f = Fmt.h(w, n, 14).trim()
if (f.contains("e") || n.isInfinity || n.isNan) return Fmt.s(w, n) // use 'normal' representation
var dix = f.indexOf(".")
if (dix >= 0) {
f = f.replace(".", "")
} else {
dix = f.count
}
// look for index of first non-zero digit if there is one
var nzix = -1
var i = (f[0] == "-") ? 1 : 0
while (i < f.count) {
if (f[i] != "0") {
nzix = i
break
}
i = i + 1
}
if (nzix == -1) return "0e00"
var delta = dix - nzix
f = (nzix+1<f.count) ? f[nzix] + "." + f[nzix+1..-1] : f[nzix]
if (n < 0) f = "-" + f
f = Fmt.h(p+2, Num.fromString(f), p).trim()
var exp = (delta >= 0) ? Fmt.dz(2, delta-1) : Fmt.dz(3, delta-1)
return Fmt.s(w, "%(f)e%(exp)")
}
 
// Works like 'e' except that the exponent symbol 'e' is replaced by upper case 'E'.
static E(w, n, p) { e(w, n, p).replace("e", "E") }
 
// Pads a number 'n' with leading spaces to a minimum width 'w' and a precision of 'p' decimal places.
Line 353 ⟶ 388:
static g(w, n, p) {
var f = f(w, n, p)
if (f.contains(".") && (f[-1] == "0" || f[-1] == " ")) {
var l1 = f.count
f = f.trimEnd("0 ")
if (f[-1] == ".") f = f + "0"
f = f + (" " * (l1 - f.count))
}
return f
}
 
// Works like 'f' except replaces any trailing zeros after the decimal point with spaces.
// If the resulting string would end with a decimal point, that is also replaced with a space.
static h(w, n, p) {
var f = f(w, n, p)
if (f.contains(".") && (f[-1] == "0" || f[-1] == " ")) {
var l1 = f.count
f = f.trimEnd("0 ")
if (f[-1] == ".") f = f[0..-2]
f = f + (" " * (l1 - f.count))
}
Line 367 ⟶ 415:
static fz(w, n, p) { (w >= 0) ? zfill(w, f(w, n, p).trimStart()) : f(w, n, p) }
static gz(w, n, p) { (w >= 0) ? zfill(w, g(w, n, p).trimStart()) : g(w, n, p) }
static hz(w, n, p) { (w >= 0) ? zfill(w, h(w, n, p).trimStart()) : h(w, n, p) }
 
// Formats the integer part of 'n' in commatized form, space padded,
Line 385 ⟶ 434:
static gc(w, n, p) {
var f = fc(w, n, p)
if (f.contains(".") && (f[-1] == "0" || f[-1] == " ")) {
var l1 = f.count
f = f.trimEnd("0 ")
if (f[-1] == ".") f = f + "0"
f = f + (" " * (l1 - f.count))
}
return f
}
 
// Works like 'fc' except replaces any trailing zeros after the decimal point with spaces.
// If the resulting string would end with a decimal point, that is also replaced with a space.
static hc(w, n, p) {
var f = fc(w, n, p)
if (f.contains(".") && (f[-1] == "0" || f[-1] == " ")) {
var l1 = f.count
f = f.trimEnd("0 ")
if (f[-1] == ".") f = f[0..-2]
f = f + (" " * (l1 - f.count))
}
Line 395 ⟶ 457:
 
// Convenience versions of the above methods which use the default precision.
static fe(w, n) { fe(w, n, precision) }
static gE(w, n) { gFmt.E(w, n, precision) }
static fzf(w, n) { fzf(w, n, precision) }
static gzg(w, n) { gzg(w, n, precision) }
static fch(w, n) { fch(w, n, precision) }
static gcfz(w, n) { gcfz(w, n, precision) }
static gz(w, n) { gz(w, n, precision) }
static hz(w, n) { hz(w, n, precision) }
static fc(w, n) { fc(w, n, precision) }
static gc(w, n) { gc(w, n, precision) }
static hc(w, n) { hc(w, n, precision) }
 
// Private worker method which calls a 'short name' method and returns its result.
static callFn_(fn, w, ev, p) {
return (fn == "d") ? d(w, ev) :
(fn == "b") ? b(w, ev) :
(fn == "t") ? t(w, ev) :
(fn == "o") ? o(w, ev) :
(fn == "x") ? x(w, ev) :
(fn == "X") ? Fmt.X(w, ev) :
(fn == "r") ? r(w, ev) :
(fn == "c") ? c(w, ev) :
(fn == "s") ? s(w, ev) :
(fn == "i") ? i(w, ev) :
(fn == "m") ? m(w, ev) :
(fn == "a") ? a(w, ev) :
(fn == "n") ? n(w, ev) :
(fn == "k") ? k(w, ev) :
(fn == "q") ? q(ev) :
(fn == "fe") ? fe(w, ev, p) :
(fn == "gE") ? gFmt.E(w, ev, p) :
(fn == "dzf") ? dzf(w, ev, p) :
(fn == "bzg") ? bzg(w, ev, p) :
(fn == "tzh") ? tzh(w, ev, p) :
(fn == "ozdz") ? ozdz(w, ev) :
(fn == "xzbz") ? xzbz(w, ev) :
(fn == "Xztz") ? Fmt.Xztz(w, ev) :
(fn == "szoz") ? szoz(w, ev) :
(fn == "izxz") ? izxz(w, ev) :
(fn == "fzXz") ? fzFmt.Xz(w, e,v) p) :
(fn == "gzsz") ? gzsz(w, e, pv) :
(fn == "dpiz") ? dpiz(w, ev) :
(fn == "dmfz") ? dmfz(w, e)v, p) :
(fn == "dcgz") ? dcgz(w, e)v, p) :
(fn == "rchz") ? rchz(w, e)v, p) :
(fn == "scdp") ? scdp(w, ev) :
(fn == "icdm") ? icdm(w, ev) :
(fn == "fcdc") ? fcdc(w, e, pv) :
(fn == "gcrc") ? gcrc(w, e, pv) : Fiber.abort("Method not recognized.") :
(fn == "sc") ? sc(w, v) :
(fn == "ic") ? ic(w, v) :
(fn == "fc") ? fc(w, v, p) :
(fn == "gc") ? gc(w, v, p) :
(fn == "hc") ? hc(w, v, p) : Fiber.abort("Method not recognized.")
}
 
Line 444 ⟶ 516:
// The method to be applied is specified (as a string) in 'fn'.
// The parameters to be passed to the method are specified in 'w' and 'p'
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'fz', 'gz', 'hz', 'fc', 'gc' or 'gchc' but is ignored otherwise.
// The resulting strings are then joined together using the separator 'sep'.
// having first applied the 'q' method, with parameter 'cc', to each of them.
Line 469 ⟶ 541:
// it is an error to provide insufficient arguments. Verbs must be given in this form:
// $[flag][width][.precision][letter] of which all bracketed items except [letter] are optional.
// The letter must be one of the 'short' methods: a, b, c, d, f, g, i, k, m, n, o, q, r, s, t, v, x or X.
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, v, x or X.
// If present, the flag (there can only be one) must be one of the following:
// + always prints a + or - sign ('dp' method)
// (space) leaves a space for the sign but only prints minus ('dm' method)
// , commatizes the following number ('dc', 'rc', 'sc', 'ic', 'fc', 'gc' or 'gchc' methods)
// # adds the appropriate prefix for the number formats: b, t, o, d, x and X.
// * reads the width from the argument before the one to be formatted
// 0 when followed by an explicit width, pads with leading zeros rather than spaces:
// ('dz', 'bz', 'tz', 'oz', 'xz, 'Xz', 'sz', iz', 'fz', 'gz' and 'hz' methods)
// If present, the width is the minimum width (+/-) to be passed to the appropriate method.
// It doesn't include any '#' flag prefix. If [width] is absent, a width of zeroone is passed.
// If present, the precision is the number of decimal places to be passed to the appropriate
// 'e', 'E', 'f', 'g' or 'gh' style method. If absent, the default precision is passed.
// Where any optional item is inappropriate to the method being used it is simply ignored.
// Where one of the arguments is a sequence (other than a string) this method senses it
Line 536 ⟶ 611:
var fn = ""
var ds = ""
if ("abcdfgikmnoqrstxXabcdeEfghikmnoqrstxX".codePoints.contains(cp)) { // format letter
fn = Conv.itoc(cp)
} else if (cp == 42) { // star
Line 571 ⟶ 646:
 
if (fn == "") {
if (!"abcdfgikmnoqrstxXabcdeEfghikmnoqrstxX".codePoints.contains(cp)) {
Fiber.abort("Unrecognized character in format string.")
}
Line 584 ⟶ 659:
fn = "dc"
}
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" || fn == "g") && comma) {
fn == "g" || fn == "h") && comma) {
fn = fn + "c"
}
if (ns == "") ns = "1"
if (ns[0] == "0" && ns.count > 1 && "dbtoxXsifgdbtoxXsifgh".contains(fn[0])) {
fn = fn[0] + "z"
}
9,488

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.