Category talk:Wren-fmt: Difference between revisions
Content added Content deleted
(→Source code: Added Name.toNum method and two more floating point formats.) |
(→Source code: Added fraction and plural formats.) |
||
Line 142: | Line 142: | ||
return fracs.containsKey(frac) ? fracs[frac] : frac |
return fracs.containsKey(frac) ? fracs[frac] : frac |
||
} |
} |
||
// Converts 's' to the plural form 'p' if and only if the absolute value of 'n' is not equal to 1. |
|||
static plural(n, s, p) { (n.abs != 1) ? p : s } |
|||
// Convenience version of the above method which forms the plural by adding 's' to the singular. |
|||
static plural(n, s) { (n.abs != 1) ? s + "s" : s } |
|||
// Returns the unicode infinity symbol. |
// Returns the unicode infinity symbol. |
||
Line 408: | Line 414: | ||
// Applies the 's' format to the the inferior (or subscript) version of a number 'n'. |
// Applies the 's' format to the the inferior (or subscript) version of a number 'n'. |
||
static I(w, n) { Fmt.s(w, Conv.subscript(n)) } |
static I(w, n) { Fmt.s(w, Conv.subscript(n)) } |
||
// Applies the 's' format to a fraction f[0] / f[1 where 'f' is a two element list. |
|||
static F(w, f) { Fmt.s(w, Conv.fraction(f[0], f[1])) } |
|||
// Applies the 's' format to the plural of l[1] depending on l[0] where 'l' is a two element list. |
|||
static P(w, l) { Fmt.s(w, Conv.plural(l[0], l[1])) } |
|||
// Pads a number 'n' with leading spaces to a minimum width 'w' and a precision of 'p' decimal places. |
// Pads a number 'n' with leading spaces to a minimum width 'w' and a precision of 'p' decimal places. |
||
Line 616: | Line 628: | ||
(fn == "S") ? Fmt.S(w, v) : |
(fn == "S") ? Fmt.S(w, v) : |
||
(fn == "I") ? Fmt.I(w, v) : |
(fn == "I") ? Fmt.I(w, v) : |
||
(fn == "F") ? Fmt.F(w, v) : |
|||
(fn == "P") ? Fmt.P(w, v) : |
|||
(fn == "f") ? f(w, v, p) : |
(fn == "f") ? f(w, v, p) : |
||
(fn == "g") ? g(w, v, p) : |
(fn == "g") ? g(w, v, p) : |
||
Line 733: | Line 747: | ||
// $[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: |
// The letter must be one of the 'short' methods: |
||
// a, b, c, d, e, E, f, g, h, i, I, j, k, l, m, n, N, o, O, q, r, s, S, t, u, x, X or z. |
// a, b, c, d, e, E, f, F, g, h, i, I, j, k, l, m, n, N, o, O, P, q, r, s, S, t, u, x, X or z. |
||
// 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', 'fp', 'gp' or 'hp' methods) |
// + always prints a + or - sign ('dp', 'fp', 'gp' or 'hp' methods) |
||
Line 803: | Line 817: | ||
var fn = "" |
var fn = "" |
||
var ds = "" |
var ds = "" |
||
if (" |
if ("abcdeEfFghiIjklmnNoOPqrsStuxXz".codePoints.contains(cp)) { // format letter |
||
fn = Conv.itoc(cp) |
fn = Conv.itoc(cp) |
||
} else if (cp == 42) { // star |
} else if (cp == 42) { // star |
||
Line 838: | Line 852: | ||
if (fn == "") { |
if (fn == "") { |
||
if (!" |
if (!"abcdeEfFghiIjklmnNoOPqrsStuxXz".codePoints.contains(cp)) { |
||
Fiber.abort("Unrecognized character in format string.") |
Fiber.abort("Unrecognized character in format string.") |
||
} |
} |
||
Line 867: | Line 881: | ||
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) && !"nuFP".contains(fn)) { |
||
if (hash && "btodxX".contains(fn[0])) { |
if (hash && "btodxX".contains(fn[0])) { |
||
var rr = [] |
var rr = [] |