diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-03 14:48:54 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-03 14:48:54 -0400 |
| commit | 29a87ff325b5d9682593d66bd17964e5c2447510 (patch) | |
| tree | c51a94d99604b535ece0bd059621908a4e96ae66 /compile.c | |
| parent | de7b564a91dae14f61732117450e23c5898f75f2 (diff) | |
Support literal Text("blah") for text that is constant ASCII strings
Diffstat (limited to 'compile.c')
| -rw-r--r-- | compile.c | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -1786,10 +1786,17 @@ CORD compile(env_t *env, ast_t *ast) case TextLiteral: { CORD literal = Match(ast, TextLiteral)->cord; if (literal == CORD_EMPTY) - return "((Text_t){.length=0})"; - CORD code = "Text$from_str(\""; + return "Text(\"\")"; + bool all_ascii = true; CORD_pos i; CORD_FOR(i, literal) { + if (!isascii(CORD_pos_fetch(i))) { + all_ascii = false; + break; + } + } + CORD code = all_ascii ? "Text(\"" : "Text$from_str(\""; + CORD_FOR(i, literal) { char c = CORD_pos_fetch(i); switch (c) { case '\\': code = CORD_cat(code, "\\\\"); break; @@ -1804,7 +1811,7 @@ CORD compile(env_t *env, ast_t *ast) if (isprint(c)) code = CORD_cat_char(code, c); else - CORD_sprintf(&code, "%r\"\"\\x%02X\"\"", code, (uint8_t)c); + CORD_sprintf(&code, "%r\\x%02X\"\"", code, (uint8_t)c); break; } } @@ -1818,7 +1825,7 @@ CORD compile(env_t *env, ast_t *ast) code_err(ast, "%s is not a valid text language name", lang); ast_list_t *chunks = Match(ast, TextJoin)->children; if (!chunks) { - return "((Text_t){.length=0})"; + return "Text(\"\")"; } else if (!chunks->next && chunks->ast->tag == TextLiteral) { return compile(env, chunks->ast); } else { |
