lutf8lib.c 6.8 KB

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