diff options
author | Peter Stephenson <pws@zsh.org> | 2016-11-09 11:54:57 +0000 |
---|---|---|
committer | Peter Stephenson <pws@zsh.org> | 2016-11-09 11:54:57 +0000 |
commit | a1633e09a761b9135a0a7084d2489d359a004e5a (patch) | |
tree | 2ba8b4f85807d7f2573eebf5be6bc976495780c7 /Src/params.c | |
parent | 49407686b47fed0e4810e0ba6127a9589c05b68b (diff) | |
download | zsh-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/params.c')
-rw-r--r-- | Src/params.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/Src/params.c b/Src/params.c index 3f0179267..772345bd1 100644 --- a/Src/params.c +++ b/Src/params.c @@ -2294,14 +2294,24 @@ getarrvalue(Value v) v->start += arrlen(s); if (v->end < 0) 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); - else - s = arrdup(s + v->start); - if (v->end <= v->start) - s[0] = NULL; - else if (arrlen_ge(s, v->end - v->start)) - s[v->end - v->start] = NULL; + } else if (v->end <= v->start) { + s = arrdup_max(s, 1); + s[0] = NULL; + } else { + /* Copy to a point before the end of the source array: + * 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; } |