Category talk:Fmt: Difference between revisions
→Source code: Bug fixes plus some minor improvements.
(Added source code for 'fmt' module to its talk page.) |
(→Source code: Bug fixes plus some minor improvements.) |
||
Line 131:
static dec(n) { itoa(n, 10) } // Ensures safe decimal integers printed as such.
static hex(n) { itoa(n, 16) } // Converts an integer to hex.
static Hex(n) {
static pdec(n) { ((n >= 0) ? "+" : "") + dec(n) } // Adds '+' for non-negative integers.
Line 172:
var c = s.count
return (w > c) ? p * (w - c) + s : s
}
// Right justifies 's' in a field of minimum width 'w' using the pad character '0'.
// Unlike rpad, any sign or elided sign (i.e. space) will be placed before the padding.
// Should normally only be used with numbers or numeric strings.
static rzpad(w, s) {
if (!w.isInteger || w < 0) Fiber.abort("Width must be a non-negative integer.")
if (!(s is String)) s = "%(s)"
var c = s.count
if (w <= c) return s
var sign = (c > 0 && (s[0] == "-" || s[0] == "+" || s[0] == " ")) ? s[0] : ""
if (sign == "") return "0" * (w - c) + s
return sign + "0" * (w - c) + s[1..-1]
}
Line 192 ⟶ 205:
// Checks whether argument is a numeric decimal string.
static isDecimal(n) { n is String && n != "" && n.all { |c| "0123456789".contains(c) } }
// Adds 'thousand separators' to a decimal integer or string.
static commatize(n, c) {
Line 206 ⟶ 219:
var i = n.count - 3
while (i >= 1) {
n = n[0...i] +
i = i - 3
}
Line 221 ⟶ 234:
/* 'Short name' methods, useful for formatting values in interpolated strings. */
//
//
// Negative 'w' left justifies, non-negative 'w' right justifies.
static d(w, n) { (w >= 0) ? rpad(w, Conv.dec(n)) : lpad(-w, Conv.dec(n)) }
Line 230 ⟶ 243:
static X(w, n) { (w >= 0) ? rpad(w, Conv.Hex(n)) : lpad(-w, Conv.Hex(n)) }
// As above but
// Any minus sign will be placed before the padding.
static dz(w, n) { (w >= 0) ? rpad(w, Conv.dec(n), "0") : lpad(-w, Conv.dec(n), "0") }▼
// When used with negative 'w' behaves the same as the above methods.
static bz(w, n) { (w >= 0) ? rpad(w, Conv.bin(n), "0") : lpad(-w, Conv.bin(n), "0") } ▼
static
static
static
▲ static
▲ static
// Formats 'n' in decimal, space padded, with a leading '+' if 'n' is non-negative or '-' otherwise.
static dp(w, n) { (w >= 0) ? rpad(w, Conv.pdec(n)) : lpad(-w, Conv.pdec(n)) }
// Formats 'n' in decimal, space padded, with a leading ' ' if 'n' is non-negative or '-' otherwise.
static dm(w, n) { (w >= 0) ? rpad(w, Conv.mdec(n)) : lpad(-w, Conv.mdec(n)) }
// Formats 'n' in commatized form, space padded, using ',' as the separator
static dc(w, n) { (w >= 0) ? rpad(w, commatize(Conv.dec(n))): lpad(-w, commatize(Conv.dec(n))) }
Line 282 ⟶ 297:
}
// As above but pads with leading zeros instead of spaces.
// Convenience version of the above method which uses the default precision.▼
// Any minus sign will be placed before the padding.
static f(w, n) { f(w, precision, n) }▼
// When used with negative 'w' behaves the same as the above method.
static fz(w, n, p) { (w >= 0) ? rzpad(w, f(w, n, p).trimStart()) : f(w, n, p) }
static fz(w, n) { fz(w, n, precision) }
}
// Type aliases for classes in case of any name clashes with other modules.
var Fmt_Str = Str
var Fmt_Conv = Conv
var Fmt_Fmt = Fmt</lang>
|