edo

Experimental text editor.
Log | Files | Refs | LICENSE

utf8.c (825B)


      1 #define _XOPEN_SOURCE
      2 #include <wchar.h>
      3 #include <grapheme.h>
      4 #include <utf8proc.h>
      5 
      6 #include "utf8.h"
      7 
      8 size_t
      9 utf8_len_compat(char *buf, int len) {
     10 	uint_least32_t cp;
     11 	int step, i;
     12 
     13 	i = step = utf8_decode(buf, len, &cp);
     14 	if(!utf8_is_combining(cp) && wcwidth(cp) < 0) return step;
     15 	while(i < len) {
     16 		step = utf8_decode(buf + i, len - i, &cp);
     17 		if(!utf8_is_combining(cp) || wcwidth(cp)) break;
     18 		i += step;
     19 	}
     20 	return i;
     21 }
     22 
     23 int
     24 utf8_len(char *buf, int len) {
     25 	return grapheme_next_character_break_utf8(buf, len);
     26 }
     27 
     28 int
     29 utf8_decode(char *buf, int len, unsigned int *cp) {
     30 	return grapheme_decode_utf8(buf, len, cp);
     31 }
     32 
     33 int
     34 utf8_is_combining(unsigned int cp) {
     35 	const utf8proc_property_t *prop = utf8proc_get_property(cp);
     36 
     37 	return (prop->category == UTF8PROC_CATEGORY_MN
     38 			|| prop->category == UTF8PROC_CATEGORY_ME);
     39 }