aboutsummaryrefslogtreecommitdiffstats
path: root/src/utf8.c
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-11 14:19:39 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-11 14:31:37 -0400
commitd3ec525b7d6ecd8de064d3f3a54d112769d1d3b3 (patch)
tree935b51f95afb513604c82c9e1efca223af60c109 /src/utf8.c
parentFix git clone link in readme (diff)
downloadcli-d3ec525b7d6ecd8de064d3f3a54d112769d1d3b3.zip
Add unicode support to justify
Diffstat (limited to 'src/utf8.c')
-rw-r--r--src/utf8.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/utf8.c b/src/utf8.c
new file mode 100644
index 0000000..07b751e
--- /dev/null
+++ b/src/utf8.c
@@ -0,0 +1,74 @@
+#include "utf8.h"
+#include <unicode/ustring.h>
+#include <unicode/uchar.h>
+#include <unicode/ubrk.h>
+#include <string.h>
+#include <stdlib.h>
+
+size_t utf8_display_width(const char *str) {
+ if (str == NULL) {
+ return 0;
+ }
+
+ UErrorCode status = U_ZERO_ERROR;
+ int32_t utf8_len = (int32_t)strlen(str);
+
+ // Convert UTF-8 to UTF-16 for ICU processing
+ UChar *utf16 = NULL;
+ int32_t utf16_len = 0;
+
+ // First call to get required length
+ u_strFromUTF8(NULL, 0, &utf16_len, str, utf8_len, &status);
+ if (status != U_BUFFER_OVERFLOW_ERROR && U_FAILURE(status)) {
+ return 0;
+ }
+
+ status = U_ZERO_ERROR;
+ utf16 = (UChar *)malloc((utf16_len + 1) * sizeof(UChar));
+ if (utf16 == NULL) {
+ return 0;
+ }
+
+ u_strFromUTF8(utf16, utf16_len + 1, NULL, str, utf8_len, &status);
+ if (U_FAILURE(status)) {
+ free(utf16);
+ return 0;
+ }
+
+ // Use break iterator to count grapheme clusters
+ UBreakIterator *bi = ubrk_open(UBRK_CHARACTER, NULL, utf16, utf16_len, &status);
+ if (U_FAILURE(status)) {
+ free(utf16);
+ return 0;
+ }
+
+ size_t width = 0;
+ int32_t start = ubrk_first(bi);
+ int32_t end = ubrk_next(bi);
+
+ while (end != UBRK_DONE) {
+ // Get the first code point of this grapheme cluster
+ UChar32 ch;
+ int32_t idx = start;
+ U16_NEXT(utf16, idx, utf16_len, ch);
+
+ // Determine display width based on East Asian Width property
+ int32_t ea_width = u_getIntPropertyValue(ch, UCHAR_EAST_ASIAN_WIDTH);
+
+ // Full-width and Wide characters take 2 columns
+ if (ea_width == U_EA_FULLWIDTH || ea_width == U_EA_WIDE) {
+ width += 2;
+ } else {
+ // All others (Narrow, Halfwidth, Neutral, Ambiguous) take 1 column
+ width += 1;
+ }
+
+ start = end;
+ end = ubrk_next(bi);
+ }
+
+ ubrk_close(bi);
+ free(utf16);
+
+ return width;
+}