8262351: Extra '0' in java.util.Formatter for '%012a' conversion with a sign character

Reviewed-by: bchristi, naoto
This commit is contained in:
Ian Graves 2021-03-10 22:46:32 +00:00 committed by Brent Christian
parent c6d74bd933
commit 6971c23a3a
2 changed files with 56 additions and 2 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -3554,7 +3554,12 @@ public final class Formatter implements Closeable, Flushable {
sb.append(upper ? "0X" : "0x");
if (f.contains(Flags.ZERO_PAD)) {
trailingZeros(sb, width - s.length() - 2);
int leadingCharacters = 2;
if(f.contains(Flags.LEADING_SPACE) ||
f.contains(Flags.PLUS) || neg) {
leadingCharacters = 3;
}
trailingZeros(sb, width - s.length() - leadingCharacters);
}
int idx = s.indexOf('p');