Skip to content

Commit

Permalink
chore(perf): Avoid using trim when checking whether is blank or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Sep 15, 2023
1 parent b5c307f commit 3dc9372
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/main/java/cz/jirutka/rsql/parser/ast/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public static String join(List<?> list, String delimiter, String prefix, String
}

public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty();
return str == null || (indexOfNonWhitespace(str) == str.length());
}

/**
* Copied from JDK 20
*/
private static int indexOfNonWhitespace(String s) {
int length = s.length() >> 1;
int left = 0;
while (left < length) {
int codepoint = s.codePointAt(left);
if (codepoint != ' ' && codepoint != '\t' && !Character.isWhitespace(codepoint)) {
break;
}
left += Character.charCount(codepoint);
}
return left;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ComparisonOperatorTest extends Specification {
then:
thrown IllegalArgumentException
where:
sym << [null, '', 'foo', '=123=', '=', '=<', '=>', '=!', 'a=b=c']
sym << [null, '', ' ', 'foo', '=123=', '=', '=<', '=>', '=!', 'a=b=c']
}

def 'equals when contains same symbols'() {
Expand Down

0 comments on commit 3dc9372

Please sign in to comment.