mirror of https://github.com/rusefi/lua.git
new implementation for 'utf8.len'
This commit is contained in:
parent
7d514af0bc
commit
3a044de5a1
31
lutf8lib.c
31
lutf8lib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lutf8lib.c,v 1.4 2014/03/20 19:36:02 roberto Exp roberto $
|
** $Id: lutf8lib.c,v 1.5 2014/04/01 14:39:55 roberto Exp roberto $
|
||||||
** Standard library for UTF-8 manipulation
|
** Standard library for UTF-8 manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -61,25 +61,30 @@ static const char *utf8_decode (const char *o, int *val) {
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** utf8len(s, [i]) --> number of codepoints in 's' after 'i';
|
** utf8len(s [, i [, j]]) --> number of codepoints in 's' between 'i';
|
||||||
** nil if 's' not well formed
|
** nil + current position if 's' not well formed
|
||||||
*/
|
*/
|
||||||
static int utflen (lua_State *L) {
|
static int utflen (lua_State *L) {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
const char *ends;
|
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), 1);
|
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
||||||
luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1,
|
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
||||||
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
||||||
"initial position out of string");
|
"initial position out of string");
|
||||||
ends = s + len;
|
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
||||||
s += posi - 1;
|
"final position out of string");
|
||||||
while (s < ends && (s = utf8_decode(s, NULL)) != NULL)
|
while (posi <= posj) {
|
||||||
|
const char *s1 = utf8_decode(s + posi, NULL);
|
||||||
|
if (s1 == NULL) { /* conversion error? */
|
||||||
|
lua_pushnil(L); /* return nil ... */
|
||||||
|
lua_pushinteger(L, posi + 1); /* ... and current position */
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
posi = s1 - s;
|
||||||
n++;
|
n++;
|
||||||
if (s == ends)
|
}
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
else
|
|
||||||
lua_pushnil(L);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue