lutf8lib.c 7.6 KB

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