This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #26073] sprintf miscounts padding when format is utf8
authorHugo van der Sanden <hv@crypt.org>
Mon, 9 Feb 2004 03:21:21 +0000 (03:21 +0000)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Mon, 9 Feb 2004 21:17:40 +0000 (21:17 +0000)
Message-Id: <200402090321.i193LL907950@zen.crypt.org>

p4raw-id: //depot/perl@22292

sv.c
t/op/sprintf2.t

diff --git a/sv.c b/sv.c
index 6e64702..1b8760a 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -9507,6 +9507,9 @@ Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV
            continue;   /* not "break" */
        }
 
+       /* calculate width before utf8_upgrade changes it */
+       have = esignlen + zeros + elen;
+
        if (is_utf8 != has_utf8) {
             if (is_utf8) {
                  if (SvCUR(sv))
@@ -9530,7 +9533,6 @@ Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV
                "Newline in left-justified string for %sprintf",
                        (PL_op->op_type == OP_PRTF) ? "" : "s");
        
-       have = esignlen + zeros + elen;
        need = (have > width ? have : width);
        gap = need - have;
 
index fef25f1..669938b 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require './test.pl';
 }   
 
-plan tests => 2;
+plan tests => 3;
 
 is(
     sprintf("%.40g ",0.01),
@@ -18,3 +18,11 @@ is(
     sprintf("%.40f", 0.01)." ",
     q(the sprintf "%.<number>f" optimization)
 );
+{
+       chop(my $utf8_format = "%-3s\x{100}");
+       is(
+               sprintf($utf8_format, "\xe4"),
+               "\xe4  ",
+               q(width calculation under utf8 upgrade)
+       );
+}