From 51d1fa43f631e69a6f2377ac873be1b72ee1d476 Mon Sep 17 00:00:00 2001 From: antares Date: Sat, 11 May 2024 02:22:18 +0800 Subject: [PATCH] feat: use bit algorithm --- src/str.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/str.c b/src/str.c index c676119..8fc5cd8 100644 --- a/src/str.c +++ b/src/str.c @@ -148,13 +148,17 @@ bool count_skipped(const char *buf, size_t max_len, size_t *skipped, size_t *len } } else { skip_next = 0; - for (int j = 0; j < 32; j++) { - if ((escape_result >> j) & 0b1) { - *skipped += 1; - j++; - if (j > 31) { - skip_next = 1; - } + int last = 0; + uint32_t x = escape_result; + while (x) { + unsigned int y = x - 1; + if ((x >> 1) & last) { + x = x & y; // skipped + } else { + last = x ^ y; + x = x & y; + skipped += 1; + if (y == 0x7fffffff) skip_next = 1; } } }