use operator += (char32_t)
This prevents an allocation of a new string for every character. Additionally, use some static constants for padding and sign characters
This commit is contained in:
parent
e25f3c0d38
commit
67eb6bed2b
1 changed files with 14 additions and 9 deletions
|
@ -5321,6 +5321,11 @@ String String::lpad(int min_length, const String &character) const {
|
||||||
// "fish %s %d pie" % ["frog", 12]
|
// "fish %s %d pie" % ["frog", 12]
|
||||||
// In case of an error, the string returned is the error description and "error" is true.
|
// 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 String::sprintf(const Array &values, bool *error) const {
|
||||||
|
static const String ZERO("0");
|
||||||
|
static const String SPACE(" ");
|
||||||
|
static const String MINUS("-");
|
||||||
|
static const String PLUS("+");
|
||||||
|
|
||||||
String formatted;
|
String formatted;
|
||||||
char32_t *self = (char32_t *)get_data();
|
char32_t *self = (char32_t *)get_data();
|
||||||
bool in_format = false;
|
bool in_format = false;
|
||||||
|
@ -5343,7 +5348,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
if (in_format) { // We have % - let's see what else we get.
|
if (in_format) { // We have % - let's see what else we get.
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '%': { // Replace %% with %
|
case '%': { // Replace %% with %
|
||||||
formatted += chr(c);
|
formatted += c;
|
||||||
in_format = false;
|
in_format = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -5393,7 +5398,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
|
|
||||||
// Padding.
|
// Padding.
|
||||||
int pad_chars_count = (negative || show_sign) ? min_chars - 1 : min_chars;
|
int pad_chars_count = (negative || show_sign) ? min_chars - 1 : min_chars;
|
||||||
String pad_char = pad_with_zeros ? String("0") : String(" ");
|
const String &pad_char = pad_with_zeros ? ZERO : SPACE;
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.rpad(pad_chars_count, pad_char);
|
str = str.rpad(pad_chars_count, pad_char);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5402,7 +5407,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
|
|
||||||
// Sign.
|
// Sign.
|
||||||
if (show_sign || negative) {
|
if (show_sign || negative) {
|
||||||
String sign_char = negative ? "-" : "+";
|
const String &sign_char = negative ? MINUS : PLUS;
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.insert(0, sign_char);
|
str = str.insert(0, sign_char);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5439,7 +5444,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
|
|
||||||
// Padding. Leave room for sign later if required.
|
// Padding. Leave room for sign later if required.
|
||||||
int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
|
int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
|
||||||
String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros
|
const String &pad_char = (pad_with_zeros && is_finite) ? ZERO : SPACE; // Never pad NaN or inf with zeros
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.rpad(pad_chars_count, pad_char);
|
str = str.rpad(pad_chars_count, pad_char);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5448,7 +5453,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
|
|
||||||
// Add sign if needed.
|
// Add sign if needed.
|
||||||
if (show_sign || is_negative) {
|
if (show_sign || is_negative) {
|
||||||
String sign_char = is_negative ? "-" : "+";
|
const String &sign_char = is_negative ? MINUS : PLUS;
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.insert(0, sign_char);
|
str = str.insert(0, sign_char);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5501,7 +5506,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
|
|
||||||
// Padding. Leave room for sign later if required.
|
// Padding. Leave room for sign later if required.
|
||||||
int pad_chars_count = val < 0 ? min_chars - 1 : min_chars;
|
int pad_chars_count = val < 0 ? min_chars - 1 : min_chars;
|
||||||
String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros
|
const String &pad_char = (pad_with_zeros && is_finite) ? ZERO : SPACE; // Never pad NaN or inf with zeros
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
number_str = number_str.rpad(pad_chars_count, pad_char);
|
number_str = number_str.rpad(pad_chars_count, pad_char);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5511,9 +5516,9 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
// Add sign if needed.
|
// Add sign if needed.
|
||||||
if (val < 0) {
|
if (val < 0) {
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
number_str = number_str.insert(0, "-");
|
number_str = number_str.insert(0, MINUS);
|
||||||
} else {
|
} else {
|
||||||
number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, "-");
|
number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, MINUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5678,7 +5683,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
||||||
in_decimals = false;
|
in_decimals = false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
formatted += chr(c);
|
formatted += c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue