Add APIs for translating between indices and rune pointers.
authorSebastian Rasmussen <[email protected]>
Tue, 10 Aug 2021 10:46:24 +0000 (12:46 +0200)
committerSebastian Rasmussen <[email protected]>
Tue, 10 Aug 2021 12:29:47 +0000 (14:29 +0200)
include/mupdf/fitz/string-util.h
source/fitz/string.c

index 6665f4c15cf3e094821b72681d30210c11e572ef..57eb2634bacb075a02e448ad34bdeeef37a5d0d2 100644 (file)
@@ -149,6 +149,30 @@ int fz_runetochar(char *str, int rune);
 */
 int fz_runelen(int rune);
 
+/**
+       Compute the index of a rune in a string.
+
+       str: Pointer to beginning of a string.
+
+       p: Pointer to a char in str.
+
+       Returns the index of the rune pointed to by p in str.
+*/
+int fz_runeidx(const char *str, const char *p);
+
+/**
+       Obtain a pointer to the char representing the rune
+       at a given index.
+
+       str: Pointer to beginning of a string.
+
+       idx: Index of a rune to return a char pointer to.
+
+       Returns a pointer to the char where the desired rune starts,
+       or NULL if the string ends before the index is reached.
+*/
+const char *fz_runeptr(const char *str, int idx);
+
 /**
        Count how many runes the UTF-8 encoded string
        consists of.
index 3388e713e93b87b410324ae62e3401cf05aab937..be19d1f3fae1df9de051a11195ba527ccd57bbf7 100644 (file)
@@ -474,6 +474,37 @@ fz_runelen(int c)
        return fz_runetochar(str, c);
 }
 
+int
+fz_runeidx(const char *s, const char *p)
+{
+       int rune;
+       int i = 0;
+       while (s < p) {
+               if (*(unsigned char *)s < Runeself)
+                       ++s;
+               else
+                       s += fz_chartorune(&rune, s);
+               ++i;
+       }
+       return i;
+}
+
+const char *
+fz_runeptr(const char *s, int i)
+{
+       int rune;
+       while (i-- > 0) {
+               rune = *(unsigned char*)s;
+               if (rune < Runeself) {
+                       if (rune == 0)
+                               return NULL;
+                       ++s;
+               } else
+                       s += fz_chartorune(&rune, s);
+       }
+       return s;
+}
+
 int
 fz_utflen(const char *s)
 {