robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
27.8 KiB760 linesraw
1" Tests for regexp in utf8 encoding
2
3func Test_regexp_literal_byte_search()
4  let save_encoding = &encoding
5  let save_re = &regexpengine
6  try
7    "for enc in ['latin1', 'utf-8']
8    for enc in ['utf-8']
9      let &encoding = enc
10      for engine in [1, 2]
11        let &regexpengine = engine
12        let chars = ['z', nr2char(0x7f), nr2char(0xff)]
13        if enc == 'utf-8'
14          call add(chars, nr2char(0x100))
15        endif
16        for c in chars
17          let prefix = repeat('x', 4096)
18          call assert_equal(-1, match('', c))
19          call assert_equal(-1, match(prefix, c))
20          call assert_equal(0, match(c .. prefix, c))
21          call assert_equal(4096, match(prefix .. c, c))
22        endfor
23        call assert_equal(-1, match('xxx', '\%d0'))
24      endfor
25    endfor
26  finally
27    let &encoding = save_encoding
28    let &regexpengine = save_re
29  endtry
30endfunc
31
32" ASCII range collections must honor Unicode case folding.
33func Test_ascii_collection_unicode_casefold()
34  let save_encoding = &encoding
35  let save_ic = &ignorecase
36  try
37    set encoding=utf-8 ignorecase
38    for engine in [1, 2]
39      for c in [nr2char(0x17f), nr2char(0x212a)]
40        for [positive, negative] in [['[a-z]', '[^a-z]'],
41              \ ['[A-Z]', '[^A-Z]'],
42              \ ['[a-zA-Z]', '[^a-zA-Z]'],
43              \ ['[a-zA-Z_]', '[^a-zA-Z_]'],
44              \ ['[a-zA-Z0-9_]', '[^a-zA-Z0-9_]']]
45          call assert_equal(0, match(c, '\%#=' .. engine .. positive))
46          call assert_equal(-1, match(c, '\%#=' .. engine .. negative))
47        endfor
48        " No character folds into [0-9a-fA-F], so the hex class is kept.
49        call assert_equal(-1, match(c, '\%#=' .. engine .. '[0-9a-fA-F]'))
50      endfor
51    endfor
52  finally
53    let &encoding = save_encoding
54    let &ignorecase = save_ic
55  endtry
56endfunc
57
58func s:equivalence_test()
59  let str = "AÀÁÂÃÄÅĀĂĄǍǞǠǺȂȦȺḀẠẢẤẦẨẪẬẮẰẲẴẶ BƁɃḂḄḆ CÇĆĈĊČƇȻḈꞒ DĎĐƊḊḌḎḐḒ EÈÉÊËĒĔĖĘĚȄȆȨɆḔḖḘḚḜẸẺẼẾỀỂỄỆ FƑḞꞘ GĜĞĠĢƓǤǦǴḠꞠ HĤĦȞḢḤḦḨḪⱧ IÌÍÎÏĨĪĬĮİƗǏȈȊḬḮỈỊ JĴɈ KĶƘǨḰḲḴⱩꝀ LĹĻĽĿŁȽḶḸḺḼⱠ MḾṀṂ NÑŃŅŇǸṄṆṈṊꞤ OÒÓÔÕÖØŌŎŐƟƠǑǪǬǾȌȎȪȬȮȰṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢ PƤṔṖⱣ QɊ RŔŖŘȐȒɌṘṚṜṞⱤꞦ SŚŜŞŠȘṠṢṤṦṨⱾꞨ TŢŤŦƬƮȚȾṪṬṮṰ UÙÚÛÜŨŪŬŮŰƯǕǙǛǓǗȔȖɄṲṴṶṸṺỤỦỨỪỬỮỰ  VƲṼṾ WŴẀẂẄẆẈ XẊẌ YÝŶŸƳȲɎẎỲỴỶỸ ZŹŻŽƵẐẒẔⱫ aàáâãäåāăąǎǟǡǻȃȧᶏḁẚạảấầẩẫậắằẳẵặⱥ bƀɓᵬᶀḃḅḇ cçćĉċčƈȼḉꞓꞔ dďđɗᵭᶁᶑḋḍḏḑḓ eèéêëēĕėęěȅȇȩɇᶒḕḗḙḛḝẹẻẽếềểễệ fƒᵮᶂḟꞙ gĝğġģǥǧǵɠᶃḡꞡ hĥħȟḣḥḧḩḫẖⱨꞕ iìíîïĩīĭįǐȉȋɨᶖḭḯỉị jĵǰɉ kķƙǩᶄḱḳḵⱪꝁ lĺļľŀłƚḷḹḻḽⱡ mᵯḿṁṃ nñńņňʼnǹᵰᶇṅṇṉṋꞥ oòóôõöøōŏőơǒǫǭǿȍȏȫȭȯȱɵṍṏṑṓọỏốồổỗộớờởỡợ pƥᵱᵽᶈṕṗ qɋʠ rŕŗřȑȓɍɽᵲᵳᶉṛṝṟꞧ sśŝşšșȿᵴᶊṡṣṥṧṩꞩ tţťŧƫƭțʈᵵṫṭṯṱẗⱦ uùúûüũūŭůűųǚǖưǔǘǜȕȗʉᵾᶙṳṵṷṹṻụủứừửữự vʋᶌṽṿ wŵẁẃẅẇẉẘ xẋẍ yýÿŷƴȳɏẏẙỳỵỷỹ zźżžƶᵶᶎẑẓẕⱬ"
60  let groups = split(str)
61  for group1 in groups
62      for c in split(group1, '\zs')
63	" next statement confirms that equivalence class matches every
64	" character in group
65        call assert_match('^[[=' .. c .. '=]]*$', group1)
66        for group2 in groups
67          if group2 != group1
68	    " next statement converts that equivalence class doesn't match
69	    " character in any other group
70            call assert_equal(-1, match(group2, '[[=' .. c .. '=]]'), c)
71          endif
72        endfor
73      endfor
74  endfor
75endfunc
76
77func Test_equivalence_re1()
78  set re=1
79  call s:equivalence_test()
80  set re=0
81endfunc
82
83func Test_equivalence_re2()
84  set re=2
85  call s:equivalence_test()
86  set re=0
87endfunc
88
89func s:classes_test()
90  if has('win32')
91    set iskeyword=@,48-57,_,192-255
92  endif
93  " set isprint=@,161-255
94  call assert_equal('Motörhead', matchstr('Motörhead', '[[:print:]]\+'))
95
96  let alnumchars = ''
97  let alphachars = ''
98  let backspacechar = ''
99  let blankchars = ''
100  let cntrlchars = ''
101  let digitchars = ''
102  let escapechar = ''
103  let graphchars = ''
104  let lowerchars = ''
105  let printchars = ''
106  let punctchars = ''
107  let returnchar = ''
108  let spacechars = ''
109  let tabchar = ''
110  let upperchars = ''
111  let xdigitchars = ''
112  let identchars = ''
113  let identchars1 = ''
114  let kwordchars = ''
115  let kwordchars1 = ''
116  let fnamechars = ''
117  let fnamechars1 = ''
118  let i = 1
119  while i <= 255
120    let c = nr2char(i)
121    if c =~ '[[:alpha:]]'
122      let alphachars .= c
123    endif
124    if c =~ '[[:alnum:]]'
125      let alnumchars .= c
126    endif
127    if c =~ '[[:backspace:]]'
128      let backspacechar .= c
129    endif
130    if c =~ '[[:blank:]]'
131      let blankchars .= c
132    endif
133    if c =~ '[[:cntrl:]]'
134      let cntrlchars .= c
135    endif
136    if c =~ '[[:digit:]]'
137      let digitchars .= c
138    endif
139    if c =~ '[[:escape:]]'
140      let escapechar .= c
141    endif
142    if c =~ '[[:graph:]]'
143      let graphchars .= c
144    endif
145    if c =~ '[[:lower:]]'
146      let lowerchars .= c
147    endif
148    if c =~ '[[:print:]]'
149      let printchars .= c
150    endif
151    if c =~ '[[:punct:]]'
152      let punctchars .= c
153    endif
154    if c =~ '[[:return:]]'
155      let returnchar .= c
156    endif
157    if c =~ '[[:space:]]'
158      let spacechars .= c
159    endif
160    if c =~ '[[:tab:]]'
161      let tabchar .= c
162    endif
163    if c =~ '[[:upper:]]'
164      let upperchars .= c
165    endif
166    if c =~ '[[:xdigit:]]'
167      let xdigitchars .= c
168    endif
169    if c =~ '[[:ident:]]'
170      let identchars .= c
171    endif
172    if c =~ '\i'
173      let identchars1 .= c
174    endif
175    if c =~ '[[:keyword:]]'
176      let kwordchars .= c
177    endif
178    if c =~ '\k'
179      let kwordchars1 .= c
180    endif
181    if c =~ '[[:fname:]]'
182      let fnamechars .= c
183    endif
184    if c =~ '\f'
185      let fnamechars1 .= c
186    endif
187    let i += 1
188  endwhile
189
190  call assert_equal('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', alphachars)
191  call assert_equal('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', alnumchars)
192  call assert_equal("\b", backspacechar)
193  call assert_equal("\t ", blankchars)
194  call assert_equal("\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0b\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\e\x1c\x1d\x1e\x1f\x7f", cntrlchars)
195  call assert_equal("0123456789", digitchars)
196  call assert_equal("\<Esc>", escapechar)
197  call assert_equal('!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', graphchars)
198  call assert_equal('abcdefghijklmnopqrstuvwxyzµßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', lowerchars)
199  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ', printchars)
200  call assert_equal('!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~', punctchars)
201  call assert_equal('ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ', upperchars)
202  call assert_equal("\r", returnchar)
203  call assert_equal("\t\n\x0b\f\r ", spacechars)
204  call assert_equal("\t", tabchar)
205  call assert_equal('0123456789ABCDEFabcdef', xdigitchars)
206
207  if has('win32')
208    let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
209    let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
210  else
211    let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
212    let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
213  endif
214
215  if has('win32')
216    let fnamechars_ok = '!#$%+,-./0123456789:=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]_abcdefghijklmnopqrstuvwxyz{}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
217  elseif has('amiga')
218    let fnamechars_ok = '$+,-./0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
219  elseif has('vms')
220    let fnamechars_ok = '#$%+,-./0123456789:;<>ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
221  else
222    let fnamechars_ok = '#$%+,-./0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
223  endif
224
225  call assert_equal(identchars_ok, identchars)
226  call assert_equal(kwordchars_ok, kwordchars)
227  call assert_equal(fnamechars_ok, fnamechars)
228
229  call assert_equal(identchars1, identchars)
230  call assert_equal(kwordchars1, kwordchars)
231  call assert_equal(fnamechars1, fnamechars)
232endfunc
233
234func Test_classes_re1()
235  set re=1
236  call s:classes_test()
237  set re=0
238endfunc
239
240func Test_classes_re2()
241  set re=2
242  call s:classes_test()
243  set re=0
244endfunc
245
246func Test_reversed_range()
247  for re in range(0, 2)
248    exe 'set re=' . re
249    call assert_fails('call match("abc def", "[c-a]")', 'E944:', re)
250  endfor
251  set re=0
252endfunc
253
254func Test_large_class()
255  set re=1
256  call assert_fails('call match("abc def", "[\u3000-\u4000]")', 'E945:')
257  set re=2
258  call assert_equal(0, 'abc def' =~# '[\u3000-\u4000]')
259  call assert_equal(1, "\u3042" =~# '[\u3000-\u4000]')
260  set re=0
261endfunc
262
263func Test_optmatch_toolong()
264  set re=1
265  " Can only handle about 8000 characters.
266  let pat = '\\%[' .. repeat('x', 9000) .. ']'
267  call assert_fails('call match("abc def", "' .. pat .. '")', 'E339:')
268  set re=0
269endfunc
270
271" Test for regexp patterns with multi-byte support, using utf-8.
272func Test_multibyte_chars()
273  " tl is a List of Lists with:
274  "    2: test auto/old/new  0: test auto/old  1: test auto/new
275  "    regexp pattern
276  "    text to test the pattern on
277  "    expected match (optional)
278  "    expected submatch 1 (optional)
279  "    expected submatch 2 (optional)
280  "    etc.
281  "  When there is no match use only the first two items.
282  let tl = []
283
284  " Multi-byte character tests.
285  call add(tl, [2, '[[:alpha:][=a=]]\+', '879 aiaãâaiuvna ', 'aiaãâaiuvna'])
286  call add(tl, [2, '[[=a=]]\+', 'ddaãâbcd', 'aãâ'])								" equivalence classes
287  call add(tl, [2, '[^ม ]\+', 'มม oijasoifjos ifjoisj f osij j มมมมม abcd', 'oijasoifjos'])
288  call add(tl, [2, ' [^ ]\+', 'start มabcdม ', ' มabcdม'])
289  call add(tl, [2, '[ม[:alpha:][=a=]]\+', '879 aiaãมâมaiuvna ', 'aiaãมâมaiuvna'])
290
291  " this is not a normal "i" but 0xec
292  call add(tl, [2, '\p\+', 'ìa', 'ìa'])
293  call add(tl, [2, '\p*', 'aあ', 'aあ'])
294
295  " Test recognition of some character classes
296  call add(tl, [2, '\i\+', '&*¨xx ', 'xx'])
297  call add(tl, [2, '\f\+', '&*Ÿfname ', 'fname'])
298
299  " Test composing character matching
300  call add(tl, [2, '.ม', 'xม่x yมy', 'yม'])
301  call add(tl, [2, '.ม่', 'xม่x yมy', 'xม่'])
302  call add(tl, [2, "\u05b9", " x\u05b9 ", "x\u05b9"])
303  call add(tl, [2, ".\u05b9", " x\u05b9 ", "x\u05b9"])
304  call add(tl, [2, "\u05b9\u05bb", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
305  call add(tl, [2, ".\u05b9\u05bb", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
306  call add(tl, [2, "\u05bb\u05b9", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
307  call add(tl, [2, ".\u05bb\u05b9", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
308  call add(tl, [2, "\u05b9", " y\u05bb x\u05b9 ", "x\u05b9"])
309  call add(tl, [2, ".\u05b9", " y\u05bb x\u05b9 ", "x\u05b9"])
310  call add(tl, [2, "\u05b9", " y\u05bb\u05b9 x\u05b9 ", "y\u05bb\u05b9"])
311  call add(tl, [2, ".\u05b9", " y\u05bb\u05b9 x\u05b9 ", "y\u05bb\u05b9"])
312  call add(tl, [1, "\u05b9\u05bb", " y\u05b9 x\u05b9\u05bb ", "x\u05b9\u05bb"])
313  call add(tl, [2, ".\u05b9\u05bb", " y\u05bb x\u05b9\u05bb ", "x\u05b9\u05bb"])
314  call add(tl, [2, "a", "ca\u0300t"])
315  call add(tl, [2, "ca", "ca\u0300t"])
316  call add(tl, [2, "a\u0300", "ca\u0300t", "a\u0300"])
317  call add(tl, [2, 'a\%C', "ca\u0300t", "a\u0300"])
318  call add(tl, [2, 'ca\%C', "ca\u0300t", "ca\u0300"])
319  call add(tl, [2, 'ca\%Ct', "ca\u0300t", "ca\u0300t"])
320
321  " Test \Z
322  call add(tl, [2, 'ú\Z', 'x'])
323  call add(tl, [2, 'יהוה\Z', 'יהוה', 'יהוה'])
324  call add(tl, [2, 'יְהוָה\Z', 'יהוה', 'יהוה'])
325  call add(tl, [2, 'יהוה\Z', 'יְהוָה', 'יְהוָה'])
326  call add(tl, [2, 'יְהוָה\Z', 'יְהוָה', 'יְהוָה'])
327  call add(tl, [2, 'יְ\Z', 'וְיַ', 'יַ'])
328  call add(tl, [2, "ק\u200d\u05b9x\\Z", "xק\u200d\u05b9xy", "ק\u200d\u05b9x"])
329  call add(tl, [2, "ק\u200d\u05b9x\\Z", "xק\u200dxy", "ק\u200dx"])
330  call add(tl, [2, "ק\u200dx\\Z", "xק\u200d\u05b9xy", "ק\u200d\u05b9x"])
331  call add(tl, [2, "ק\u200dx\\Z", "xק\u200dxy", "ק\u200dx"])
332  call add(tl, [2, "\u05b9\\Z", "xyz"])
333  call add(tl, [2, "\\Z\u05b9", "xyz"])
334  call add(tl, [2, "\u05b9\\Z", "xy\u05b9z", "y\u05b9"])
335  call add(tl, [2, "\\Z\u05b9", "xy\u05b9z", "y\u05b9"])
336  call add(tl, [1, "\u05b9\\+\\Z", "xy\u05b9z\u05b9 ", "y\u05b9z\u05b9"])
337  call add(tl, [1, "\\Z\u05b9\\+", "xy\u05b9z\u05b9 ", "y\u05b9z\u05b9"])
338
339  " Combining different tests and features
340  call add(tl, [2, '[^[=a=]]\+', 'ddaãâbcd', 'dd'])
341
342  " Run the tests
343  for t in tl
344    let re = t[0]
345    let pat = t[1]
346    let text = t[2]
347    let matchidx = 3
348    for engine in [0, 1, 2]
349      if engine == 2 && re == 0 || engine == 1 && re == 1
350        continue
351      endif
352      let &regexpengine = engine
353      try
354        let l = matchlist(text, pat)
355      catch
356        call assert_report('Error ' . engine . ': pat: \"' . pat .
357		    \ '\", text: \"' . text .
358		    \ '\", caused an exception: \"' . v:exception . '\"')
359      endtry
360      " check the match itself
361      if len(l) == 0 && len(t) > matchidx
362        call assert_report('Error ' . engine . ': pat: \"' . pat .
363		    \ '\", text: \"' . text .
364		    \ '\", did not match, expected: \"' . t[matchidx] . '\"')
365      elseif len(l) > 0 && len(t) == matchidx
366        call assert_report('Error ' . engine . ': pat: \"' . pat .
367		    \ '\", text: \"' . text . '\", match: \"' . l[0] .
368		    \ '\", expected no match')
369      elseif len(t) > matchidx && l[0] != t[matchidx]
370        call assert_report('Error ' . engine . ': pat: \"' . pat .
371		    \ '\", text: \"' . text . '\", match: \"' . l[0] .
372		    \ '\", expected: \"' . t[matchidx] . '\"')
373      else
374        " Test passed
375      endif
376      if len(l) > 0
377        " check all the nine submatches
378        for i in range(1, 9)
379          if len(t) <= matchidx + i
380            let e = ''
381          else
382            let e = t[matchidx + i]
383          endif
384          if l[i] != e
385            call assert_report('Error ' . engine . ': pat: \"' . pat .
386                  \ '\", text: \"' . text . '\", submatch ' . i .
387                  \ ': \"' . l[i] . '\", expected: \"' . e . '\"')
388          endif
389        endfor
390        unlet i
391      endif
392    endfor
393  endfor
394  set regexpengine&
395endfunc
396
397" check that 'ambiwidth' does not change the meaning of \p
398func Test_regexp_ambiwidth()
399  set regexpengine=1 ambiwidth=single
400  call assert_equal(0, match("\u00EC", '\p'))
401  set regexpengine=1 ambiwidth=double
402  call assert_equal(0, match("\u00EC", '\p'))
403  set regexpengine=2 ambiwidth=single
404  call assert_equal(0, match("\u00EC", '\p'))
405  set regexpengine=2 ambiwidth=double
406  call assert_equal(0, match("\u00EC", '\p'))
407  set regexpengine& ambiwidth&
408endfunc
409
410func Run_regexp_ignore_case()
411  call assert_equal('iIİ', substitute('iIİ', '\([iIİ]\)', '\1', 'g'))
412
413  call assert_equal('iIx', substitute('iIİ', '\c\([İ]\)', 'x', 'g'))
414  call assert_equal('xxİ', substitute('iIİ', '\(i\c\)', 'x', 'g'))
415  call assert_equal('iIx', substitute('iIİ', '\(İ\c\)', 'x', 'g'))
416  call assert_equal('iIx', substitute('iIİ', '\c\(\%u0130\)', 'x', 'g'))
417  call assert_equal('iIx', substitute('iIİ', '\c\([\u0130]\)', 'x', 'g'))
418  call assert_equal('iIx', substitute('iIİ', '\c\([\u012f-\u0131]\)', 'x', 'g'))
419
420  " Ignoring case in a literal string that starts with a character longer
421  " than a following one must still match.
422  call assert_equal('Über', matchstr('Überraschung', '\cüber'))
423  call assert_equal('Ünder', matchstr('Ünderdog', '\cünder'))
424  call assert_equal('αaaa', matchstr('αaaaa', '\cαaaa'))
425endfunc
426
427func Test_regexp_ignore_case()
428  set regexpengine=1
429  call Run_regexp_ignore_case()
430  set regexpengine=2
431  call Run_regexp_ignore_case()
432  set regexpengine&
433endfunc
434
435" Tests for regexp with multi-byte encoding and various magic settings
436func Run_regexp_multibyte_magic()
437  let text =<< trim END
438    1 a aa abb abbccc
439    2 d dd dee deefff
440    3 g gg ghh ghhiii
441    4 j jj jkk jkklll
442    5 m mm mnn mnnooo
443    6 x ^aa$ x
444    7 (a)(b) abbaa
445    8 axx [ab]xx
446    9 หม่x อมx
447    a อมx หม่x
448    b ちカヨは
449    c x ¬€x
450    d 天使x
451    e ������y
452    f ������z
453    g a啷bb
454    j 0123❤x
455    k combinations
456    l äö üᾱ̆́
457  END
458
459  new
460  call setline(1, text)
461  exe 'normal /a*b\{2}c\+/e' .. "\<CR>x"
462  call assert_equal('1 a aa abb abbcc', getline('.'))
463  exe 'normal /\Md\*e\{2}f\+/e' .. "\<CR>x"
464  call assert_equal('2 d dd dee deeff', getline('.'))
465  set nomagic
466  exe 'normal /g\*h\{2}i\+/e' .. "\<CR>x"
467  call assert_equal('3 g gg ghh ghhii', getline('.'))
468  exe 'normal /\mj*k\{2}l\+/e' .. "\<CR>x"
469  call assert_equal('4 j jj jkk jkkll', getline('.'))
470  exe 'normal /\vm*n{2}o+/e' .. "\<CR>x"
471  call assert_equal('5 m mm mnn mnnoo', getline('.'))
472  exe 'normal /\V^aa$/' .. "\<CR>x"
473  call assert_equal('6 x aa$ x', getline('.'))
474  set magic
475  exe 'normal /\v(a)(b)\2\1\1/e' .. "\<CR>x"
476  call assert_equal('7 (a)(b) abba', getline('.'))
477  exe 'normal /\V[ab]\(\[xy]\)\1' .. "\<CR>x"
478  call assert_equal('8 axx ab]xx', getline('.'))
479
480  " search for multi-byte without composing char
481  exe 'normal /ม' .. "\<CR>x"
482  call assert_equal('9 หม่x อx', getline('.'))
483
484  " search for multi-byte with composing char
485  exe 'normal /ม่' .. "\<CR>x"
486  call assert_equal('a อมx หx', getline('.'))
487
488  " find word by change of word class
489  exe 'normal /ち\<カヨ\>は' .. "\<CR>x"
490  call assert_equal('b カヨは', getline('.'))
491
492  " Test \%u, [\u] and friends
493  " c
494  exe 'normal /\%u20ac' .. "\<CR>x"
495  call assert_equal('c x ¬x', getline('.'))
496  " d
497  exe 'normal /[\u4f7f\u5929]\+' .. "\<CR>x"
498  call assert_equal('d 使x', getline('.'))
499  " e
500  exe 'normal /\%U12345678' .. "\<CR>x"
501  call assert_equal('e y', getline('.'))
502  " f
503  exe 'normal /[\U1234abcd\u1234\uabcd]' .. "\<CR>x"
504  call assert_equal('f z', getline('.'))
505  " g
506  exe 'normal /\%d21879b' .. "\<CR>x"
507  call assert_equal('g abb', getline('.'))
508
509  " j Test backwards search from a multi-byte char
510  exe "normal /x\<CR>x?.\<CR>x"
511  call assert_equal('j 012❤', getline('.'))
512  " k
513  let @w=':%s#comb[i]nations#œ̄ṣ́m̥̄ᾱ̆́#g'
514  @w
515  call assert_equal('k œ̄ṣ́m̥̄ᾱ̆́', getline(18))
516
517  bw!
518endfunc
519
520func Test_regexp_multibyte_magic()
521  set regexpengine=1
522  call Run_regexp_multibyte_magic()
523  set regexpengine=2
524  call Run_regexp_multibyte_magic()
525  set regexpengine&
526endfunc
527
528" Test for 7.3.192
529" command ":s/ \?/ /g" splits multi-byte characters into bytes
530func Test_split_multibyte_to_bytes()
531  new
532  call setline(1, 'l äö üᾱ̆́')
533  s/ \?/ /g
534  call assert_equal(' l ä ö ü ᾱ̆́', getline(1))
535  bw!
536endfunc
537
538" Test for matchstr() with multibyte characters
539func Test_matchstr_multibyte()
540  new
541  call assert_equal('ב', matchstr("אבגד", ".", 0, 2))
542  call assert_equal('בג', matchstr("אבגד", "..", 0, 2))
543  call assert_equal('א', matchstr("אבגד", ".", 0, 0))
544  call assert_equal('ג', matchstr("אבגד", ".", 4, -1))
545  bw!
546endfunc
547
548" Test for 7.4.636
549" A search with end offset gets stuck at end of file.
550func Test_search_with_end_offset()
551  new
552  call setline(1, ['', 'dog(a', 'cat('])
553  exe "normal /(/e+\<CR>"
554  normal n"ayn
555  call assert_equal("a\ncat(", @a)
556  bw!
557endfunc
558
559" Check that "^" matches even when the line starts with a combining char
560func Test_match_start_of_line_combining()
561  new
562  call setline(1, ['', "\u05ae", ''])
563  exe "normal gg/^\<CR>"
564  call assert_equal(2, getcurpos()[1])
565  bwipe!
566endfunc
567
568" Check that [[:upper:]] matches for automatic engine
569func Test_match_char_class_upper()
570  new
571
572  " Test 1: [[:upper:]]\{2,\}
573  set regexpengine=0
574  call setline(1, ['05. ПЕСНЯ О ГЕРОЯХ муз. А. Давиденко, М. Коваля и Б. Шехтера ...', '05. PJESNJA O GJEROJAKH mus. A. Davidjenko, M. Kovalja i B. Shjekhtjera ...'])
575  call cursor(1,1)
576  let search_cmd='norm /\<[[:upper:]]\{2,\}\>' .. "\<CR>"
577  exe search_cmd
578  call assert_equal(4, searchcount().total, 'TEST 1')
579  set regexpengine=1
580  exe search_cmd
581  call assert_equal(2, searchcount().total, 'TEST 1')
582  set regexpengine=2
583  exe search_cmd
584  call assert_equal(4, searchcount().total, 'TEST 1')
585
586  " Test 2: [[:upper:]].\+
587  let search_cmd='norm /\<[[:upper:]].\+\>' .. "\<CR>"
588  set regexpengine=0
589  exe search_cmd
590  call assert_equal(2, searchcount().total, 'TEST 2')
591  set regexpengine=1
592  exe search_cmd
593  call assert_equal(1, searchcount().total, 'TEST 2')
594  set regexpengine=2
595  exe search_cmd
596  call assert_equal(2, searchcount().total, 'TEST 2')
597
598  " Test 3: [[:lower:]]\+
599  let search_cmd='norm /\<[[:lower:]]\+\>' .. "\<CR>"
600  set regexpengine=0
601  exe search_cmd
602  call assert_equal(4, searchcount().total, 'TEST 3 lower')
603  set regexpengine=1
604  exe search_cmd
605  call assert_equal(2, searchcount().total, 'TEST 3 lower')
606  set regexpengine=2
607  exe search_cmd
608  call assert_equal(4, searchcount().total, 'TEST 3 lower')
609
610  " clean up
611  set regexpengine=0
612  bwipe!
613endfunc
614
615func Test_match_invalid_byte()
616  call writefile(0z630a.765d30aa0a.2e0a.790a.4030, 'Xinvalid')
617  new
618  source Xinvalid
619  bwipe!
620  call delete('Xinvalid')
621endfunc
622
623func Test_match_illegal_byte()
624  " Text has illegal bytes which need to be set explicitly
625  let lines = ["norm :set no\x01\<CR>", "silent n\xff", "silent norm :b\xff\<CR>"]
626  call writefile(lines, 'Xregexp')
627  call system(GetVimCommand() .. ' -X -Z -e -s -S Xregexp -c qa!')
628
629  call delete('Xregexp')
630endfunc
631
632func Test_match_too_complicated()
633  set regexpengine=1
634  exe "noswapfile vsplit \xeb\xdb\x99"
635  silent! buf \&\zs*\zs*0
636  bwipe!
637  set regexpengine=0
638endfunc
639
640func Test_combining_chars_in_collection()
641  new
642  for i in range(0,2)
643    exe "set re=".i
644    put =['ɔ̃', 'ɔ',  '̃  ã', 'abcd']
645    :%s/[ɔ̃]//
646    call assert_equal(['', '', 'ɔ', '̃  ã', 'abcd'], getline(1,'$'))
647    %d
648  endfor
649  bw!
650endfunc
651
652func Test_search_multibyte_match_ascii()
653  new
654  " Match single 'ſ' and 's'
655  call setline(1,  'das abc heraus abc ſich abc ſind')
656  for i in range(0, 2)
657    exe "set re="..i
658    let ic_match = matchbufline('%', '\c\%u17f', 1, '$')->mapnew({idx, val -> val.text})
659    let noic_match = matchbufline('%', '\C\%u17f', 1, '$')->mapnew({idx, val -> val.text})
660    call assert_equal(['s', 's', 'ſ','ſ'], ic_match, "Ignorecase Regex-engine: " .. &re)
661    call assert_equal(['ſ','ſ'], noic_match, "No-Ignorecase Regex-engine: " .. &re)
662  endfor
663  " Match several 'ſſ' and 'ss'
664  call setline(1,  'das abc herauss abc ſſich abc ſind')
665  for i in range(0, 2)
666    exe "set re="..i
667    let ic_match = matchbufline('%', '\c\%u17f\%u17f', 1, '$')->mapnew({idx, val -> val.text})
668    let noic_match = matchbufline('%', '\C\%u17f\%u17f', 1, '$')->mapnew({idx, val -> val.text})
669    let ic_match2 = matchbufline('%', '\c\%u17f\+', 1, '$')->mapnew({idx, val -> val.text})
670    let noic_match2 = matchbufline('%', '\C\%u17f\+', 1, '$')->mapnew({idx, val -> val.text})
671    let ic_match3 = matchbufline('%', '\c[\u17f]\+', 1, '$')->mapnew({idx, val -> val.text})
672    let noic_match3 = matchbufline('%', '\C[\u17f]\+', 1, '$')->mapnew({idx, val -> val.text})
673
674    call assert_equal(['ss', 'ſſ'], ic_match, "Ignorecase Regex-engine: " .. &re)
675    call assert_equal(['ſſ'], noic_match, "No-Ignorecase Regex-engine: " .. &re)
676    call assert_equal(['s', 'ss', 'ſſ', 'ſ'], ic_match2, "Ignorecase Regex-engine: " .. &re)
677    call assert_equal(['ſſ','ſ'], noic_match2, "No-Ignorecase Regex-engine: " .. &re)
678    call assert_equal(['s', 'ss', 'ſſ', 'ſ'], ic_match3, "Ignorecase Collection Regex-engine: " .. &re)
679    call assert_equal(['ſſ','ſ'], noic_match3, "No-Ignorecase Collection Regex-engine: " .. &re)
680  endfor
681  set re&vim
682  bw!
683endfunc
684
685func Test_replace_multibyte_match_in_multi_lines()
686  new
687  let text = ['ab 1c', 'ab 1c', 'def', '是否 a', '是否 a', 'ghi', '是否a', '是否a', '是否 1', '是否 1']
688  let expected = ['', 'def', '', 'ghi', '', '']
689  for i in range(0, 2)
690    exe "set ignorecase re="..i
691    :%d _
692    call setline(1, text)
693    :%s/\(.\+\)\n\1//g
694    call assert_equal(expected, getline(1, '$'))
695  endfor
696  bw!
697  set ignorecase&vim re&vim
698endfun
699
700func Test_regex_collection_range_with_composing_crash()
701  " Regression test: composing char in collection range caused NFA crash/E874
702  new
703  call setline(1, ['00', '0ֻ', '01'])
704  let patterns = [ '0[0-0ֻ]\@<!','0[0ֻ]\@<!']
705
706  for pat in patterns
707    " Should compile and execute without crash or error
708    for re in range(3)
709      let regex = '\%#=' .. re .. pat
710      call search(regex)
711      call assert_fails($"/{regex}\<cr>", 'E486:')
712    endfor
713  endfor
714
715  bwipe!
716endfunc
717
718" A submatch in a look-behind must not start on the previous line.  See
719" issue #20802.
720func Test_lookbehind_submatch_on_second_line()
721  new
722  for re in range(0, 2)
723    exe "set re=" .. re
724    call setline(1, ['testing', 'testing'])
725    %s/\v(.)@<=/[\1]/g
726    call assert_equal(['t[t]e[e]s[s]t[t]i[i]n[n]g',
727	  \ 't[t]e[e]s[s]t[t]i[i]n[n]g'], getline(1, '$'), 're=' .. re)
728    %d _
729
730    " With "\_." the look-behind can match the line break, then the submatch
731    " does start on the previous line.
732    call setline(1, ['abc', 'def'])
733    %s/\v(\_.)@<=/[\1]/g
734    call assert_equal(['a[a]b[b]c[c]', '[]d[d]e[e]f[f]'],
735	  \ getline(1, '$'), 're=' .. re)
736    %d _
737  endfor
738
739  set re&
740  bwipe!
741endfunc
742
743" The old (backtracking) engine converts the negated collections [^0-9],
744" [^a-zA-Z0-9_], [^a-zA-Z_] and [^a-zA-Z] into the \D, \W, \H and \A classes.
745" These classes match a multibyte character, just like the collections do, so
746" the conversion must give the same result.  A Latin1 test cannot exercise this,
747" so check it here with the old engine forced.
748func Test_recognize_char_class_multibyte_old_engine()
749  let mb = 'α'
750  call assert_equal(mb, matchstr('0' .. mb .. '9', '\%#=1[^0-9]\+'))
751  call assert_equal(mb, matchstr('a' .. mb .. 'Z', '\%#=1[^a-zA-Z0-9_]\+'))
752  " The '_' distinguishes [^a-zA-Z_] (\H) from [^a-zA-Z] (\A).
753  call assert_equal('9' .. mb, matchstr('a9' .. mb .. '_z', '\%#=1[^a-zA-Z_]\+'))
754  call assert_equal('9' .. mb .. '_', matchstr('a9' .. mb .. '_z', '\%#=1[^a-zA-Z]\+'))
755  " The positive classes must not match the multibyte character.
756  call assert_equal('12', matchstr('12' .. mb, '\%#=1[0-9]\+'))
757  call assert_equal('ab', matchstr('ab' .. mb, '\%#=1[a-zA-Z0-9_]\+'))
758endfunc
759
760" vim: shiftwidth=2 sts=2 expandtab