1 #define UNICODE 2 #define Unknown win_Unknown 3 #include <windows.h> 4 #include <winbase.h> 5 #undef Unknown 6 #undef Sleep 7 #include "dat.h" 8 #include "fns.h" 9 #include "error.h" 10 #include "r16.h" 11 12 Rune16* 13 runesdup(Rune16 *r) 14 { 15 int n; 16 Rune16 *s; 17 18 n = runeslen(r) + 1; 19 s = malloc(n * sizeof(Rune16)); 20 if(s == nil) 21 error(Enomem); 22 memmove(s, r, n * sizeof(Rune16)); 23 return s; 24 } 25 26 int 27 runeslen(Rune16 *r) 28 { 29 int n; 30 31 n = 0; 32 while(*r++ != 0) 33 n++; 34 return n; 35 } 36 37 char* 38 runestoutf(char *p, Rune16 *r, int nc) 39 { 40 char *op, *ep; 41 int n, c; 42 Rune rc; 43 44 op = p; 45 ep = p + nc; 46 while(c = *r++) { 47 n = 1; 48 if(c >= Runeself) 49 n = runelen(c); 50 if(p + n >= ep) 51 break; 52 rc = c; 53 if(c < Runeself) 54 *p++ = c; 55 else 56 p += runetochar(p, &rc); 57 } 58 *p = '\0'; 59 return op; 60 } 61 62 Rune16* 63 utftorunes(Rune16 *r, char *p, int nc) 64 { 65 Rune16 *or, *er; 66 Rune rc; 67 68 or = r; 69 er = r + nc; 70 while(*p != '\0' && r + 1 < er){ 71 p += chartorune(&rc, p); 72 *r++ = rc; /* we'll ignore surrogate pairs */ 73 } 74 *r = '\0'; 75 return or; 76 } 77 78 int 79 runescmp(Rune16 *s1, Rune16 *s2) 80 { 81 Rune16 r1, r2; 82 83 for(;;) { 84 r1 = *s1++; 85 r2 = *s2++; 86 if(r1 != r2) { 87 if(r1 > r2) 88 return 1; 89 return -1; 90 } 91 if(r1 == 0) 92 return 0; 93 } 94 } 95