Fix left aligned integer sign in string formatting
This commit is contained in:
parent
f12f41d343
commit
767134fd8d
|
@ -4213,7 +4213,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
int min_chars = 0;
|
||||
int min_decimals = 0;
|
||||
bool in_decimals = false;
|
||||
bool pad_with_zeroes = false;
|
||||
bool pad_with_zeros = false;
|
||||
bool left_justified = false;
|
||||
bool show_sign = false;
|
||||
|
||||
|
@ -4264,7 +4264,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
|
||||
// Padding.
|
||||
int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
|
||||
String pad_char = pad_with_zeroes ? String("0") : String(" ");
|
||||
String pad_char = pad_with_zeros ? String("0") : String(" ");
|
||||
if (left_justified) {
|
||||
str = str.rpad(pad_chars_count, pad_char);
|
||||
} else {
|
||||
|
@ -4272,10 +4272,13 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
}
|
||||
|
||||
// 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, "-");
|
||||
if (show_sign || value < 0) {
|
||||
String sign_char = value < 0 ? "-" : "+";
|
||||
if (left_justified) {
|
||||
str = str.insert(0, sign_char);
|
||||
} else {
|
||||
str = str.insert(pad_with_zeros ? 0 : str.length() - number_len, sign_char);
|
||||
}
|
||||
}
|
||||
|
||||
formatted += str;
|
||||
|
@ -4304,13 +4307,9 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
|
||||
// Padding. Leave room for sign later if required.
|
||||
int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
|
||||
String pad_char = pad_with_zeroes ? String("0") : String(" ");
|
||||
String pad_char = pad_with_zeros ? String("0") : String(" ");
|
||||
if (left_justified) {
|
||||
if (pad_with_zeroes) {
|
||||
return "left justification cannot be used with zeros as the padding";
|
||||
} else {
|
||||
str = str.rpad(pad_chars_count, pad_char);
|
||||
}
|
||||
str = str.rpad(pad_chars_count, pad_char);
|
||||
} else {
|
||||
str = str.lpad(pad_chars_count, pad_char);
|
||||
}
|
||||
|
@ -4321,7 +4320,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
if (left_justified) {
|
||||
str = str.insert(0, sign_char);
|
||||
} else {
|
||||
str = str.insert(pad_with_zeroes ? 0 : str.length() - initial_len, sign_char);
|
||||
str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4408,7 +4407,11 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
min_decimals += n;
|
||||
} else {
|
||||
if (c == '0' && min_chars == 0) {
|
||||
pad_with_zeroes = true;
|
||||
if (left_justified) {
|
||||
WARN_PRINT("'0' flag ignored with '-' flag in string format");
|
||||
} else {
|
||||
pad_with_zeros = true;
|
||||
}
|
||||
} else {
|
||||
min_chars *= 10;
|
||||
min_chars += n;
|
||||
|
@ -4457,7 +4460,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||
// Back to defaults:
|
||||
min_chars = 0;
|
||||
min_decimals = 6;
|
||||
pad_with_zeroes = false;
|
||||
pad_with_zeros = false;
|
||||
left_justified = false;
|
||||
show_sign = false;
|
||||
in_decimals = false;
|
||||
|
|
|
@ -555,6 +555,42 @@ bool test_28() {
|
|||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Negative int left padded with spaces.
|
||||
format = "fish %5d frog";
|
||||
args.clear();
|
||||
args.push_back(-5);
|
||||
output = format.sprintf(args, &error);
|
||||
success = (output == String("fish -5 frog") && !error);
|
||||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Negative int left padded with zeros.
|
||||
format = "fish %05d frog";
|
||||
args.clear();
|
||||
args.push_back(-5);
|
||||
output = format.sprintf(args, &error);
|
||||
success = (output == String("fish -0005 frog") && !error);
|
||||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Negative int right padded with spaces.
|
||||
format = "fish %-5d frog";
|
||||
args.clear();
|
||||
args.push_back(-5);
|
||||
output = format.sprintf(args, &error);
|
||||
success = (output == String("fish -5 frog") && !error);
|
||||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Negative int right padded with zeros. (0 ignored)
|
||||
format = "fish %-05d frog";
|
||||
args.clear();
|
||||
args.push_back(-5);
|
||||
output = format.sprintf(args, &error);
|
||||
success = (output == String("fish -5 frog") && !error);
|
||||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Hex (lower)
|
||||
format = "fish %x frog";
|
||||
args.clear();
|
||||
|
@ -656,6 +692,15 @@ bool test_28() {
|
|||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
// Negative real right padded with zeros. (0 ignored)
|
||||
format = "fish %-011f frog";
|
||||
args.clear();
|
||||
args.push_back(-99.99);
|
||||
output = format.sprintf(args, &error);
|
||||
success = (output == String("fish -99.990000 frog") && !error);
|
||||
OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
|
||||
state = state && success;
|
||||
|
||||
/////// Strings.
|
||||
|
||||
// String
|
||||
|
|
Loading…
Reference in New Issue