lutf8lib.c 6.8 KB

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