diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2019-12-15 11:11:12 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2019-12-15 11:11:12 +0000 |
| commit | 2f475d7b44fd2e0426c0b96d980605debaede15a (patch) | |
| tree | 00089fafb21d60e397dcb67d1eb1b808cca0682a | |
| parent | vt: add commented out cases for 8-bit C1 control characters (diff) | |
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).
| -rw-r--r-- | selection.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/selection.c b/selection.c index bbe990d7..91043921 100644 --- a/selection.c +++ b/selection.c @@ -587,15 +587,23 @@ fdm_receive(struct fdm *fdm, int fd, int events, void *data) if (count == 0) break; - /* Replace \r\n with \n */ - for (size_t i = 0; i < count - 1; i++) { - if (text[i] == '\r' && text[i + 1] == '\n') { - memmove(&text[i], &text[i + 1], count - i - 1); - count--; + /* Call cb while at same time replacing \r\n with \n */ + const char *p = text; + size_t left = count; + again: + 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: |
