mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-01-01 05:16:05 +01:00
39995 (from 39977): Optimise string parameter assignment.
If setter is the standard one and string length is unchnaged we can copy into place.
This commit is contained in:
parent
a2426747da
commit
368884a3aa
3 changed files with 174 additions and 8 deletions
|
@ -1,5 +1,8 @@
|
||||||
2016-11-20 Peter Stephenson <p.w.stephenson@ntlworld.com>
|
2016-11-20 Peter Stephenson <p.w.stephenson@ntlworld.com>
|
||||||
|
|
||||||
|
* 39995 (from 39977): Src/params.c, Test/A06assign.ztst:
|
||||||
|
optimisation of string assignment if length is unchanged.
|
||||||
|
|
||||||
* Mikel Ward: 39978: Doc/Zsh/jobs.yo: more accurate
|
* Mikel Ward: 39978: Doc/Zsh/jobs.yo: more accurate
|
||||||
documentation of searching for job by string.
|
documentation of searching for job by string.
|
||||||
|
|
||||||
|
|
39
Src/params.c
39
Src/params.c
|
@ -2436,9 +2436,10 @@ assignstrvalue(Value v, char *val, int flags)
|
||||||
v->pm->width = strlen(val);
|
v->pm->width = strlen(val);
|
||||||
} else {
|
} else {
|
||||||
char *z, *x;
|
char *z, *x;
|
||||||
int zlen;
|
int zlen, vlen, newsize;
|
||||||
|
|
||||||
z = dupstring_glen(v->pm->gsu.s->getfn(v->pm), (unsigned*) &zlen);
|
z = v->pm->gsu.s->getfn(v->pm);
|
||||||
|
zlen = strlen(z);
|
||||||
|
|
||||||
if ((v->flags & VALFLAG_INV) && unset(KSHARRAYS))
|
if ((v->flags & VALFLAG_INV) && unset(KSHARRAYS))
|
||||||
v->start--, v->end--;
|
v->start--, v->end--;
|
||||||
|
@ -2469,12 +2470,34 @@ assignstrvalue(Value v, char *val, int flags)
|
||||||
}
|
}
|
||||||
else if (v->end > zlen)
|
else if (v->end > zlen)
|
||||||
v->end = zlen;
|
v->end = zlen;
|
||||||
x = (char *) zalloc(v->start + strlen(val) + zlen - v->end + 1);
|
|
||||||
strncpy(x, z, v->start);
|
vlen = strlen(val);
|
||||||
strcpy(x + v->start, val);
|
/* Characters preceding start index +
|
||||||
strcat(x + v->start, z + v->end);
|
characters of what is assigned +
|
||||||
v->pm->gsu.s->setfn(v->pm, x);
|
characters following end index */
|
||||||
zsfree(val);
|
newsize = v->start + vlen + (zlen - v->end);
|
||||||
|
|
||||||
|
/* Does new size differ? */
|
||||||
|
if (newsize != zlen || v->pm->gsu.s->setfn != strsetfn) {
|
||||||
|
x = (char *) zalloc(newsize + 1);
|
||||||
|
strncpy(x, z, v->start);
|
||||||
|
strcpy(x + v->start, val);
|
||||||
|
strcat(x + v->start, z + v->end);
|
||||||
|
v->pm->gsu.s->setfn(v->pm, x);
|
||||||
|
} else {
|
||||||
|
Param pm = v->pm;
|
||||||
|
/* Size doesn't change, can limit actions to only
|
||||||
|
* overwriting bytes in already allocated string */
|
||||||
|
strncpy(z + v->start, val, vlen);
|
||||||
|
/* Implement remainder of strsetfn */
|
||||||
|
if (!(pm->node.flags & PM_HASHELEM) &&
|
||||||
|
((pm->node.flags & PM_NAMEDDIR) ||
|
||||||
|
isset(AUTONAMEDIRS))) {
|
||||||
|
pm->node.flags |= PM_NAMEDDIR;
|
||||||
|
adduserdir(pm->node.nam, z, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
zsfree(val);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PM_INTEGER:
|
case PM_INTEGER:
|
||||||
|
|
|
@ -489,3 +489,143 @@
|
||||||
print $array
|
print $array
|
||||||
0:slice beyond length of array
|
0:slice beyond length of array
|
||||||
>FIRST
|
>FIRST
|
||||||
|
|
||||||
|
# tests of string assignments
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[1]=x
|
||||||
|
print $a
|
||||||
|
0:overwrite first character in string
|
||||||
|
>xbc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[2]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite middle character in string
|
||||||
|
>axc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[3]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite last character in string
|
||||||
|
>abx
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-1]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -1 character in string
|
||||||
|
>abx
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-2]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -2 character (middle) in string
|
||||||
|
>axc
|
||||||
|
|
||||||
|
a="ab"
|
||||||
|
a[-2]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -2 character (first) in string
|
||||||
|
>xb
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-3]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -3 character (first) in string
|
||||||
|
>xbc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-4]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -4 character (before first) in string
|
||||||
|
>xabc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-5]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite -5 character (before-before first) in string
|
||||||
|
>xabc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-4,0]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-4,0] characters (before first) in string
|
||||||
|
>xabc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-4,-4]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-4,-4] character (before first) in string
|
||||||
|
>xabc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-40,-30]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-40,-30] characters (far before first) in string
|
||||||
|
>xabc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-40,1]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-40,1] characters in short string
|
||||||
|
>xbc
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-40,40]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-40,40] characters in short string
|
||||||
|
>x
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[2,40]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [2,40] characters in short string
|
||||||
|
>ax
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[2,-1]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [2,-1] characters in short string
|
||||||
|
>ax
|
||||||
|
|
||||||
|
a="abc"
|
||||||
|
a[-2,-1]="x"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-2,-1] characters in short string
|
||||||
|
>ax
|
||||||
|
|
||||||
|
a="a"
|
||||||
|
a[-1]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-1] character with "xx"
|
||||||
|
>xx
|
||||||
|
|
||||||
|
a="a"
|
||||||
|
a[-2]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-2] character (before first) with "xx"
|
||||||
|
>xxa
|
||||||
|
|
||||||
|
a="a"
|
||||||
|
a[2]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [2] character (after last) with "xx"
|
||||||
|
>axx
|
||||||
|
|
||||||
|
a=""
|
||||||
|
a[1]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [1] character (string: "") with "xx"
|
||||||
|
>xx
|
||||||
|
|
||||||
|
a=""
|
||||||
|
a[-1]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [-1] character (string: "") with "xx"
|
||||||
|
>xx
|
||||||
|
|
||||||
|
a=""
|
||||||
|
a[2]="xx"
|
||||||
|
print $a
|
||||||
|
0:overwrite [2] character (string: "") with "xx"
|
||||||
|
>xx
|
||||||
|
|
Loading…
Reference in a new issue