Category talk:Wren-fmt: Difference between revisions
Content added Content deleted
(→Source code: Several minor enhancements.) |
(→Source code: Further tinkerings.) |
||
Line 196: | Line 196: | ||
static commatize(n) { commatize(n, ",") } |
static commatize(n) { commatize(n, ",") } |
||
// |
// Adds 'thousand' separators' to an ordinal number. |
||
static ordinalize(n) { commatize(n) + Conv.ord(n)[-2..-1] } |
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. |
// Private helper method for 'abbreviate' method. |
||
Line 206: | Line 209: | ||
// Doesn't abbreviate a string unless at least one character would need to be suppressed. |
// Doesn't abbreviate a string unless at least one character would need to be suppressed. |
||
static abbreviate(w, s, sep) { |
static abbreviate(w, s, sep) { |
||
if (!(w is Num && w.isInteger && w.abs >= 1)) Fiber.abort("Maximum width must be a |
if (!(w is Num && w.isInteger && w.abs >= 1)) Fiber.abort("Maximum width must be a positive integer.") |
||
if (!(sep is String)) Fiber.abort("Separator must be a string.") |
if (!(sep is String)) Fiber.abort("Separator must be a string.") |
||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
Line 255: | Line 258: | ||
// Ranks a non-negative integer 'n' i.e. expresses it in ordinal form, space padded. |
// 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)) } |
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'. |
// Pads a character (equivalent to the codepoint 'n') with spaces to a minimum width of 'w'. |
||
Line 282: | Line 288: | ||
// Middles a string or value 'v' within a field of minimum width 'w'. Pads with spaces. |
// Middles a string or value 'v' within a field of minimum width 'w'. Pads with spaces. |
||
static m(w, v) { cjust(w, v) } |
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'. |
// Applies the 's' format to the kind (i.e. type) of 'v'. |
||
Line 400: | Line 415: | ||
(fn == "i") ? i(w, e) : |
(fn == "i") ? i(w, e) : |
||
(fn == "m") ? m(w, e) : |
(fn == "m") ? m(w, e) : |
||
(fn == "a") ? a(w, e) : |
|||
(fn == "n") ? n(w, e) : |
|||
(fn == "k") ? k(w, e) : |
(fn == "k") ? k(w, e) : |
||
(fn == "q") ? q(e) : |
(fn == "q") ? q(e) : |
||
Line 417: | Line 434: | ||
(fn == "dm") ? dm(w, e) : |
(fn == "dm") ? dm(w, e) : |
||
(fn == "dc") ? dc(w, e) : |
(fn == "dc") ? dc(w, e) : |
||
(fn == "rc") ? rc(w, e) : |
|||
(fn == "sc") ? sc(w, e) : |
(fn == "sc") ? sc(w, e) : |
||
(fn == "ic") ? ic(w, e) : |
(fn == "ic") ? ic(w, e) : |
||
Line 451: | Line 469: | ||
// it is an error to provide insufficient arguments. Verbs must be given in this form: |
// 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. |
// $[flag][width][.precision][letter] of which all bracketed items except [letter] are optional. |
||
// The letter must be one of the 'short' methods: b, c, d, f, g, m, o, q, r, s, t, v, x or X. |
// 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: |
// If present, the flag (there can only be one) must be one of the following: |
||
// + always prints a + or - sign ('dp' method) |
// + always prints a + or - sign ('dp' method) |
||
// (space) leaves a space for the sign but only prints minus ('dm' method) |
// (space) leaves a space for the sign but only prints minus ('dm' method) |
||
// , commatizes the following number ('dc', 'sc', 'ic', 'fc' or 'gc' methods) |
// , 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. |
// # 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 |
// * reads the width from the argument before the one to be formatted |
||
Line 518: | Line 536: | ||
var fn = "" |
var fn = "" |
||
var ds = "" |
var ds = "" |
||
if (" |
if ("abcdfgikmnoqrstxX".codePoints.contains(cp)) { // format letter |
||
fn = Conv.itoc(cp) |
fn = Conv.itoc(cp) |
||
} else if (cp == 42) { // star |
} else if (cp == 42) { // star |
||
Line 553: | Line 571: | ||
if (fn == "") { |
if (fn == "") { |
||
if (!" |
if (!"abcdfgikmnoqrstxX".codePoints.contains(cp)) { |
||
Fiber.abort("Unrecognized character in format string.") |
Fiber.abort("Unrecognized character in format string.") |
||
} |
} |
||
Line 566: | Line 584: | ||
fn = "dc" |
fn = "dc" |
||
} |
} |
||
} else if ((fn == "s" || fn == "i" || fn == "f" || fn == "g") && comma) { |
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" || fn == "g") && comma) { |
||
fn = fn + "c" |
fn = fn + "c" |
||
} |
} |
||
Line 577: | Line 595: | ||
if (next < a.count) { |
if (next < a.count) { |
||
var e = a[next] |
var e = a[next] |
||
if ((e is Sequence) && !(e is String)) { |
if ((e is Sequence) && !(e is String) && fn != "n") { |
||
if (hash && "btodxX".contains(fn[0])) { |
if (hash && "btodxX".contains(fn[0])) { |
||
var rr = [] |
var rr = [] |