#Can't Save High Value Characters in Variable to Be Printed

1 messages · Page 1 of 1 (latest)

heavy junco
#

I am creating a terminal game which has a border on the top consisting of the ┌┐─│└┘ characters. I generate the border with this code:

    fn hBorders(top: bool, num_cols: usize, piece_size: usize) error{OutOfMemory}![]u16 {
        var len: usize = (num_cols + 2) * piece_size;
        const border: []u16 = try allocator.alloc(u16, len);
        if (top) {
            border[0] = '┌';
            border[len - 1] = '┐';
        } else {
            border[0] = '└';
            border[len - 1] = '┘';
        }
        for (border[1 .. len - 1]) |*c| {
            c.* = '─';
        }
        return border;
    }

When I use the type of []u8 in border I get this error:

error: type 'u8' cannot represent integer value '9484'
 border[0] = '┌';

When I use u16 I can no longer read the data as a string. And if I user {any} it just prints the character values. I believe this should be possible as I can print the desired characters like this,

        try buf_wrtr.print("{s}", .{"┌┐─│└┘"});

Just fine. I am not sure how to save that character in a variable though.
I also tried using

border[0] = '\xe2\x94\x8c';

But that has a syntax error: expected expression, found 'invalid bytes' (expected_expr)

left egret
#

Simplest option is probably something like this: ```rs
var border = std.ArrayList(u8).init(allocator);
if (top) {
try border.appendSlice("┌");
} else {
try border.appendSlice("└");
}
for (0 .. num_cols - 2) |_| {
try border.appendSlice("─");
}
if (top) {
try border.appendSlice("┐");
} else {
try border.appendSlice("┘");
}
return border.toOwnedSlice();

#

You could also compute the length ahead of time but that's kind of annoying to do since the codepoints you're using aren't all the same length when encoded as UTF-8

heavy junco
#

Does that for loop syntax work for you?
I am copying that code but get:
expected ')', found '..'

left egret
#

What zig version?

heavy junco
#

0.10.1, I also copied direct from the docsbut that also says its wrong

left egret
#

Ah yeah that's master branch for loop syntax

#

Replace it with var i: usize = 0; while (i < num_cols - 2) : (i += 1)

heavy junco
#

Oh okay