utf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE
  11. * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "utf.h"
  17. #include "utfdata.h"
  18. #define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
  19. typedef unsigned char uchar;
  20. enum
  21. {
  22. Bit1 = 7,
  23. Bitx = 6,
  24. Bit2 = 5,
  25. Bit3 = 4,
  26. Bit4 = 3,
  27. Bit5 = 2,
  28. T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
  29. Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
  30. T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
  31. T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
  32. T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
  33. T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
  34. Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */
  35. Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0000 0000 0111 1111 1111 */
  36. Rune3 = (1<<(Bit3+2*Bitx))-1, /* 0000 0000 1111 1111 1111 1111 */
  37. Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
  38. Maskx = (1<<Bitx)-1, /* 0011 1111 */
  39. Testx = Maskx ^ 0xFF, /* 1100 0000 */
  40. Bad = Runeerror
  41. };
  42. int
  43. chartorune(Rune *rune, const char *str)
  44. {
  45. int c, c1, c2, c3;
  46. int l;
  47. /* overlong null character */
  48. if((uchar)str[0] == 0xc0 && (uchar)str[1] == 0x80) {
  49. *rune = 0;
  50. return 2;
  51. }
  52. /*
  53. * one character sequence
  54. * 00000-0007F => T1
  55. */
  56. c = *(uchar*)str;
  57. if(c < Tx) {
  58. *rune = c;
  59. return 1;
  60. }
  61. /*
  62. * two character sequence
  63. * 0080-07FF => T2 Tx
  64. */
  65. c1 = *(uchar*)(str+1) ^ Tx;
  66. if(c1 & Testx)
  67. goto bad;
  68. if(c < T3) {
  69. if(c < T2)
  70. goto bad;
  71. l = ((c << Bitx) | c1) & Rune2;
  72. if(l <= Rune1)
  73. goto bad;
  74. *rune = l;
  75. return 2;
  76. }
  77. /*
  78. * three character sequence
  79. * 0800-FFFF => T3 Tx Tx
  80. */
  81. c2 = *(uchar*)(str+2) ^ Tx;
  82. if(c2 & Testx)
  83. goto bad;
  84. if(c < T4) {
  85. l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
  86. if(l <= Rune2)
  87. goto bad;
  88. *rune = l;
  89. return 3;
  90. }
  91. /*
  92. * four character sequence
  93. * 10000-10FFFF => T4 Tx Tx Tx
  94. */
  95. if(UTFmax >= 4) {
  96. c3 = *(uchar*)(str+3) ^ Tx;
  97. if(c3 & Testx)
  98. goto bad;
  99. if(c < T5) {
  100. l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
  101. if(l <= Rune3)
  102. goto bad;
  103. if(l > Runemax)
  104. goto bad;
  105. *rune = l;
  106. return 4;
  107. }
  108. }
  109. /*
  110. * bad decoding
  111. */
  112. bad:
  113. *rune = Bad;
  114. return 1;
  115. }
  116. int
  117. runetochar(char *str, const Rune *rune)
  118. {
  119. int c = *rune;
  120. /* overlong null character */
  121. if (c == 0) {
  122. str[0] = (char)0xc0;
  123. str[1] = (char)0x80;
  124. return 2;
  125. }
  126. /*
  127. * one character sequence
  128. * 00000-0007F => 00-7F
  129. */
  130. if(c <= Rune1) {
  131. str[0] = c;
  132. return 1;
  133. }
  134. /*
  135. * two character sequence
  136. * 00080-007FF => T2 Tx
  137. */
  138. if(c <= Rune2) {
  139. str[0] = T2 | (c >> 1*Bitx);
  140. str[1] = Tx | (c & Maskx);
  141. return 2;
  142. }
  143. /*
  144. * three character sequence
  145. * 00800-0FFFF => T3 Tx Tx
  146. */
  147. if(c > Runemax)
  148. c = Runeerror;
  149. if(c <= Rune3) {
  150. str[0] = T3 | (c >> 2*Bitx);
  151. str[1] = Tx | ((c >> 1*Bitx) & Maskx);
  152. str[2] = Tx | (c & Maskx);
  153. return 3;
  154. }
  155. /*
  156. * four character sequence
  157. * 010000-1FFFFF => T4 Tx Tx Tx
  158. */
  159. str[0] = T4 | (c >> 3*Bitx);
  160. str[1] = Tx | ((c >> 2*Bitx) & Maskx);
  161. str[2] = Tx | ((c >> 1*Bitx) & Maskx);
  162. str[3] = Tx | (c & Maskx);
  163. return 4;
  164. }
  165. int
  166. runelen(int c)
  167. {
  168. Rune rune;
  169. char str[10];
  170. rune = c;
  171. return runetochar(str, &rune);
  172. }
  173. static const Rune *
  174. ucd_bsearch(Rune c, const Rune *t, int n, int ne)
  175. {
  176. const Rune *p;
  177. int m;
  178. while(n > 1) {
  179. m = n/2;
  180. p = t + m*ne;
  181. if(c >= p[0]) {
  182. t = p;
  183. n = n-m;
  184. } else
  185. n = m;
  186. }
  187. if(n && c >= t[0])
  188. return t;
  189. return 0;
  190. }
  191. Rune
  192. tolowerrune(Rune c)
  193. {
  194. const Rune *p;
  195. p = ucd_bsearch(c, ucd_tolower2, nelem(ucd_tolower2)/3, 3);
  196. if(p && c >= p[0] && c <= p[1])
  197. return c + p[2];
  198. p = ucd_bsearch(c, ucd_tolower1, nelem(ucd_tolower1)/2, 2);
  199. if(p && c == p[0])
  200. return c + p[1];
  201. return c;
  202. }
  203. Rune
  204. toupperrune(Rune c)
  205. {
  206. const Rune *p;
  207. p = ucd_bsearch(c, ucd_toupper2, nelem(ucd_toupper2)/3, 3);
  208. if(p && c >= p[0] && c <= p[1])
  209. return c + p[2];
  210. p = ucd_bsearch(c, ucd_toupper1, nelem(ucd_toupper1)/2, 2);
  211. if(p && c == p[0])
  212. return c + p[1];
  213. return c;
  214. }
  215. int
  216. islowerrune(Rune c)
  217. {
  218. const Rune *p;
  219. p = ucd_bsearch(c, ucd_toupper2, nelem(ucd_toupper2)/3, 3);
  220. if(p && c >= p[0] && c <= p[1])
  221. return 1;
  222. p = ucd_bsearch(c, ucd_toupper1, nelem(ucd_toupper1)/2, 2);
  223. if(p && c == p[0])
  224. return 1;
  225. return 0;
  226. }
  227. int
  228. isupperrune(Rune c)
  229. {
  230. const Rune *p;
  231. p = ucd_bsearch(c, ucd_tolower2, nelem(ucd_tolower2)/3, 3);
  232. if(p && c >= p[0] && c <= p[1])
  233. return 1;
  234. p = ucd_bsearch(c, ucd_tolower1, nelem(ucd_tolower1)/2, 2);
  235. if(p && c == p[0])
  236. return 1;
  237. return 0;
  238. }
  239. int
  240. isalpharune(Rune c)
  241. {
  242. const Rune *p;
  243. p = ucd_bsearch(c, ucd_alpha2, nelem(ucd_alpha2)/2, 2);
  244. if(p && c >= p[0] && c <= p[1])
  245. return 1;
  246. p = ucd_bsearch(c, ucd_alpha1, nelem(ucd_alpha1), 1);
  247. if(p && c == p[0])
  248. return 1;
  249. return 0;
  250. }