commit f75845357147c3985a5bd7444900e14a639f775e
parent 9771eec6b1452b89006b1fa3d3ac729e03ff0296
Author: minus <minus@mnus.de>
Date: Sun, 17 Jul 2022 16:48:07 +0200
strings::template: Optimize escaped dollar sign
Signed-off-by: minus <minus@mnus.de>
Diffstat:
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/strings/template/template.ha b/strings/template/template.ha
@@ -34,11 +34,21 @@ export fn compile(input: str) (template | errors::invalid) = {
};
if (rn == '$') {
- const lit = strio::string(&buf);
- append(instrs, strings::dup(lit): literal);
- strio::reset(&buf);
-
- parse_variable(&instrs, &iter, &buf)?;
+ match (strings::next(&iter)) {
+ case let next_rn: rune =>
+ if (next_rn == '$') {
+ strio::appendrune(&buf, rn)!;
+ } else {
+ strings::prev(&iter);
+ const lit = strio::string(&buf);
+ append(instrs, strings::dup(lit): literal);
+ strio::reset(&buf);
+
+ parse_variable(&instrs, &iter, &buf)?;
+ };
+ case =>
+ return errors::invalid;
+ };
} else {
strio::appendrune(&buf, rn)!;
};
@@ -101,18 +111,6 @@ fn parse_variable(
iter: *strings::iterator,
buf: *strio::stream,
) (void | errors::invalid) = {
- const rn = match (strings::next(iter)) {
- case let rn: rune =>
- if (rn == '$') {
- append(instrs, strings::dup("$"): literal);
- return;
- };
- yield rn;
- case =>
- return errors::invalid;
- };
- strio::appendrune(buf, rn)!;
-
for (true) {
const rn = match (strings::next(iter)) {
case let rn: rune =>