aboutsummaryrefslogtreecommitdiff
path: root/lutf8lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lutf8lib.c')
-rw-r--r--lutf8lib.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/lutf8lib.c b/lutf8lib.c
index 3a5b9bc3..7b747937 100644
--- a/lutf8lib.c
+++ b/lutf8lib.c
@@ -181,8 +181,8 @@ static int utfchar (lua_State *L) {
181 181
182 182
183/* 183/*
184** offset(s, n, [i]) -> index where n-th character counting from 184** offset(s, n, [i]) -> indices where n-th character counting from
185** position 'i' starts; 0 means character at 'i'. 185** position 'i' starts and ends; 0 means character at 'i'.
186*/ 186*/
187static int byteoffset (lua_State *L) { 187static int byteoffset (lua_State *L) {
188 size_t len; 188 size_t len;
@@ -217,11 +217,19 @@ static int byteoffset (lua_State *L) {
217 } 217 }
218 } 218 }
219 } 219 }
220 if (n == 0) /* did it find given character? */ 220 if (n != 0) { /* did not find given character? */
221 lua_pushinteger(L, posi + 1);
222 else /* no such character */
223 luaL_pushfail(L); 221 luaL_pushfail(L);
224 return 1; 222 return 1;
223 }
224 lua_pushinteger(L, posi + 1); /* initial position */
225 if ((s[posi] & 0x80) != 0) { /* multi-byte character? */
226 do {
227 posi++;
228 } while (iscontp(s + posi + 1)); /* skip to final byte */
229 }
230 /* else one-byte character: final position is the initial one */
231 lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */
232 return 2;
225} 233}
226 234
227 235