** offset(s, n, [i]) -> index where n-th character counting from ** position 'i' starts; 0 means character at 'i'. */
| 158 | ** position 'i' starts; 0 means character at 'i'. |
| 159 | */ |
| 160 | static int byteoffset (lua_State *L) { |
| 161 | size_t len; |
| 162 | const char *s = luaL_checklstring(L, 1, &len); |
| 163 | lua_Integer n = luaL_checkinteger(L, 2); |
| 164 | lua_Integer posi = (n >= 0) ? 1 : len + 1; |
| 165 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); |
| 166 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, |
| 167 | "position out of range"); |
| 168 | if (n == 0) { |
| 169 | /* find beginning of current byte sequence */ |
| 170 | while (posi > 0 && iscont(s + posi)) posi--; |
| 171 | } |
| 172 | else { |
| 173 | if (iscont(s + posi)) |
| 174 | return luaL_error(L, "initial position is a continuation byte"); |
| 175 | if (n < 0) { |
| 176 | while (n < 0 && posi > 0) { /* move back */ |
| 177 | do { /* find beginning of previous character */ |
| 178 | posi--; |
| 179 | } while (posi > 0 && iscont(s + posi)); |
| 180 | n++; |
| 181 | } |
| 182 | } |
| 183 | else { |
| 184 | n--; /* do not move for 1st character */ |
| 185 | while (n > 0 && posi < (lua_Integer)len) { |
| 186 | do { /* find beginning of next character */ |
| 187 | posi++; |
| 188 | } while (iscont(s + posi)); /* (cannot pass final '\0') */ |
| 189 | n--; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | if (n == 0) /* did it find given character? */ |
| 194 | lua_pushinteger(L, posi + 1); |
| 195 | else /* no such character */ |
| 196 | lua_pushnil(L); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static int iter_aux (lua_State *L) { |
nothing calls this directly
no test coverage detected