zig 中的大写 ascii 字符

Uppercase ascii character in zig

提问人:Simon Berens 提问时间:10/22/2023 最后编辑:FObersteinerSimon Berens 更新时间:10/26/2023 访问量:70

问:

给定一个 zig 中的字符 (),如何确保它是 ASCII 大写的?u8

标准库是否为此提供了辅助函数?

ASCII 之字形

评论


答:

2赞 Simon Berens 10/22/2023 #1

使用 std.ascii.toUpper(c)

通常,标准库提供以下功能:

以及小写字母的相应函数:

以下是一些用法示例:

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