Category talk:Wren-fmt: Difference between revisions

Content added Content deleted
(→‎Source code: Fixed a bug in 'g', h', 'gc' and 'hc' formats when dealing with scientific notation.)
(→‎Source code: Added Conv.printable method and supporting formats.)
Line 147: Line 147:
// Convenience version of the above method which forms the plural by adding 's' to the singular.
// 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 }
static plural(n, s) { (n.abs != 1) ? s + "s" : s }

// Makes a UTF-8 or byte string printable by replacing non-printable characters
// with escaped ones. If 'asBytes' is true, not just control characters
// but all bytes > 127 are regarded as non-printable.
static printable(s, asBytes) {
var res = ""
var chars = asBytes ? s.bytes : s.codePoints
var lets = "abtnvfr"
for (c in chars) {
if (c == -1) {
res = res + "\ufffd"
} else if (c == 0) {
res = res + "\\0"
} else if (c >= 7 && c <= 13) {
res = res + "\\" + lets[c - 7]
} else if (c == 27) {
res = res + "\\e"
} else if (c < 32 || c == 127 || (asBytes && c > 127)) {
res = res + "\\x" + Fmt.swrite("$02x", c)
} else if (!asBytes && c >= 128 && c < 160) {
res = res + esc + "\\u00" + Fmt.swrite("$02x", c)
} else {
res = res + String.fromCodePoint(c)
}
}
return res
}


// Returns the unicode infinity symbol.
// Returns the unicode infinity symbol.
Line 404: Line 431:


// Works like 'e' except that the exponent symbol 'e' is replaced by upper case 'E'.
// Works like 'e' except that the exponent symbol 'e' is replaced by upper case 'E'.
static E(w, n, p) { e(w, n, p).replace("e", "E") }
static E(w, n, p) { Fmt.e(w, n, p).replace("e", "E") }


// Applies the 's' format to the name of a number 'n'.
// Applies the 's' format to the name of a number 'n'.
Line 423: Line 450:
// Applies the 's' format to the plural of l[1] depending on l[0] and, if present, l[2] where 'l' is a
// Applies the 's' format to the plural of l[1] depending on l[0] and, if present, l[2] where 'l' is a
// two or three element list. Forms the plural by just adding "s" to l[1] if 'l' is a two element list.
// two or three element list. Forms the plural by just adding "s" to l[1] if 'l' is a two element list.
static P(w, l) { s(w, Conv.plural(l[0], l[1], l.count == 2 ? l[1] + "s" : l[2])) }
static P(w, l) { Fmt.s(w, Conv.plural(l[0], l[1], l.count == 2 ? l[1] + "s" : l[2])) }

// Makes a byte string printable and applies the 's' format to the result.
static B(w, v) { Fmt.s(w, Conv.printable(v, true)) }

// Makes a UTF-8 string printable and applies the 's' format to the result.
static U(w, v) { Fmt.s(w, Conv.printable(v, false)) }


// Repeats 'v', a string or value, 'n' times.
// Repeats 'v', a string or value, 'n' times.
Line 655: Line 688:
(fn == "F") ? Fmt.F(w, v) :
(fn == "F") ? Fmt.F(w, v) :
(fn == "P") ? Fmt.P(w, v) :
(fn == "P") ? Fmt.P(w, v) :
(fn == "B") ? Fmt.B(w, v) :
(fn == "U") ? Fmt.U(w, v) :
(fn == "R") ? Fmt.R(w, v) :
(fn == "R") ? Fmt.R(w, v) :
(fn == "f") ? f(w, v, p) :
(fn == "f") ? f(w, v, p) :
Line 774: Line 809:
// $[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, F, g, h, i, I, j, k, l, m, n, N, o, O, P, q, r, R, s, S, t, u, x, X, y or z.
// a, b, B, c, d, e, E, f, F, g, h, i, I, j, k, l, m, n, N, o, O, P, q, r, R, s, S, t, u, U, x, X, y 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', 'hp' or 'zp' methods)
// + always prints a + or - sign ('dp', 'fp', 'gp', 'hp' or 'zp' methods)
Line 844: Line 879:
var fn = ""
var fn = ""
var ds = ""
var ds = ""
if ("abcdeEfFghiIjklmnNoOPqrRsStuxXyz".codePoints.contains(cp)) { // format letter
if ("abBcdeEfFghiIjklmnNoOPqrRsStuUxXyz".codePoints.contains(cp)) { // format letter
fn = Conv.itoc(cp)
fn = Conv.itoc(cp)
} else if (cp == 42) { // star
} else if (cp == 42) { // star
Line 879: Line 914:


if (fn == "") {
if (fn == "") {
if (!"abcdeEfFghiIjklmnNoOPqrRsStuxXyz".codePoints.contains(cp)) {
if (!"abBcdeEfFghiIjklmnNoOPqrRsStuUxXyz".codePoints.contains(cp)) {
Fiber.abort("Unrecognized character in format string.")
Fiber.abort("Unrecognized character in format string.")
}
}