From: Hugo van der Sanden <hv@crypt.org>
Date: Mon, 9 Feb 2004 03:21:21 +0000 (+0000)
Subject: Re: [perl #26073] sprintf miscounts padding when format is utf8
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6c94ec8ba7f0b5a05c74409397b9f1735413262d;p=p5sagit%2Fp5-mst-13.2.git

Re: [perl #26073] sprintf miscounts padding when format is utf8
Message-Id: <200402090321.i193LL907950@zen.crypt.org>

p4raw-id: //depot/perl@22292
---

diff --git a/sv.c b/sv.c
index 6e64702..1b8760a 100644
--- 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;
 
diff --git a/t/op/sprintf2.t b/t/op/sprintf2.t
index fef25f1..669938b 100644
--- a/t/op/sprintf2.t
+++ b/t/op/sprintf2.t
@@ -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)
+	);
+}