From 5beee8b2d4d1a84ce0d53c0ea787d2714693aec1 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sun, 13 Jun 2021 07:15:27 -0400 Subject: [PATCH] [core] buffer_path_simplify() quick(er) path scan to detect (potential) need for path simplification (repeated '/' or "/.") before copying each char in string --- src/buffer.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 03d2a32f..e765b194 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -813,7 +813,22 @@ void buffer_path_simplify(buffer *b) *end = '/'; /*(end of path modified to avoid need to check '\0')*/ char *walk = out; - if (__builtin_expect( (*walk != '/'), 0)) { + if (__builtin_expect( (*walk == '/'), 1)) { + /* scan to detect (potential) need for path simplification + * (repeated '/' or "/.") */ + do { + if (*++walk == '.' || *walk == '/') + break; + do { ++walk; } while (*walk != '/'); + } while (walk != end); + if (__builtin_expect( (walk == end), 1)) { + /* common case: no repeated '/' or "/." */ + *end = '\0'; /* overwrite extra '/' added to end of path */ + return; + } + out = walk-1; + } + else { if (walk[0] == '.' && walk[1] == '/') *out = *++walk; else if (walk[0] == '.' && walk[1] == '.' && walk[2] == '/') @@ -822,8 +837,8 @@ void buffer_path_simplify(buffer *b) while (*++walk != '/') ; out = walk; } + ++walk; } - ++walk; while (walk <= end) { /* previous char is '/' at this point (or start of string w/o '/') */