Skip to content

Commit

Permalink
refactor(multibyte): neo-casefolding without allocation
Browse files Browse the repository at this point in the history
bfredl authored and Naofal-Helal committed Sep 29, 2024
1 parent 3420f8e commit d0f7dd7
Showing 2 changed files with 9 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/nvim/mbyte.c
Original file line number Diff line number Diff line change
@@ -1379,22 +1379,11 @@ int utf_fold(int a)
return a;
}

utf8proc_uint8_t input_str[16] = { 0 };
if (utf8proc_encode_char(a, input_str) <= 0) {
return a;
}

utf8proc_uint8_t *fold_str_utf;
if (utf8proc_map((utf8proc_uint8_t *)input_str, 0, &fold_str_utf,
UTF8PROC_NULLTERM | UTF8PROC_CASEFOLD) < 0) {
return a;
}

int fold_codepoint_utf = utf_ptr2char((char *)fold_str_utf);
utf8proc_int32_t result[1];

xfree(fold_str_utf);
utf8proc_ssize_t res = utf8proc_decompose_char(a, result, 1, UTF8PROC_CASEFOLD, NULL);

return fold_codepoint_utf;
return (res == 1) ? result[0] : a;
}

// Vim's own character class functions. These exist because many library
6 changes: 6 additions & 0 deletions test/unit/mbyte_spec.lua
Original file line number Diff line number Diff line change
@@ -351,6 +351,12 @@ describe('mbyte', function()
describe('utf_fold', function()
itp('does not crash with surrogates #30527', function()
eq(0xDDFB, lib.utf_fold(0xDDFB))
eq(0xd800, lib.utf_fold(0xd800)) -- high surrogate, invalid as a character
end)

itp("doesn't crash on invalid codepoints", function()
eq(9000000, lib.utf_fold(9000000))
eq(0, lib.utf_fold(0))
end)
end)
end)

0 comments on commit d0f7dd7

Please sign in to comment.