]> git.lizzy.rs Git - linenoise.git/blobdiff - utf8.h
Add support for utf-8 display width
[linenoise.git] / utf8.h
diff --git a/utf8.h b/utf8.h
index 5aee96b2a296d4be8bf80a638448a87e95d0d438..d4fb206bf27747e1c8cd17d9ed2ce5193b46f28b 100644 (file)
--- a/utf8.h
+++ b/utf8.h
@@ -18,9 +18,11 @@ extern "C" {
 
 /* No utf-8 support. 1 byte = 1 char */
 #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
+#define utf8_strwidth(S, B) utf8_strlen((S), (B))
 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
 #define utf8_index(C, I) (I)
 #define utf8_charlen(C) 1
+ #define utf8_width(C) 1
 
 #else
 /**
@@ -53,6 +55,12 @@ int utf8_charlen(int c);
  */
 int utf8_strlen(const char *str, int bytelen);
 
+/**
+ * Calculates the display width of the first 'charlen' characters in 'str'.
+ * See utf8_width()
+ */
+int utf8_strwidth(const char *str, int charlen);
+
 /**
  * Returns the byte index of the given character in the utf-8 string.
  *
@@ -79,6 +87,12 @@ int utf8_index(const char *str, int charindex);
  */
 int utf8_tounicode(const char *str, int *uc);
 
+/**
+ * Returns the width (in characters) of the given unicode codepoint.
+ * This is 1 for normal letters and 0 for combining characters and 2 for wide characters.
+ */
+int utf8_width(int ch);
+
 #endif
 
 #ifdef __cplusplus