diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index 4a75d3aa0..c2cdf3d03 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -69,7 +69,7 @@ public int tabWidth() { /** Returns the indentation string for the given visual column width. */ public String indentString(int indent) { if (!useTabs()) { - return " ".repeat(indent); + return JavaOutput.spaces(indent); } int tabWidth = tabWidth(); return "\t".repeat(indent / tabWidth) + " ".repeat(indent % tabWidth); diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java index 498de6622..5af399521 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java @@ -62,6 +62,24 @@ public final class JavaOutput extends Output { private StringBuilder lineBuilder = new StringBuilder(); private StringBuilder spacesPending = new StringBuilder(); + private static final int MAX_CACHED_SPACES = 100; + private static final String[] spaces = new String[MAX_CACHED_SPACES + 1]; + + /** Returns an indentation string of the given number of spaces. */ + static String spaces(int indent) { + if (indent <= 0) { + return ""; + } + if (indent <= MAX_CACHED_SPACES) { + String result = spaces[indent]; + if (result == null) { + result = spaces[indent] = " ".repeat(indent); + } + return result; + } + return " ".repeat(indent); + } + /** * {@code JavaOutput} constructor. * @@ -69,7 +87,7 @@ public final class JavaOutput extends Output { * @param commentsHelper the {@link CommentsHelper}, used to rewrite comments */ public JavaOutput(String lineSeparator, Input javaInput, CommentsHelper commentsHelper) { - this(lineSeparator, javaInput, commentsHelper, indent -> " ".repeat(Math.max(0, indent))); + this(lineSeparator, javaInput, commentsHelper, JavaOutput::spaces); } /** diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index c6862b6ca..7414e4e50 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -829,6 +829,12 @@ public void styleVisualLengthAndIndentString() { assertThat(google.indentString(0)).isEmpty(); assertThat(googleTabs.indentString(0)).isEmpty(); assertThat(aospTabs.indentString(0)).isEmpty(); + assertThat(google.indentString(4)).isSameInstanceAs(google.indentString(4)); + assertThat(google.indentString(104)).isEqualTo(" ".repeat(104)); + assertThat(googleTabs.indentString(104)).isEqualTo("\t".repeat(52)); + assertThat(JavaOutput.spaces(4)).isSameInstanceAs(JavaOutput.spaces(4)); + assertThat(JavaOutput.spaces(0)).isEmpty(); + assertThat(JavaOutput.spaces(104)).isEqualTo(" ".repeat(104)); } @Test