3 ** Standard library for UTF-8 manipulation
4 ** See Copyright Notice in lua.h
24 #define MAXUNICODE 0x10FFFFu
26 #define MAXUTF 0x7FFFFFFFu
29 #define MSGInvalid "invalid UTF-8 code"
32 ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
34 #if (UINT_MAX >> 30) >= 1
35 typedef unsigned int utfint
;
37 typedef unsigned long utfint
;
41 #define iscont(c) (((c) & 0xC0) == 0x80)
42 #define iscontp(p) iscont(*(p))
46 /* translate a relative string position: negative means back from end */
47 static lua_Integer
u_posrelat(lua_Integer pos
, size_t len
) {
48 if (pos
>= 0) return pos
;
49 else if (0u - (size_t)pos
> len
) return 0;
50 else return (lua_Integer
)len
+ pos
+ 1;
55 ** Decode one UTF-8 sequence, returning NULL if byte sequence is
56 ** invalid. The array 'limits' stores the minimum value for each
57 ** sequence length, to check for overlong representations. Its first
58 ** entry forces an error for non-ascii bytes with no continuation
59 ** bytes (count == 0).
61 static const char *utf8_decode(const char *s
, utfint
*val
, int strict
) {
62 static const utfint limits
[] =
63 {~(utfint
)0, 0x80, 0x800, 0x10000u
, 0x200000u
, 0x4000000u
};
64 unsigned int c
= (unsigned char)s
[0];
65 utfint res
= 0; /* final result */
66 if (c
< 0x80) /* ascii? */
69 int count
= 0; /* to count number of continuation bytes */
70 for (; c
& 0x40; c
<<= 1) { /* while it needs continuation bytes... */
71 unsigned int cc
= (unsigned char)s
[++count
]; /* read next byte */
72 if (!iscont(cc
)) /* not a continuation byte? */
73 return NULL
; /* invalid byte sequence */
74 res
= (res
<< 6) | (cc
& 0x3F); /* add lower 6 bits from cont. byte */
76 res
|= ((utfint
)(c
& 0x7F) << (count
* 5)); /* add first byte */
77 if (count
> 5 || res
> MAXUTF
|| res
< limits
[count
])
78 return NULL
; /* invalid byte sequence */
79 s
+= count
; /* skip continuation bytes read */
82 /* check for invalid code points; too large or surrogates */
83 if (res
> MAXUNICODE
|| (0xD800u
<= res
&& res
<= 0xDFFFu
))
87 return s
+ 1; /* +1 to include first byte */
92 ** utf8len(s [, i [, j [, lax]]]) --> number of characters that
93 ** start in the range [i,j], or nil + current position if 's' is not
94 ** well formed in that interval
96 static int utflen(lua_State
*L
) {
97 lua_Integer n
= 0; /* counter for the number of characters */
98 size_t len
; /* string length in bytes */
99 const char *s
= luaL_checklstring(L
, 1, &len
);
100 lua_Integer posi
= u_posrelat(luaL_optinteger(L
, 2, 1), len
);
101 lua_Integer posj
= u_posrelat(luaL_optinteger(L
, 3, -1), len
);
102 int lax
= lua_toboolean(L
, 4);
103 luaL_argcheck(L
, 1 <= posi
&& --posi
<= (lua_Integer
)len
, 2,
104 "initial position out of bounds");
105 luaL_argcheck(L
, --posj
< (lua_Integer
)len
, 3,
106 "final position out of bounds");
107 while (posi
<= posj
) {
108 const char *s1
= utf8_decode(s
+ posi
, NULL
, !lax
);
109 if (s1
== NULL
) { /* conversion error? */
110 luaL_pushfail(L
); /* return fail ... */
111 lua_pushinteger(L
, posi
+ 1); /* ... and current position */
117 lua_pushinteger(L
, n
);
123 ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
124 ** characters that start in the range [i,j]
126 static int codepoint(lua_State
*L
) {
128 const char *s
= luaL_checklstring(L
, 1, &len
);
129 lua_Integer posi
= u_posrelat(luaL_optinteger(L
, 2, 1), len
);
130 lua_Integer pose
= u_posrelat(luaL_optinteger(L
, 3, posi
), len
);
131 int lax
= lua_toboolean(L
, 4);
134 luaL_argcheck(L
, posi
>= 1, 2, "out of bounds");
135 luaL_argcheck(L
, pose
<= (lua_Integer
)len
, 3, "out of bounds");
136 if (posi
> pose
) return 0; /* empty interval; return no values */
137 if (pose
- posi
>= INT_MAX
) /* (lua_Integer -> int) overflow? */
138 return luaL_error(L
, "string slice too long");
139 n
= (int)(pose
- posi
) + 1; /* upper bound for number of returns */
140 luaL_checkstack(L
, n
, "string slice too long");
141 n
= 0; /* count the number of returns */
142 se
= s
+ pose
; /* string end */
143 for (s
+= posi
- 1; s
< se
;) {
145 s
= utf8_decode(s
, &code
, !lax
);
147 return luaL_error(L
, MSGInvalid
);
148 lua_pushinteger(L
, code
);
155 static void pushutfchar(lua_State
*L
, int arg
) {
156 lua_Unsigned code
= (lua_Unsigned
)luaL_checkinteger(L
, arg
);
157 luaL_argcheck(L
, code
<= MAXUTF
, arg
, "value out of range");
158 lua_pushfstring(L
, "%U", (long)code
);
163 ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
165 static int utfchar(lua_State
*L
) {
166 int n
= lua_gettop(L
); /* number of arguments */
167 if (n
== 1) /* optimize common case of single char */
172 luaL_buffinit(L
, &b
);
173 for (i
= 1; i
<= n
; i
++) {
184 ** offset(s, n, [i]) -> index where n-th character counting from
185 ** position 'i' starts; 0 means character at 'i'.
187 static int byteoffset(lua_State
*L
) {
189 const char *s
= luaL_checklstring(L
, 1, &len
);
190 lua_Integer n
= luaL_checkinteger(L
, 2);
191 lua_Integer posi
= (n
>= 0) ? 1 : len
+ 1;
192 posi
= u_posrelat(luaL_optinteger(L
, 3, posi
), len
);
193 luaL_argcheck(L
, 1 <= posi
&& --posi
<= (lua_Integer
)len
, 3,
194 "position out of bounds");
196 /* find beginning of current byte sequence */
197 while (posi
> 0 && iscontp(s
+ posi
)) posi
--;
199 if (iscontp(s
+ posi
))
200 return luaL_error(L
, "initial position is a continuation byte");
202 while (n
< 0 && posi
> 0) { /* move back */
203 do { /* find beginning of previous character */
205 } while (posi
> 0 && iscontp(s
+ posi
));
209 n
--; /* do not move for 1st character */
210 while (n
> 0 && posi
< (lua_Integer
)len
) {
211 do { /* find beginning of next character */
213 } while (iscontp(s
+ posi
)); /* (cannot pass final '\0') */
218 if (n
== 0) /* did it find given character? */
219 lua_pushinteger(L
, posi
+ 1);
220 else /* no such character */
226 static int iter_aux(lua_State
*L
, int strict
) {
228 const char *s
= luaL_checklstring(L
, 1, &len
);
229 lua_Unsigned n
= (lua_Unsigned
)lua_tointeger(L
, 2);
231 while (iscontp(s
+ n
)) n
++; /* go to next character */
233 if (n
>= len
) /* (also handles original 'n' being negative) */
234 return 0; /* no more codepoints */
237 const char *next
= utf8_decode(s
+ n
, &code
, strict
);
238 if (next
== NULL
|| iscontp(next
))
239 return luaL_error(L
, MSGInvalid
);
240 lua_pushinteger(L
, n
+ 1);
241 lua_pushinteger(L
, code
);
247 static int iter_auxstrict(lua_State
*L
) {
248 return iter_aux(L
, 1);
251 static int iter_auxlax(lua_State
*L
) {
252 return iter_aux(L
, 0);
256 static int iter_codes(lua_State
*L
) {
257 int lax
= lua_toboolean(L
, 2);
258 const char *s
= luaL_checkstring(L
, 1);
259 luaL_argcheck(L
, !iscontp(s
), 1, MSGInvalid
);
260 lua_pushcfunction(L
, lax
? iter_auxlax
: iter_auxstrict
);
262 lua_pushinteger(L
, 0);
267 /* pattern to match a single UTF-8 character */
268 #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
271 static const luaL_Reg funcs
[] = {
272 {"offset", byteoffset
},
273 {"codepoint", codepoint
},
276 {"codes", iter_codes
},
278 {"charpattern", NULL
},
283 LUAMOD_API
int luaopen_utf8(lua_State
*L
) {
284 luaL_newlib(L
, funcs
);
285 lua_pushlstring(L
, UTF8PATT
, sizeof(UTF8PATT
) / sizeof(char) - 1);
286 lua_setfield(L
, -2, "charpattern");