MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / byteoffset

Function byteoffset

3rd/lua-5.4.3/src/lutf8lib.c:183–221  ·  view source on GitHub ↗

** offset(s, n, [i]) -> index where n-th character counting from ** position 'i' starts; 0 means character at 'i'. */

Source from the content-addressed store, hash-verified

181** position 'i' starts; 0 means character at 'i'.
182*/
183static 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 bounds");
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
224static int iter_aux (lua_State *L, int strict) {

Callers

nothing calls this directly

Calls 6

luaL_checklstringFunction · 0.85
luaL_checkintegerFunction · 0.85
u_posrelatFunction · 0.85
luaL_optintegerFunction · 0.85
luaL_errorFunction · 0.85
lua_pushintegerFunction · 0.85

Tested by

no test coverage detected