aboutsummaryrefslogtreecommitdiff
path: root/sys/libutf/runewidth-14.0.0.c
diff options
context:
space:
mode:
authorNicholas <nbnoll@eml.cc>2021-11-10 20:12:45 -0800
committerNicholas <nbnoll@eml.cc>2021-11-11 08:16:47 -0800
commit7ea1cdb7d31f00024f5a1d124b42cd19a03b959a (patch)
treef65abf9fa32856287de586129d4ed7c1c473864a /sys/libutf/runewidth-14.0.0.c
parent43688fe7190d0350349d47727c3663421d5618dc (diff)
chore: libunicode -> libutf
Diffstat (limited to 'sys/libutf/runewidth-14.0.0.c')
-rw-r--r--sys/libutf/runewidth-14.0.0.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/sys/libutf/runewidth-14.0.0.c b/sys/libutf/runewidth-14.0.0.c
new file mode 100644
index 0000000..113c35e
--- /dev/null
+++ b/sys/libutf/runewidth-14.0.0.c
@@ -0,0 +1,71 @@
+#include <u.h>
+#include <libutf.h>
+
+static
+rune*
+rangesearch(rune c, rune *t, int n, int ne)
+{
+ rune *p;
+ int m;
+ while(n > 1) {
+ m = n >> 1;
+ p = t + m*ne;
+ if(c >= p[0]){
+ t = p;
+ n = n-m;
+ }else
+ n = m;
+ }
+ if(n && c >= t[0])
+ return t;
+ return 0;
+}
+
+static rune width0_range[] = {
+ 0x2028, 0x2029,
+};
+
+static int
+iswidth0(rune c)
+{
+ rune *p;
+
+ p = rangesearch(c, width0_range, arrlen(width0_range)/2, 2);
+ if(p && c >= p[0] && c <= p[1])
+ return 1;
+ return 0;
+}
+
+static rune width1_single[] = {
+ 0x00ad,
+};
+
+static int
+iswidth1(rune c)
+{
+ rune *p;
+
+ p = rangesearch(c, width1_single, arrlen(width1_single), 1);
+ if(p && c == p[0])
+ return 1;
+ return 0;
+}
+
+static int
+iswidth2(rune c)
+{
+ rune *p;
+
+ return 0;
+}
+
+
+int
+utf8·runewidth(rune c)
+{
+ if(iswidth1(c))
+ return 1;
+ if(iswidth2(c))
+ return 2;
+ return 0;
+}