From 5d8a0a6ce5dbb12963323f97220767fcf7c08b5c Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 20 Mar 2025 11:24:13 +0100 Subject: Add initial wide-character support Adds support for the emoticon, hiragana, katakana and CJK unicode blocks. --- tui/width.ha | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tui/width.ha (limited to 'tui/width.ha') diff --git a/tui/width.ha b/tui/width.ha new file mode 100644 index 0000000..6f9631c --- /dev/null +++ b/tui/width.ha @@ -0,0 +1,32 @@ +use strings; + +export fn runewidth(r: rune) uint = { + let ru = r: u32; + // emoticons: https://en.wikipedia.org/wiki/Emoticons_(Unicode_block) + if (ru >= '\U0001F600' && ru <= '\U0001F64F') { + return 2; + }; + // hiragana: https://en.wikipedia.org/wiki/Hiragana_%28Unicode_block%29 + if (ru >= '\U00003040' && ru <= '\U0000309F') { + return 2; + }; + // katakana: https://en.wikipedia.org/wiki/Katakana_(Unicode_block) + if (ru >= '\U000030A0' && ru <= '\U000030FF') { + return 2; + }; + // CJK: https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) + if (ru >= '\U00004E00' && ru <= '\U00009FFF') { + return 2; + }; + return 1; +}; + +export fn strwidth(s: str) uint = { + const runes = strings::torunes(s); + defer free(runes); + let sum = 0u; + for (let r .. runes) { + sum += runewidth(r); + }; + return sum; +}; -- cgit v1.2.3