Welcome to little lamb

Code » limb » commit b5774e7

escall_scan(): Optimization for "raw" parts

author Olivier Brunel
2023-05-12 12:59:04 UTC
committer Olivier Brunel
2023-07-05 07:37:02 UTC
parent 220d23390da9519b834d7fefcb1ea0db35055763

escall_scan(): Optimization for "raw" parts

Instead of doing every non-escaped character one by one, wa can just
memmove the whole thing.

src/liblimb/esc.h/escall_scan.c +29 -20

diff --git a/src/liblimb/esc.h/escall_scan.c b/src/liblimb/esc.h/escall_scan.c
index abe1000..edbd9dd 100644
--- a/src/liblimb/esc.h/escall_scan.c
+++ b/src/liblimb/esc.h/escall_scan.c
@@ -12,29 +12,38 @@ escall_scan(char *dst, size_t dlen, const char *sce, size_t slen, size_t *w, siz
     if (*w > dlen || *r > slen) return (errno = EINVAL, 0);
 
     while ((!dst || *w < dlen) && *r < slen) {
-        if (sce[*r] == '\\') {
+        size_t n = byte_chr(sce + *r, slen - *r, '\\');
+        if (dst) {
+            if (n > dlen - *w)
+                n = dlen - *w;
+            memmove(dst + *w, sce + *r, n);
+        }
+        *w += n;
+        *r += n;
+
+        if ((dst && *w == dlen) || *r == slen)
+            break;
+
+        /* sce[*r] == '\\' */
+        ++*r;
+        if (sce[*r] == '\\' || sce[*r] == '"') {
+            if (dst) dst[*w] = sce[*r];
+        } else if (sce[*r] == 'x') {
             ++*r;
-            if (sce[*r] == '\\' || sce[*r] == '"') {
-                if (dst) dst[*w] = sce[*r];
-            } else if (sce[*r] == 'x') {
-                ++*r;
-                if (dst) {
-                    char c = fmtscan_num(sce[*r], 16);
-                    if (c >= 16) return (errno = EINVAL, 0);
-                    dst[*w] = c << 4;
-                    c = fmtscan_num(sce[*r + 1], 16);
-                    if (c >= 16) return (errno = EINVAL, 0);
-                    dst[*w] += c;
-                }
-                ++*r;
-            } else {
-                const char esc[7] = "abtnvfr";
-                size_t n = byte_chr(esc, 7, sce[*r]);
-                if (n == 7) return (errno = EINVAL, 0);
-                if (dst) dst[*w] = 7 + n;
+            if (dst) {
+                char c = fmtscan_num(sce[*r], 16);
+                if (c >= 16) return (errno = EINVAL, 0);
+                dst[*w] = c << 4;
+                c = fmtscan_num(sce[*r + 1], 16);
+                if (c >= 16) return (errno = EINVAL, 0);
+                dst[*w] += c;
             }
+            ++*r;
         } else {
-            if (dst) dst[*w] = sce[*r];
+            const char esc[7] = "abtnvfr";
+            size_t n = byte_chr(esc, 7, sce[*r]);
+            if (n == 7) return (errno = EINVAL, 0);
+            if (dst) dst[*w] = 7 + n;
         }
         ++*w;
         ++*r;