MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / byteoffset

Function byteoffset

lib/lua/src/lutf8lib.c:187–225  ·  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

185** position 'i' starts; 0 means character at 'i'.
186*/
187static int byteoffset (lua_State *L) {
188 size_t len;
189 const char *s = luaL_checklstring(L, 1, &len);
190 lua_Integer n = luaL_checkinteger(L, 2);
191 lua_Integer posi = (n >= 0) ? 1 : len + 1;
192 posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
193 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
194 "position out of bounds");
195 if (n == 0) {
196 /* find beginning of current byte sequence */
197 while (posi > 0 && iscontp(s + posi)) posi--;
198 }
199 else {
200 if (iscontp(s + posi))
201 return luaL_error(L, "initial position is a continuation byte");
202 if (n < 0) {
203 while (n < 0 && posi > 0) { /* move back */
204 do { /* find beginning of previous character */
205 posi--;
206 } while (posi > 0 && iscontp(s + posi));
207 n++;
208 }
209 }
210 else {
211 n--; /* do not move for 1st character */
212 while (n > 0 && posi < (lua_Integer)len) {
213 do { /* find beginning of next character */
214 posi++;
215 } while (iscontp(s + posi)); /* (cannot pass final '\0') */
216 n--;
217 }
218 }
219 }
220 if (n == 0) /* did it find given character? */
221 lua_pushinteger(L, posi + 1);
222 else /* no such character */
223 luaL_pushfail(L);
224 return 1;
225}
226
227
228static 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