| @@ -1,186 +0,0 @@ |
| 1 | | −#define _WIN32_WINNT 0x0500 |
| 2 | | −#include <windows.h> |
| 3 | | − |
| 4 | | −#include "u.h" |
| 5 | | −#include "lib.h" |
| 6 | | −#include "r16.h" |
| 7 | | − |
| 8 | | −#define Bit(i) (7-(i)) |
| 9 | | −/* N 0's preceded by i 1's, T(Bit(2)) is 1100 0000 */ |
| 10 | | −#define T(i) (((1 << (Bit(i)+1))-1) ^ 0xFF) |
| 11 | | −/* 0000 0000 0000 0111 1111 1111 */ |
| 12 | | −#define RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1) |
| 13 | | − |
| 14 | | −enum |
| 15 | | −{ |
| 16 | | − Bitx = Bit(1), |
| 17 | | − |
| 18 | | − Tx = T(1), /* 1000 0000 */ |
| 19 | | − Rune1 = (1<<(Bit(0)+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */ |
| 20 | | − |
| 21 | | − Maskx = (1<<Bitx)-1, /* 0011 1111 */ |
| 22 | | − Testx = Maskx ^ 0xFF, /* 1100 0000 */ |
| 23 | | − |
| 24 | | − SurrogateMax = 0xDFFF, |
| 25 | | − HiSurrogate = 0xD800, |
| 26 | | − LoSurrogate = 0xDC00, |
| 27 | | − |
| 28 | | − Bad = Runeerror, |
| 29 | | −}; |
| 30 | | − |
| 31 | | −Rune16* |
| 32 | | −runes16dup(Rune16 *r) |
| 33 | | −{ |
| 34 | | − int n; |
| 35 | | − Rune16 *s; |
| 36 | | − |
| 37 | | − n = runes16len(r) + 1; |
| 38 | | − s = calloc(n, sizeof(Rune16)); |
| 39 | | − memcpy(s, r, n * sizeof(Rune16)); |
| 40 | | − return s; |
| 41 | | −} |
| 42 | | − |
| 43 | | −int |
| 44 | | −runes16len(Rune16 *r) |
| 45 | | −{ |
| 46 | | − int n; |
| 47 | | − |
| 48 | | − n = 0; |
| 49 | | − while(*r++ != 0) |
| 50 | | − n++; |
| 51 | | − return n; |
| 52 | | −} |
| 53 | | − |
| 54 | | −char* |
| 55 | | −runes16toutf(char *p, Rune16 *r, int nc) |
| 56 | | −{ |
| 57 | | − char *op, *ep; |
| 58 | | − int n, c; |
| 59 | | − Rune rc; |
| 60 | | − |
| 61 | | − op = p; |
| 62 | | − ep = p + nc; |
| 63 | | − while(c = *r++){ |
| 64 | | − n = 1; |
| 65 | | − if(c >= Runeself) |
| 66 | | − n = runelen(c); |
| 67 | | − if(p + n >= ep) |
| 68 | | − break; |
| 69 | | − if(c < Runeself){ |
| 70 | | − *p++ = c; |
| 71 | | − continue; |
| 72 | | − } |
| 73 | | − rc = c; |
| 74 | | − if(c >= LoSurrogate && c <= SurrogateMax) |
| 75 | | − rc = Bad; |
| 76 | | − else if(c >= HiSurrogate && c <= 0xDBFF){ /* decode a surrogate pair properly */ |
| 77 | | − if(p + n+1 >= ep) |
| 78 | | − rc = Bad; |
| 79 | | − else if((c = *r) >= LoSurrogate && c <= SurrogateMax){ |
| 80 | | − rc = 0x10000 | (*(r-1) - HiSurrogate) << 10 | (c - LoSurrogate); |
| 81 | | − r++; |
| 82 | | − }else |
| 83 | | − rc = Bad; |
| 84 | | − } |
| 85 | | − p += runetochar(p, &rc); |
| 86 | | − } |
| 87 | | − *p = '\0'; |
| 88 | | − return op; |
| 89 | | −} |
| 90 | | − |
| 91 | | −int |
| 92 | | −rune16nlen(Rune16 *r, int nrune) |
| 93 | | −{ |
| 94 | | − int nb, i; |
| 95 | | − Rune c; |
| 96 | | − |
| 97 | | − nb = 0; |
| 98 | | − while(nrune--) { |
| 99 | | − c = *r++; |
| 100 | | − if(c <= Rune1){ |
| 101 | | − nb++; |
| 102 | | − } else { |
| 103 | | − for(i = 2; i < UTFmax + 1; i++) |
| 104 | | − if(c <= RuneX(i) || i == UTFmax){ |
| 105 | | − nb += i; |
| 106 | | − break; |
| 107 | | − } |
| 108 | | − } |
| 109 | | − } |
| 110 | | − return nb; |
| 111 | | −} |
| 112 | | − |
| 113 | | −Rune16* |
| 114 | | −utftorunes16(Rune16 *r, char *p, int nc) |
| 115 | | −{ |
| 116 | | − Rune16 *or, *er; |
| 117 | | − Rune rc; |
| 118 | | − |
| 119 | | − or = r; |
| 120 | | − er = r + nc; |
| 121 | | − while(*p != '\0' && r + 1 < er){ |
| 122 | | − p += chartorune(&rc, p); |
| 123 | | − if(rc >= 0x10000){ /* got to encode it in a surrogate pair */ |
| 124 | | − rc -= 0x10000; |
| 125 | | − *r++ = (rc >> 10)+HiSurrogate; |
| 126 | | − *r++ = (rc & 0x3FF)+LoSurrogate; |
| 127 | | − }else |
| 128 | | − *r++ = rc; |
| 129 | | − } |
| 130 | | − *r = '\0'; |
| 131 | | − return or; |
| 132 | | −} |
| 133 | | − |
| 134 | | −int |
| 135 | | −runes16cmp(Rune16 *s1, Rune16 *s2) |
| 136 | | −{ |
| 137 | | − Rune16 r1, r2; |
| 138 | | − |
| 139 | | − for(;;) { |
| 140 | | − r1 = *s1++; |
| 141 | | − r2 = *s2++; |
| 142 | | − if(r1 != r2) { |
| 143 | | − if(r1 > r2) |
| 144 | | − return 1; |
| 145 | | − return -1; |
| 146 | | − } |
| 147 | | − if(r1 == 0) |
| 148 | | − return 0; |
| 149 | | − } |
| 150 | | −} |
| 151 | | − |
| 152 | | −wchar_t * |
| 153 | | −widen(char *s) |
| 154 | | −{ |
| 155 | | − int n; |
| 156 | | − wchar_t *ws; |
| 157 | | − |
| 158 | | − n = utflen(s) + 1; |
| 159 | | − ws = calloc(n, sizeof(wchar_t)); |
| 160 | | − utftorunes16(ws, s, n); |
| 161 | | − return ws; |
| 162 | | −} |
| 163 | | − |
| 164 | | − |
| 165 | | −char * |
| 166 | | −narrowen(wchar_t *ws) |
| 167 | | −{ |
| 168 | | − char *s; |
| 169 | | − int n; |
| 170 | | − |
| 171 | | − n = widebytes(ws); |
| 172 | | − s = malloc(n); |
| 173 | | − runes16toutf(s, ws, n); |
| 174 | | − return s; |
| 175 | | −} |
| 176 | | − |
| 177 | | − |
| 178 | | −int |
| 179 | | −widebytes(wchar_t *ws) |
| 180 | | −{ |
| 181 | | − int n = 0; |
| 182 | | − |
| 183 | | − while(*ws) |
| 184 | | − n += runelen(*ws++); |
| 185 | | − return n+1; |
| 186 | | −} |