Skip to content

Commit 3c73bed

Browse files
committed
Merge pull request #110354 from bruvzg/dbl_neg
Fix duplicate minus in print output.
2 parents 295435d + a9ef3e5 commit 3c73bed

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

core/string/ustring.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5262,7 +5262,11 @@ String String::sprintf(const Span<Variant> &values, bool *error) const {
52625262
// Get basic number.
52635263
String str;
52645264
if (!as_unsigned) {
5265-
str = String::num_int64(Math::abs(value), base, capitalize);
5265+
if (value == INT64_MIN) { // INT64_MIN can't be represented as positive value.
5266+
str = String::num_int64(value, base, capitalize).trim_prefix("-");
5267+
} else {
5268+
str = String::num_int64(Math::abs(value), base, capitalize);
5269+
}
52665270
} else {
52675271
uint64_t uvalue = *((uint64_t *)&value);
52685272
// In unsigned hex, if the value fits in 32 bits, trim it down to that.

tests/core/string/test_string.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,38 @@ TEST_CASE("[String] sprintf") {
992992
REQUIRE(error == false);
993993
CHECK(output == String("fish 143 frog"));
994994

995+
// INT64_MIN
996+
format = "fish %d frog";
997+
args.clear();
998+
args.push_back(INT64_MIN);
999+
output = format.sprintf(args, &error);
1000+
REQUIRE(error == false);
1001+
CHECK(output == String("fish -9223372036854775808 frog"));
1002+
1003+
// INT64_MIN hex (lower)
1004+
format = "fish %x frog";
1005+
args.clear();
1006+
args.push_back(INT64_MIN);
1007+
output = format.sprintf(args, &error);
1008+
REQUIRE(error == false);
1009+
CHECK(output == String("fish -8000000000000000 frog"));
1010+
1011+
// INT64_MIN hex (upper)
1012+
format = "fish %X frog";
1013+
args.clear();
1014+
args.push_back(INT64_MIN);
1015+
output = format.sprintf(args, &error);
1016+
REQUIRE(error == false);
1017+
CHECK(output == String("fish -8000000000000000 frog"));
1018+
1019+
// INT64_MIN octal
1020+
format = "fish %o frog";
1021+
args.clear();
1022+
args.push_back(INT64_MIN);
1023+
output = format.sprintf(args, &error);
1024+
REQUIRE(error == false);
1025+
CHECK(output == String("fish -1000000000000000000000 frog"));
1026+
9951027
///// Reals
9961028

9971029
// Real

0 commit comments

Comments
 (0)