ref: c66161595cd3e4638122777d550ab61dc100b973
parent: 52eca667bbd1cf5a929d3f2352c3c1ee8e91625d
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Dec 25 11:30:17 EST 2017
Make the copy loop byte based. No more pesky UTF8 restrictions in the input string.
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -164,7 +164,7 @@
var buf : byte[256], param : (byte[:], byte[:])[8]
var state, startp, endp, starta, nbuf
var nfmt, nvarargs, nparam
- var c
+ var b, i
nvarargs = ap.tc.nelt
nfmt = 0
@@ -174,14 +174,15 @@
nbuf = 0
endp = 0
state = `Copy
- while fmt.len != 0
- (c, fmt) = charstep(fmt)
- match (state, c)
+ i = 0
+ while i != fmt.len
+ b = fmt[i++]
+ match (state, (b : char))
/* raw bytes */
| (`Copy, '{'):
- if decode(fmt) == '{'
- (c, fmt) = charstep(fmt)
- sbputc(sb, '{')
+ if (fmt[i] : char) == '{'
+ b = fmt[i++]
+ sbputb(sb, ('{' : byte))
else
state = `ParamOpt
nparam = 0
@@ -190,11 +191,11 @@
nbuf = 0
;;
| (`Copy, '}'):
- if decode(fmt) == '}'
- sbputc(sb, '}')
+ if i < fmt.len && (fmt[i] : char) == '}'
+ sbputb(sb, ('}' : byte))
;;
- | (`Copy, chr):
- sbputc(sb, chr)
+ | (`Copy, _):
+ sbputb(sb, b)
/* {param */
| (`ParamOpt, '='):
@@ -213,10 +214,9 @@
fmtval(sb, vatype(ap), ap, param[:nparam])
nfmt++
| (`ParamOpt, '\\'):
- (c, fmt) = charstep(fmt)
- nbuf += std.encode(buf[nbuf:], c)
+ buf[nbuf++] = fmt[i++]
| (`ParamOpt, chr):
- nbuf += std.encode(buf[nbuf:], chr)
+ buf[nbuf++] = b
/* {param=arg} */
| (`ParamArg, ','):
@@ -232,12 +232,10 @@
fmtval(sb, vatype(ap), ap, param[:nparam])
nfmt++
| (`ParamArg, '\\'):
- (c, fmt) = charstep(fmt)
- nbuf += std.encode(buf[nbuf:], c)
+ buf[nbuf++] = fmt[i++]
| (`ParamArg, chr):
- nbuf += std.encode(buf[nbuf:], chr)
+ buf[nbuf++] = b
;;
-:fmtdone
;;
if nfmt != nvarargs
die("too many params for fmt\n")
@@ -450,7 +448,7 @@
while subap.tc.nelt != 0
fmtval(sb, vatype(&subap), &subap, [][:])
if subap.tc.nelt > 0
- sbfmt(sb, join)
+ sbputs(sb, join)
;;
;;
if !joined
--- a/lib/std/test/fmt.myr
+++ b/lib/std/test/fmt.myr
@@ -100,6 +100,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)
+ check("1}2}3 true", "{j=\\}} {}", [1,2,3][:], true)
check("1... 2... 3 true", "{j=... } {}", [1,2,3][:], true)
}