commit e6b7fff5d99fad70dbdea3da13705d02064f3af7
parent 21a1b705d9a411a64267d9ba38ce34944c9c78da
Author: Sebastian <sebastian@sebsite.pw>
Date: Thu, 7 Nov 2024 19:07:20 -0500
strings::template: allow underscores in variable names
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/strings/template/README b/strings/template/README
@@ -3,11 +3,12 @@ the scope of [[fmt::]]. A template is compiled using [[compile]], then executed
with [[execute]] to print formatted text to an [[io::handle]].
The template format is a string with variables substituted using "$". Variable
-names must be alphanumeric ASCII characters (i.e. for which [[ascii::isalnum]]
-returns true). A literal "$" may be printed by using it twice: "$$". Variables
-may also be used with braces, i.e. ${variable}, so that they can be placed
-immediately next to alphanumeric characters; such variables may include
-non-alphanumeric characters other than '{' and '}'.
+names consist of alphanumeric ASCII characters (i.e. for which
+[[ascii::isalnum]] returns true) or underscores ('_'). A literal "$" may be
+printed by using it twice: "$$". Variables may also be used with braces, i.e.
+${variable}, so that they can be placed immediately next to alphanumeric
+characters; such variables may include non-alphanumeric characters other than
+'{' and '}'.
const src = "Hello, $user! Your balance is $$$balance.\n";
const template = template::compile(src)!;
diff --git a/strings/template/template.ha b/strings/template/template.ha
@@ -139,7 +139,7 @@ fn parse_variable(
break;
};
} else {
- if (ascii::isalnum(rn)) {
+ if (ascii::isalnum(rn) || rn == '_') {
memio::appendrune(buf, rn)!;
} else {
strings::prev(iter);