From 1a1e1cfea92d43c0a078273ed28fb44de3c300ee Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Wed, 10 Jun 2026 08:34:50 -0700 Subject: [PATCH] Fix String.padStart/padEnd (#1672) Fixes the following two bugs: * Pads to incorrect string length * Does not append full code point --- .../java/org/pkl/core/stdlib/base/StringNodes.java | 10 ++++------ .../files/LanguageSnippetTests/input/api/string.pkl | 6 ++++++ .../files/LanguageSnippetTests/output/api/string.pcf | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java b/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java index 9b07f2af0..cf8d0976a 100644 --- a/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java +++ b/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java @@ -745,13 +745,12 @@ public final class StringNodes { @TruffleBoundary @Specialization protected String eval(String self, long width, String ch) { - var length = self.length(); + var length = self.codePointCount(0, self.length()); if (length >= width) return self; var result = new StringBuilder(VmSafeMath.toInt32(width)); - var c = ch.charAt(0); for (var i = 0; i < width - length; i++) { - result.append(c); + result.append(ch); } result.append(self); return result.toString(); @@ -762,14 +761,13 @@ public final class StringNodes { @TruffleBoundary @Specialization protected String eval(String self, long width, String ch) { - var length = self.length(); + var length = self.codePointCount(0, self.length()); if (length >= width) return self; var result = new StringBuilder(VmSafeMath.toInt32(width)); result.append(self); - var c = ch.charAt(0); for (var i = 0; i < width - length; i++) { - result.append(c); + result.append(ch); } return result.toString(); } diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/api/string.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/api/string.pkl index 90100a814..afb766426 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/api/string.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/api/string.pkl @@ -452,6 +452,9 @@ examples { str1.padStart(10, " ") module.catch(() -> str1.padStart(10, "")) module.catch(() -> str1.padStart(10, "aa")) + "🙃".padStart(5, "h") + "🙃".padStart(5, "🏀") + "hi".padStart(5, "🏀") } ["padEnd()"] { @@ -463,6 +466,9 @@ examples { str1.padEnd(10, " ") module.catch(() -> str1.padEnd(10, "")) module.catch(() -> str1.padEnd(10, "aa")) + "🙃".padEnd(5, "h") + "🙃".padEnd(5, "🏀") + "hi".padEnd(5, "🏀") } ["toBooleanOrNull()"] { diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/api/string.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/api/string.pcf index c14daed1c..f8b5588cd 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/api/string.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/api/string.pcf @@ -386,6 +386,9 @@ examples { " abcdefg" "Type constraint `length == 1` violated. Value: \"\"" "Type constraint `length == 1` violated. Value: \"aa\"" + "hhhh🙃" + "🏀🏀🏀🏀🙃" + "🏀🏀🏀hi" } ["padEnd()"] { "" @@ -396,6 +399,9 @@ examples { "abcdefg " "Type constraint `length == 1` violated. Value: \"\"" "Type constraint `length == 1` violated. Value: \"aa\"" + "🙃hhhh" + "🙃🏀🏀🏀🏀" + "hi🏀🏀🏀" } ["toBooleanOrNull()"] { true