rune.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include "util/utf.h"
  17. namespace re2 {
  18. enum
  19. {
  20. Bit1 = 7,
  21. Bitx = 6,
  22. Bit2 = 5,
  23. Bit3 = 4,
  24. Bit4 = 3,
  25. Bit5 = 2,
  26. T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
  27. Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
  28. T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
  29. T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
  30. T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
  31. T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
  32. Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0111 1111 */
  33. Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0111 1111 1111 */
  34. Rune3 = (1<<(Bit3+2*Bitx))-1, /* 1111 1111 1111 1111 */
  35. Rune4 = (1<<(Bit4+3*Bitx))-1,
  36. /* 0001 1111 1111 1111 1111 1111 */
  37. Maskx = (1<<Bitx)-1, /* 0011 1111 */
  38. Testx = Maskx ^ 0xFF, /* 1100 0000 */
  39. Bad = Runeerror,
  40. };
  41. int
  42. chartorune(Rune *rune, const char *str)
  43. {
  44. int c, c1, c2, c3;
  45. Rune l;
  46. /*
  47. * one character sequence
  48. * 00000-0007F => T1
  49. */
  50. c = *(unsigned char*)str;
  51. if(c < Tx) {
  52. *rune = c;
  53. return 1;
  54. }
  55. /*
  56. * two character sequence
  57. * 0080-07FF => T2 Tx
  58. */
  59. c1 = *(unsigned char*)(str+1) ^ Tx;
  60. if(c1 & Testx)
  61. goto bad;
  62. if(c < T3) {
  63. if(c < T2)
  64. goto bad;
  65. l = ((c << Bitx) | c1) & Rune2;
  66. if(l <= Rune1)
  67. goto bad;
  68. *rune = l;
  69. return 2;
  70. }
  71. /*
  72. * three character sequence
  73. * 0800-FFFF => T3 Tx Tx
  74. */
  75. c2 = *(unsigned char*)(str+2) ^ Tx;
  76. if(c2 & Testx)
  77. goto bad;
  78. if(c < T4) {
  79. l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
  80. if(l <= Rune2)
  81. goto bad;
  82. *rune = l;
  83. return 3;
  84. }
  85. /*
  86. * four character sequence (21-bit value)
  87. * 10000-1FFFFF => T4 Tx Tx Tx
  88. */
  89. c3 = *(unsigned char*)(str+3) ^ Tx;
  90. if (c3 & Testx)
  91. goto bad;
  92. if (c < T5) {
  93. l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
  94. if (l <= Rune3)
  95. goto bad;
  96. *rune = l;
  97. return 4;
  98. }
  99. /*
  100. * Support for 5-byte or longer UTF-8 would go here, but
  101. * since we don't have that, we'll just fall through to bad.
  102. */
  103. /*
  104. * bad decoding
  105. */
  106. bad:
  107. *rune = Bad;
  108. return 1;
  109. }
  110. int
  111. runetochar(char *str, const Rune *rune)
  112. {
  113. /* Runes are signed, so convert to unsigned for range check. */
  114. unsigned int c;
  115. /*
  116. * one character sequence
  117. * 00000-0007F => 00-7F
  118. */
  119. c = *rune;
  120. if(c <= Rune1) {
  121. str[0] = static_cast<char>(c);
  122. return 1;
  123. }
  124. /*
  125. * two character sequence
  126. * 0080-07FF => T2 Tx
  127. */
  128. if(c <= Rune2) {
  129. str[0] = T2 | static_cast<char>(c >> 1*Bitx);
  130. str[1] = Tx | (c & Maskx);
  131. return 2;
  132. }
  133. /*
  134. * If the Rune is out of range, convert it to the error rune.
  135. * Do this test here because the error rune encodes to three bytes.
  136. * Doing it earlier would duplicate work, since an out of range
  137. * Rune wouldn't have fit in one or two bytes.
  138. */
  139. if (c > Runemax)
  140. c = Runeerror;
  141. /*
  142. * three character sequence
  143. * 0800-FFFF => T3 Tx Tx
  144. */
  145. if (c <= Rune3) {
  146. str[0] = T3 | static_cast<char>(c >> 2*Bitx);
  147. str[1] = Tx | ((c >> 1*Bitx) & Maskx);
  148. str[2] = Tx | (c & Maskx);
  149. return 3;
  150. }
  151. /*
  152. * four character sequence (21-bit value)
  153. * 10000-1FFFFF => T4 Tx Tx Tx
  154. */
  155. str[0] = T4 | static_cast<char>(c >> 3*Bitx);
  156. str[1] = Tx | ((c >> 2*Bitx) & Maskx);
  157. str[2] = Tx | ((c >> 1*Bitx) & Maskx);
  158. str[3] = Tx | (c & Maskx);
  159. return 4;
  160. }
  161. int
  162. runelen(Rune rune)
  163. {
  164. char str[10];
  165. return runetochar(str, &rune);
  166. }
  167. int
  168. fullrune(const char *str, int n)
  169. {
  170. if (n > 0) {
  171. int c = *(unsigned char*)str;
  172. if (c < Tx)
  173. return 1;
  174. if (n > 1) {
  175. if (c < T3)
  176. return 1;
  177. if (n > 2) {
  178. if (c < T4 || n > 3)
  179. return 1;
  180. }
  181. }
  182. }
  183. return 0;
  184. }
  185. int
  186. utflen(const char *s)
  187. {
  188. int c;
  189. int n;
  190. Rune rune;
  191. n = 0;
  192. for(;;) {
  193. c = *(unsigned char*)s;
  194. if(c < Runeself) {
  195. if(c == 0)
  196. return n;
  197. s++;
  198. } else
  199. s += chartorune(&rune, s);
  200. n++;
  201. }
  202. return 0;
  203. }
  204. char*
  205. utfrune(const char *s, Rune c)
  206. {
  207. int c1;
  208. Rune r;
  209. int n;
  210. if(c < Runesync) /* not part of utf sequence */
  211. return strchr((char*)s, c);
  212. for(;;) {
  213. c1 = *(unsigned char*)s;
  214. if(c1 < Runeself) { /* one byte rune */
  215. if(c1 == 0)
  216. return 0;
  217. if(c1 == c)
  218. return (char*)s;
  219. s++;
  220. continue;
  221. }
  222. n = chartorune(&r, s);
  223. if(r == c)
  224. return (char*)s;
  225. s += n;
  226. }
  227. return 0;
  228. }
  229. } // namespace re2