Skip to content

Commit

Permalink
Simplify and reduce alloc for space padding
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Oct 9, 2023
1 parent f93f4c2 commit 1018609
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions core/src/main/java/org/jruby/util/cli/OutputStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jruby.util.SafePropertyAccessor;

import java.time.LocalDate;
import java.util.Arrays;

/**
* Utility methods to generate the command-line output strings for help,
Expand Down Expand Up @@ -167,13 +168,23 @@ private static String strBold(String str, boolean tty) {
return "\033[1m" + str + "\033[0m";
}

private static final int SPACES_MAX = 256;
private static final char[] SPACES = new char[SPACES_MAX];
static {
Arrays.fill(SPACES, ' ');
}

private static String generateSpaces(int total) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < total; i++) {
sb.append(" ");
char[] spaces;

if (total > SPACES_MAX) {
spaces = new char[total];
Arrays.fill(spaces, ' ');
} else {
spaces = SPACES;
}

return sb.toString();
return new String(spaces, 0, total);
}

private static String breakLine(String str, int index, int spaces) {
Expand Down

0 comments on commit 1018609

Please sign in to comment.