libutf

UTF-8 library
git clone git://git.suckless.org/libutf
Log | Files | Refs | README | LICENSE

rune.c (3145B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "utf.h"
      3 
      4 #define MIN(x,y)  ((x) < (y) ? (x) : (y))
      5 
      6 #define UTFSEQ(x) ((((x) & 0x80) == 0x00) ? 1 /* 0xxxxxxx */ \
      7                  : (((x) & 0xC0) == 0x80) ? 0 /* 10xxxxxx */ \
      8                  : (((x) & 0xE0) == 0xC0) ? 2 /* 110xxxxx */ \
      9                  : (((x) & 0xF0) == 0xE0) ? 3 /* 1110xxxx */ \
     10                  : (((x) & 0xF8) == 0xF0) ? 4 /* 11110xxx */ \
     11                  : (((x) & 0xFC) == 0xF8) ? 5 /* 111110xx */ \
     12                  : (((x) & 0xFE) == 0xFC) ? 6 /* 1111110x */ \
     13                                           : 0 )
     14 
     15 #define BADRUNE(x) ((x) < 0 || (x) > Runemax \
     16                 || ((x) & 0xFFFE) == 0xFFFE \
     17                 || ((x) >= 0xD800 && (x) <= 0xDFFF) \
     18                 || ((x) >= 0xFDD0 && (x) <= 0xFDEF))
     19 
     20 int
     21 runetochar(char *s, const Rune *p)
     22 {
     23 	Rune r = *p;
     24 
     25 	switch(runelen(r)) {
     26 	case 1: /* 0aaaaaaa */
     27 		s[0] = r;
     28 		return 1;
     29 	case 2: /* 00000aaa aabbbbbb */
     30 		s[0] = 0xC0 | ((r & 0x0007C0) >>  6); /* 110aaaaa */
     31 		s[1] = 0x80 |  (r & 0x00003F);        /* 10bbbbbb */
     32 		return 2;
     33 	case 3: /* aaaabbbb bbcccccc */
     34 		s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
     35 		s[1] = 0x80 | ((r & 0x000FC0) >>  6); /* 10bbbbbb */
     36 		s[2] = 0x80 |  (r & 0x00003F);        /* 10cccccc */
     37 		return 3;
     38 	case 4: /* 000aaabb bbbbcccc ccdddddd */
     39 		s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
     40 		s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
     41 		s[2] = 0x80 | ((r & 0x000FC0) >>  6); /* 10cccccc */
     42 		s[3] = 0x80 |  (r & 0x00003F);        /* 10dddddd */
     43 		return 4;
     44 	default:
     45 		return 0; /* error */
     46 	}
     47 }
     48 
     49 int
     50 chartorune(Rune *p, const char *s)
     51 {
     52 	return charntorune(p, s, UTFmax);
     53 }
     54 
     55 int
     56 charntorune(Rune *p, const char *s, size_t len)
     57 {
     58 	unsigned int i, n;
     59 	Rune r;
     60 
     61 	if(len == 0) /* can't even look at s[0] */
     62 		return 0;
     63 
     64 	switch((n = UTFSEQ(s[0]))) {
     65 	case 1: r = s[0];        break; /* 0xxxxxxx */
     66 	case 2: r = s[0] & 0x1F; break; /* 110xxxxx */
     67 	case 3: r = s[0] & 0x0F; break; /* 1110xxxx */
     68 	case 4: r = s[0] & 0x07; break; /* 11110xxx */
     69 	case 5: r = s[0] & 0x03; break; /* 111110xx */
     70 	case 6: r = s[0] & 0x01; break; /* 1111110x */
     71 	default: /* invalid sequence */
     72 		*p = Runeerror;
     73 		return 1;
     74 	}
     75 	/* add values from continuation bytes */
     76 	for(i = 1; i < MIN(n, len); i++)
     77 		if((s[i] & 0xC0) == 0x80) {
     78 			/* add bits from continuation byte to rune value
     79 	 		 * cannot overflow: 6 byte sequences contain 31 bits */
     80 			r = (r << 6) | (s[i] & 0x3F); /* 10xxxxxx */
     81 		}
     82 		else { /* expected continuation */
     83 			*p = Runeerror;
     84 			return i;
     85 		}
     86 
     87 	if(i < n) /* must have reached len limit */
     88 		return 0;
     89 
     90 	/* reject invalid or overlong sequences */
     91 	if(BADRUNE(r) || runelen(r) < (int)n)
     92 		r = Runeerror;
     93 
     94 	*p = r;
     95 	return n;
     96 }
     97 
     98 int
     99 runelen(Rune r)
    100 {
    101 	if(BADRUNE(r))
    102 		return 0; /* error */
    103 	else if(r <= 0x7F)
    104 		return 1;
    105 	else if(r <= 0x07FF)
    106 		return 2;
    107 	else if(r <= 0xFFFF)
    108 		return 3;
    109 	else
    110 		return 4;
    111 }
    112 
    113 size_t
    114 runenlen(const Rune *p, size_t len)
    115 {
    116 	size_t i, n = 0;
    117 
    118 	for(i = 0; i < len; i++)
    119 		n += runelen(p[i]);
    120 	return n;
    121 }
    122 
    123 int
    124 fullrune(const char *s, size_t len)
    125 {
    126 	Rune r;
    127 
    128 	return charntorune(&r, s, len) > 0;
    129 }