** offset(s, n, [i]) -> index where n-th character counting from ** position 'i' starts; 0 means character at 'i'. */
| 181 | ** position 'i' starts; 0 means character at 'i'. |
| 182 | */ |
| 183 | static int byteoffset (lua_State *L) { |
| 184 | size_t len; |
| 185 | const char *s = luaL_checklstring(L, 1, &len); |
| 186 | lua_Integer n = luaL_checkinteger(L, 2); |
| 187 | lua_Integer posi = (n >= 0) ? 1 : len + 1; |
| 188 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); |
| 189 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, |
| 190 | "position out of range"); |
| 191 | if (n == 0) { |
| 192 | /* find beginning of current byte sequence */ |
| 193 | while (posi > 0 && iscont(s + posi)) posi--; |
| 194 | } |
| 195 | else { |
| 196 | if (iscont(s + posi)) |
| 197 | return luaL_error(L, "initial position is a continuation byte"); |
| 198 | if (n < 0) { |
| 199 | while (n < 0 && posi > 0) { /* move back */ |
| 200 | do { /* find beginning of previous character */ |
| 201 | posi--; |
| 202 | } while (posi > 0 && iscont(s + posi)); |
| 203 | n++; |
| 204 | } |
| 205 | } |
| 206 | else { |
| 207 | n--; /* do not move for 1st character */ |
| 208 | while (n > 0 && posi < (lua_Integer)len) { |
| 209 | do { /* find beginning of next character */ |
| 210 | posi++; |
| 211 | } while (iscont(s + posi)); /* (cannot pass final '\0') */ |
| 212 | n--; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | if (n == 0) /* did it find given character? */ |
| 217 | lua_pushinteger(L, posi + 1); |
| 218 | else /* no such character */ |
| 219 | luaL_pushfail(L); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static int iter_aux (lua_State *L, int strict) { |
nothing calls this directly
no test coverage detected