Category talk:Wren-fmt: Difference between revisions

→‎Source code: Further tinkerings.
(→‎Source code: Several minor enhancements.)
(→‎Source code: Further tinkerings.)
Line 196:
static commatize(n) { commatize(n, ",") }
 
// ConvenienceAdds method'thousand' whichseparators' commatizesto an ordinal number using a comma as the separator.
static ordinalize(n, c) { commatize(n, c) + Conv.ord(n)[-2..-1] }
 
// Convenience version of the above method which uses a comma as the separator.
static ordinalize(n) { ordinalize(n, ",") }
 
// Private helper method for 'abbreviate' method.
Line 206 ⟶ 209:
// Doesn't abbreviate a string unless at least one character would need to be suppressed.
static abbreviate(w, s, sep) {
if (!(w is Num && w.isInteger && w.abs >= 1)) Fiber.abort("Maximum width must be a non-zeropositive integer.")
if (!(sep is String)) Fiber.abort("Separator must be a string.")
if (!(s is String)) s = "%(s)"
Line 255 ⟶ 258:
// Ranks a non-negative integer 'n' i.e. expresses it in ordinal form, space padded.
static r(w, n) { (w >= 0) ? rjust(w, Conv.ord(n)) : ljust(-w, Conv.ord(n)) }
 
// As the above method but commatizes the ordinal number, using ',' as the separator.
static rc(w, n) { (w >= 0) ? rjust(w, ordinalize(n)) : ljust(-w, ordinalize(n)) }
 
// Pads a character (equivalent to the codepoint 'n') with spaces to a minimum width of 'w'.
Line 282 ⟶ 288:
// Middles a string or value 'v' within a field of minimum width 'w'. Pads with spaces.
static m(w, v) { cjust(w, v) }
 
// 'Short name' synonym for abbreviate(w, s) method except doesn't abbreviate (rather than throwing
// an error) if a width of '0' is passed.
static a(w, v) { (w != 0) ? abbreviate(w, v) : s(0, v) }
 
// Enables a value to be printed in its 'normal' form (i.e. by applying the 'toString' method),
// within a space-padded minimum field of width 'w', notwithstanding any special formatting
// that would otherwise be applied by 'short name' methods.
static n(w, v) { s(w, v.toString) }
 
// Applies the 's' format to the kind (i.e. type) of 'v'.
Line 400 ⟶ 415:
(fn == "i") ? i(w, e) :
(fn == "m") ? m(w, e) :
(fn == "a") ? a(w, e) :
(fn == "n") ? n(w, e) :
(fn == "k") ? k(w, e) :
(fn == "q") ? q(e) :
Line 417 ⟶ 434:
(fn == "dm") ? dm(w, e) :
(fn == "dc") ? dc(w, e) :
(fn == "rc") ? rc(w, e) :
(fn == "sc") ? sc(w, e) :
(fn == "ic") ? ic(w, e) :
Line 451 ⟶ 469:
// 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.
// 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' or 'gc' 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
Line 518 ⟶ 536:
var fn = ""
var ds = ""
if ("bcdfgikmoqrstxXabcdfgikmnoqrstxX".codePoints.contains(cp)) { // format letter
fn = Conv.itoc(cp)
} else if (cp == 42) { // star
Line 553 ⟶ 571:
 
if (fn == "") {
if (!"bcdfgikmoqrstxXabcdfgikmnoqrstxX".codePoints.contains(cp)) {
Fiber.abort("Unrecognized character in format string.")
}
Line 566 ⟶ 584:
fn = "dc"
}
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" || fn == "g") && comma) {
fn = fn + "c"
}
Line 577 ⟶ 595:
if (next < a.count) {
var e = a[next]
if ((e is Sequence) && !(e is String) && fn != "n") {
if (hash && "btodxX".contains(fn[0])) {
var rr = []
9,479

edits