[PATCH] ash.c: speedup ${s:} substring (no quotes)

Alin Mr almr.oss at outlook.com
Wed Jul 28 08:40:01 UTC 2021


This trivial patch makes ${s:...} at least as fast as ${s#??..} in 
simple tests. It's probably faster for longer substrings, but then one 
wouldn't use ${s#"1024???s"} anyway -- one would switch away from sh. 
memmove could possibly be replaced by memcpy.

I haven't found much on testing (or contributing for that matter) on the 
website or in the source. `make test` produces a bunch of failures on 
master anyway, so I'm not sure how to test new code.

Signed-off-by: Alin Mr <almr.oss at outlook.com>
---
  shell/ash.c | 23 +++++++++++++++++------
  1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index b5947147a..df4d82e39 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -7198,14 +7198,25 @@ subevalvar(char *start, char *str, int strloc,
  		if ((unsigned)len > (orig_len - pos))
  			len = orig_len - pos;

-		for (vstr = startp; pos; vstr++, pos--) {
-			if (quotes && (unsigned char)*vstr == CTLESC)
-				vstr++;
+		if (! quotes) {
+			vstr = startp + pos;
+			pos = 0;
+		} else {
+			for (vstr = startp; pos; vstr++, pos--) {
+				if ((unsigned char)*vstr == CTLESC)
+					vstr++;
+			}
  		}
-		for (loc = startp; len; len--) {
-			if (quotes && (unsigned char)*vstr == CTLESC)
+		if (! quotes) {
+			memmove (loc, vstr, len);
+			loc = startp + len;
+			len = 0;
+		} else {
+			for (loc = startp; len; len--) {
+				if ((unsigned char)*vstr == CTLESC)
+					*loc++ = *vstr++;
  				*loc++ = *vstr++;
-			*loc++ = *vstr++;
+			}
  		}
  		*loc = '\0';
  		goto out;
-- 
2.30.2




More information about the busybox mailing list