robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.unit.testutil' ) 2local describe , before_each =t . describe , t . before_each 3local itp =t . gen_itp ( t . it ) 4 5local ffi =t . ffi 6local eq =t . eq 7local to_cstr =t . to_cstr 8 9local lib =t . cimport ( 10'./src/nvim/mbyte.h' , 11'./src/nvim/charset.h' , 12'./src/nvim/grid.h' , 13'./src/nvim/option_vars.h' 14) 15 16describe ( 'mbyte' , function () 17-- Convert from bytes to string 18local function to_string ( bytes ) 19local s ={} 20for i =1 , # bytes do 21s [ i ] =string . char ( bytes [ i ]) 22end 23return table . concat ( s ) 24end 25 26before_each ( function () end ) 27 28itp ( 'utf_ptr2char' , function () 29-- For strings with length 1 the first byte is returned. 30for c =0 , 255 do 31eq ( c , lib . utf_ptr2char ( to_string ( { c , 0 } ))) 32end 33 34-- Some ill formed byte sequences that should not be recognized as UTF-8 35-- First byte: 0xc0 or 0xc1 36-- Second byte: 0x80 .. 0xbf 37--eq(0x00c0, lib.utf_ptr2char(to_string({0xc0, 0x80}))) 38--eq(0x00c1, lib.utf_ptr2char(to_string({0xc1, 0xbf}))) 39-- 40-- Sequences with more than four bytes 41end ) 42 43for n =0 , 0xF do 44itp (( 'utf_char2bytes for chars 0x%x - 0x%x' ): format ( n * 0x1000 , n * 0x1000 + 0xFFF ), function () 45local char_p =ffi . typeof ( 'char[?]' ) 46for c =n * 0x1000 , n * 0x1000 + 0xFFF do 47local p =char_p ( 4 , 0 ) 48lib . utf_char2bytes ( c , p ) 49eq ( c , lib . utf_ptr2char ( p )) 50eq ( lib . vim_iswordc ( c ), lib . vim_iswordp ( p )) 51end 52end ) 53end 54 55describe ( 'utfc_ptr2schar' , function () 56local function test_seq ( seq ) 57local firstc =ffi . new ( 'int[1]' ) 58local buf =ffi . new ( 'char[32]' ) 59lib . schar_get ( buf , lib . utfc_ptr2schar ( to_string ( seq ), firstc )) 60local str =ffi . string ( buf ) 61if 1 > 2 then -- for debugging 62local tbl ={} 63for i =1 , # str do 64table . insert ( tbl , string . format ( '0x%02x' , string . byte ( str , i ))) 65end 66( '{ ' .. table . concat ( tbl , ', ' ) .. ' }' ) 67io . stdout : flush () 68end 69return { str , firstc [ 0 ] } 70end 71 72local function byte ( val ) 73return { string . char ( val ), val } 74end 75 76itp ( '1-byte sequences' , function () 77eq ( { '' , 0 } , test_seq { 0 } ) 78for c =1 , 127 do 79eq ( byte ( c ), test_seq { c } ) 80end 81for c =128 , 255 do 82eq ( { '' , c } , test_seq { c } ) 83end 84end ) 85 86itp ( '2-byte sequences' , function () 87-- No combining characters 88eq ( byte ( 0x7f ), test_seq { 0x7f , 0x7f } ) 89-- No combining characters 90eq ( byte ( 0x7f ), test_seq { 0x7f , 0x80 } ) 91 92-- No UTF-8 sequence 93eq ( { '' , 0xc2 } , test_seq { 0xc2 , 0x7f } ) 94-- One UTF-8 character 95eq ( { '\xc2\x80' , 0x80 } , test_seq { 0xc2 , 0x80 } ) 96-- No UTF-8 sequence 97eq ( { '' , 0xc2 } , test_seq { 0xc2 , 0xc0 } ) 98end ) 99 100itp ( '3-byte sequences' , function () 101-- No second UTF-8 character 102eq ( byte ( 0x7f ), test_seq { 0x7f , 0x80 , 0x80 } ) 103-- No combining character 104eq ( byte ( 0x7f ), test_seq { 0x7f , 0xc2 , 0x80 } ) 105 106-- Combining character is U+0300 107eq ( { '\x29\xcc\x80' , 0x29 } , test_seq { 0x29 , 0xcc , 0x80 } ) 108-- invalid start byte for combining 109eq ( { '\x7f' , 0x7f } , test_seq { 0x7f , 0xcc , 0x80 } ) 110 111-- No UTF-8 sequence 112eq ( { '' , 0xc2 } , test_seq { 0xc2 , 0x7f , 0xcc } ) 113-- Incomplete combining character 114eq ( { '\xc2\x80' , 0x80 } , test_seq { 0xc2 , 0x80 , 0xcc } ) 115 116-- One UTF-8 character (composing only) 117eq ( { ' \xe2\x83\x90' , 0x20d0 } , test_seq { 0xe2 , 0x83 , 0x90 } ) 118end ) 119 120itp ( '4-byte sequences' , function () 121-- No following combining character 122eq ( byte ( 0x7f ), test_seq { 0x7f , 0x7f , 0xcc , 0x80 } ) 123eq ( byte ( 0x29 ), test_seq { 0x29 , 0x29 , 0xcc , 0x80 } ) 124-- No second UTF-8 character 125eq ( byte ( 0x7f ), test_seq { 0x7f , 0xc2 , 0xcc , 0x80 } ) 126 127-- Combining character U+0300 128eq ( { '\x29\xcc\x80' , 0x29 } , test_seq { 0x29 , 0xcc , 0x80 , 0xcc } ) 129 130-- No UTF-8 sequence 131eq ( { '' , 0xc2 } , test_seq { 0xc2 , 0x7f , 0xcc , 0x80 } ) 132-- No following UTF-8 character 133eq ( { '\xc2\x80' , 0x80 } , test_seq { 0xc2 , 0x80 , 0xcc , 0xcc } ) 134-- Combining character U+0301 135eq ( { '\xc2\xbc\xcc\x81' , 0xbc } , test_seq { 0xc2 , 0xbc , 0xcc , 0x81 } ) 136-- U+0080 : not a valid start char 137eq ( { '\xc2\x80' , 0x80 } , test_seq { 0xc2 , 0x80 , 0xcc , 0x81 } ) 138 139-- One UTF-8 character 140eq ( { '\xf4\x80\x80\x80' , 0x100000 } , test_seq { 0xf4 , 0x80 , 0x80 , 0x80 } ) 141end ) 142 143itp ( '5+-byte sequences' , function () 144-- No following combining character 145eq ( byte ( 0x7f ), test_seq { 0x7f , 0x7f , 0xcc , 0x80 , 0x80 } ) 146-- No second UTF-8 character 147eq ( byte ( 0x7f ), test_seq { 0x7f , 0xc2 , 0xcc , 0x80 , 0x80 } ) 148 149-- Combining character U+0300 150eq ( { '\x29\xcc\x80' , 0x29 } , test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x00 } ) 151 152-- Combining characters U+0300 and U+0301 153eq ( { '\x29\xcc\x80\xcc\x81' , 0x29 } , test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 } ) 154-- Combining characters U+0300, U+0301, U+0302 155eq ( 156{ '\x29\xcc\x80\xcc\x81\xcc\x82' , 0x29 } , 157test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 , 0xcc , 0x82 } 158) 159-- Combining characters U+0300, U+0301, U+0302, U+0303 160eq ( 161{ '\x29\xcc\x80\xcc\x81\xcc\x82\xcc\x83' , 0x29 } , 162test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 , 0xcc , 0x82 , 0xcc , 0x83 } 163) 164-- Combining characters U+0300, U+0301, U+0302, U+0303, U+0304 165eq ( 166{ '\x29\xcc\x80\xcc\x81\xcc\x82\xcc\x83\xcc\x84' , 0x29 } , 167test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 , 0xcc , 0x82 , 0xcc , 0x83 , 0xcc , 0x84 } 168) 169-- Combining characters U+0300, U+0301, U+0302, U+0303, U+0304, U+0305 170eq ( 171{ '\x29\xcc\x80\xcc\x81\xcc\x82\xcc\x83\xcc\x84\xcc\x85' , 0x29 } , 172test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 , 0xcc , 0x82 , 0xcc , 0x83 , 0xcc , 0x84 , 0xcc , 0x85 } 173) 174 175-- Combining characters U+0300, U+0301, U+0302, U+0303, U+0304, U+0305, U+0306 176eq ( 177{ '\x29\xcc\x80\xcc\x81\xcc\x82\xcc\x83\xcc\x84\xcc\x85\xcc\x86' , 0x29 } , 178test_seq { 1790x29 , 1800xcc , 1810x80 , 1820xcc , 1830x81 , 1840xcc , 1850x82 , 1860xcc , 1870x83 , 1880xcc , 1890x84 , 1900xcc , 1910x85 , 1920xcc , 1930x86 , 194} 195) 196 197-- Only three following combining characters U+0300, U+0301, U+0302 198eq ( 199{ '\x29\xcc\x80\xcc\x81\xcc\x82' , 0x29 } , 200test_seq { 0x29 , 0xcc , 0x80 , 0xcc , 0x81 , 0xcc , 0x82 , 0xc2 , 0x80 , 0xcc , 0x84 , 0xcc , 0x85 } 201) 202 203-- No UTF-8 sequence 204eq ( { '' , 0xc2 } , test_seq { 0xc2 , 0x7f , 0xcc , 0x80 , 0x80 } ) 205-- No following UTF-8 character 206eq ( { '\xc2\xbc' , 0xbc } , test_seq { 0xc2 , 0xbc , 0xcc , 0xcc , 0x80 } ) 207-- Combining character U+0301 208eq ( { '\xc2\xbc\xcc\x81' , 0xbc } , test_seq { 0xc2 , 0xbc , 0xcc , 0x81 , 0x7f } ) 209-- Combining character U+0301 210eq ( { '\xc2\xbc\xcc\x81' , 0xbc } , test_seq { 0xc2 , 0xbc , 0xcc , 0x81 , 0xcc } ) 211 212-- One UTF-8 character 213eq ( { '\xf4\x80\x80\x80' , 0x100000 } , test_seq { 0xf4 , 0x80 , 0x80 , 0x80 , 0x7f } ) 214 215-- One UTF-8 character 216eq ( { '\xf4\x80\x80\x80' , 0x100000 } , test_seq { 0xf4 , 0x80 , 0x80 , 0x80 , 0x80 } ) 217-- One UTF-8 character 218eq ( { '\xf4\x80\x80\x80' , 0x100000 } , test_seq { 0xf4 , 0x80 , 0x80 , 0x80 , 0xcc } ) 219 220-- Combining characters U+1AB0 and U+0301 221eq ( 222{ '\xf4\x80\x80\x80\xe1\xaa\xb0\xcc\x81' , 0x100000 } , 223test_seq { 0xf4 , 0x80 , 0x80 , 0x80 , 0xe1 , 0xaa , 0xb0 , 0xcc , 0x81 } 224) 225end ) 226end ) 227 228describe ( 'utf_cp_bounds_len' , function () 229local tests ={ 230{ 231name ='for valid string' , 232str ='iÀiiⱠiⱠⱠ𐀀i' , 233offsets ={ 234b ={ 0 , 0 , 1 , 0 , 0 , 0 , 1 , 2 , 0 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 3 , 0 } , 235e ={ 1 , 2 , 1 , 1 , 1 , 3 , 2 , 1 , 1 , 3 , 2 , 1 , 3 , 2 , 1 , 4 , 3 , 2 , 1 , 1 } , 236} , 237} , 238{ 239name ='for string with incomplete sequence' , 240str ='i\xC3iÀⱠiÀ\xE2\xB1Ⱡ\xF0\x90\x80' , 241offsets ={ 242b ={ 0 , 0 , 0 , 0 , 1 , 0 , 1 , 2 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 2 , 0 , 0 , 0 } , 243e ={ 1 , 1 , 1 , 2 , 1 , 3 , 2 , 1 , 1 , 2 , 1 , 1 , 1 , 3 , 2 , 1 , 1 , 1 , 1 } , 244} , 245} , 246{ 247name ='for string with trailing bytes after multibyte' , 248str ='iÀ\xA0Ⱡ\xA0Ⱡ𐀀\xA0i' , 249offsets ={ 250b ={ 0 , 0 , 1 , 0 , 0 , 1 , 2 , 0 , 0 , 1 , 2 , 0 , 1 , 2 , 3 , 0 , 0 } , 251e ={ 1 , 2 , 1 , 1 , 3 , 2 , 1 , 1 , 3 , 2 , 1 , 4 , 3 , 2 , 1 , 1 , 1 } , 252} , 253} , 254} 255 256for _ , test in ipairs ( tests ) do 257itp ( test . name , function () 258local cstr =to_cstr ( test . str ) 259local b_offsets , e_offsets ={} , {} 260for i =1 , # test . str do 261local result =lib . utf_cp_bounds_len ( cstr , cstr + i - 1 , # test . str - ( i - 1 )) 262table . insert ( b_offsets , result . begin_off ) 263table . insert ( e_offsets , result . end_off ) 264end 265eq ( test . offsets , { b =b_offsets , e =e_offsets } ) 266end ) 267end 268 269itp ( 'does not read before start' , function () 270local str ='𐀀' 271local expected_offsets ={ b ={ 0 , 0 , 0 } , e ={ 1 , 1 , 1 } } 272local cstr =to_cstr ( str ) + 1 273local b_offsets , e_offsets ={} , {} 274for i =1 , 3 do 275local result =lib . utf_cp_bounds_len ( cstr , cstr + i - 1 , 3 - ( i - 1 )) 276table . insert ( b_offsets , result . begin_off ) 277table . insert ( e_offsets , result . end_off ) 278end 279eq ( expected_offsets , { b =b_offsets , e =e_offsets } ) 280end ) 281 282itp ( 'does not read past the end' , function () 283local str ='𐀀' 284local expected_offsets ={ b ={ 0 , 0 , 0 } , e ={ 1 , 1 , 1 } } 285local cstr =to_cstr ( str ) 286local b_offsets , e_offsets ={} , {} 287for i =1 , 3 do 288local result =lib . utf_cp_bounds_len ( cstr , cstr + i - 1 , 3 - ( i - 1 )) 289table . insert ( b_offsets , result . begin_off ) 290table . insert ( e_offsets , result . end_off ) 291end 292eq ( expected_offsets , { b =b_offsets , e =e_offsets } ) 293end ) 294end ) 295 296describe ( 'utf_ptr2cells' , function () 297local function check ( str , expected ) 298eq ( expected , lib . utf_ptr2cells ( to_cstr ( str ))) 299eq ( expected , lib . utf_ptr2cells_len ( to_cstr ( str ), # str )) 300end 301 302itp ( 'gives a spacing mark its own cell' , function () 303check ( 'ำ' , 1 ) 304check ( 'ท' , 1 ) 305check ( 'ทำ' , 2 ) 306check ( 'का' , 2 ) 307end ) 308 309itp ( 'gives the halfwidth katakana sound marks their own cell' , function () 310check ( 'カ' , 1 ) 311check ( 'ガ' , 2 ) 312check ( 'パ' , 2 ) -- U+FF9F, the semi-voiced mark 313end ) 314 315itp ( 'does not widen clusters of non-spacing marks' , function () 316check ( 'ท่' , 1 ) 317check ( 'é' , 1 ) -- decomposed, so the acute joins the cluster 318end ) 319 320itp ( 'clamps to the maximum cells per cluster' , function () 321check ( 'ทำำ' , 2 ) 322end ) 323 324itp ( 'leaves double-width and illegal bytes alone' , function () 325check ( '漢' , 2 ) 326eq ( 4 , lib . utf_ptr2cells ( to_cstr ( '\xff' ))) -- illegal byte, shown as <ff> 327end ) 328end ) 329 330itp ( 'utf_head_off' , function () 331local function check ( str , expected_glyphs ) 332local len =# str 333local cstr =to_cstr ( str ) 334local breaks ={ 0 } -- SOT 335local pos =0 336local mb_glyphs ={} 337while pos < len do 338local clen =lib . utfc_ptr2len ( cstr + pos ) 339if clen == 0 then 340eq ( 0 , string . byte ( str , pos + 1 )) -- only NUL bytes can has length zery 341clen =1 -- but skip it, otherwise we get stuck 342end 343if clen > 1 then 344table . insert ( mb_glyphs , string . sub ( str , pos + 1 , pos + clen )) 345end 346pos =pos + clen 347table . insert ( breaks , pos ) 348end 349eq ( len , breaks [ # breaks ]) -- include EOT as break 350-- we could also send in breaks, but this is more human readable 351eq ( expected_glyphs , mb_glyphs ) 352 353for i =1 , # breaks - 1 do 354local start , next =breaks [ i ], breaks [ i + 1 ] 355 356for p =start , next - 1 do 357eq ( p - start , lib . utf_head_off ( cstr , cstr + p )) 358end 359end 360eq ( 0 , lib . utf_head_off ( cstr , cstr + len )) -- NUL byte is safe 361end 362-- stylua doesn't like ZWJ chars.. 363-- stylua: ignore start 364check ( 'hej och hå 🧑🌾!' , { 'å' , '🧑🌾' } ) 365 366-- emoji (various kinds of combinations, use g8 to see them) 367check ( "🏳️⚧️🧑🌾❤️😂🏴☠️" , { "🏳️⚧️" , "🧑🌾" , "❤️" , "😂" , "🏴☠️" } ) 368check ( '🏳️⚧️xy🧑🌾\r❤️😂å🏴☠️' , { '🏳️⚧️' , '🧑🌾' , '❤️' , '😂' , 'å' , '🏴☠️' , '' } ) 369check ( '🏳️⚧️\000🧑🌾\000❤️\000😂\000å\000🏴☠️\000' , { '🏳️⚧️' , '🧑🌾' , '❤️' , '😂' , 'å' , '🏴☠️' , '' } ) 370check ( '\195🏳️⚧️\198🧑🌾\165❤️\168\195😂\255🏴☠️\129\165' , { '🏳️⚧️' , '🧑🌾' , '❤️' , '😂' , '🏴☠️' , '' } ) 371 372check ( '🇦🅱️ 🇦🇽 🇦🇨🇦 🇲🇽🇹🇱' , { '🇦' , '🅱️' , '🇦🇽' , '🇦🇨' , '🇦' , '🇲🇽' , '🇹🇱' } ) 373check ( '🏴🏴' , { '🏴' , '🏴' } ) 374 375check ( 'å\165ü\195aëq\168β\000\169本\255' , { 'å' , 'ü' , 'ë' , 'β' , '本' } ) 376 377lib . p_arshape =true -- default 378check ( 'سلام' , { 'س' , 'لا' , 'م' } ) 379lib . p_arshape =false 380check ( 'سلام' , { 'س' , 'ل' , 'ا' , 'م' } ) 381 382check ( 'L̓̉̑̒̌̚ơ̗̌̒̄̀ŕ̈̈̎̐̕è̇̅̄̄̐m̖̟̟̅̄̚' , { 'L̓̉̑̒̌̚' , 'ơ̗̌̒̄̀' , 'ŕ̈̈̎̐̕' , 'è̇̅̄̄̐' , 'm̖̟̟̅̄̚' } ) 383-- stylua: ignore end 384end ) 385 386describe ( 'utf_fold' , function () 387itp ( 'does not crash with surrogates #30527' , function () 388eq ( 0xddfb , lib . utf_fold ( 0xddfb )) -- low surrogate, invalid as a character 389eq ( 0xd800 , lib . utf_fold ( 0xd800 )) -- high surrogate, invalid as a character 390end ) 391 392itp ( "doesn't crash on invalid codepoints" , function () 393eq ( 9000000 , lib . utf_fold ( 9000000 )) 394eq ( 0 , lib . utf_fold ( 0 )) 395end ) 396end ) 397end )