Skip to content

Commit 9570703

Browse files
yegappanbrammool
authored andcommitted
patch 9.0.1629: having utf16idx() rounding up is inconvenient
Problem: Having utf16idx() rounding up is inconvenient. Solution: Make utf16idx() round down. (Yegappan Lakshmanan, closes vim#12523)
1 parent d5b952a commit 9570703

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

runtime/doc/builtin.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10127,8 +10127,8 @@ utf16idx({string}, {idx} [, {countcc} [, {charidx}]])
1012710127
When {charidx} is present and TRUE, {idx} is used as the
1012810128
character index in the String {string} instead of as the byte
1012910129
index.
10130-
An {idx} in the middle of a UTF-8 sequence is rounded upwards
10131-
to the end of that sequence.
10130+
An {idx} in the middle of a UTF-8 sequence is rounded
10131+
downwards to the beginning of that sequence.
1013210132

1013310133
Returns -1 if the arguments are invalid or if there are less
1013410134
than {idx} bytes in {string}. If there are exactly {idx} bytes

src/strings.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -1743,8 +1743,10 @@ f_strtrans(typval_T *argvars, typval_T *rettv)
17431743

17441744

17451745
/*
1746-
*
17471746
* "utf16idx()" function
1747+
*
1748+
* Converts a byte or character offset in a string to the corresponding UTF-16
1749+
* code unit offset.
17481750
*/
17491751
void
17501752
f_utf16idx(typval_T *argvars, typval_T *rettv)
@@ -1780,6 +1782,7 @@ f_utf16idx(typval_T *argvars, typval_T *rettv)
17801782

17811783
char_u *p;
17821784
int len;
1785+
int utf16idx = 0;
17831786
for (p = str, len = 0; charidx ? idx >= 0 : p <= str + idx; len++)
17841787
{
17851788
if (*p == NUL)
@@ -1791,6 +1794,7 @@ f_utf16idx(typval_T *argvars, typval_T *rettv)
17911794
rettv->vval.v_number = len;
17921795
return;
17931796
}
1797+
utf16idx = len;
17941798
int clen = ptr2len(p);
17951799
int c = (clen > 1) ? utf_ptr2char(p) : *p;
17961800
if (c > 0xFFFF)
@@ -1800,7 +1804,7 @@ f_utf16idx(typval_T *argvars, typval_T *rettv)
18001804
idx--;
18011805
}
18021806

1803-
rettv->vval.v_number = len > 0 ? len - 1 : 0;
1807+
rettv->vval.v_number = utf16idx;
18041808
}
18051809

18061810
/*

src/testdir/test_functions.vim

+10-10
Original file line numberDiff line numberDiff line change
@@ -1518,14 +1518,14 @@ func Test_utf16idx_from_byteidx()
15181518
" UTF-16 index of a string with four byte characters
15191519
let str = 'a😊😊b'
15201520
call assert_equal(0, utf16idx(str, 0))
1521-
call assert_equal(2, utf16idx(str, 1))
1522-
call assert_equal(2, utf16idx(str, 2))
1523-
call assert_equal(2, utf16idx(str, 3))
1524-
call assert_equal(2, utf16idx(str, 4))
1525-
call assert_equal(4, utf16idx(str, 5))
1526-
call assert_equal(4, utf16idx(str, 6))
1527-
call assert_equal(4, utf16idx(str, 7))
1528-
call assert_equal(4, utf16idx(str, 8))
1521+
call assert_equal(1, utf16idx(str, 1))
1522+
call assert_equal(1, utf16idx(str, 2))
1523+
call assert_equal(1, utf16idx(str, 3))
1524+
call assert_equal(1, utf16idx(str, 4))
1525+
call assert_equal(3, utf16idx(str, 5))
1526+
call assert_equal(3, utf16idx(str, 6))
1527+
call assert_equal(3, utf16idx(str, 7))
1528+
call assert_equal(3, utf16idx(str, 8))
15291529
call assert_equal(5, utf16idx(str, 9))
15301530
call assert_equal(6, utf16idx(str, 10))
15311531
call assert_equal(-1, utf16idx(str, 11))
@@ -1621,8 +1621,8 @@ func Test_utf16idx_from_charidx()
16211621
" UTF-16 index of a string with four byte characters
16221622
let str = "a😊😊b"
16231623
call assert_equal(0, utf16idx(str, 0, v:false, v:true))
1624-
call assert_equal(2, utf16idx(str, 1, v:false, v:true))
1625-
call assert_equal(4, utf16idx(str, 2, v:false, v:true))
1624+
call assert_equal(1, utf16idx(str, 1, v:false, v:true))
1625+
call assert_equal(3, utf16idx(str, 2, v:false, v:true))
16261626
call assert_equal(5, utf16idx(str, 3, v:false, v:true))
16271627
call assert_equal(6, utf16idx(str, 4, v:false, v:true))
16281628
call assert_equal(-1, utf16idx(str, 5, v:false, v:true))

src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static char *(features[]) =
695695

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1629,
698700
/**/
699701
1628,
700702
/**/

0 commit comments

Comments
 (0)