From fc6f32f9ee409bf755b420263eb698e8e5d6ccca Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Tue, 9 May 2023 22:56:55 +0200 Subject: [PATCH] Stop _ from tearing multibyte UTF-8 sequences Fixes #4887 [ja: add test] --- src/normal.cc | 9 +++++---- test/regression/4887-torn-utf8-sequence/cmd | 1 + test/regression/4887-torn-utf8-sequence/in | 0 test/regression/4887-torn-utf8-sequence/out | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 test/regression/4887-torn-utf8-sequence/cmd create mode 100644 test/regression/4887-torn-utf8-sequence/in create mode 100644 test/regression/4887-torn-utf8-sequence/out diff --git a/src/normal.cc b/src/normal.cc index e3220521..2bc7fc17 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1768,6 +1768,7 @@ void spaces_to_tabs(Context& context, NormalParams params) void trim_selections(Context& context, NormalParams) { + using Utf8It = utf8::iterator; auto& buffer = context.buffer(); ScopedSelectionEdition selection_edition{context}; auto& selections = context.selections(); @@ -1776,8 +1777,8 @@ void trim_selections(Context& context, NormalParams) for (int i = 0; i < (int)selections.size(); ++i) { auto& sel = selections[i]; - auto beg = buffer.iterator_at(sel.min()); - auto end = buffer.iterator_at(sel.max()); + auto beg = Utf8It{buffer.iterator_at(sel.min()), buffer}; + auto end = Utf8It{buffer.iterator_at(sel.max()), buffer}; while (beg != end and is_blank(*beg)) ++beg; while (beg != end and is_blank(*end)) @@ -1787,8 +1788,8 @@ void trim_selections(Context& context, NormalParams) to_remove.push_back(i); else { - sel.min() = beg.coord(); - sel.max() = end.coord(); + sel.min() = beg.base().coord(); + sel.max() = end.base().coord(); } } diff --git a/test/regression/4887-torn-utf8-sequence/cmd b/test/regression/4887-torn-utf8-sequence/cmd new file mode 100644 index 00000000..8a70e75f --- /dev/null +++ b/test/regression/4887-torn-utf8-sequence/cmd @@ -0,0 +1 @@ +:exec %{esc>x_iabesc>} diff --git a/test/regression/4887-torn-utf8-sequence/in b/test/regression/4887-torn-utf8-sequence/in new file mode 100644 index 00000000..e69de29b diff --git a/test/regression/4887-torn-utf8-sequence/out b/test/regression/4887-torn-utf8-sequence/out new file mode 100644 index 00000000..155dd4fa --- /dev/null +++ b/test/regression/4887-torn-utf8-sequence/out @@ -0,0 +1 @@ +abç