tmot: fix lbuf_pair() in lines with multi-byte characters - neatvi - [fork] simple vi-type editor with UTF-8 support (HTM) git clone git://src.adamsgaard.dk/neatvi (DIR) Log (DIR) Files (DIR) Refs (DIR) README --- (DIR) commit 0cda11bd3981b5cdf7c73774420019413e9fe032 (DIR) parent e4391d06a150886b08bc2fa1965957a7bb27f12a (HTM) Author: Ali Gholami Rudi <ali@rudi.ir> Date: Wed, 1 Sep 2021 19:02:16 +0430 mot: fix lbuf_pair() in lines with multi-byte characters Reported and tested by Kyryl Melekhin <k.melekhin@gmail.com>. Diffstat: M mot.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) --- (DIR) diff --git a/mot.c b/mot.c t@@ -194,21 +194,20 @@ int lbuf_wordend(struct lbuf *lb, int big, int dir, int *row, int *off) int lbuf_pair(struct lbuf *lb, int *row, int *off) { int r = *row, o = *off; - char *ln = lbuf_get(lb, *row); char *pairs = "()[]{}"; - int p; /* index for pairs[] */ + int pchr; /* parenthesis character */ + int pidx; /* index into pairs[] */ int dep = 1; /* parenthesis depth */ - if (!ln || !ln[o]) + while ((pchr = (unsigned char) lbuf_chr(lb, r, o)[0]) && !strchr(pairs, pchr)) + o++; + if (!pchr) return 1; - while (!strchr(pairs, ln[o])) - if (!ln[++o]) - return 1; - p = strchr(pairs, ln[o]) - pairs; - while (!lbuf_next(lb, (p & 1) ? -1 : +1, &r, &o)) { + pidx = strchr(pairs, pchr) - pairs; + while (!lbuf_next(lb, (pidx & 1) ? -1 : +1, &r, &o)) { int c = (unsigned char) lbuf_chr(lb, r, o)[0]; - if (c == pairs[p ^ 1]) + if (c == pairs[pidx ^ 1]) dep--; - if (c == pairs[p]) + if (c == pairs[pidx]) dep++; if (!dep) { *row = r;