Uppercase ascii character in zig

370 Views Asked by At

Given a character (u8) in zig, how can I make sure it is in ASCII uppercase?

Does the standard library provide helper functions for this?

1

There are 1 best solutions below

0
On

Use std.ascii.toUpper(c)

In general, the standard library provides the following functions:

And the corresponding functions for lowercase letters:

Here is some example usage:

const std = @import("std")
const expect = std.testing.expect;

const upper_a: u8 = 'A';
const lower_a: u8 = 'a';

const mixed_str: []const u8 = "AbCd";
var out_str = [_]u8{'0', '0', '0', '0'};

try expect(std.ascii.isUpper(upper_a));
try expect(std.ascii.toUpper(lower_a) == upper_a);
try expect(std.mem.eql(u8, std.ascii.upperString(&out_str, mixed_str), "ABCD"));
try expect(std.mem.eql(u8, &out_str, "ABCD")); // upperString writes to out_str

try expect(std.ascii.isLower(lower_a));
try expect(std.ascii.toLower(upper_a) == lower_a);
try expect(std.mem.eql(u8, std.ascii.lowerString(&out_str, mixed_str), "abcd"));
try expect(std.mem.eql(u8, &out_str, "abcd")); // lowerString writes to out_str