aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-06-14 20:54:11 +0800
committerGitHub <noreply@github.com>2023-06-14 20:54:11 +0800
commitbbb934e7755a3b6f14c4d94334b8f54c63daebf1 (patch)
treec65a2455494b81743473539023b77fd2187358c8
parent79a5b89d66db74560e751561542064674e980146 (diff)
downloadrneovim-bbb934e7755a3b6f14c4d94334b8f54c63daebf1.tar.gz
rneovim-bbb934e7755a3b6f14c4d94334b8f54c63daebf1.tar.bz2
rneovim-bbb934e7755a3b6f14c4d94334b8f54c63daebf1.zip
vim-patch:9.0.1629: having utf16idx() rounding up is inconvenient (#24019)
Problem: Having utf16idx() rounding up is inconvenient. Solution: Make utf16idx() round down. (Yegappan Lakshmanan, closes vim/vim#12523) https://github.com/vim/vim/commit/95707037afa1aeae4f3494dc623a721ceed7fc4e Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
-rw-r--r--runtime/doc/builtin.txt4
-rw-r--r--src/nvim/strings.c7
-rw-r--r--test/old/testdir/test_functions.vim20
3 files changed, 18 insertions, 13 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 1fea6ad715..938523b6ee 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -9146,8 +9146,8 @@ utf16idx({string}, {idx} [, {countcc} [, {charidx}]])
When {charidx} is present and TRUE, {idx} is used as the
character index in the String {string} instead of as the byte
index.
- An {idx} in the middle of a UTF-8 sequence is rounded upwards
- to the end of that sequence.
+ An {idx} in the middle of a UTF-8 sequence is rounded
+ downwards to the beginning of that sequence.
Returns -1 if the arguments are invalid or if there are less
than {idx} bytes in {string}. If there are exactly {idx} bytes
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index a0d62f5df5..ada3a45d60 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -2014,6 +2014,9 @@ void f_strtrans(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
/// "utf16idx()" function
+///
+/// Converts a byte or character offset in a string to the corresponding UTF-16
+/// code unit offset.
void f_utf16idx(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->vval.v_number = -1;
@@ -2050,6 +2053,7 @@ void f_utf16idx(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
const char *p;
int len;
+ int utf16idx = 0;
for (p = str, len = 0; charidx ? idx >= 0 : p <= str + idx; len++) {
if (*p == NUL) {
// If the index is exactly the number of bytes or characters in the
@@ -2059,6 +2063,7 @@ void f_utf16idx(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
return;
}
+ utf16idx = len;
const int clen = ptr2len(p);
const int c = (clen > 1) ? utf_ptr2char(p) : *p;
if (c > 0xFFFF) {
@@ -2070,7 +2075,7 @@ void f_utf16idx(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
}
- rettv->vval.v_number = len > 0 ? len - 1 : 0;
+ rettv->vval.v_number = utf16idx;
}
/// "tolower(string)" function
diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim
index d80004d70f..0fca0e322e 100644
--- a/test/old/testdir/test_functions.vim
+++ b/test/old/testdir/test_functions.vim
@@ -1395,14 +1395,14 @@ func Test_utf16idx_from_byteidx()
" UTF-16 index of a string with four byte characters
let str = 'a😊😊b'
call assert_equal(0, utf16idx(str, 0))
- call assert_equal(2, utf16idx(str, 1))
- call assert_equal(2, utf16idx(str, 2))
- call assert_equal(2, utf16idx(str, 3))
- call assert_equal(2, utf16idx(str, 4))
- call assert_equal(4, utf16idx(str, 5))
- call assert_equal(4, utf16idx(str, 6))
- call assert_equal(4, utf16idx(str, 7))
- call assert_equal(4, utf16idx(str, 8))
+ call assert_equal(1, utf16idx(str, 1))
+ call assert_equal(1, utf16idx(str, 2))
+ call assert_equal(1, utf16idx(str, 3))
+ call assert_equal(1, utf16idx(str, 4))
+ call assert_equal(3, utf16idx(str, 5))
+ call assert_equal(3, utf16idx(str, 6))
+ call assert_equal(3, utf16idx(str, 7))
+ call assert_equal(3, utf16idx(str, 8))
call assert_equal(5, utf16idx(str, 9))
call assert_equal(6, utf16idx(str, 10))
call assert_equal(-1, utf16idx(str, 11))
@@ -1498,8 +1498,8 @@ func Test_utf16idx_from_charidx()
" UTF-16 index of a string with four byte characters
let str = "a😊😊b"
call assert_equal(0, utf16idx(str, 0, v:false, v:true))
- call assert_equal(2, utf16idx(str, 1, v:false, v:true))
- call assert_equal(4, utf16idx(str, 2, v:false, v:true))
+ call assert_equal(1, utf16idx(str, 1, v:false, v:true))
+ call assert_equal(3, utf16idx(str, 2, v:false, v:true))
call assert_equal(5, utf16idx(str, 3, v:false, v:true))
call assert_equal(6, utf16idx(str, 4, v:false, v:true))
call assert_equal(-1, utf16idx(str, 5, v:false, v:true))