diff options
author | Michael Lingelbach <m.j.lbach@gmail.com> | 2021-10-30 10:30:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-30 10:30:40 -0700 |
commit | 2230b578d1d36728d156f82fb0e1a44c1f810b8c (patch) | |
tree | a4f94d41d9b89fc54b5b58ffdcbd1826757ed860 /src/nvim/lua/stdlib.c | |
parent | 97ae0ab4d8e4cdc5be2dab43e328f0a9d248b30a (diff) | |
download | rneovim-2230b578d1d36728d156f82fb0e1a44c1f810b8c.tar.gz rneovim-2230b578d1d36728d156f82fb0e1a44c1f810b8c.tar.bz2 rneovim-2230b578d1d36728d156f82fb0e1a44c1f810b8c.zip |
feat: add vim.str_utf_{start,end} (#16129)
vim.str_utf_{start,end} return the offset from the current position to
the start and end of the current utf-character (nearest codepoint)
respectively.
Diffstat (limited to 'src/nvim/lua/stdlib.c')
-rw-r--r-- | src/nvim/lua/stdlib.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 2d969357b4..788819ab03 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -216,6 +216,45 @@ static int nlua_str_utf_pos(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL return 1; } +/// Return the offset from the 1-indexed byte position to the first byte of the +/// current character. +/// +/// Expects a string and an int. +/// +/// Returns the byte offset to the first byte of the current character +/// pointed into by the offset. +static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL +{ + size_t s1_len; + const char *s1 = luaL_checklstring(lstate, 1, &s1_len); + long offset = luaL_checkinteger(lstate, 2); + if (offset < 0 || offset > (intptr_t)s1_len) { + return luaL_error(lstate, "index out of range"); + } + int tail_offset = mb_head_off((char_u *)s1, (char_u *)s1 + (char_u)offset - 1); + lua_pushinteger(lstate, tail_offset); + return 1; +} + +/// Return the offset from the 1-indexed byte position to the last +/// byte of the current character. +/// +/// Expects a string and an int. +/// +/// Returns the byte offset to the last byte of the current character +/// pointed into by the offset. +static int nlua_str_utf_end(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL +{ + size_t s1_len; + const char *s1 = luaL_checklstring(lstate, 1, &s1_len); + long offset = luaL_checkinteger(lstate, 2); + if (offset < 0 || offset > (intptr_t)s1_len) { + return luaL_error(lstate, "index out of range"); + } + int tail_offset = mb_tail_off((char_u *)s1, (char_u *)s1 + (char_u)offset - 1); + lua_pushinteger(lstate, tail_offset); + return 1; +} /// convert UTF-32 or UTF-16 indices to byte index. /// @@ -439,6 +478,12 @@ void nlua_state_add_stdlib(lua_State *const lstate) // str_utf_pos lua_pushcfunction(lstate, &nlua_str_utf_pos); lua_setfield(lstate, -2, "str_utf_pos"); + // str_utf_start + lua_pushcfunction(lstate, &nlua_str_utf_start); + lua_setfield(lstate, -2, "str_utf_start"); + // str_utf_end + lua_pushcfunction(lstate, &nlua_str_utf_end); + lua_setfield(lstate, -2, "str_utf_end"); // regex lua_pushcfunction(lstate, &nlua_regex); lua_setfield(lstate, -2, "regex"); |