lutf8lib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** $Id: lutf8lib.c $
  3. ** Standard library for UTF-8 manipulation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lutf8lib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #define MAXUNICODE 0x10FFFF
  17. /*
  18. ** Integer type for decoded UTF-8 values; MAXUNICODE needs 21 bits.
  19. */
  20. #if LUAI_BITSINT >= 21
  21. typedef unsigned int utfint;
  22. #else
  23. typedef unsigned long utfint;
  24. #endif
  25. #define iscont(p) ((*(p) & 0xC0) == 0x80)
  26. /* from strlib */
  27. /* translate a relative string position: negative means back from end */
  28. static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
  29. if (pos >= 0) return pos;
  30. else if (0u - (size_t)pos > len) return 0;
  31. else return (lua_Integer)len + pos + 1;
  32. }
  33. /*
  34. ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
  35. */
  36. static const char *utf8_decode (const char *o, utfint *val) {
  37. static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
  38. const unsigned char *s = (const unsigned char *)o;
  39. unsigned int c = s[0];
  40. utfint res = 0; /* final result */
  41. if (c < 0x80) /* ascii? */
  42. res = c;
  43. else {
  44. int count = 0; /* to count number of continuation bytes */
  45. while (c & 0x40) { /* still have continuation bytes? */
  46. int cc = s[++count]; /* read next byte */
  47. if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
  48. return NULL; /* invalid byte sequence */
  49. res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
  50. c <<= 1; /* to test next bit */
  51. }
  52. res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
  53. if (count > 3 || res > MAXUNICODE || res <= limits[count])
  54. return NULL; /* invalid byte sequence */
  55. s += count; /* skip continuation bytes read */
  56. }
  57. if (val) *val = res;
  58. return (const char *)s + 1; /* +1 to include first byte */
  59. }
  60. /*
  61. ** utf8len(s [, i [, j]]) --> number of characters that start in the
  62. ** range [i,j], or nil + current position if 's' is not well formed in
  63. ** that interval
  64. */
  65. static int utflen (lua_State *L) {
  66. lua_Integer n = 0; /* counter for the number of characters */
  67. size_t len; /* string length in bytes */
  68. const char *s = luaL_checklstring(L, 1, &len);
  69. lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
  70. lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
  71. luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
  72. "initial position out of string");
  73. luaL_argcheck(L, --posj < (lua_Integer)len, 3,
  74. "final position out of string");
  75. while (posi <= posj) {
  76. const char *s1 = utf8_decode(s + posi, NULL);
  77. if (s1 == NULL) { /* conversion error? */
  78. lua_pushnil(L); /* return nil ... */
  79. lua_pushinteger(L, posi + 1); /* ... and current position */
  80. return 2;
  81. }
  82. posi = s1 - s;
  83. n++;
  84. }
  85. lua_pushinteger(L, n);
  86. return 1;
  87. }
  88. /*
  89. ** codepoint(s, [i, [j]]) -> returns codepoints for all characters
  90. ** that start in the range [i,j]
  91. */
  92. static int codepoint (lua_State *L) {
  93. size_t len;
  94. const char *s = luaL_checklstring(L, 1, &len);
  95. lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
  96. lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
  97. int n;
  98. const char *se;
  99. luaL_argcheck(L, posi >= 1, 2, "out of range");
  100. luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
  101. if (posi > pose) return 0; /* empty interval; return no values */
  102. if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
  103. return luaL_error(L, "string slice too long");
  104. n = (int)(pose - posi) + 1; /* upper bound for number of returns */
  105. luaL_checkstack(L, n, "string slice too long");
  106. n = 0; /* count the number of returns */
  107. se = s + pose; /* string end */
  108. for (s += posi - 1; s < se;) {
  109. utfint code;
  110. s = utf8_decode(s, &code);
  111. if (s == NULL)
  112. return luaL_error(L, "invalid UTF-8 code");
  113. lua_pushinteger(L, code);
  114. n++;
  115. }
  116. return n;
  117. }
  118. static void pushutfchar (lua_State *L, int arg) {
  119. lua_Integer code = luaL_checkinteger(L, arg);
  120. luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
  121. lua_pushfstring(L, "%U", (long)code);
  122. }
  123. /*
  124. ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
  125. */
  126. static int utfchar (lua_State *L) {
  127. int n = lua_gettop(L); /* number of arguments */
  128. if (n == 1) /* optimize common case of single char */
  129. pushutfchar(L, 1);
  130. else {
  131. int i;
  132. luaL_Buffer b;
  133. luaL_buffinit(L, &b);
  134. for (i = 1; i <= n; i++) {
  135. pushutfchar(L, i);
  136. luaL_addvalue(&b);
  137. }
  138. luaL_pushresult(&b);
  139. }
  140. return 1;
  141. }
  142. /*
  143. ** offset(s, n, [i]) -> index where n-th character counting from
  144. ** position 'i' starts; 0 means character at 'i'.
  145. */
  146. static int byteoffset (lua_State *L) {
  147. size_t len;
  148. const char *s = luaL_checklstring(L, 1, &len);
  149. lua_Integer n = luaL_checkinteger(L, 2);
  150. lua_Integer posi = (n >= 0) ? 1 : len + 1;
  151. posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
  152. luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
  153. "position out of range");
  154. if (n == 0) {
  155. /* find beginning of current byte sequence */
  156. while (posi > 0 && iscont(s + posi)) posi--;
  157. }
  158. else {
  159. if (iscont(s + posi))
  160. return luaL_error(L, "initial position is a continuation byte");
  161. if (n < 0) {
  162. while (n < 0 && posi > 0) { /* move back */
  163. do { /* find beginning of previous character */
  164. posi--;
  165. } while (posi > 0 && iscont(s + posi));
  166. n++;
  167. }
  168. }
  169. else {
  170. n--; /* do not move for 1st character */
  171. while (n > 0 && posi < (lua_Integer)len) {
  172. do { /* find beginning of next character */
  173. posi++;
  174. } while (iscont(s + posi)); /* (cannot pass final '\0') */
  175. n--;
  176. }
  177. }
  178. }
  179. if (n == 0) /* did it find given character? */
  180. lua_pushinteger(L, posi + 1);
  181. else /* no such character */
  182. lua_pushnil(L);
  183. return 1;
  184. }
  185. static int iter_aux (lua_State *L) {
  186. size_t len;
  187. const char *s = luaL_checklstring(L, 1, &len);
  188. lua_Integer n = lua_tointeger(L, 2) - 1;
  189. if (n < 0) /* first iteration? */
  190. n = 0; /* start from here */
  191. else if (n < (lua_Integer)len) {
  192. n++; /* skip current byte */
  193. while (iscont(s + n)) n++; /* and its continuations */
  194. }
  195. if (n >= (lua_Integer)len)
  196. return 0; /* no more codepoints */
  197. else {
  198. utfint code;
  199. const char *next = utf8_decode(s + n, &code);
  200. if (next == NULL || iscont(next))
  201. return luaL_error(L, "invalid UTF-8 code");
  202. lua_pushinteger(L, n + 1);
  203. lua_pushinteger(L, code);
  204. return 2;
  205. }
  206. }
  207. static int iter_codes (lua_State *L) {
  208. luaL_checkstring(L, 1);
  209. lua_pushcfunction(L, iter_aux);
  210. lua_pushvalue(L, 1);
  211. lua_pushinteger(L, 0);
  212. return 3;
  213. }
  214. /* pattern to match a single UTF-8 character */
  215. #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
  216. static const luaL_Reg funcs[] = {
  217. {"offset", byteoffset},
  218. {"codepoint", codepoint},
  219. {"char", utfchar},
  220. {"len", utflen},
  221. {"codes", iter_codes},
  222. /* placeholders */
  223. {"charpattern", NULL},
  224. {NULL, NULL}
  225. };
  226. LUAMOD_API int luaopen_utf8 (lua_State *L) {
  227. luaL_newlib(L, funcs);
  228. lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
  229. lua_setfield(L, -2, "charpattern");
  230. return 1;
  231. }