if typeset -i -Z<n>, pad the value not the base

This commit is contained in:
tg 2008-04-19 21:04:09 +00:00
parent 3e728f7797
commit cfee2b2a61
2 changed files with 31 additions and 3 deletions

16
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.175 2008/04/16 21:56:00 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.176 2008/04/19 21:04:08 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -4277,6 +4277,20 @@ stdin:
expected-stdout:
<0hall0 > < 0hall0> <hall0 > <00000hall0> <0000 hallo>
---
name: typeset-padding-2
description:
Check if base-!10 integers are padded right
stdin:
typeset -Uui16 -L9 ln=16#1
typeset -Uui16 -R9 rn=16#1
typeset -Uui16 -Z9 zn=16#1
typeset -L9 ls=16#1
typeset -R9 rs=16#1
typeset -Z9 zs=16#1
print "<$ln> <$rn> <$zn> <$ls> <$rs> <$zs>"
expected-stdout:
<16#1 > < 16#1> <16#000001> <16#1 > < 16#1> <0000016#1>
---
name: utf8bom-1
description:
Check that the UTF-8 Byte Order Mark is ignored as the first

18
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.52 2008/04/19 17:21:55 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.53 2008/04/19 21:04:09 tg Exp $");
/*
* Variables
@ -508,6 +508,7 @@ formatstr(struct tbl *vp, const char *s)
if (vp->flag & RJUST) {
const char *qq = s;
int n = 0;
for (i = 0; i < slen; ++i)
utf_widthadj(qq, &qq);
@ -516,13 +517,26 @@ formatstr(struct tbl *vp, const char *s)
--qq;
--slen;
}
if (vp->flag & ZEROFIL && vp->flag & INTEGER) {
if (s[1] == '#')
n = 2;
else if (s[2] == '#')
n = 3;
if (vp->u2.field <= n)
n = 0;
}
if (n) {
memcpy(p, s, n);
s += n;
}
while (slen > vp->u2.field) {
utf_widthadj(s, &s);
--slen;
}
if (vp->u2.field - slen)
memset(p, (vp->flag & ZEROFIL) ? '0' : ' ',
memset(p + n, (vp->flag & ZEROFIL) ? '0' : ' ',
vp->u2.field - slen);
slen -= n;
shf_snprintf(p + vp->u2.field - slen,
psiz - (vp->u2.field - slen),
"%.*s", slen, s);