mirror of
https://github.com/zsh-users/zsh
synced 2024-09-16 06:30:26 +00:00
39886 based on 39877: Optimise arrdup to arrdup_max.
Only duplicate as much of the array as is needed.
This commit is contained in:
parent
49407686b4
commit
a1633e09a7
|
@ -1,5 +1,8 @@
|
||||||
2016-11-09 Peter Stephenson <p.stephenson@samsung.com>
|
2016-11-09 Peter Stephenson <p.stephenson@samsung.com>
|
||||||
|
|
||||||
|
* 39886 based on 39877 (Sebastian): Src/params.c, Src/utils.c:
|
||||||
|
add arrdup_max() so as not to duplicate entire arrays unnecessarily.
|
||||||
|
|
||||||
* Sebastian: 39875: Src/params.c, Src/string.c: add
|
* Sebastian: 39875: Src/params.c, Src/string.c: add
|
||||||
dupstring_glen() to avoid redundant strlen() calls.
|
dupstring_glen() to avoid redundant strlen() calls.
|
||||||
|
|
||||||
|
|
24
Src/params.c
24
Src/params.c
|
@ -2294,14 +2294,24 @@ getarrvalue(Value v)
|
||||||
v->start += arrlen(s);
|
v->start += arrlen(s);
|
||||||
if (v->end < 0)
|
if (v->end < 0)
|
||||||
v->end += arrlen(s) + 1;
|
v->end += arrlen(s) + 1;
|
||||||
if (arrlen_lt(s, v->start) || v->start < 0)
|
|
||||||
|
/* Null if 1) array too short, 2) index still negative */
|
||||||
|
if (arrlen_lt(s, v->start) || v->start < 0) {
|
||||||
s = arrdup(nular);
|
s = arrdup(nular);
|
||||||
else
|
} else if (v->end <= v->start) {
|
||||||
s = arrdup(s + v->start);
|
s = arrdup_max(s, 1);
|
||||||
if (v->end <= v->start)
|
s[0] = NULL;
|
||||||
s[0] = NULL;
|
} else {
|
||||||
else if (arrlen_ge(s, v->end - v->start))
|
/* Copy to a point before the end of the source array:
|
||||||
s[v->end - v->start] = NULL;
|
* arrdup_max will copy at most v->end - v->start elements,
|
||||||
|
* starting from v->start element. Original code said:
|
||||||
|
* s[v->end - v->start] = NULL
|
||||||
|
* which means that there are exactly the same number of
|
||||||
|
* elements as the value of the above *0-based* index.
|
||||||
|
*/
|
||||||
|
s = arrdup_max(s + v->start, v->end - v->start);
|
||||||
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
Src/utils.c
25
Src/utils.c
|
@ -4229,6 +4229,31 @@ arrdup(char **s)
|
||||||
return y;
|
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 **
|
mod_export char **
|
||||||
zarrdup(char **s)
|
zarrdup(char **s)
|
||||||
|
|
Loading…
Reference in a new issue