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) { convConv.Itoa(n, 16) } // Converts an integer to hex (upper case digits).
 
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] + ","c + n[i..-1]
i = i - 3
}
Line 221 ⟶ 234:
/* 'Short name' methods, useful for formatting values in interpolated strings. */
 
// FormatFormats an integer 'n' in (d)ecimal, (b)inary, (o)ctal, he(x) or upper case he(X).
// PadPads with spaces to a minimum width of 'w'.
// 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 padpads with leading zeros instead of spaces.
// 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 ozdz(w, n) { (w >= 0) ? rpadrzpad(w, Conv.octdec(n), "0") : lpad(-w, Conv.octdec(n), "0") }
static xzbz(w, n) { (w >= 0) ? rpadrzpad(w, Conv.hexbin(n), "0") : lpad(-w, Conv.hexbin(n), "0") }
static Xzoz(w, n) { (w >= 0) ? rpadrzpad(w, Conv.Hexoct(n), "0") : lpad(-w, Conv.Hexoct(n), "0") }
static dzxz(w, n) { (w >= 0) ? rpadrzpad(w, Conv.dechex(n), "0") : lpad(-w, Conv.dechex(n), "0") }
static bzXz(w, n) { (w >= 0) ? rpadrzpad(w, Conv.binHex(n), "0") : lpad(-w, Conv.binHex(n), "0") }
 
// 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.
}</lang>
static fz(w, n, p) { (w >= 0) ? rzpad(w, f(w, n, p).trimStart()) : f(w, n, p) }
 
// Convenience versionversions of the above methodmethods which usesuse the default precision.
static f(w, n) { f(w, precisionn, nprecision) }
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>
9,485

edits