]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/utf8.c
utf8: add utf8_n_codepoints() for counting complete utf8 codepoints in a string
[thirdparty/systemd.git] / src / basic / utf8.c
index 4da9a405cb85914c13457f5a6a4c0b6358499353..b17f420264089df0291908d92e8d99b979efdc45 100644 (file)
@@ -408,3 +408,22 @@ int utf8_encoded_valid_unichar(const char *str) {
 
         return len;
 }
+
+size_t utf8_n_codepoints(const char *str) {
+        size_t n = 0;
+
+        /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
+
+        while (*str != 0) {
+                int k;
+
+                k = utf8_encoded_valid_unichar(str);
+                if (k < 0)
+                        return (size_t) -1;
+
+                str += k;
+                n++;
+        }
+
+        return n;
+}