lutf8lib.c 7.2 KB

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