aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/mbyte.c
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2019-08-04 15:12:07 +0200
committerBjörn Linse <bjorn.linse@gmail.com>2019-08-08 20:10:14 +0200
commit1f54f68732b4273c2e26c3535851d645b544065e (patch)
treeb46ded90257aade6488dfd32d0b1e65b642afd03 /src/nvim/mbyte.c
parentce628e11877f426851d68ff1215c1cc25d9b5292 (diff)
downloadrneovim-1f54f68732b4273c2e26c3535851d645b544065e.tar.gz
rneovim-1f54f68732b4273c2e26c3535851d645b544065e.tar.bz2
rneovim-1f54f68732b4273c2e26c3535851d645b544065e.zip
lua: minimal UTF-16 support needed for LSP
Diffstat (limited to 'src/nvim/mbyte.c')
-rw-r--r--src/nvim/mbyte.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c
index bf8ce46113..c9ac335f7b 100644
--- a/src/nvim/mbyte.c
+++ b/src/nvim/mbyte.c
@@ -1470,6 +1470,31 @@ void mb_utflen(const char_u *s, size_t len, size_t *codepoints,
*codeunits += count + extra;
}
+ssize_t mb_utf_index_to_bytes(const char_u *s, size_t len,
+ size_t index, bool use_utf16_units)
+ FUNC_ATTR_NONNULL_ALL
+{
+ size_t count = 0;
+ size_t clen, i;
+ if (index == 0) {
+ return 0;
+ }
+ for (i = 0; i < len && s[i] != NUL; i += clen) {
+ clen = utf_ptr2len_len(s+i, len-i);
+ // NB: gets the byte value of invalid sequence bytes.
+ // we only care whether the char fits in the BMP or not
+ int c = (clen > 1) ? utf_ptr2char(s+i) : s[i];
+ count++;
+ if (use_utf16_units && c > 0xFFFF) {
+ count++;
+ }
+ if (count >= index) {
+ return i+clen;
+ }
+ }
+ return -1;
+}
+
/*
* Version of strnicmp() that handles multi-byte characters.