mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-18 05:34:02 -04:00
selection: optimize \r\n -> \n when receiving clipboard data
Instead of first memmoving, possibly lots of data lots of times, the received buffer, and _then_ calling the callback, simply call the callback multiple times, and just skip the \r character(s).
This commit is contained in:
parent
2c4af8728d
commit
2f475d7b44
1 changed files with 14 additions and 6 deletions
20
selection.c
20
selection.c
|
|
@ -587,15 +587,23 @@ fdm_receive(struct fdm *fdm, int fd, int events, void *data)
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Replace \r\n with \n */
|
/* Call cb while at same time replacing \r\n with \n */
|
||||||
for (size_t i = 0; i < count - 1; i++) {
|
const char *p = text;
|
||||||
if (text[i] == '\r' && text[i + 1] == '\n') {
|
size_t left = count;
|
||||||
memmove(&text[i], &text[i + 1], count - i - 1);
|
again:
|
||||||
count--;
|
for (size_t i = 0; i < left - 1; i++) {
|
||||||
|
if (p[i] == '\r' && p[i + 1] == '\n') {
|
||||||
|
ctx->cb(p, i, ctx->user);
|
||||||
|
|
||||||
|
assert(i + 1 <= left);
|
||||||
|
p += i + 1;
|
||||||
|
left -= i + 1;
|
||||||
|
goto again;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->cb(text, count, ctx->user);
|
ctx->cb(p, left, ctx->user);
|
||||||
|
left = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue