feat(utils): add a utility function to count the codepoints in a string

This commit is contained in:
utox39
2025-08-25 01:23:52 +02:00
parent 02406a7644
commit 7ad5181381

View File

@@ -77,3 +77,24 @@ test "getLongestSysInfoStringLen" {
try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..])); try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..]));
} }
pub fn countCodepoints(str: []const u8) !usize {
var count: usize = 0;
var i: usize = 0;
while (i < str.len) {
const byte_len = try std.unicode.utf8ByteSequenceLength(str[i]);
if ((i + byte_len > str.len) or (!std.unicode.utf8ValidateSlice(str[i .. i + byte_len]))) return error.InvalidUtf8;
count += 1;
i += byte_len;
}
return count;
}
test "countCodepoints" {
const str = " ████████████████";
try std.testing.expectEqual(28, try countCodepoints(str));
}