lutf8lib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ** $Id: $
  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 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 an 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]) --> number of codepoints in 's' after 'i';
  52. ** nil if 's' not well formed
  53. */
  54. static int utflen (lua_State *L) {
  55. int n = 0;
  56. const char *ends;
  57. size_t len;
  58. const char *s = luaL_checklstring(L, 1, &len);
  59. lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), 1);
  60. luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1,
  61. "initial position out of string");
  62. ends = s + len;
  63. s += posi - 1;
  64. while (s < ends && (s = utf8_decode(s, NULL)) != NULL)
  65. n++;
  66. if (s == ends)
  67. lua_pushinteger(L, n);
  68. else
  69. lua_pushnil(L);
  70. return 1;
  71. }
  72. /*
  73. ** codepoint(s, [i, [j]]) -> returns codepoints for all characters
  74. ** between i and j
  75. */
  76. static int codepoint (lua_State *L) {
  77. size_t len;
  78. const char *s = luaL_checklstring(L, 1, &len);
  79. lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), len);
  80. lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), len);
  81. int n;
  82. const char *se;
  83. luaL_argcheck(L, posi >= 1, 2, "out of range");
  84. luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
  85. if (posi > pose) return 0; /* empty interval; return no values */
  86. n = (int)(pose - posi + 1);
  87. if (posi + n <= pose) /* (lua_Integer -> int) overflow? */
  88. return luaL_error(L, "string slice too long");
  89. luaL_checkstack(L, n, "string slice too long");
  90. n = 0;
  91. se = s + pose;
  92. for (s += posi - 1; s < se;) {
  93. int code;
  94. s = utf8_decode(s, &code);
  95. if (s == NULL)
  96. luaL_error(L, "invalid UTF-8 code");
  97. lua_pushinteger(L, code);
  98. n++;
  99. }
  100. return n;
  101. }
  102. static void pushutfchar (lua_State *L, int arg) {
  103. int code = luaL_checkint(L, arg);
  104. luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
  105. lua_pushfstring(L, "%U", code);
  106. }
  107. /*
  108. ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
  109. */
  110. static int utfchar (lua_State *L) {
  111. int n = lua_gettop(L); /* number of arguments */
  112. if (n == 1) /* optimize common case of single char */
  113. pushutfchar(L, 1);
  114. else {
  115. int i;
  116. luaL_Buffer b;
  117. luaL_buffinit(L, &b);
  118. for (i = 1; i <= n; i++) {
  119. pushutfchar(L, i);
  120. luaL_addvalue(&b);
  121. }
  122. luaL_pushresult(&b);
  123. }
  124. return 1;
  125. }
  126. /*
  127. ** offset(s, n, [i]) -> index where n-th character *after*
  128. ** position 'i' starts; 0 means character at 'i'.
  129. */
  130. static int byteoffset (lua_State *L) {
  131. size_t len;
  132. const char *s = luaL_checklstring(L, 1, &len);
  133. int n = luaL_checkint(L, 2);
  134. lua_Integer posi = posrelat(luaL_optinteger(L, 3, 1), len) - 1;
  135. luaL_argcheck(L, 0 <= posi && posi <= (lua_Integer)len, 3,
  136. "position out of range");
  137. if (n == 0) {
  138. /* find beginning of current byte sequence */
  139. while (posi > 0 && iscont(s + posi)) posi--;
  140. }
  141. else if (n < 0) {
  142. while (n < 0 && posi > 0) { /* move back */
  143. do { /* find beginning of previous character */
  144. posi--;
  145. } while (posi > 0 && iscont(s + posi));
  146. n++;
  147. }
  148. }
  149. else {
  150. n--; /* do not move for 1st character */
  151. while (n > 0 && posi < (lua_Integer)len) {
  152. do { /* find beginning of next character */
  153. posi++;
  154. } while (iscont(s + posi)); /* ('\0' is not continuation) */
  155. n--;
  156. }
  157. }
  158. if (n == 0)
  159. lua_pushinteger(L, posi + 1);
  160. else
  161. lua_pushnil(L); /* no such position */
  162. return 1;
  163. }
  164. static int iter_aux (lua_State *L) {
  165. size_t len;
  166. const char *s = luaL_checklstring(L, 1, &len);
  167. int n = lua_tointeger(L, 2) - 1;
  168. if (n < 0) /* first iteration? */
  169. n = 0; /* start from here */
  170. else if (n < (lua_Integer)len) {
  171. n++; /* skip current byte */
  172. while (iscont(s + n)) n++; /* and its continuations */
  173. }
  174. if (n >= (lua_Integer)len)
  175. return 0; /* no more codepoints */
  176. else {
  177. int code;
  178. const char *next = utf8_decode(s + n, &code);
  179. if (next == NULL || iscont(next))
  180. luaL_error(L, "invalid UTF-8 code");
  181. lua_pushinteger(L, n + 1);
  182. lua_pushinteger(L, code);
  183. return 2;
  184. }
  185. }
  186. static int iter_codes (lua_State *L) {
  187. luaL_checkstring(L, 1);
  188. lua_pushcfunction(L, iter_aux);
  189. lua_pushvalue(L, 1);
  190. lua_pushinteger(L, 0);
  191. return 3;
  192. }
  193. /* pattern to match a single UTF-8 character */
  194. #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
  195. static struct luaL_Reg funcs[] = {
  196. {"offset", byteoffset},
  197. {"codepoint", codepoint},
  198. {"char", utfchar},
  199. {"len", utflen},
  200. {"codes", iter_codes},
  201. {NULL, NULL}
  202. };
  203. int luaopen_utf8 (lua_State *L) {
  204. luaL_newlib(L, funcs);
  205. lua_pushliteral(L, UTF8PATT);
  206. lua_setfield(L, -2, "charpatt");
  207. return 1;
  208. }