9
3

lutf8lib.c 6.9 KB

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