tlbuf: optimize performing many single line changes - neatvi - [fork] simple vi-type editor with UTF-8 support
HTML git clone git://src.adamsgaard.dk/neatvi
DIR Log
DIR Files
DIR Refs
DIR README
---
DIR commit cb01fab3ac9abd167109a9d1a6ac1211aaeaf873
DIR parent 1f5e321f1f46b3e29f0b99015c2dd76a4b790e9b
HTML Author: Ali Gholami Rudi <ali@rudi.ir>
Date: Wed, 15 Sep 2021 18:26:04 +0430
lbuf: optimize performing many single line changes
Reported by Kyryl Melekhin <k.melekhin@gmail.com>.
Diffstat:
M lbuf.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
---
DIR diff --git a/lbuf.c b/lbuf.c
t@@ -136,7 +136,7 @@ static void lbuf_replace(struct lbuf *lb, char *s, int pos, int n_del)
int n_ins = linecount(s);
int i;
while (lb->ln_n + n_ins - n_del >= lb->ln_sz) {
- int nsz = lb->ln_sz + 512;
+ int nsz = lb->ln_sz + (lb->ln_sz ? lb->ln_sz : 512);
char **nln = malloc(nsz * sizeof(nln[0]));
char *nln_glob = malloc(nsz * sizeof(nln_glob[0]));
memcpy(nln, lb->ln, lb->ln_n * sizeof(lb->ln[0]));
t@@ -149,10 +149,12 @@ static void lbuf_replace(struct lbuf *lb, char *s, int pos, int n_del)
}
for (i = 0; i < n_del; i++)
free(lb->ln[pos + i]);
- memmove(lb->ln + pos + n_ins, lb->ln + pos + n_del,
- (lb->ln_n - pos - n_del) * sizeof(lb->ln[0]));
- memmove(lb->ln_glob + pos + n_ins, lb->ln_glob + pos + n_del,
- (lb->ln_n - pos - n_del) * sizeof(lb->ln_glob[0]));
+ if (n_ins != n_del) {
+ memmove(lb->ln + pos + n_ins, lb->ln + pos + n_del,
+ (lb->ln_n - pos - n_del) * sizeof(lb->ln[0]));
+ memmove(lb->ln_glob + pos + n_ins, lb->ln_glob + pos + n_del,
+ (lb->ln_n - pos - n_del) * sizeof(lb->ln_glob[0]));
+ }
lb->ln_n += n_ins - n_del;
for (i = 0; i < n_ins; i++) {
int l = s ? linelength(s) : 0;
t@@ -187,7 +189,7 @@ static void lbuf_opt(struct lbuf *lb, char *buf, int pos, int n_del)
lopt_done(&lb->hist[i]);
lb->hist_n = lb->hist_u;
if (lb->hist_n == lb->hist_sz) {
- int sz = lb->hist_sz + 128;
+ int sz = lb->hist_sz + (lb->hist_sz ? lb->hist_sz : 128);
struct lopt *hist = malloc(sz * sizeof(hist[0]));
memcpy(hist, lb->hist, lb->hist_n * sizeof(hist[0]));
free(lb->hist);