robin/cdl

cute fetch client written in zig

git clone https://git.robinwobin.dev/robin/cdl

robinwip: init4a5e316

main
877 B40 linesraw
1// Copyright june 2026
2
3const std = @import("std");
4const unicode = std.unicode;
5const mem = std.mem;
6const c = std.c;
7
8const wc = @import("wchar");
9
10pub fn width(s: []const u8) !usize {
11    var w: usize = 0;
12    var i: usize = 0;
13
14    while (i < s.len) {
15        const len = try unicode.utf8ByteSequenceLength(s[i]);
16        const dec = try unicode.utf8Decode(s[i .. i + len]);
17
18        const cw = wc.wcwidth(@intCast(dec));
19        if (cw > 0) w += @intCast(cw);
20
21        i += len;
22    }
23
24    return w;
25}
26
27test "strwidth" {
28    try std.testing.expectEqual(16, width(
29        \\      へ  ♡  ╱|、
30    ));
31    try std.testing.expectEqual(17, width(
32        \\   ૮ -  ՛)  (` - 7
33    ));
34    try std.testing.expectEqual(18, width(
35        \\    / ⁻ ៸|   |、⁻〵
36    ));
37    try std.testing.expectEqual(22, width(
38        \\ 乀(ˍ, ل ل   じしˍ,)ノ
39    ));
40}