diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2025-03-20 11:31:11 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2025-03-20 11:31:11 +0100 |
| commit | 8d4bc50570fcb9654ba6fe931dc937ea476b659e (patch) | |
| tree | 85f7576006a82469f1d0b83dc99eef69afaaaaef /tui/width.ha | |
| parent | a65da39854d650498ce20dedca423cd17b3a87e0 (diff) | |
| parent | 6e8a2af6bd47fd05e6b22937c5da7397549e7cbb (diff) | |
| download | hare-tui-8d4bc50570fcb9654ba6fe931dc937ea476b659e.tar.gz | |
Merge branch 'master' into nomem
Diffstat (limited to 'tui/width.ha')
| -rw-r--r-- | tui/width.ha | 32 |
1 files changed, 32 insertions, 0 deletions
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; +}; |
