commit dbc8751dc6c034967d2b3133a58a627834992e8c
Author:     Jan Klemkow <[email protected]>
AuthorDate: Sun Oct 2 00:59:19 2022 +0200
Commit:     Jan Klemkow <[email protected]>
CommitDate: Sun Oct 2 01:00:03 2022 +0200

    use libgrapheme instead of libutf

diff --git a/config.mk b/config.mk
index 8315b62..15f86cc 100644
--- a/config.mk
+++ b/config.mk
@@ -9,7 +9,7 @@ CFLAGS = -std=c99 -pedantic -Wall -Wextra -g
 
 # utf.h
 CFLAGS += -I/usr/local/include
-LIBS = -L/usr/local/lib -lutf
+LIBS = -L/usr/local/lib -lgrapheme
 
 # For sbase users:
 #CFLAGS += -I../sbase
diff --git a/slackline.c b/slackline.c
index 0dc1f8e..d93fe7a 100644
--- a/slackline.c
+++ b/slackline.c
@@ -19,7 +19,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <utf.h>
+#include <grapheme.h>
 
 #include "slackline.h"
 
@@ -74,15 +74,8 @@ sl_postobyte(struct slackline *sl, size_t pos)
        char *ptr = &sl->buf[0];
        size_t byte = 0;
 
-       for (;pos > 0; pos--) {
-               for (size_t i = 0;; i++) {
-                       if (fullrune(ptr, i) == 1) {
-                               ptr += i;
-                               byte += i;
-                               break;
-                       }
-               }
-       }
+       for (;pos > 0; pos--)
+               byte += grapheme_next_character_break(ptr+byte, sl->blen-byte);
 
        return byte;
 }
@@ -90,18 +83,7 @@ sl_postobyte(struct slackline *sl, size_t pos)
 static char *
 sl_postoptr(struct slackline *sl, size_t pos)
 {
-       char *ptr = &sl->buf[0];
-
-       for (;pos > 0; pos--) {
-               for (size_t i = 0;; i++) {
-                       if (fullrune(ptr, i) == 1) {
-                               ptr += i;
-                               break;
-                       }
-               }
-       }
-
-       return ptr;
+       return &sl->buf[sl_postobyte(sl, pos)];
 }
 
 static void
@@ -131,6 +113,8 @@ sl_backspace(struct slackline *sl)
 int
 sl_keystroke(struct slackline *sl, int key)
 {
+       uint_least32_t cp;
+
        if (sl == NULL || sl->rlen < sl->rcur)
                return -1;
 
@@ -245,7 +229,8 @@ sl_keystroke(struct slackline *sl, int key)
 compose:
        /* byte-wise composing of UTF-8 runes */
        sl->ubuf[sl->ubuf_len++] = key;
-       if (fullrune(sl->ubuf, sl->ubuf_len) == 0)
+       if (grapheme_decode_utf8(sl->ubuf, sl->ubuf_len, &cp) > sl->ubuf_len ||
+           cp == GRAPHEME_INVALID_CODEPOINT)
                return 0;
 
        if (sl->blen + sl->ubuf_len >= sl->bufsize) {

Reply via email to