Add 'u' format modifier for unsigned int/hex
This commit is contained in:
parent
cba15cd625
commit
208c1020f5
@ -4842,6 +4842,7 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||||||
bool pad_with_zeros = false;
|
bool pad_with_zeros = false;
|
||||||
bool left_justified = false;
|
bool left_justified = false;
|
||||||
bool show_sign = false;
|
bool show_sign = false;
|
||||||
|
bool as_unsigned = false;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
*error = true;
|
*error = true;
|
||||||
@ -4882,16 +4883,27 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||||||
case 'x':
|
case 'x':
|
||||||
break;
|
break;
|
||||||
case 'X':
|
case 'X':
|
||||||
base = 16;
|
|
||||||
capitalize = true;
|
capitalize = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Get basic number.
|
// Get basic number.
|
||||||
String str = String::num_int64(ABS(value), base, capitalize);
|
String str;
|
||||||
|
if (!as_unsigned) {
|
||||||
|
str = String::num_int64(ABS(value), base, capitalize);
|
||||||
|
} else {
|
||||||
|
uint64_t uvalue = *((uint64_t *)&value);
|
||||||
|
// In unsigned hex, if the value fits in 32 bits, trim it down to that.
|
||||||
|
if (base == 16 && value < 0 && value >= INT32_MIN) {
|
||||||
|
uvalue &= 0xffffffff;
|
||||||
|
}
|
||||||
|
str = String::num_uint64(uvalue, base, capitalize);
|
||||||
|
}
|
||||||
int number_len = str.length();
|
int number_len = str.length();
|
||||||
|
|
||||||
|
bool negative = value < 0 && !as_unsigned;
|
||||||
|
|
||||||
// Padding.
|
// Padding.
|
||||||
int pad_chars_count = (value < 0 || 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(" ");
|
String pad_char = pad_with_zeros ? String("0") : String(" ");
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.rpad(pad_chars_count, pad_char);
|
str = str.rpad(pad_chars_count, pad_char);
|
||||||
@ -4900,8 +4912,8 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sign.
|
// Sign.
|
||||||
if (show_sign || value < 0) {
|
if (show_sign || negative) {
|
||||||
String sign_char = value < 0 ? "-" : "+";
|
String sign_char = negative ? "-" : "+";
|
||||||
if (left_justified) {
|
if (left_justified) {
|
||||||
str = str.insert(0, sign_char);
|
str = str.insert(0, sign_char);
|
||||||
} else {
|
} else {
|
||||||
@ -5094,6 +5106,10 @@ String String::sprintf(const Array &values, bool *error) const {
|
|||||||
show_sign = true;
|
show_sign = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'u': { // Treat as unsigned (for int/hex).
|
||||||
|
as_unsigned = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
case '0':
|
case '0':
|
||||||
case '1':
|
case '1':
|
||||||
case '2':
|
case '2':
|
||||||
|
Loading…
Reference in New Issue
Block a user