From a9ef3e5804167c8d596b376214199f5a1a5577ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Tue, 9 Sep 2025 19:04:56 +0300 Subject: [PATCH] Fix duplicate minus in print output. Co-authored-by: kleonc <9283098+kleonc@users.noreply.github.com> --- core/string/ustring.cpp | 6 +++++- tests/core/string/test_string.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 1704bf52cc2..654f1671ecb 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -5262,7 +5262,11 @@ String String::sprintf(const Span &values, bool *error) const { // Get basic number. String str; if (!as_unsigned) { - str = String::num_int64(Math::abs(value), base, capitalize); + if (value == INT64_MIN) { // INT64_MIN can't be represented as positive value. + str = String::num_int64(value, base, capitalize).trim_prefix("-"); + } else { + str = String::num_int64(Math::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. diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 68183f52be7..de0ae8cc65c 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -992,6 +992,38 @@ TEST_CASE("[String] sprintf") { REQUIRE(error == false); CHECK(output == String("fish 143 frog")); + // INT64_MIN + format = "fish %d frog"; + args.clear(); + args.push_back(INT64_MIN); + output = format.sprintf(args, &error); + REQUIRE(error == false); + CHECK(output == String("fish -9223372036854775808 frog")); + + // INT64_MIN hex (lower) + format = "fish %x frog"; + args.clear(); + args.push_back(INT64_MIN); + output = format.sprintf(args, &error); + REQUIRE(error == false); + CHECK(output == String("fish -8000000000000000 frog")); + + // INT64_MIN hex (upper) + format = "fish %X frog"; + args.clear(); + args.push_back(INT64_MIN); + output = format.sprintf(args, &error); + REQUIRE(error == false); + CHECK(output == String("fish -8000000000000000 frog")); + + // INT64_MIN octal + format = "fish %o frog"; + args.clear(); + args.push_back(INT64_MIN); + output = format.sprintf(args, &error); + REQUIRE(error == false); + CHECK(output == String("fish -1000000000000000000000 frog")); + ///// Reals // Real