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