patch 9.1.0296: regexp: engines do not handle case-folding well Commit: https://github.com/vim/vim/commit/7a27c108e0509f3255ebdcb6558e896c223e4d23 Author: Christian Brabandt <c...@256bit.org> Date: Tue Apr 9 22:53:19 2024 +0200
patch 9.1.0296: regexp: engines do not handle case-folding well Problem: Regex engines do not handle case-folding well Solution: Correctly calculate byte length of characters to skip When the regexp engine compares two utf-8 codepoints case insensitively it may match an adjacent character, because it assumes it can step over as many bytes as the pattern contains. This however is not necessarily true because of case-folding, a multi-byte UTF-8 character can be considered equal to some single-byte value. Let's consider the pattern 'ſ' and the string 's'. When comparing and ignoring case, the single character 's' matches, and since it matches Vim will try to step over the match (by the amount of bytes of the pattern), assuming that since it matches, the length of both strings is the same. However in that case, it should only step over the single byte value 's' so by 1 byte and try to start matching after it again. So for the backtracking engine we need to ensure: - we try to match the correct length for the pattern and the text - in case of a match, we step over it correctly The same thing can happen for the NFA engine, when skipping to the next character to test for a match. We are skipping over the regstart pointer, however we do not consider the case that because of case-folding we may need to adjust the number of bytes to skip over. So this needs to be adjusted in find_match_text() as well. A related issue turned out, when prog->match_text is actually empty. In that case we should try to find the next match and skip this condition. fixes: #14294 closes: #14433 Signed-off-by: Christian Brabandt <c...@256bit.org> diff --git a/src/mbyte.c b/src/mbyte.c index d6fb7ecc7..3be75099f 100644 --- a/src/mbyte.c +++ b/src/mbyte.c @@ -3800,6 +3800,15 @@ utf_strnicmp( * Returns zero if s1 and s2 are equal (ignoring case), the difference between * two characters otherwise. */ + int +mb_strnicmp2(char_u *s1, char_u *s2, int n1, int n2) +{ + if (n1 == n2 || !enc_utf8) + return mb_strnicmp(s1, s2, n1); + else + return utf_strnicmp(s1, s2, n1, n2); +} + int mb_strnicmp(char_u *s1, char_u *s2, size_t nn) { diff --git a/src/proto/mbyte.pro b/src/proto/mbyte.pro index 7883b3b4c..c49f7e707 100644 --- a/src/proto/mbyte.pro +++ b/src/proto/mbyte.pro @@ -48,6 +48,7 @@ int utf_islower(int a); int utf_tolower(int a); int utf_isupper(int a); int mb_strnicmp(char_u *s1, char_u *s2, size_t nn); +int mb_strnicmp2(char_u *s1, char_u *s2, int n1, int n2); void show_utf8(void); int latin_head_off(char_u *base, char_u *p); int dbcs_screen_head_off(char_u *base, char_u *p); diff --git a/src/regexp.c b/src/regexp.c index 4373ae0cf..4e85ebc29 100644 --- a/src/regexp.c +++ b/src/regexp.c @@ -1606,7 +1606,9 @@ mb_decompose(int c, int *c1, int *c2, int *c3) /* * Compare two strings, ignore case if rex.reg_ic set. * Return 0 if strings match, non-zero otherwise. - * Correct the length "*n" when composing characters are ignored. + * Correct the length "*n" when composing characters are ignored + * or for utf8 when both utf codepoints are considered equal because of + * case-folding but have different length (e.g. 's' and 'ſ') */ static int cstrncmp(char_u *s1, char_u *s2, int *n) @@ -1615,6 +1617,13 @@ cstrncmp(char_u *s1, char_u *s2, int *n) if (!rex.reg_ic) result = STRNCMP(s1, s2, *n); + else if (enc_utf8) + { + int l2 = mb_ptr2len(s2); + result = MB_STRNICMP2(s1, s2, *n, l2); + if (result == 0 && l2 < *n) + *n = l2; + } else result = MB_STRNICMP(s1, s2, *n); diff --git a/src/regexp_bt.c b/src/regexp_bt.c index 5d9450d87..2a03fec57 100644 --- a/src/regexp_bt.c +++ b/src/regexp_bt.c @@ -3816,6 +3816,14 @@ regmatch( } } } + else if (enc_utf8) + { + if (cstrncmp(opnd, rex.input, &len) != 0) + { + status = RA_NOMATCH; + break; + } + } else for (i = 0; i < len; ++i) if (opnd[i] != rex.input[i]) diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c index 5e4fadd02..451720a09 100644 --- a/src/regexp_nfa.c +++ b/src/regexp_nfa.c @@ -5666,7 +5666,12 @@ find_match_text(colnr_T *startcol, int regstart, char_u *match_text) for (;;) { match = TRUE; - len2 = MB_CHAR2LEN(regstart); // skip regstart + // skip regstart + len2 = MB_CHAR2LEN(regstart); + if (enc_utf8 && len2 > 1 && MB_CHAR2LEN(PTR2CHAR(rex.line + col)) != len2) + // because of case-folding of the previously matched text, we may need + // to skip fewer bytes than mb_char2len(regstart) + len2 = mb_char2len(utf_fold(regstart)); for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) { c1 = PTR2CHAR(match_text + len1); @@ -7503,7 +7508,7 @@ nfa_regexec_both( // If match_text is set it contains the full text that must match. // Nothing else to try. Doesn't handle combining chars well. - if (prog->match_text != NULL && !rex.reg_icombine) + if (prog->match_text != NULL && *prog->match_text != NUL && !rex.reg_icombine) { retval = find_match_text(&col, prog->regstart, prog->match_text); if (REG_MULTI) diff --git a/src/testdir/test_regexp_utf8.vim b/src/testdir/test_regexp_utf8.vim index 6669dee57..9980e5b7f 100644 --- a/src/testdir/test_regexp_utf8.vim +++ b/src/testdir/test_regexp_utf8.vim @@ -587,4 +587,32 @@ func Test_combining_chars_in_collection() bw! endfunc +func Test_search_multibyte_match_ascii() + new + " Match single 'ſ' and 's' + call setline(1, 'das abc heraus abc ſich abc ſind') + for i in range(0, 2) + exe "set re="..i + let ic_match = matchbufline('%', ' -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/E1ruIZc-000t3C-2p%40256bit.org.