aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/mbyte.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-05-12 23:48:58 +0800
committerGitHub <noreply@github.com>2022-05-12 23:48:58 +0800
commitde5ccf2348a1fabf3867da9256e4740a7dfcf004 (patch)
tree5be2509a126d1f0894f2a31e3d7f4833d8854b50 /src/nvim/mbyte.c
parent0c8e48c78dc1088547f1586267b75d5b9754d687 (diff)
parent8c0510af713d7caf02f35ff55bb75b9f8f042d6d (diff)
downloadrneovim-de5ccf2348a1fabf3867da9256e4740a7dfcf004.tar.gz
rneovim-de5ccf2348a1fabf3867da9256e4740a7dfcf004.tar.bz2
rneovim-de5ccf2348a1fabf3867da9256e4740a7dfcf004.zip
Merge pull request #18540 from zeertzjq/vim-8.2.4919
vim-patch:8.2.{4919,4921}: can add invalid bytes with :spellgood
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 1d65529b75..37b4af5172 100644
--- a/src/nvim/mbyte.c
+++ b/src/nvim/mbyte.c
@@ -1992,6 +1992,31 @@ theend:
convert_setup(&vimconv, NULL, NULL);
}
+/// @return true if string "s" is a valid utf-8 string.
+/// When "end" is NULL stop at the first NUL.
+/// When "end" is positive stop there.
+bool utf_valid_string(const char_u *s, const char_u *end)
+{
+ const char_u *p = s;
+
+ while (end == NULL ? *p != NUL : p < end) {
+ int l = utf8len_tab_zero[*p];
+ if (l == 0) {
+ return false; // invalid lead byte
+ }
+ if (end != NULL && p + l > end) {
+ return false; // incomplete byte sequence
+ }
+ p++;
+ while (--l > 0) {
+ if ((*p++ & 0xc0) != 0x80) {
+ return false; // invalid trail byte
+ }
+ }
+ }
+ return true;
+}
+
/*
* If the cursor moves on an trail byte, set the cursor on the lead byte.
* Thus it moves left if necessary.