diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-05-27 11:29:39 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-05-27 11:29:39 -0300 |
commit | 814213b65fa4ab2b1a7216d06f68a6f3df89efcd (patch) | |
tree | 899a187277f8645f9bc0b48ae55be4c31a61ae39 /lutf8lib.c | |
parent | cbdf4969ec425f1df1ade358425c0bf0bf811d83 (diff) | |
download | lua-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.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -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 | */ |
187 | static int byteoffset (lua_State *L) { | 187 | static 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 | ||