11-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22-- SPDX-License-Identifier: BSD-2-Clause
33
4- -- Temporary wrapper to give us compatibility between Neovim v0.10.4 and
5- -- more recent nightlies; see: https://github.com/wincent/command-t/issues/434
6- --
7- -- `:help str_utfindex` in v0.10.4:
8- --
9- -- vim.str_utfindex({str}, {index}) *vim.str_utfindex()*
10- -- Convert byte index to UTF-32 and UTF-16 indices. If {index} is not
11- -- supplied, the length of the string is used. All indices are zero-based.
12- --
13- -- Embedded NUL bytes are treated as terminating the string. Invalid UTF-8
14- -- bytes, and embedded surrogates are counted as one code point each. An
15- -- {index} in the middle of a UTF-8 sequence is rounded upwards to the end of
16- -- that sequence.
17- --
18- -- Parameters: ~
19- -- • {str} (`string`)
20- -- • {index} (`integer?`)
21- --
22- -- Return (multiple): ~
23- -- (`integer`) UTF-32 index
24- -- (`integer`) UTF-16 index
25- --
26- --
27- -- `:help str_utfindex` in nightly:
28- --
29- -- *vim.str_utfindex()*
30- -- vim.str_utfindex({s}, {encoding}, {index}, {strict_indexing})
31- -- Convert byte index to UTF-32, UTF-16 or UTF-8 indices. If {index} is not
32- -- supplied, the length of the string is used. All indices are zero-based.
33- --
34- -- If {strict_indexing} is false then an out of range index will return
35- -- string length instead of throwing an error. Invalid UTF-8 bytes, and
36- -- embedded surrogates are counted as one code point each. An {index} in the
37- -- middle of a UTF-8 sequence is rounded upwards to the end of that sequence.
38- --
39- -- Parameters: ~
40- -- • {s} (`string`)
41- -- • {encoding} (`"utf-8"|"utf-16"|"utf-32"`)
42- -- • {index} (`integer?`)
43- -- • {strict_indexing} (`boolean?`) default: true
44- --
45- -- Return: ~
46- -- (`integer`)
47-
4+ --- Temporary wrapper to give us compatibility between Neovim v0.10.4 and
5+ --- more recent nightlies; see: https://github.com/wincent/command-t/issues/434
6+ ---
7+ --- `:help str_utfindex` in v0.10.4:
8+ ---
9+ --- vim.str_utfindex({str}, {index}) *vim.str_utfindex()*
10+ --- Convert byte index to UTF-32 and UTF-16 indices. If {index} is not
11+ --- supplied, the length of the string is used. All indices are zero-based.
12+ ---
13+ --- Embedded NUL bytes are treated as terminating the string. Invalid UTF-8
14+ --- bytes, and embedded surrogates are counted as one code point each. An
15+ --- {index} in the middle of a UTF-8 sequence is rounded upwards to the end of
16+ --- that sequence.
17+ ---
18+ --- Parameters: ~
19+ --- • {str} (`string`)
20+ --- • {index} (`integer?`)
21+ ---
22+ --- Return (multiple): ~
23+ --- (`integer`) UTF-32 index
24+ --- (`integer`) UTF-16 index
25+ ---
26+ ---
27+ --- `:help str_utfindex` in nightly:
28+ ---
29+ --- *vim.str_utfindex()*
30+ --- vim.str_utfindex({s}, {encoding}, {index}, {strict_indexing})
31+ --- Convert byte index to UTF-32, UTF-16 or UTF-8 indices. If {index} is not
32+ --- supplied, the length of the string is used. All indices are zero-based.
33+ ---
34+ --- If {strict_indexing} is false then an out of range index will return
35+ --- string length instead of throwing an error. Invalid UTF-8 bytes, and
36+ --- embedded surrogates are counted as one code point each. An {index} in the
37+ --- middle of a UTF-8 sequence is rounded upwards to the end of that sequence.
38+ ---
39+ --- Parameters: ~
40+ --- • {s} (`string`)
41+ --- • {encoding} (`"utf-8"|"utf-16"|"utf-32"`)
42+ --- • {index} (`integer?`)
43+ --- • {strict_indexing} (`boolean?`) default: true
44+ ---
45+ --- Return: ~
46+ --- (`integer`)
47+ ---
48+ --- @param str string
49+ --- @param encoding ' utf-8' | ' utf-16' | ' utf-32'
50+ --- @param start_char integer ?
51+ --- @param strict_indexing boolean ?
52+ --- @return integer
4853local function str_utfindex (str , encoding , start_char , strict_indexing )
4954 local success , result = pcall (function ()
5055 -- Nightly.
@@ -58,7 +63,10 @@ local function str_utfindex(str, encoding, start_char, strict_indexing)
5863 return vim .str_utfindex (str , start_char )
5964end
6065
61- -- Unicode-aware `len` implementation.
66+ --- Unicode-aware `len` implementation.
67+ ---
68+ --- @param str string
69+ --- @return integer
6270local function len (str )
6371 return str_utfindex (str , ' utf-32' )
6472end
0 commit comments