diff --git a/utf.c b/utf.c index 6a93a19..3d6a4c8 100644 --- a/utf.c +++ b/utf.c @@ -46,7 +46,7 @@ int chartorune(Rune *rune, const char *str) { int c, c1, c2; - long l; + int l; /* * one character sequence @@ -101,7 +101,7 @@ bad: int runetochar(char *str, const Rune *rune) { - long c; + int c; /* * one character sequence @@ -134,7 +134,7 @@ runetochar(char *str, const Rune *rune) } int -runelen(long c) +runelen(int c) { Rune rune; char str[10]; @@ -143,244 +143,11 @@ runelen(long c) return runetochar(str, &rune); } -int -runenlen(const Rune *r, int nrune) -{ - int nb, c; - - nb = 0; - while(nrune--) { - c = *r++; - if(c <= Rune1) - nb++; - else - if(c <= Rune2) - nb += 2; - else - nb += 3; - } - return nb; -} - -int -fullrune(const char *str, int n) -{ - int c; - - if(n > 0) { - c = *(uchar*)str; - if(c < Tx) - return 1; - if(n > 1) - if(c < T3 || n > 2) - return 1; - } - return 0; -} - -Rune* -runestrcat(Rune *s1, const Rune *s2) -{ - runestrcpy(runestrchr(s1, 0), s2); - return s1; -} - -Rune* -runestrchr(const Rune *s, Rune c) -{ - Rune c0 = c; - Rune c1; - - if(c == 0) { - while(*s++) - ; - return (Rune*) s-1; - } - - while((c1 = *s++)) - if(c1 == c0) - return (Rune*) s-1; - return 0; -} - -int -runestrcmp(const Rune *s1, const Rune *s2) -{ - Rune c1, c2; - - for(;;) { - c1 = *s1++; - c2 = *s2++; - if(c1 != c2) { - if(c1 > c2) - return 1; - return -1; - } - if(c1 == 0) - return 0; - } -} - -Rune* -runestrcpy(Rune *s1, const Rune *s2) -{ - Rune *os1; - - os1 = s1; - while((*s1++ = *s2++)) - ; - return os1; -} - -Rune* -runestrdup(const Rune *s) -{ - Rune *ns; - - ns = malloc(sizeof(Rune)*(runestrlen(s) + 1)); - if(ns == 0) - return 0; - - return runestrcpy(ns, s); -} - -Rune* -runestrecpy(Rune *s1, Rune *es1, const Rune *s2) -{ - if(s1 >= es1) - return s1; - - while((*s1++ = *s2++)){ - if(s1 == es1){ - *--s1 = '\0'; - break; - } - } - return s1; -} - -long -runestrlen(const Rune *s) -{ - - return runestrchr(s, 0) - s; -} - -Rune* -runestrncat(Rune *s1, const Rune *s2, long n) -{ - Rune *os1; - - os1 = s1; - s1 = runestrchr(s1, 0); - while((*s1++ = *s2++)) - if(--n < 0) { - s1[-1] = 0; - break; - } - return os1; -} - -int -runestrncmp(const Rune *s1, const Rune *s2, long n) -{ - Rune c1, c2; - - while(n > 0) { - c1 = *s1++; - c2 = *s2++; - n--; - if(c1 != c2) { - if(c1 > c2) - return 1; - return -1; - } - if(c1 == 0) - break; - } - return 0; -} - -Rune* -runestrncpy(Rune *s1, const Rune *s2, long n) -{ - int i; - Rune *os1; - - os1 = s1; - for(i = 0; i < n; i++) - if((*s1++ = *s2++) == 0) { - while(++i < n) - *s1++ = 0; - return os1; - } - return os1; -} - -Rune* -runestrrchr(const Rune *s, Rune c) -{ - Rune *r; - - if(c == 0) - return runestrchr(s, 0); - r = 0; - while((s = runestrchr(s, c))) - r = (Rune*) s++; - return r; -} - -/* - * Return pointer to first occurrence of s2 in s1, - * 0 if none - */ -Rune* -runestrstr(const Rune *s1, const Rune *s2) -{ - const Rune *p, *pa, *pb; - int c0, c; - - c0 = *s2; - if(c0 == 0) - return (Rune*) s1; - s2++; - for(p=runestrchr(s1, c0); p; p=runestrchr(p+1, c0)) { - pa = p; - for(pb=s2;; pb++) { - c = *pb; - if(c == 0) - return (Rune*) p; - if(c != *++pa) - break; - } - } - return 0; -} - -char* -utfecpy(char *to, char *e, const char *from) -{ - char *end; - - if(to >= e) - return to; - end = memccpy(to, from, '\0', e - to); - if(end == 0){ - end = e-1; - while(end>to && (*--end&0xC0)==0x80) - ; - *end = '\0'; - }else{ - end--; - } - return end; -} - int utflen(const char *s) { int c; - long n; + int n; Rune rune; n = 0; @@ -395,105 +162,3 @@ utflen(const char *s) n++; } } - -int -utfnlen(const char *s, long m) -{ - int c; - long n; - Rune rune; - const char *es; - - es = s + m; - for(n = 0; s < es; n++) { - c = *(uchar*)s; - if(c < Runeself){ - if(c == '\0') - break; - s++; - continue; - } - if(!fullrune(s, es-s)) - break; - s += chartorune(&rune, s); - } - return n; -} - -char* -utfrrune(const char *s, long c) -{ - long c1; - Rune r; - const char *s1; - - if(c < Runesync) /* not part of utf sequence */ - return strrchr(s, c); - - s1 = 0; - for(;;) { - c1 = *(uchar*)s; - if(c1 < Runeself) { /* one byte rune */ - if(c1 == 0) - return (char*) s1; - if(c1 == c) - s1 = s; - s++; - continue; - } - c1 = chartorune(&r, s); - if(r == c) - s1 = s; - s += c1; - } -} - -char* -utfrune(const char *s, long c) -{ - long c1; - Rune r; - int n; - - if(c < Runesync) /* not part of utf sequence */ - return strchr(s, c); - - for(;;) { - c1 = *(uchar*)s; - if(c1 < Runeself) { /* one byte rune */ - if(c1 == 0) - return 0; - if(c1 == c) - return (char*) s; - s++; - continue; - } - n = chartorune(&r, s); - if(r == c) - return (char*) s; - s += n; - } -} - -/* - * Return pointer to first occurrence of s2 in s1, - * 0 if none - */ -char* -utfutf(const char *s1, const char *s2) -{ - const char *p; - long f, n1, n2; - Rune r; - - n1 = chartorune(&r, s2); - f = r; - if(f <= Runesync) /* represents self */ - return strstr(s1, s2); - - n2 = strlen(s2); - for(p=s1; (p=utfrune(p, f)); p+=n1) - if(strncmp(p, s2, n2) == 0) - return (char*) p; - return 0; -} diff --git a/utf.h b/utf.h index 7839982..9e8ab87 100644 --- a/utf.h +++ b/utf.h @@ -4,36 +4,18 @@ typedef unsigned short Rune; /* 16 bits */ #define chartorune jsU_chartorune -#define fullrune jsU_fullrune +#define runetochar jsU_runetochar +#define runelen jsU_runelen +#define utflen jsU_utflen + #define isalpharune jsU_isalpharune #define islowerrune jsU_islowerrune #define isspacerune jsU_isspacerune #define istitlerune jsU_istitlerune #define isupperrune jsU_isupperrune -#define runelen jsU_runelen -#define runenlen jsU_runenlen -#define runestrcat jsU_runestrcat -#define runestrchr jsU_runestrchr -#define runestrcmp jsU_runestrcmp -#define runestrcpy jsU_runestrcpy -#define runestrdup jsU_runestrdup -#define runestrecpy jsU_runestrecpy -#define runestrlen jsU_runestrlen -#define runestrncat jsU_runestrncat -#define runestrncmp jsU_runestrncmp -#define runestrncpy jsU_runestrncpy -#define runestrrchr jsU_runestrrchr -#define runestrstr jsU_runestrstr -#define runetochar jsU_runetochar #define tolowerrune jsU_tolowerrune #define totitlerune jsU_totitlerune #define toupperrune jsU_toupperrune -#define utfecpy jsU_utfecpy -#define utflen jsU_utflen -#define utfnlen jsU_utfnlen -#define utfrrune jsU_utfrrune -#define utfrune jsU_utfrune -#define utfutf jsU_utfutf enum { @@ -44,35 +26,17 @@ enum }; int chartorune(Rune *rune, const char *str); -int fullrune(const char *str, int n); +int runetochar(char *str, const Rune *rune); +int runelen(int c); +int utflen(const char *s); + int isalpharune(Rune c); int islowerrune(Rune c); int isspacerune(Rune c); int istitlerune(Rune c); int isupperrune(Rune c); -int runelen(long c); -int runenlen(const Rune *r, int nrune); -Rune* runestrcat(Rune *s1, const Rune *s2); -Rune* runestrchr(const Rune *s, Rune c); -int runestrcmp(const Rune *s1, const Rune *s2); -Rune* runestrcpy(Rune *s1, const Rune *s2); -Rune* runestrdup(const Rune *s) ; -Rune* runestrecpy(Rune *s1, Rune *es1, const Rune *s2); -long runestrlen(const Rune *s); -Rune* runestrncat(Rune *s1, const Rune *s2, long n); -int runestrncmp(const Rune *s1, const Rune *s2, long n); -Rune* runestrncpy(Rune *s1, const Rune *s2, long n); -Rune* runestrrchr(const Rune *s, Rune c); -Rune* runestrstr(const Rune *s1, const Rune *s2); -int runetochar(char *str, const Rune *rune); Rune tolowerrune(Rune c); Rune totitlerune(Rune c); Rune toupperrune(Rune c); -char* utfecpy(char *to, char *e, const char *from); -int utflen(const char *s); -int utfnlen(const char *s, long m); -char* utfrrune(const char *s, long c); -char* utfrune(const char *s, long c); -char* utfutf(const char *s1, const char *s2); #endif