commit ab0bd2f1708829d387db797af79458717c86abc4
parent c3d4f4e42068ee9a74856d83c2f89c2062029701
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 1 Apr 2021 12:57:57 -0400
Do not emit a NUL terminator for string constants
Diffstat:
4 files changed, 6 insertions(+), 8 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -429,7 +429,7 @@ check_expr_assert(struct context *ctx,
expr->assert.message->type = EXPR_CONSTANT;
expr->assert.message->result = &builtin_type_const_str;
expr->assert.message->constant.string.value = s;
- expr->assert.message->constant.string.len = n;
+ expr->assert.message->constant.string.len = n - 1;
}
if (expr->assert.is_static) {
diff --git a/src/emit.c b/src/emit.c
@@ -246,9 +246,7 @@ emit_data_string(const char *str, size_t sz, FILE *out)
}
}
if (q) {
- fprintf(out, "\", b 0");
- } else {
- fprintf(out, "b 0");
+ fprintf(out, "\"");
}
}
diff --git a/src/gen.c b/src/gen.c
@@ -306,7 +306,7 @@ gen_fixed_abort(struct gen_context *ctx, struct location loc,
eloc.type = EXPR_CONSTANT;
eloc.result = &builtin_type_const_str;
eloc.constant.string.value = s;
- eloc.constant.string.len = n;
+ eloc.constant.string.len = n - 1;
alloc_temp(ctx, &location, &builtin_type_const_str, "str.%d");
qval_deref(&location);
gen_expression(ctx, &eloc, &location);
diff --git a/tests/04-strings.ha b/tests/04-strings.ha
@@ -15,7 +15,7 @@ fn charptr() void = {
const x = "Hello!";
const y = x: *const char;
const z = y: *[*]u8;
- const expected = ['H', 'e', 'l', 'l', 'o', '!', '\0'];
+ const expected = ['H', 'e', 'l', 'l', 'o', '!'];
for (let i = 0z; i < len(expected); i += 1) {
assert(z[i] == expected[i]: u32: u8);
};
@@ -33,7 +33,7 @@ fn storage() void = {
// UTF-8 encoded
const expected: [_]u8 = [
0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81,
- 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00,
+ 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF,
];
for (let i = 0z; i < len(expected); i += 1) {
assert(ptr.data[i] == expected[i]);
@@ -53,7 +53,7 @@ fn concat() void = {
const t = s: *const char: *[*]u8;
const expected = [
'H', 'e', 'l', 'l', 'o', ',', ' ',
- 'w', 'o', 'r', 'l', 'd', '!', '\0',
+ 'w', 'o', 'r', 'l', 'd', '!',
];
for (let i = 0z; i < len(expected); i += 1) {
assert(t[i] == expected[i]: u32: u8);