Merge pull request #7830 from volzhs/str-format-3

Fix zero padding formatting
This commit is contained in:
Rémi Verschelde 2017-02-26 20:15:10 +01:00 committed by GitHub
commit 9c2542cf26

View file

@ -4101,12 +4101,8 @@ String String::sprintf(const Array& values, bool* error) const {
case 'X': base = 16; capitalize = true; break; case 'X': base = 16; capitalize = true; break;
} }
// Get basic number. // Get basic number.
String str = String::num_int64(value, base, capitalize); String str = String::num_int64(ABS(value), base, capitalize);
int number_len = str.length();
// Sign.
if (show_sign && value >= 0) {
str = str.insert(0, "+");
}
// Padding. // Padding.
String pad_char = pad_with_zeroes ? String("0") : String(" "); String pad_char = pad_with_zeroes ? String("0") : String(" ");
@ -4116,6 +4112,13 @@ String String::sprintf(const Array& values, bool* error) const {
str = str.lpad(min_chars, pad_char); str = str.lpad(min_chars, pad_char);
} }
// Sign.
if (show_sign && value >= 0) {
str = str.insert(pad_with_zeroes?0:str.length()-number_len, "+");
} else if (value < 0) {
str = str.insert(pad_with_zeroes?0:str.length()-number_len, "-");
}
formatted += str; formatted += str;
++value_index; ++value_index;
in_format = false; in_format = false;