about summary refs log tree commit diff
path: root/Src/utils.c
diff options
context:
space:
mode:
authorPeter Stephenson <pws@zsh.org>2016-11-09 11:54:57 +0000
committerPeter Stephenson <pws@zsh.org>2016-11-09 11:54:57 +0000
commita1633e09a761b9135a0a7084d2489d359a004e5a (patch)
tree2ba8b4f85807d7f2573eebf5be6bc976495780c7 /Src/utils.c
parent49407686b47fed0e4810e0ba6127a9589c05b68b (diff)
downloadzsh-a1633e09a761b9135a0a7084d2489d359a004e5a.tar.gz
zsh-a1633e09a761b9135a0a7084d2489d359a004e5a.tar.xz
zsh-a1633e09a761b9135a0a7084d2489d359a004e5a.zip
39886 based on 39877: Optimise arrdup to arrdup_max.
Only duplicate as much of the array as is needed.
Diffstat (limited to 'Src/utils.c')
-rw-r--r--Src/utils.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/Src/utils.c b/Src/utils.c
index 93e757ce8..3d535b85c 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -4229,6 +4229,31 @@ arrdup(char **s)
     return y;
 }
 
+/* Duplicate at most max elements of the array s with heap memory */
+
+/**/
+mod_export char **
+arrdup_max(char **s, unsigned max)
+{
+    char **x, **y, **send;
+    int len;
+
+    len = arrlen(s);
+
+    /* Limit has sense only if not equal to len */
+    if (max > len)
+        max = len;
+
+    y = x = (char **) zhalloc(sizeof(char *) * (max + 1));
+
+    send = s + max;
+    while (s < send)
+	*x++ = dupstring(*s++);
+    *x = NULL;
+
+    return y;
+}
+
 /**/
 mod_export char **
 zarrdup(char **s)