MCPcopy Create free account
hub / github.com/BigPig0/RelayLive / byteoffset

Function byteoffset

ThirdParty/lua/lua/lutf8lib.c:159–197  ·  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

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

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected