Bladeren bron

Fix erroneous pad_zeros warning

MewPurPur 2 jaren geleden
bovenliggende
commit
cc5500f7de
2 gewijzigde bestanden met toevoegingen van 7 en 4 verwijderingen
  1. 5 4
      core/string/ustring.cpp
  2. 2 0
      tests/core/string/test_string.h

+ 5 - 4
core/string/ustring.cpp

@@ -4281,12 +4281,13 @@ String String::pad_zeros(int p_digits) const {
 		begin++;
 	}
 
-	if (begin >= end) {
+	int zeros_to_add = p_digits - (end - begin);
+
+	if (zeros_to_add <= 0) {
 		return s;
+	} else {
+		return s.insert(begin, String("0").repeat(zeros_to_add));
 	}
-
-	int zeros_to_add = p_digits - (end - begin);
-	return s.insert(begin, String("0").repeat(zeros_to_add));
 }
 
 String String::trim_prefix(const String &p_prefix) const {

+ 2 - 0
tests/core/string/test_string.h

@@ -1095,7 +1095,9 @@ TEST_CASE("[String] pad") {
 
 	s = String("10.10");
 	CHECK(s.pad_decimals(4) == U"10.1000");
+	CHECK(s.pad_decimals(1) == U"10.1");
 	CHECK(s.pad_zeros(4) == U"0010.10");
+	CHECK(s.pad_zeros(1) == U"10.10");
 }
 
 TEST_CASE("[String] is_subsequence_of") {