1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-25 05:10:28 +02:00

21967: add ${(#)...} substitution

This commit is contained in:
Peter Stephenson 2005-11-01 18:04:24 +00:00
parent f53996f14b
commit f42d15278d
3 changed files with 53 additions and 0 deletions

View file

@ -1,3 +1,8 @@
2005-11-01 Peter Stephenson <pws@csr.com>
* 21967 (docmentation tweaked): Doc/Zsh/expn.yo, Src/subst.c:
${(#)foo} uses matheval to produce characters.
2005-10-31 Wayne Davison <wayned@users.sourceforge.net> 2005-10-31 Wayne Davison <wayned@users.sourceforge.net>
* 21949 (modified): Src/zsh.h, Src/Zle/zle.h, Src/Zle/zle_misc.c, * 21949 (modified): Src/zsh.h, Src/Zle/zle.h, Src/Zle/zle_misc.c,

View file

@ -632,6 +632,11 @@ means the same thing as the more readable `(tt(%%qqq))'. The
following flags are supported: following flags are supported:
startitem() startitem()
item(tt(#))(
Evaluate the resulting words as numeric expressions and output the
characters corresponding to the resulting integer. Note that this form is
entirely distinct from use of the tt(#) without parentheses.
)
item(tt(%))( item(tt(%))(
Expand all tt(%) escapes in the resulting words in the same way as in in Expand all tt(%) escapes in the resulting words in the same way as in in
prompts (see noderef(Prompt Expansion)). If this flag is given twice, prompts (see noderef(Prompt Expansion)). If this flag is given twice,

View file

@ -914,6 +914,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
* on if qt is passed down. * on if qt is passed down.
*/ */
int globsubst = isset(GLOBSUBST); int globsubst = isset(GLOBSUBST);
/*
* Indicates ${(#)...}.
*/
int evalchar = 0;
/* /*
* Indicates ${#pm}, massaged by whichlen which is set by * Indicates ${#pm}, massaged by whichlen which is set by
* the (c), (w), and (W) flags to indicate how we take the length. * the (c), (w), and (W) flags to indicate how we take the length.
@ -1320,6 +1324,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
unique = 1; unique = 1;
break; break;
case '#':
case Pound:
evalchar = 1;
break;
default: default:
flagerr: flagerr:
zerr("error in flags", NULL, 0); zerr("error in flags", NULL, 0);
@ -2233,6 +2242,40 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
} }
if (errflag) if (errflag)
return NULL; return NULL;
if (evalchar) {
/*
* Evaluate the value numerically and output the result as
* a character.
*
* Note this doesn't yet handle Unicode or multibyte characters:
* that will need handling more generally probably by
* an additional flag of some sort.
*/
zlong ires;
if (isarr) {
char **aval2, **avptr, **av2ptr;
aval2 = (char **)zhalloc((arrlen(aval)+1)*sizeof(char *));
for (avptr = aval, av2ptr = aval2; *avptr; avptr++, av2ptr++)
{
ires = mathevali(*avptr);
if (errflag)
return NULL;
*av2ptr = zhalloc(2);
sprintf(*av2ptr, "%c", (int)ires);
}
*av2ptr = NULL;
aval = aval2;
} else {
ires = mathevali(val);
if (errflag)
return NULL;
val = zhalloc(2);
sprintf(val, "%c", (int)ires);
}
}
/* /*
* This handles taking a length with ${#foo} and variations. * This handles taking a length with ${#foo} and variations.
* TODO: again. one might naively have thought this had the * TODO: again. one might naively have thought this had the