From 09a8dd7b3b50136a4ef54a11c30ff04e0df1057b Mon Sep 17 00:00:00 2001 From: Serhat Date: Thu, 1 Oct 2020 23:06:12 +0300 Subject: [PATCH] Fixed padding bug of sprintf function (cherry picked from commit 9f2cdfea8261fae28dbd45a74195b9cff4db6dd5) --- core/ustring.cpp | 5 +++-- modules/gdscript/doc_classes/@GDScript.xml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index 3df30e096d6..1abfa8c9967 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -4182,11 +4182,12 @@ String String::sprintf(const Array &values, bool *error) const { int number_len = str.length(); // Padding. + int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars; String pad_char = pad_with_zeroes ? String("0") : String(" "); if (left_justified) { - str = str.rpad(min_chars, pad_char); + str = str.rpad(pad_chars_count, pad_char); } else { - str = str.lpad(min_chars, pad_char); + str = str.lpad(pad_chars_count, pad_char); } // Sign. diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index a72bbdec170..1e369658742 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -767,7 +767,7 @@ Returns the integer modulus of [code]a/b[/code] that wraps equally in positive and negative. [codeblock] for i in range(-3, 4): - print("%2.0f %2.0f %2.0f" % [i, i % 3, posmod(i, 3)]) + print("%2d %2d %2d" % [i, i % 3, posmod(i, 3)]) [/codeblock] Produces: [codeblock]