diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TFormatter.java b/classlib/src/main/java/org/teavm/classlib/java/util/TFormatter.java index fac4340e9..8c5054df0 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TFormatter.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TFormatter.java @@ -516,7 +516,7 @@ public final class TFormatter implements Closeable, Flushable { } private void formatGivenString(boolean upperCase, String str) throws IOException { - if (precision > 0) { + if (precision > 0 && precision < str.length()) { str = str.substring(0, precision); } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/FormatterTest.java b/tests/src/test/java/org/teavm/classlib/java/util/FormatterTest.java index a94a542fb..198e29ac2 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/FormatterTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/FormatterTest.java @@ -89,6 +89,11 @@ public class FormatterTest { assertEquals("0:2:-1", new Formatter().format("%2s", new A()).toString()); assertEquals("0:2:3", new Formatter().format("%2.3s", new A()).toString()); assertEquals("1:3:-1", new Formatter().format("%-3s", new A()).toString()); + + assertEquals("abcde", new Formatter().format("%-3.5s", "abcdefg").toString()); + assertEquals("abcd", new Formatter().format("%-3.5s", "abcd").toString()); + assertEquals("ab ", new Formatter().format("%-3.5s", "ab").toString()); + assertEquals(" ab", new Formatter().format("%3.5s", " ab").toString()); } static class A implements Formattable {