Merge pull request #1396 from Spooner/fix_sprintf_errors
Fix sprintf errors
This commit is contained in:
commit
0302ea5b82
5 changed files with 102 additions and 110 deletions
|
|
@ -3573,8 +3573,8 @@ String String::lpad(int min_length, const String& character) const {
|
|||
// sprintf is implemented in GDScript via:
|
||||
// "fish %s pie" % "frog"
|
||||
// "fish %s %d pie" % ["frog", 12]
|
||||
String String::sprintf(const Array& values) const {
|
||||
|
||||
// In case of an error, the string returned is the error description and "error" is true.
|
||||
String String::sprintf(const Array& values, bool* error) const {
|
||||
String formatted;
|
||||
CharType* self = (CharType*)c_str();
|
||||
int num_items = values.size();
|
||||
|
|
@ -3587,6 +3587,7 @@ String String::sprintf(const Array& values) const {
|
|||
bool left_justified;
|
||||
bool show_sign;
|
||||
|
||||
*error = true;
|
||||
|
||||
for (; *self; self++) {
|
||||
const CharType c = *self;
|
||||
|
|
@ -3603,13 +3604,11 @@ String String::sprintf(const Array& values) const {
|
|||
case 'x': // Hexadecimal (lowercase)
|
||||
case 'X': { // Hexadecimal (uppercase)
|
||||
if (value_index >= values.size()) {
|
||||
ERR_EXPLAIN("not enough arguments for format string");
|
||||
ERR_FAIL_V("");
|
||||
return "not enough arguments for format string";
|
||||
}
|
||||
|
||||
if (!values[value_index].is_num()) {
|
||||
ERR_EXPLAIN("a number is required");
|
||||
ERR_FAIL_V("");
|
||||
return "a number is required";
|
||||
}
|
||||
|
||||
int64_t value = values[value_index];
|
||||
|
|
@ -3645,13 +3644,11 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
case 'f': { // Float
|
||||
if (value_index >= values.size()) {
|
||||
ERR_EXPLAIN("not enough arguments for format string");
|
||||
ERR_FAIL_V("");
|
||||
return "not enough arguments for format string";
|
||||
}
|
||||
|
||||
if (!values[value_index].is_num()) {
|
||||
ERR_EXPLAIN("a number is required");
|
||||
ERR_FAIL_V("");
|
||||
return "a number is required";
|
||||
}
|
||||
|
||||
double value = values[value_index];
|
||||
|
|
@ -3680,8 +3677,7 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
case 's': { // String
|
||||
if (value_index >= values.size()) {
|
||||
ERR_EXPLAIN("not enough arguments for format string");
|
||||
ERR_FAIL_V("");
|
||||
return "not enough arguments for format string";
|
||||
}
|
||||
|
||||
String str = values[value_index];
|
||||
|
|
@ -3699,8 +3695,7 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
case 'c': {
|
||||
if (value_index >= values.size()) {
|
||||
ERR_EXPLAIN("not enough arguments for format string");
|
||||
ERR_FAIL_V("");
|
||||
return "not enough arguments for format string";
|
||||
}
|
||||
|
||||
// Convert to character.
|
||||
|
|
@ -3708,22 +3703,18 @@ String String::sprintf(const Array& values) const {
|
|||
if (values[value_index].is_num()) {
|
||||
int value = values[value_index];
|
||||
if (value < 0) {
|
||||
ERR_EXPLAIN("unsigned byte integer is lower than maximum")
|
||||
ERR_FAIL_V("");
|
||||
return "unsigned byte integer is lower than maximum";
|
||||
} else if (value > 255) {
|
||||
ERR_EXPLAIN("unsigned byte integer is greater than maximum")
|
||||
ERR_FAIL_V("");
|
||||
return "unsigned byte integer is greater than maximum";
|
||||
}
|
||||
str = chr(values[value_index]);
|
||||
} else if (values[value_index].get_type() == Variant::STRING) {
|
||||
str = values[value_index];
|
||||
if (str.length() != 1) {
|
||||
ERR_EXPLAIN("%c requires number or single-character string");
|
||||
ERR_FAIL_V("");
|
||||
return "%c requires number or single-character string";
|
||||
}
|
||||
} else {
|
||||
ERR_EXPLAIN("%c requires number or single-character string");
|
||||
ERR_FAIL_V("");
|
||||
return "%c requires number or single-character string";
|
||||
}
|
||||
|
||||
// Padding.
|
||||
|
|
@ -3764,8 +3755,7 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
case '.': { // Float separtor.
|
||||
if (in_decimals) {
|
||||
ERR_EXPLAIN("too many decimal points in format");
|
||||
ERR_FAIL_V("");
|
||||
return "too many decimal points in format";
|
||||
}
|
||||
in_decimals = true;
|
||||
min_decimals = 0; // We want to add the value manually.
|
||||
|
|
@ -3774,13 +3764,11 @@ String String::sprintf(const Array& values) const {
|
|||
|
||||
case '*': { // Dyanmic width, based on value.
|
||||
if (value_index >= values.size()) {
|
||||
ERR_EXPLAIN("not enough arguments for format string");
|
||||
ERR_FAIL_V("");
|
||||
return "not enough arguments for format string";
|
||||
}
|
||||
|
||||
if (!values[value_index].is_num()) {
|
||||
ERR_EXPLAIN("* wants number");
|
||||
ERR_FAIL_V("");
|
||||
return "* wants number";
|
||||
}
|
||||
|
||||
int size = values[value_index];
|
||||
|
|
@ -3796,8 +3784,7 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
|
||||
default: {
|
||||
ERR_EXPLAIN("unsupported format character");
|
||||
ERR_FAIL_V("");
|
||||
return "unsupported format character";
|
||||
}
|
||||
}
|
||||
} else { // Not in format string.
|
||||
|
|
@ -3819,14 +3806,13 @@ String String::sprintf(const Array& values) const {
|
|||
}
|
||||
|
||||
if (in_format) {
|
||||
ERR_EXPLAIN("incomplete format");
|
||||
ERR_FAIL_V("");
|
||||
return "incomplete format";
|
||||
}
|
||||
|
||||
if (value_index != values.size()) {
|
||||
ERR_EXPLAIN("not all arguments converted during string formatting");
|
||||
ERR_FAIL_V("");
|
||||
return "not all arguments converted during string formatting";
|
||||
}
|
||||
|
||||
*error = false;
|
||||
return formatted;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue