aboutsummaryrefslogtreecommitdiff
path: root/lutf8lib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-05-27 11:29:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-05-27 11:29:39 -0300
commit814213b65fa4ab2b1a7216d06f68a6f3df89efcd (patch)
tree899a187277f8645f9bc0b48ae55be4c31a61ae39 /lutf8lib.c
parentcbdf4969ec425f1df1ade358425c0bf0bf811d83 (diff)
downloadlua-814213b65fa4ab2b1a7216d06f68a6f3df89efcd.tar.gz
lua-814213b65fa4ab2b1a7216d06f68a6f3df89efcd.tar.bz2
lua-814213b65fa4ab2b1a7216d06f68a6f3df89efcd.zip
utf8.offset returns also final position of character
'utf8.offset' returns two values: the initial and the final position of the given character.
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