refactor(lua): remove unnecessary strlen() in nlua_expand_pat() (#29388)

Also change the initial value of `status` to `FAIL`, as that'll avoid
unnecessary assignments.
This commit is contained in:
zeertzjq
2024-06-18 12:02:31 +08:00
committed by GitHub
parent 1a1c766049
commit b0c336eaf8
2 changed files with 8 additions and 20 deletions

View File

@@ -1931,7 +1931,7 @@ static garray_T expand_result_array = GA_EMPTY_INIT_VALUE;
void nlua_expand_pat(expand_T *xp, const char *pat) void nlua_expand_pat(expand_T *xp, const char *pat)
{ {
lua_State *const lstate = global_lstate; lua_State *const lstate = global_lstate;
int status = OK; int status = FAIL;
// [ vim ] // [ vim ]
lua_getglobal(lstate, "vim"); lua_getglobal(lstate, "vim");
@@ -1940,12 +1940,11 @@ void nlua_expand_pat(expand_T *xp, const char *pat)
lua_getfield(lstate, -1, "_expand_pat"); lua_getfield(lstate, -1, "_expand_pat");
luaL_checktype(lstate, -1, LUA_TFUNCTION); luaL_checktype(lstate, -1, LUA_TFUNCTION);
// [ vim, vim._expand_pat, buf ] // [ vim, vim._expand_pat, pat ]
lua_pushlstring(lstate, pat, strlen(pat)); lua_pushstring(lstate, pat);
if (nlua_pcall(lstate, 1, 2) != 0) { if (nlua_pcall(lstate, 1, 2) != 0) {
nlua_error(lstate, nlua_error(lstate, _("Error executing vim._expand_pat: %.*s"));
_("Error executing vim._expand_pat: %.*s"));
return; return;
} }
@@ -1954,30 +1953,27 @@ void nlua_expand_pat(expand_T *xp, const char *pat)
Arena arena = ARENA_EMPTY; Arena arena = ARENA_EMPTY;
int prefix_len = (int)nlua_pop_Integer(lstate, &arena, &err); int prefix_len = (int)nlua_pop_Integer(lstate, &arena, &err);
if (ERROR_SET(&err)) { if (ERROR_SET(&err)) {
status = FAIL;
goto cleanup; goto cleanup;
} }
Array completions = nlua_pop_Array(lstate, &arena, &err); Array completions = nlua_pop_Array(lstate, &arena, &err);
if (ERROR_SET(&err)) { if (ERROR_SET(&err)) {
status = FAIL;
goto cleanup_array; goto cleanup_array;
} }
ga_clear(&expand_result_array); ga_clear(&expand_result_array);
ga_init(&expand_result_array, (int)sizeof(char *), 80); ga_init(&expand_result_array, (int)sizeof(char *), 80);
for (size_t i = 0; i < completions.size; i++) { for (size_t i = 0; i < completions.size; i++) {
Object v = completions.items[i]; Object v = completions.items[i];
if (v.type != kObjectTypeString) { if (v.type != kObjectTypeString) {
status = FAIL;
goto cleanup_array; goto cleanup_array;
} }
GA_APPEND(char *, &expand_result_array, string_to_cstr(v.data.string)); GA_APPEND(char *, &expand_result_array, string_to_cstr(v.data.string));
} }
xp->xp_pattern += prefix_len; xp->xp_pattern += prefix_len;
status = OK;
cleanup_array: cleanup_array:
arena_mem_free(arena_finish(&arena)); arena_mem_free(arena_finish(&arena));

View File

@@ -816,22 +816,14 @@ describe('completion', function()
feed(':lua math.a<Tab>') feed(':lua math.a<Tab>')
screen:expect([[ screen:expect([[
| |
{1:~ }| {1:~ }|*5
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{100:abs}{3: acos asin atan atan2 }| {100:abs}{3: acos asin atan atan2 }|
:lua math.abs^ | :lua math.abs^ |
]]) ]])
feed('<Tab>') feed('<Tab>')
screen:expect([[ screen:expect([[
| |
{1:~ }| {1:~ }|*5
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{3:abs }{100:acos}{3: asin atan atan2 }| {3:abs }{100:acos}{3: asin atan atan2 }|
:lua math.acos^ | :lua math.acos^ |
]]) ]])