robin/cdl
cute fetch client written in zig
git clone https://git.robinwobin.dev/robin/cdl
4a5e316
main
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 { 11var w : usize =0 ; 12var i : usize =0 ; 13 14while ( i < s . len ) { 15const len =try unicode . utf8ByteSequenceLength ( s [ i ]); 16const dec =try unicode . utf8Decode ( s [ i .. i + len ]); 17 18const cw =wc . wcwidth ( @intCast ( dec )); 19if ( cw > 0 ) w += @intCast ( cw ); 20 21i += len ; 22} 23 24return w ; 25} 26 27test "strwidth" { 28try std . testing . expectEqual ( 16 , width ( 29\\ へ ♡ ╱|、 30)); 31try std . testing . expectEqual ( 17 , width ( 32\\ ૮ - ՛) (` - 7 33)); 34try std . testing . expectEqual ( 18 , width ( 35\\ / ⁻ ៸| |、⁻〵 36)); 37try std . testing . expectEqual ( 22 , width ( 38\\ 乀(ˍ, ل ل じしˍ,)ノ 39)); 40}