MCPcopy Create free account
hub / github.com/DFHack/dfhack / utflen

Function utflen

depends/lua/src/lutf8lib.c:71–93  ·  view source on GitHub ↗

** utf8len(s [, i [, j]]) --> number of characters that start in the ** range [i,j], or nil + current position if 's' is not well formed in ** that interval */

Source from the content-addressed store, hash-verified

69** that interval
70*/
71static int utflen (lua_State *L) {
72 int n = 0;
73 size_t len;
74 const char *s = luaL_checklstring(L, 1, &len);
75 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
76 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
77 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
78 "initial position out of string");
79 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
80 "final position out of string");
81 while (posi <= posj) {
82 const char *s1 = utf8_decode(s + posi, NULL);
83 if (s1 == NULL) { /* conversion error? */
84 lua_pushnil(L); /* return nil ... */
85 lua_pushinteger(L, posi + 1); /* ... and current position */
86 return 2;
87 }
88 posi = s1 - s;
89 n++;
90 }
91 lua_pushinteger(L, n);
92 return 1;
93}
94
95
96/*

Callers

nothing calls this directly

Calls 6

luaL_checklstringFunction · 0.85
u_posrelatFunction · 0.85
luaL_optintegerFunction · 0.85
utf8_decodeFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushintegerFunction · 0.85

Tested by

no test coverage detected