MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / utflen

Function utflen

extlibs/lua/src/lutf8lib.c:92–115  ·  view source on GitHub ↗

** utf8len(s [, i [, j [, lax]]]) --> 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

90** well formed in that interval
91*/
92static int utflen (lua_State *L) {
93 lua_Integer n = 0; /* counter for the number of characters */
94 size_t len; /* string length in bytes */
95 const char *s = luaL_checklstring(L, 1, &len);
96 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
97 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
98 int lax = lua_toboolean(L, 4);
99 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
100 "initial position out of string");
101 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
102 "final position out of string");
103 while (posi <= posj) {
104 const char *s1 = utf8_decode(s + posi, NULL, !lax);
105 if (s1 == NULL) { /* conversion error? */
106 luaL_pushfail(L); /* return fail ... */
107 lua_pushinteger(L, posi + 1); /* ... and current position */
108 return 2;
109 }
110 posi = s1 - s;
111 n++;
112 }
113 lua_pushinteger(L, n);
114 return 1;
115}
116
117
118/*

Callers

nothing calls this directly

Calls 6

luaL_checklstringFunction · 0.85
u_posrelatFunction · 0.85
luaL_optintegerFunction · 0.85
lua_tobooleanFunction · 0.85
lua_pushintegerFunction · 0.85
utf8_decodeFunction · 0.70

Tested by

no test coverage detected