Reimplemented sprintf. Moved it to Array.

This commit is contained in:
Relintai 2023-12-31 19:50:23 +01:00
parent c87f9fd541
commit 96ae4a1f7e
6 changed files with 297 additions and 301 deletions

View File

@ -1657,299 +1657,6 @@ String String::rpad(int min_length, const String &character) const {
return s;
}
// sprintf is implemented in GDScript via:
// "fish %s pie" % "frog"
// "fish %s %d pie" % ["frog", 12]
// 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 *)get_data();
bool in_format = false;
int value_index = 0;
int min_chars = 0;
int min_decimals = 0;
bool in_decimals = false;
bool pad_with_zeros = false;
bool left_justified = false;
bool show_sign = false;
if (error) {
*error = true;
}
for (; *self; self++) {
const CharType c = *self;
if (in_format) { // We have % - let's see what else we get.
switch (c) {
case '%': { // Replace %% with %
formatted += chr(c);
in_format = false;
break;
}
case 'd': // Integer (signed)
case 'o': // Octal
case 'x': // Hexadecimal (lowercase)
case 'X': { // Hexadecimal (uppercase)
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "a number is required";
}
int64_t value = values[value_index];
int base = 16;
bool capitalize = false;
switch (c) {
case 'd':
base = 10;
break;
case 'o':
base = 8;
break;
case 'x':
break;
case 'X':
base = 16;
capitalize = true;
break;
}
// Get basic number.
String str = String::num_int64(ABS(value), base, capitalize);
int number_len = str.length();
// Padding.
int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
String pad_char = pad_with_zeros ? String("0") : String(" ");
if (left_justified) {
str = str.rpad(pad_chars_count, pad_char);
} else {
str = str.lpad(pad_chars_count, pad_char);
}
// Sign.
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;
++value_index;
in_format = false;
break;
}
case 'f': { // Float
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "a number is required";
}
double value = values[value_index];
bool is_negative = (value < 0);
String str = String::num(ABS(value), min_decimals);
bool not_numeric = isinf(value) || isnan(value);
// Pad decimals out.
if (!not_numeric) {
str = str.pad_decimals(min_decimals);
}
int initial_len = str.length();
// 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_zeros && !not_numeric) ? String("0") : String(" "); // Never pad NaN or inf with zeros
if (left_justified) {
str = str.rpad(pad_chars_count, pad_char);
} else {
str = str.lpad(pad_chars_count, pad_char);
}
// Add sign if needed.
if (show_sign || is_negative) {
String sign_char = is_negative ? "-" : "+";
if (left_justified) {
str = str.insert(0, sign_char);
} else {
str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
}
}
formatted += str;
++value_index;
in_format = false;
break;
}
case 's': { // String
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
String str = values[value_index];
// Padding.
if (left_justified) {
str = str.rpad(min_chars);
} else {
str = str.lpad(min_chars);
}
formatted += str;
++value_index;
in_format = false;
break;
}
case 'c': {
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
// Convert to character.
String str;
if (values[value_index].is_num()) {
int value = values[value_index];
if (value < 0) {
return "unsigned integer is lower than minimum";
} else if (value >= 0xd800 && value <= 0xdfff) {
return "unsigned integer is invalid Unicode character";
} else if (value > 0x10ffff) {
return "unsigned 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) {
return "%c requires number or single-character string";
}
} else {
return "%c requires number or single-character string";
}
// Padding.
if (left_justified) {
str = str.rpad(min_chars);
} else {
str = str.lpad(min_chars);
}
formatted += str;
++value_index;
in_format = false;
break;
}
case '-': { // Left justify
left_justified = true;
break;
}
case '+': { // Show + if positive.
show_sign = true;
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
int n = c - '0';
if (in_decimals) {
min_decimals *= 10;
min_decimals += n;
} else {
if (c == '0' && min_chars == 0) {
if (left_justified) {
LOG_WARN("'0' flag ignored with '-' flag in string format");
} else {
pad_with_zeros = true;
}
} else {
min_chars *= 10;
min_chars += n;
}
}
break;
}
case '.': { // Float separator.
if (in_decimals) {
return "too many decimal points in format";
}
in_decimals = true;
min_decimals = 0; // We want to add the value manually.
break;
}
case '*': { // Dynamic width, based on value.
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "* wants number";
}
int size = values[value_index];
if (in_decimals) {
min_decimals = size;
} else {
min_chars = size;
}
++value_index;
break;
}
default: {
return "unsupported format character";
}
}
} else { // Not in format string.
switch (c) {
case '%':
in_format = true;
// Back to defaults:
min_chars = 0;
min_decimals = 6;
pad_with_zeros = false;
left_justified = false;
show_sign = false;
in_decimals = false;
break;
default:
formatted += chr(c);
}
}
}
if (in_format) {
return "incomplete format";
}
if (value_index != values.size()) {
return "not all arguments converted during string formatting";
}
if (error) {
*error = false;
}
return formatted;
}
*/
String String::quote(String quotechar) const {
return quotechar + *this + quotechar;
}

View File

@ -316,6 +316,7 @@ public:
String lpad(int min_length, const String &character = " ") const;
String rpad(int min_length, const String &character = " ") const;
//Moved this to Array to simplify the library's design
//String sprintf(const Array &values, bool *error) const;
String quote(String quotechar = "\"") const;

View File

@ -9,6 +9,7 @@
#include "core/vector.h"
#include "object/object.h"
#include "object/variant.h"
#include "core/ustring.h"
class ArrayPrivate {
public:
@ -456,6 +457,295 @@ const void *Array::id() const {
return _p;
}
String Array::sprintf(const String &p_format, bool *error) const {
String formatted;
CharType *self = (CharType *)p_format.get_data();
bool in_format = false;
int value_index = 0;
int min_chars = 0;
int min_decimals = 0;
bool in_decimals = false;
bool pad_with_zeros = false;
bool left_justified = false;
bool show_sign = false;
const Array &values = *this;
if (error) {
*error = true;
}
for (; *self; self++) {
const CharType c = *self;
if (in_format) { // We have % - let's see what else we get.
switch (c) {
case '%': { // Replace %% with %
formatted += p_format.chr(c);
in_format = false;
break;
}
case 'd': // Integer (signed)
case 'o': // Octal
case 'x': // Hexadecimal (lowercase)
case 'X': { // Hexadecimal (uppercase)
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "a number is required";
}
int64_t value = values[value_index];
int base = 16;
bool capitalize = false;
switch (c) {
case 'd':
base = 10;
break;
case 'o':
base = 8;
break;
case 'x':
break;
case 'X':
base = 16;
capitalize = true;
break;
}
// Get basic number.
String str = String::num_int64(ABS(value), base, capitalize);
int number_len = str.length();
// Padding.
int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
String pad_char = pad_with_zeros ? String("0") : String(" ");
if (left_justified) {
str = str.rpad(pad_chars_count, pad_char);
} else {
str = str.lpad(pad_chars_count, pad_char);
}
// Sign.
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;
++value_index;
in_format = false;
break;
}
case 'f': { // Float
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "a number is required";
}
double value = values[value_index];
bool is_negative = (value < 0);
String str = String::num(ABS(value), min_decimals);
bool not_numeric = isinf(value) || isnan(value);
// Pad decimals out.
if (!not_numeric) {
str = str.pad_decimals(min_decimals);
}
int initial_len = str.length();
// 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_zeros && !not_numeric) ? String("0") : String(" "); // Never pad NaN or inf with zeros
if (left_justified) {
str = str.rpad(pad_chars_count, pad_char);
} else {
str = str.lpad(pad_chars_count, pad_char);
}
// Add sign if needed.
if (show_sign || is_negative) {
String sign_char = is_negative ? "-" : "+";
if (left_justified) {
str = str.insert(0, sign_char);
} else {
str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
}
}
formatted += str;
++value_index;
in_format = false;
break;
}
case 's': { // String
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
String str = values[value_index];
// Padding.
if (left_justified) {
str = str.rpad(min_chars);
} else {
str = str.lpad(min_chars);
}
formatted += str;
++value_index;
in_format = false;
break;
}
case 'c': {
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
// Convert to character.
String str;
if (values[value_index].is_num()) {
int value = values[value_index];
if (value < 0) {
return "unsigned integer is lower than minimum";
} else if (value >= 0xd800 && value <= 0xdfff) {
return "unsigned integer is invalid Unicode character";
} else if (value > 0x10ffff) {
return "unsigned integer is greater than maximum";
}
str = p_format.chr(values[value_index]);
} else if (values[value_index].get_type() == Variant::STRING) {
str = values[value_index];
if (str.length() != 1) {
return "%c requires number or single-character string";
}
} else {
return "%c requires number or single-character string";
}
// Padding.
if (left_justified) {
str = str.rpad(min_chars);
} else {
str = str.lpad(min_chars);
}
formatted += str;
++value_index;
in_format = false;
break;
}
case '-': { // Left justify
left_justified = true;
break;
}
case '+': { // Show + if positive.
show_sign = true;
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
int n = c - '0';
if (in_decimals) {
min_decimals *= 10;
min_decimals += n;
} else {
if (c == '0' && min_chars == 0) {
if (left_justified) {
LOG_WARN("'0' flag ignored with '-' flag in string format");
} else {
pad_with_zeros = true;
}
} else {
min_chars *= 10;
min_chars += n;
}
}
break;
}
case '.': { // Float separator.
if (in_decimals) {
return "too many decimal points in format";
}
in_decimals = true;
min_decimals = 0; // We want to add the value manually.
break;
}
case '*': { // Dynamic width, based on value.
if (value_index >= values.size()) {
return "not enough arguments for format string";
}
if (!values[value_index].is_num()) {
return "* wants number";
}
int size = values[value_index];
if (in_decimals) {
min_decimals = size;
} else {
min_chars = size;
}
++value_index;
break;
}
default: {
return "unsupported format character";
}
}
} else { // Not in format string.
switch (c) {
case '%':
in_format = true;
// Back to defaults:
min_chars = 0;
min_decimals = 6;
pad_with_zeros = false;
left_justified = false;
show_sign = false;
in_decimals = false;
break;
default:
formatted += p_format.chr(c);
}
}
}
if (in_format) {
return "incomplete format";
}
if (value_index != values.size()) {
return "not all arguments converted during string formatting";
}
if (error) {
*error = false;
}
return formatted;
}
Array::Array(const Array &p_from) {
_p = nullptr;
_ref(p_from);

View File

@ -12,6 +12,7 @@ class Variant;
class ArrayPrivate;
class Object;
class StringName;
class String;
class Array {
mutable ArrayPrivate *_p;
@ -79,6 +80,8 @@ public:
const void *id() const;
String sprintf(const String &p_format, bool *error) const;
Array(const Array &p_from);
Array();
~Array();

View File

@ -3728,10 +3728,7 @@ String vformat(const String &p_text, const Variant &p1, const Variant &p2, const
}
bool error = false;
//TODO
//String fmt = p_text.sprintf(args, &error);
String fmt;
String fmt = args.sprintf(p_text, &error);
ERR_FAIL_COND_V_MSG(error, String(), fmt);

View File

@ -1260,14 +1260,12 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
if (p_b.type == ARRAY) {
// e.g. "frog %s %d" % ["fish", 12]
const Array *args = reinterpret_cast<const Array *>(p_b._data._mem);
//TODO
//result = format->sprintf(*args, &error);
result = args->sprintf(*format, &error);
} else {
// e.g. "frog %d" % 12
Array args;
args.push_back(p_b);
//TODO
//result = format->sprintf(args, &error);
result = args.sprintf(*format, &error);
}
r_valid = !error;
_RETURN(result);