robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

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