ref: 20cea781b95e1a6c1fa2f6cd2bb6c05ec9a4480d
parent: 5d433bdf4af606e395b54aace8c0a27f745ae9ce
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Oct 10 07:24:21 EDT 2016
Add slice format join option. You can remove the end delimiters and join the values in a slice when printing using the 'j=' specifier: std.fmt("{j= }\n", [1,2,3][:]) will output: 1 2 3 instead of: [1, 2, 3]
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -347,14 +347,7 @@
strfmt(sb, s_val, params)
| _:
subap = vaenter(ap)
- sbputs(sb, "[")
- while subap.tc.nelt != 0
- fmtval(sb, vatype(&subap), &subap, "")
- if subap.tc.nelt > 0
- sbfmt(sb, ", ")
- ;;
- ;;
- sbputs(sb, "]")
+ fmtslice(sb, subap, params)
vabytes(ap)
;;
| `Tyfunc tc:
@@ -422,6 +415,37 @@
subap = vaenter(ap)
fmtval(sb, desc, &subap, params)
vabytes(ap)
+ ;;
+}
+
+const fmtslice = {sb, subap, params
+ var opts, join, joined
+
+ opts = parseparams(params, [
+ ("j", true),
+ ][:])
+ join = ", "
+ joined = false
+ for o in opts
+ match o
+ | ("j", j):
+ joined = true
+ join = j
+ | _: std.die("unreacahable")
+ ;;
+ ;;
+
+ if !joined
+ sbputs(sb, "[")
+ ;;
+ while subap.tc.nelt != 0
+ fmtval(sb, vatype(&subap), &subap, "")
+ if subap.tc.nelt > 0
+ sbfmt(sb, join)
+ ;;
+ ;;
+ if !joined
+ sbputs(sb, "]")
;;
}
--- a/lib/std/test/fmt.myr
+++ b/lib/std/test/fmt.myr
@@ -88,6 +88,8 @@
check("[1, 2, 3] true", "{} {}", [1,2,3], true)
check("[1, 2, 3] true", "{} {}", [1,2,3][:], true)
+ check("1:2:3 true", "{j=:} {}", [1,2,3][:], true)
+ check("1... 2... 3 true", "{j=... } {}", [1,2,3][:], true)
}
const installed = {