** 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 */
| 90 | ** well formed in that interval |
| 91 | */ |
| 92 | static 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 | /* |
nothing calls this directly
no test coverage detected