robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for regexp in utf8 encoding 2 3func Test_regexp_literal_byte_search () 4let save_encoding = & encoding 5let save_re = & regexpengine 6try 7"for enc in ['latin1', 'utf-8'] 8for enc in [ 'utf-8' ] 9let & encoding = enc 10for engine in [ 1 , 2 ] 11let & regexpengine = engine 12let chars = [ 'z' , nr2char ( 0x7f ), nr2char ( 0xff )] 13if enc == 'utf-8' 14call add ( chars , nr2char ( 0x100 )) 15endif 16for c in chars 17let prefix = repeat ( 'x' , 4096 ) 18call assert_equal ( -1 , match ( '' , c )) 19call assert_equal ( -1 , match ( prefix , c )) 20call assert_equal ( 0 , match ( c .. prefix , c )) 21call assert_equal ( 4096 , match ( prefix .. c , c )) 22endfor 23call assert_equal ( -1 , match ( 'xxx' , '\%d0' )) 24endfor 25endfor 26finally 27let & encoding = save_encoding 28let & regexpengine = save_re 29endtry 30endfunc 31 32" ASCII range collections must honor Unicode case folding. 33func Test_ascii_collection_unicode_casefold () 34let save_encoding = & encoding 35let save_ic = & ignorecase 36try 37set encoding = utf-8ignorecase 38for engine in [ 1 , 2 ] 39for c in [ nr2char ( 0x17f ), nr2char ( 0x212a )] 40for [ 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_]' ]] 45call assert_equal ( 0 , match ( c , '\%#=' .. engine .. positive )) 46call assert_equal ( -1 , match ( c , '\%#=' .. engine .. negative )) 47endfor 48" No character folds into [0-9a-fA-F], so the hex class is kept. 49call assert_equal ( -1 , match ( c , '\%#=' .. engine .. '[0-9a-fA-F]' )) 50endfor 51endfor 52finally 53let & encoding = save_encoding 54let & ignorecase = save_ic 55endtry 56endfunc 57 58func s: equivalence_test () 59let 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źżžƶᵶᶎẑẓẕⱬ" 60let groups = split ( str ) 61for group1 in groups 62for c in split ( group1 , '\zs' ) 63" next statement confirms that equivalence class matches every 64" character in group 65call assert_match ( '^[[=' .. c .. '=]]*$' , group1 ) 66for group2 in groups 67if group2 != group1 68" next statement converts that equivalence class doesn't match 69" character in any other group 70call assert_equal ( -1 , match ( group2 , '[[=' .. c .. '=]]' ), c ) 71endif 72endfor 73endfor 74endfor 75endfunc 76 77func Test_equivalence_re1 () 78set re = 1 79call s: equivalence_test () 80set re = 0 81endfunc 82 83func Test_equivalence_re2 () 84set re = 2 85call s: equivalence_test () 86set re = 0 87endfunc 88 89func s: classes_test () 90if has ( 'win32' ) 91set iskeyword = @,48-57,_,192-255 92endif 93" set isprint=@,161-255 94call assert_equal ( 'Motörhead' , matchstr ( 'Motörhead' , '[[:print:]]\+' )) 95 96let alnumchars = '' 97let alphachars = '' 98let backspacechar = '' 99let blankchars = '' 100let cntrlchars = '' 101let digitchars = '' 102let escapechar = '' 103let graphchars = '' 104let lowerchars = '' 105let printchars = '' 106let punctchars = '' 107let returnchar = '' 108let spacechars = '' 109let tabchar = '' 110let upperchars = '' 111let xdigitchars = '' 112let identchars = '' 113let identchars1 = '' 114let kwordchars = '' 115let kwordchars1 = '' 116let fnamechars = '' 117let fnamechars1 = '' 118let i = 1 119while i <= 255 120let c = nr2char ( i ) 121if c =~ '[[:alpha:]]' 122let alphachars .= c 123endif 124if c =~ '[[:alnum:]]' 125let alnumchars .= c 126endif 127if c =~ '[[:backspace:]]' 128let backspacechar .= c 129endif 130if c =~ '[[:blank:]]' 131let blankchars .= c 132endif 133if c =~ '[[:cntrl:]]' 134let cntrlchars .= c 135endif 136if c =~ '[[:digit:]]' 137let digitchars .= c 138endif 139if c =~ '[[:escape:]]' 140let escapechar .= c 141endif 142if c =~ '[[:graph:]]' 143let graphchars .= c 144endif 145if c =~ '[[:lower:]]' 146let lowerchars .= c 147endif 148if c =~ '[[:print:]]' 149let printchars .= c 150endif 151if c =~ '[[:punct:]]' 152let punctchars .= c 153endif 154if c =~ '[[:return:]]' 155let returnchar .= c 156endif 157if c =~ '[[:space:]]' 158let spacechars .= c 159endif 160if c =~ '[[:tab:]]' 161let tabchar .= c 162endif 163if c =~ '[[:upper:]]' 164let upperchars .= c 165endif 166if c =~ '[[:xdigit:]]' 167let xdigitchars .= c 168endif 169if c =~ '[[:ident:]]' 170let identchars .= c 171endif 172if c =~ '\i' 173let identchars1 .= c 174endif 175if c =~ '[[:keyword:]]' 176let kwordchars .= c 177endif 178if c =~ '\k' 179let kwordchars1 .= c 180endif 181if c =~ '[[:fname:]]' 182let fnamechars .= c 183endif 184if c =~ '\f' 185let fnamechars1 .= c 186endif 187let i += 1 188endwhile 189 190call assert_equal ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' , alphachars ) 191call assert_equal ( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' , alnumchars ) 192call assert_equal ( "\b" , backspacechar ) 193call assert_equal ( "\t " , blankchars ) 194call 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 ) 195call assert_equal ( "0123456789" , digitchars ) 196call assert_equal ( "\<Esc>" , escapechar ) 197call assert_equal ( '!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' , graphchars ) 198call assert_equal ( 'abcdefghijklmnopqrstuvwxyzµßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' , lowerchars ) 199call assert_equal ( ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' , printchars ) 200call assert_equal ( '!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~' , punctchars ) 201call assert_equal ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ' , upperchars ) 202call assert_equal ( "\r" , returnchar ) 203call assert_equal ( "\t\n\x0b\f\r " , spacechars ) 204call assert_equal ( "\t" , tabchar ) 205call assert_equal ( '0123456789ABCDEFabcdef' , xdigitchars ) 206 207if has ( 'win32' ) 208let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz ¡¢£¤¥¦§µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' 209let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 210else 211let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 212let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 213endif 214 215if has ( 'win32' ) 216let fnamechars_ok = '!#$%+,-./0123456789:=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]_abcdefghijklmnopqrstuvwxyz{}~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 217elseif has ( 'amiga' ) 218let fnamechars_ok = '$+,-./0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 219elseif has ( 'vms' ) 220let fnamechars_ok = '#$%+,-./0123456789:;<>ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 221else 222let fnamechars_ok = '#$%+,-./0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' 223endif 224 225call assert_equal ( identchars_ok , identchars ) 226call assert_equal ( kwordchars_ok , kwordchars ) 227call assert_equal ( fnamechars_ok , fnamechars ) 228 229call assert_equal ( identchars1 , identchars ) 230call assert_equal ( kwordchars1 , kwordchars ) 231call assert_equal ( fnamechars1 , fnamechars ) 232endfunc 233 234func Test_classes_re1 () 235set re = 1 236call s: classes_test () 237set re = 0 238endfunc 239 240func Test_classes_re2 () 241set re = 2 242call s: classes_test () 243set re = 0 244endfunc 245 246func Test_reversed_range () 247for re in range ( 0 , 2 ) 248exe 'set re=' . re 249call assert_fails ( 'call match("abc def", "[c-a]")' , 'E944:' , re ) 250endfor 251set re = 0 252endfunc 253 254func Test_large_class () 255set re = 1 256call assert_fails ( 'call match("abc def", "[\u3000-\u4000]")' , 'E945:' ) 257set re = 2 258call assert_equal ( 0 , 'abc def' =~# '[\u3000-\u4000]' ) 259call assert_equal ( 1 , "\u3042" =~# '[\u3000-\u4000]' ) 260set re = 0 261endfunc 262 263func Test_optmatch_toolong () 264set re = 1 265" Can only handle about 8000 characters. 266let pat = '\\%[' .. repeat ( 'x' , 9000 ) .. ']' 267call assert_fails ( 'call match("abc def", "' .. pat .. '")' , 'E339:' ) 268set 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. 282let tl = [] 283 284" Multi-byte character tests. 285call add ( tl , [ 2 , '[[:alpha:][=a=]]\+' , '879 aiaãâaiuvna ' , 'aiaãâaiuvna' ]) 286call add ( tl , [ 2 , '[[=a=]]\+' , 'ddaãâbcd' , 'aãâ' ]) " equivalence classes 287call add ( tl , [ 2 , '[^ม ]\+' , 'มม oijasoifjos ifjoisj f osij j มมมมม abcd' , 'oijasoifjos' ]) 288call add ( tl , [ 2 , ' [^ ]\+' , 'start มabcdม ' , ' มabcdม' ]) 289call add ( tl , [ 2 , '[ม[:alpha:][=a=]]\+' , '879 aiaãมâมaiuvna ' , 'aiaãมâมaiuvna' ]) 290 291" this is not a normal "i" but 0xec 292call add ( tl , [ 2 , '\p\+' , 'ìa' , 'ìa' ]) 293call add ( tl , [ 2 , '\p*' , 'aあ' , 'aあ' ]) 294 295" Test recognition of some character classes 296call add ( tl , [ 2 , '\i\+' , '&*¨xx ' , 'xx' ]) 297call add ( tl , [ 2 , '\f\+' , '&*fname ' , 'fname' ]) 298 299" Test composing character matching 300call add ( tl , [ 2 , '.ม' , 'xม่x yมy' , 'yม' ]) 301call add ( tl , [ 2 , '.ม่' , 'xม่x yมy' , 'xม่' ]) 302call add ( tl , [ 2 , "\u05b9" , " x\u05b9 " , "x\u05b9" ]) 303call add ( tl , [ 2 , ".\u05b9" , " x\u05b9 " , "x\u05b9" ]) 304call add ( tl , [ 2 , "\u05b9\u05bb" , " x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 305call add ( tl , [ 2 , ".\u05b9\u05bb" , " x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 306call add ( tl , [ 2 , "\u05bb\u05b9" , " x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 307call add ( tl , [ 2 , ".\u05bb\u05b9" , " x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 308call add ( tl , [ 2 , "\u05b9" , " y\u05bb x\u05b9 " , "x\u05b9" ]) 309call add ( tl , [ 2 , ".\u05b9" , " y\u05bb x\u05b9 " , "x\u05b9" ]) 310call add ( tl , [ 2 , "\u05b9" , " y\u05bb\u05b9 x\u05b9 " , "y\u05bb\u05b9" ]) 311call add ( tl , [ 2 , ".\u05b9" , " y\u05bb\u05b9 x\u05b9 " , "y\u05bb\u05b9" ]) 312call add ( tl , [ 1 , "\u05b9\u05bb" , " y\u05b9 x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 313call add ( tl , [ 2 , ".\u05b9\u05bb" , " y\u05bb x\u05b9\u05bb " , "x\u05b9\u05bb" ]) 314call add ( tl , [ 2 , "a" , "ca\u0300t" ]) 315call add ( tl , [ 2 , "ca" , "ca\u0300t" ]) 316call add ( tl , [ 2 , "a\u0300" , "ca\u0300t" , "a\u0300" ]) 317call add ( tl , [ 2 , 'a\%C' , "ca\u0300t" , "a\u0300" ]) 318call add ( tl , [ 2 , 'ca\%C' , "ca\u0300t" , "ca\u0300" ]) 319call add ( tl , [ 2 , 'ca\%Ct' , "ca\u0300t" , "ca\u0300t" ]) 320 321" Test \Z 322call add ( tl , [ 2 , 'ú\Z' , 'x' ]) 323call add ( tl , [ 2 , 'יהוה\Z' , 'יהוה' , 'יהוה' ]) 324call add ( tl , [ 2 , 'יְהוָה\Z' , 'יהוה' , 'יהוה' ]) 325call add ( tl , [ 2 , 'יהוה\Z' , 'יְהוָה' , 'יְהוָה' ]) 326call add ( tl , [ 2 , 'יְהוָה\Z' , 'יְהוָה' , 'יְהוָה' ]) 327call add ( tl , [ 2 , 'יְ\Z' , 'וְיַ' , 'יַ' ]) 328call add ( tl , [ 2 , "ק\u200d\u05b9x\\Z" , "xק\u200d\u05b9xy" , "ק\u200d\u05b9x" ]) 329call add ( tl , [ 2 , "ק\u200d\u05b9x\\Z" , "xק\u200dxy" , "ק\u200dx" ]) 330call add ( tl , [ 2 , "ק\u200dx\\Z" , "xק\u200d\u05b9xy" , "ק\u200d\u05b9x" ]) 331call add ( tl , [ 2 , "ק\u200dx\\Z" , "xק\u200dxy" , "ק\u200dx" ]) 332call add ( tl , [ 2 , "\u05b9\\Z" , "xyz" ]) 333call add ( tl , [ 2 , "\\Z\u05b9" , "xyz" ]) 334call add ( tl , [ 2 , "\u05b9\\Z" , "xy\u05b9z" , "y\u05b9" ]) 335call add ( tl , [ 2 , "\\Z\u05b9" , "xy\u05b9z" , "y\u05b9" ]) 336call add ( tl , [ 1 , "\u05b9\\+\\Z" , "xy\u05b9z\u05b9 " , "y\u05b9z\u05b9" ]) 337call add ( tl , [ 1 , "\\Z\u05b9\\+" , "xy\u05b9z\u05b9 " , "y\u05b9z\u05b9" ]) 338 339" Combining different tests and features 340call add ( tl , [ 2 , '[^[=a=]]\+' , 'ddaãâbcd' , 'dd' ]) 341 342" Run the tests 343for t in tl 344let re = t [ 0 ] 345let pat = t [ 1 ] 346let text = t [ 2 ] 347let matchidx = 3 348for engine in [ 0 , 1 , 2 ] 349if engine == 2 && re == 0 || engine == 1 && re == 1 350continue 351endif 352let & regexpengine = engine 353try 354let l = matchlist ( text , pat ) 355catch 356call assert_report ( 'Error ' . engine . ': pat: \"' . pat . 357 \'\", text: \"' . text . 358 \'\", caused an exception: \"' . v: exception . '\"' ) 359endtry 360" check the match itself 361if len ( l ) == 0 && len ( t ) > matchidx 362call assert_report ( 'Error ' . engine . ': pat: \"' . pat . 363 \'\", text: \"' . text . 364 \'\", did not match, expected: \"' . t [ matchidx ] . '\"' ) 365elseif len ( l ) > 0 && len ( t ) == matchidx 366call assert_report ( 'Error ' . engine . ': pat: \"' . pat . 367 \'\", text: \"' . text . '\", match: \"' . l [ 0 ] . 368 \'\", expected no match' ) 369elseif len ( t ) > matchidx && l [ 0 ] != t [ matchidx ] 370call assert_report ( 'Error ' . engine . ': pat: \"' . pat . 371 \'\", text: \"' . text . '\", match: \"' . l [ 0 ] . 372 \'\", expected: \"' . t [ matchidx ] . '\"' ) 373else 374" Test passed 375endif 376if len ( l ) > 0 377" check all the nine submatches 378for i in range ( 1 , 9 ) 379if len ( t ) <= matchidx + i 380let e = '' 381else 382let e = t [ matchidx + i ] 383endif 384if l [ i ] != e 385call assert_report ( 'Error ' . engine . ': pat: \"' . pat . 386 \'\", text: \"' . text . '\", submatch ' . i . 387 \': \"' . l [ i ] . '\", expected: \"' . e . '\"' ) 388endif 389endfor 390unlet i 391endif 392endfor 393endfor 394set regexpengine & 395endfunc 396 397" check that 'ambiwidth' does not change the meaning of \p 398func Test_regexp_ambiwidth () 399set regexpengine = 1 ambiwidth = single 400call assert_equal ( 0 , match ( "\u00EC" , '\p' )) 401set regexpengine = 1 ambiwidth = double 402call assert_equal ( 0 , match ( "\u00EC" , '\p' )) 403set regexpengine = 2 ambiwidth = single 404call assert_equal ( 0 , match ( "\u00EC" , '\p' )) 405set regexpengine = 2 ambiwidth = double 406call assert_equal ( 0 , match ( "\u00EC" , '\p' )) 407set regexpengine & ambiwidth & 408endfunc 409 410func Run_regexp_ignore_case () 411call assert_equal ( 'iIİ' , substitute ( 'iIİ' , '\([iIİ]\)' , '\1' , 'g' )) 412 413call assert_equal ( 'iIx' , substitute ( 'iIİ' , '\c\([İ]\)' , 'x' , 'g' )) 414call assert_equal ( 'xxİ' , substitute ( 'iIİ' , '\(i\c\)' , 'x' , 'g' )) 415call assert_equal ( 'iIx' , substitute ( 'iIİ' , '\(İ\c\)' , 'x' , 'g' )) 416call assert_equal ( 'iIx' , substitute ( 'iIİ' , '\c\(\%u0130\)' , 'x' , 'g' )) 417call assert_equal ( 'iIx' , substitute ( 'iIİ' , '\c\([\u0130]\)' , 'x' , 'g' )) 418call 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. 422call assert_equal ( 'Über' , matchstr ( 'Überraschung' , '\cüber' )) 423call assert_equal ( 'Ünder' , matchstr ( 'Ünderdog' , '\cünder' )) 424call assert_equal ( 'αaaa' , matchstr ( 'αaaaa' , '\cαaaa' )) 425endfunc 426 427func Test_regexp_ignore_case () 428set regexpengine = 1 429call Run_regexp_ignore_case () 430set regexpengine = 2 431call Run_regexp_ignore_case () 432set regexpengine & 433endfunc 434 435" Tests for regexp with multi-byte encoding and various magic settings 436func Run_regexp_multibyte_magic () 437let text =<< trim END 4381 a aa abb abbccc 4392 d dd dee deefff 4403 g gg ghh ghhiii 4414 j jj jkk jkklll 4425 m mm mnn mnnooo 4436 x ^aa$ x 4447 (a)(b) abbaa 4458 axx [ab]xx 4469 หม่x อมx 447a อมx หม่x 448b ちカヨは 449c x ¬€x 450d 天使x 451e ������y 452f ������z 453g a啷bb 454j 0123❤x 455k combinations 456l äö üᾱ̆́ 457END 458 459new 460call setline ( 1 , text ) 461exe 'normal /a*b\{2}c\+/e' .. "\<CR>x" 462call assert_equal ( '1 a aa abb abbcc' , getline ( '.' )) 463exe 'normal /\Md\*e\{2}f\+/e' .. "\<CR>x" 464call assert_equal ( '2 d dd dee deeff' , getline ( '.' )) 465set nomagic 466exe 'normal /g\*h\{2}i\+/e' .. "\<CR>x" 467call assert_equal ( '3 g gg ghh ghhii' , getline ( '.' )) 468exe 'normal /\mj*k\{2}l\+/e' .. "\<CR>x" 469call assert_equal ( '4 j jj jkk jkkll' , getline ( '.' )) 470exe 'normal /\vm*n{2}o+/e' .. "\<CR>x" 471call assert_equal ( '5 m mm mnn mnnoo' , getline ( '.' )) 472exe 'normal /\V^aa$/' .. "\<CR>x" 473call assert_equal ( '6 x aa$ x' , getline ( '.' )) 474set magic 475exe 'normal /\v(a)(b)\2\1\1/e' .. "\<CR>x" 476call assert_equal ( '7 (a)(b) abba' , getline ( '.' )) 477exe 'normal /\V[ab]\(\[xy]\)\1' .. "\<CR>x" 478call assert_equal ( '8 axx ab]xx' , getline ( '.' )) 479 480" search for multi-byte without composing char 481exe 'normal /ม' .. "\<CR>x" 482call assert_equal ( '9 หม่x อx' , getline ( '.' )) 483 484" search for multi-byte with composing char 485exe 'normal /ม่' .. "\<CR>x" 486call assert_equal ( 'a อมx หx' , getline ( '.' )) 487 488" find word by change of word class 489exe 'normal /ち\<カヨ\>は' .. "\<CR>x" 490call assert_equal ( 'b カヨは' , getline ( '.' )) 491 492" Test \%u, [\u] and friends 493" c 494exe 'normal /\%u20ac' .. "\<CR>x" 495call assert_equal ( 'c x ¬x' , getline ( '.' )) 496" d 497exe 'normal /[\u4f7f\u5929]\+' .. "\<CR>x" 498call assert_equal ( 'd 使x' , getline ( '.' )) 499" e 500exe 'normal /\%U12345678' .. "\<CR>x" 501call assert_equal ( 'e y' , getline ( '.' )) 502" f 503exe 'normal /[\U1234abcd\u1234\uabcd]' .. "\<CR>x" 504call assert_equal ( 'f z' , getline ( '.' )) 505" g 506exe 'normal /\%d21879b' .. "\<CR>x" 507call assert_equal ( 'g abb' , getline ( '.' )) 508 509" j Test backwards search from a multi-byte char 510exe "normal /x\<CR>x?.\<CR>x" 511call assert_equal ( 'j 012❤' , getline ( '.' )) 512" k 513let @w= ':%s#comb[i]nations#œ̄ṣ́m̥̄ᾱ̆́#g' 514 @w 515call assert_equal ( 'k œ̄ṣ́m̥̄ᾱ̆́' , getline ( 18 )) 516 517bw ! 518endfunc 519 520func Test_regexp_multibyte_magic () 521set regexpengine = 1 522call Run_regexp_multibyte_magic () 523set regexpengine = 2 524call Run_regexp_multibyte_magic () 525set 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 () 531new 532call setline ( 1 , 'l äö üᾱ̆́' ) 533s / \?/ /g 534call assert_equal ( ' l ä ö ü ᾱ̆́' , getline ( 1 )) 535bw ! 536endfunc 537 538" Test for matchstr() with multibyte characters 539func Test_matchstr_multibyte () 540new 541call assert_equal ( 'ב' , matchstr ( "אבגד" , "." , 0 , 2 )) 542call assert_equal ( 'בג' , matchstr ( "אבגד" , ".." , 0 , 2 )) 543call assert_equal ( 'א' , matchstr ( "אבגד" , "." , 0 , 0 )) 544call assert_equal ( 'ג' , matchstr ( "אבגד" , "." , 4 , -1 )) 545bw ! 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 () 551new 552call setline ( 1 , [ '' , 'dog(a' , 'cat(' ]) 553exe "normal /(/e+\<CR>" 554normal n"ayn 555call assert_equal ( "a\ncat(" , @a) 556bw ! 557endfunc 558 559" Check that "^" matches even when the line starts with a combining char 560func Test_match_start_of_line_combining () 561new 562call setline ( 1 , [ '' , "\u05ae" , '' ]) 563exe "normal gg/^\<CR>" 564call assert_equal ( 2 , getcurpos ()[ 1 ]) 565bwipe ! 566endfunc 567 568" Check that [[:upper:]] matches for automatic engine 569func Test_match_char_class_upper () 570new 571 572" Test 1: [[:upper:]]\{2,\} 573set regexpengine = 0 574call setline ( 1 , [ '05. ПЕСНЯ О ГЕРОЯХ муз. А. Давиденко, М. Коваля и Б. Шехтера ...' , '05. PJESNJA O GJEROJAKH mus. A. Davidjenko, M. Kovalja i B. Shjekhtjera ...' ]) 575call cursor ( 1 , 1 ) 576let search_cmd = 'norm /\<[[:upper:]]\{2,\}\>' .. "\<CR>" 577exe search_cmd 578call assert_equal ( 4 , searchcount (). total , 'TEST 1' ) 579set regexpengine = 1 580exe search_cmd 581call assert_equal ( 2 , searchcount (). total , 'TEST 1' ) 582set regexpengine = 2 583exe search_cmd 584call assert_equal ( 4 , searchcount (). total , 'TEST 1' ) 585 586" Test 2: [[:upper:]].\+ 587let search_cmd = 'norm /\<[[:upper:]].\+\>' .. "\<CR>" 588set regexpengine = 0 589exe search_cmd 590call assert_equal ( 2 , searchcount (). total , 'TEST 2' ) 591set regexpengine = 1 592exe search_cmd 593call assert_equal ( 1 , searchcount (). total , 'TEST 2' ) 594set regexpengine = 2 595exe search_cmd 596call assert_equal ( 2 , searchcount (). total , 'TEST 2' ) 597 598" Test 3: [[:lower:]]\+ 599let search_cmd = 'norm /\<[[:lower:]]\+\>' .. "\<CR>" 600set regexpengine = 0 601exe search_cmd 602call assert_equal ( 4 , searchcount (). total , 'TEST 3 lower' ) 603set regexpengine = 1 604exe search_cmd 605call assert_equal ( 2 , searchcount (). total , 'TEST 3 lower' ) 606set regexpengine = 2 607exe search_cmd 608call assert_equal ( 4 , searchcount (). total , 'TEST 3 lower' ) 609 610" clean up 611set regexpengine = 0 612bwipe ! 613endfunc 614 615func Test_match_invalid_byte () 616call writefile ( 0 z630a.765d30aa0a.2e0a.790a.4030, 'Xinvalid') 617new 618source Xinvalid 619bwipe ! 620call delete ( 'Xinvalid' ) 621endfunc 622 623func Test_match_illegal_byte () 624" Text has illegal bytes which need to be set explicitly 625let lines = [ "norm :set no\x01\<CR>" , "silent n\xff" , "silent norm :b\xff\<CR>" ] 626call writefile ( lines , 'Xregexp' ) 627call system ( GetVimCommand () .. ' -X -Z -e -s -S Xregexp -c qa!' ) 628 629call delete ( 'Xregexp' ) 630endfunc 631 632func Test_match_too_complicated () 633set regexpengine = 1 634exe "noswapfile vsplit \xeb\xdb\x99" 635silent ! buf \&\zs*\zs*0 636bwipe ! 637set regexpengine = 0 638endfunc 639 640func Test_combining_chars_in_collection () 641new 642for i in range ( 0 , 2 ) 643exe "set re=" . i 644put =['ɔ̃','ɔ' ,'̃ ã' ,'abcd' ] 645: %s / [ ɔ̃ ] // 646call assert_equal ([ '' , '' , 'ɔ' , '̃ ã' , 'abcd' ], getline ( 1 , '$' )) 647 %d 648endfor 649bw ! 650endfunc 651 652func Test_search_multibyte_match_ascii () 653new 654" Match single 'ſ' and 's' 655call setline ( 1 , 'das abc heraus abc ſich abc ſind' ) 656for i in range ( 0 , 2 ) 657exe "set re=" .. i 658let ic_match = matchbufline ( '%' , '\c\%u17f' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 659let noic_match = matchbufline ( '%' , '\C\%u17f' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 660call assert_equal ([ 's' , 's' , 'ſ' , 'ſ' ], ic_match , "Ignorecase Regex-engine: " .. & re ) 661call assert_equal ([ 'ſ' , 'ſ' ], noic_match , "No-Ignorecase Regex-engine: " .. & re ) 662endfor 663" Match several 'ſſ' and 'ss' 664call setline ( 1 , 'das abc herauss abc ſſich abc ſind' ) 665for i in range ( 0 , 2 ) 666exe "set re=" .. i 667let ic_match = matchbufline ( '%' , '\c\%u17f\%u17f' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 668let noic_match = matchbufline ( '%' , '\C\%u17f\%u17f' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 669let ic_match2 = matchbufline ( '%' , '\c\%u17f\+' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 670let noic_match2 = matchbufline ( '%' , '\C\%u17f\+' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 671let ic_match3 = matchbufline ( '%' , '\c[\u17f]\+' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 672let noic_match3 = matchbufline ( '%' , '\C[\u17f]\+' , 1 , '$' ) ->mapnew ({ idx , val ->val . text }) 673 674call assert_equal ([ 'ss' , 'ſſ' ], ic_match , "Ignorecase Regex-engine: " .. & re ) 675call assert_equal ([ 'ſſ' ], noic_match , "No-Ignorecase Regex-engine: " .. & re ) 676call assert_equal ([ 's' , 'ss' , 'ſſ' , 'ſ' ], ic_match2 , "Ignorecase Regex-engine: " .. & re ) 677call assert_equal ([ 'ſſ' , 'ſ' ], noic_match2 , "No-Ignorecase Regex-engine: " .. & re ) 678call assert_equal ([ 's' , 'ss' , 'ſſ' , 'ſ' ], ic_match3 , "Ignorecase Collection Regex-engine: " .. & re ) 679call assert_equal ([ 'ſſ' , 'ſ' ], noic_match3 , "No-Ignorecase Collection Regex-engine: " .. & re ) 680endfor 681set re & vim 682bw ! 683endfunc 684 685func Test_replace_multibyte_match_in_multi_lines () 686new 687let text = [ 'ab 1c' , 'ab 1c' , 'def' , '是否 a' , '是否 a' , 'ghi' , '是否a' , '是否a' , '是否 1' , '是否 1' ] 688let expected = [ '' , 'def' , '' , 'ghi' , '' , '' ] 689for i in range ( 0 , 2 ) 690exe "set ignorecase re=" .. i 691: %d _ 692call setline ( 1 , text ) 693: %s /\(.\+\)\n\1//g 694call assert_equal ( expected , getline ( 1 , '$' )) 695endfor 696bw ! 697set 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 702new 703call setline ( 1 , [ '00' , '0ֻ' , '01' ]) 704let patterns = [ '0[0-0ֻ]\@<!' , '0[0ֻ]\@<!' ] 705 706for pat in patterns 707" Should compile and execute without crash or error 708for re in range ( 3 ) 709let regex = '\%#=' .. re .. pat 710call search ( regex ) 711call assert_fails ( $ "/{regex}\<cr>", 'E486:') 712endfor 713endfor 714 715bwipe ! 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 () 721new 722for re in range ( 0 , 2 ) 723exe "set re=" .. re 724call setline ( 1 , [ 'testing' , 'testing' ]) 725 %s /\v(.)@<=/ [ \1 ] /g 726call 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. 732call setline ( 1 , [ 'abc' , 'def' ]) 733 %s /\v(\_.)@<=/ [ \1 ] /g 734call assert_equal ([ 'a[a]b[b]c[c]' , '[]d[d]e[e]f[f]' ], 735 \getline ( 1 , '$' ), 're=' .. re ) 736 %d _ 737endfor 738 739set re & 740bwipe ! 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 () 749let mb = 'α' 750call assert_equal ( mb , matchstr ( '0' .. mb .. '9' , '\%#=1[^0-9]\+' )) 751call assert_equal ( mb , matchstr ( 'a' .. mb .. 'Z' , '\%#=1[^a-zA-Z0-9_]\+' )) 752" The '_' distinguishes [^a-zA-Z_] (\H) from [^a-zA-Z] (\A). 753call assert_equal ( '9' .. mb , matchstr ( 'a9' .. mb .. '_z' , '\%#=1[^a-zA-Z_]\+' )) 754call assert_equal ( '9' .. mb .. '_' , matchstr ( 'a9' .. mb .. '_z' , '\%#=1[^a-zA-Z]\+' )) 755" The positive classes must not match the multibyte character. 756call assert_equal ( '12' , matchstr ( '12' .. mb , '\%#=1[0-9]\+' )) 757call assert_equal ( 'ab' , matchstr ( 'ab' .. mb , '\%#=1[a-zA-Z0-9_]\+' )) 758endfunc 759 760" vim: shiftwidth=2 sts=2 expandtab