Skip to content

Commit e9ccb0b

Browse files
StringUtils micro optimizations (#7366)
1 parent fb1c969 commit e9ccb0b

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
public class StringUtils {
3232
private static final Pattern LINE_BREAK = Pattern.compile("\\R");
33+
private static final Pattern URI_PROTOCOL_PATTERN = Pattern.compile("(?<!\\\\)://");
3334

3435
private StringUtils() {
3536
}
@@ -177,9 +178,19 @@ public static boolean isBlank(@Nullable String string) {
177178
if (string == null || string.isEmpty()) {
178179
return true;
179180
}
180-
for (int i = 0; i < string.length(); i++) {
181-
if (!Character.isWhitespace(string.charAt(i))) {
181+
int len = string.length();
182+
for (int i = 0; i < len; i++) {
183+
char c = string.charAt(i);
184+
// Fast-path for ASCII: avoid Character.isWhitespace() overhead
185+
if (c <= 0x7F) {
186+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\u000B') {
187+
continue;
188+
}
182189
return false;
190+
} else {
191+
if (!Character.isWhitespace(c)) {
192+
return false;
193+
}
183194
}
184195
}
185196
return true;
@@ -526,18 +537,16 @@ private static boolean different(char ch, char other) {
526537
}
527538

528539
public static String indent(String text) {
529-
StringBuilder indent = new StringBuilder();
530-
for (int i = 0; i < text.length(); i++) {
540+
int length = text.length();
541+
for (int i = 0; i < length; i++) {
531542
char c = text.charAt(i);
532543
if (c == '\n' || c == '\r') {
533-
return indent.toString();
534-
} else if (Character.isWhitespace(c)) {
535-
indent.append(c);
536-
} else {
537-
return indent.toString();
544+
return text.substring(0, i);
545+
} else if (!Character.isWhitespace(c)) {
546+
return text.substring(0, i);
538547
}
539548
}
540-
return indent.toString();
549+
return text;
541550
}
542551

543552
/**
@@ -700,13 +709,14 @@ public static int indexOfNextNonWhitespace(int cursor, String source) {
700709
boolean inSingleLineComment = false;
701710

702711
int length = source.length();
712+
char[] chars = source.toCharArray();
703713
for (; cursor < length; cursor++) {
704-
char current = source.charAt(cursor);
714+
char current = chars[cursor];
705715
if (inSingleLineComment) {
706716
inSingleLineComment = current != '\n';
707717
continue;
708-
} else if (length > cursor + 1) {
709-
char next = source.charAt(cursor + 1);
718+
} else if (cursor + 1 < length) {
719+
char next = chars[cursor + 1];
710720
if (inMultiLineComment) {
711721
if (current == '*' && next == '/') {
712722
inMultiLineComment = false;
@@ -731,7 +741,7 @@ public static int indexOfNextNonWhitespace(int cursor, String source) {
731741
}
732742

733743
public static String formatUriForPropertiesFile(String uri) {
734-
return uri.replaceAll("(?<!\\\\)://", "\\\\://");
744+
return URI_PROTOCOL_PATTERN.matcher(uri).replaceAll("\\\\://");
735745
}
736746

737747
public static boolean hasLineBreak(@Nullable String s) {

0 commit comments

Comments
 (0)