1 /* $NetBSD: lutf8lib.c,v 1.3 2015/10/08 13:21:00 mbalmer Exp $ */
4 ** Id: lutf8lib.c,v 1.15 2015/03/28 19:16:55 roberto Exp
5 ** Standard library for UTF-8 manipulation
6 ** See Copyright Notice in lua.h
27 #define MAXUNICODE 0x10FFFF
29 #define iscont(p) ((*(p) & 0xC0) == 0x80)
33 /* translate a relative string position: negative means back from end */
34 static 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;
42 ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
44 static 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? */
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 */
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 */
66 return (const char *)s
+ 1; /* +1 to include first byte */
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
75 static int utflen (lua_State
*L
) {
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 */
95 lua_pushinteger(L
, n
);
101 ** codepoint(s, [i, [j]]) -> returns codepoints for all characters
102 ** that start in the range [i,j]
104 static int codepoint (lua_State
*L
) {
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
);
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");
120 for (s
+= posi
- 1; s
< se
;) {
122 s
= utf8_decode(s
, &code
);
124 return luaL_error(L
, "invalid UTF-8 code");
125 lua_pushinteger(L
, code
);
132 static 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
);
140 ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
142 static int utfchar (lua_State
*L
) {
143 int n
= lua_gettop(L
); /* number of arguments */
144 if (n
== 1) /* optimize common case of single char */
149 luaL_buffinit(L
, &b
);
150 for (i
= 1; i
<= n
; i
++) {
161 ** offset(s, n, [i]) -> index where n-th character counting from
162 ** position 'i' starts; 0 means character at 'i'.
164 static int byteoffset (lua_State
*L
) {
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");
173 /* find beginning of current byte sequence */
174 while (posi
> 0 && iscont(s
+ posi
)) posi
--;
177 if (iscont(s
+ posi
))
178 luaL_error(L
, "initial position is a continuation byte");
180 while (n
< 0 && posi
> 0) { /* move back */
181 do { /* find beginning of previous character */
183 } while (posi
> 0 && iscont(s
+ posi
));
188 n
--; /* do not move for 1st character */
189 while (n
> 0 && posi
< (lua_Integer
)len
) {
190 do { /* find beginning of next character */
192 } while (iscont(s
+ posi
)); /* (cannot pass final '\0') */
197 if (n
== 0) /* did it find given character? */
198 lua_pushinteger(L
, posi
+ 1);
199 else /* no such character */
205 static int iter_aux (lua_State
*L
) {
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 */
215 if (n
>= (lua_Integer
)len
)
216 return 0; /* no more codepoints */
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
);
229 static int iter_codes (lua_State
*L
) {
230 luaL_checkstring(L
, 1);
231 lua_pushcfunction(L
, iter_aux
);
233 lua_pushinteger(L
, 0);
238 /* pattern to match a single UTF-8 character */
239 #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
242 static const luaL_Reg funcs
[] = {
243 {"offset", byteoffset
},
244 {"codepoint", codepoint
},
247 {"codes", iter_codes
},
249 {"charpattern", NULL
},
254 LUAMOD_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");