Skip to content

Commit

Permalink
ICU-22101 Error prone reports "StringSplitter" error in PluralRules.java
Browse files Browse the repository at this point in the history
String.split(String) and Pattern.split(CharSequence) have surprising behaviour.
"a::b:".split(":") produces ["a", "b"], when one would expect ["a", "", "b", ""]

The recommended fix is to use the Guava Splitter, or setting an explicit limit:
String.split(String,int limit) and Pattern.split(CharSequence,int limit)
  • Loading branch information
mihnita authored and markusicu committed Aug 11, 2022
1 parent d99abb6 commit 0eecb25
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ static FixedDecimalSamples parse(String source) {
}
source = source.substring(7).trim(); // remove both

for (String range : COMMA_SEPARATED.split(source)) {
for (String range : COMMA_SEPARATED.split(source, 0)) {
if (range.equals("…") || range.equals("...")) {
bounded2 = false;
haveBound = true;
Expand All @@ -1245,7 +1245,7 @@ static FixedDecimalSamples parse(String source) {
if (haveBound) {
throw new IllegalArgumentException("Can only have … at the end of samples: " + range);
}
String[] rangeParts = TILDE_SEPARATED.split(range);
String[] rangeParts = TILDE_SEPARATED.split(range, 0);
switch (rangeParts.length) {
case 1:
FixedDecimal sample = new FixedDecimal(rangeParts[0]);
Expand Down Expand Up @@ -1410,10 +1410,10 @@ private static Constraint parseConstraint(String description)
throws ParseException {

Constraint result = null;
String[] or_together = OR_SEPARATED.split(description);
String[] or_together = OR_SEPARATED.split(description, 0);
for (int i = 0; i < or_together.length; ++i) {
Constraint andConstraint = null;
String[] and_together = AND_SEPARATED.split(or_together[i]);
String[] and_together = AND_SEPARATED.split(or_together[i], 0);
for (int j = 0; j < and_together.length; ++j) {
Constraint newConstraint = NO_CONSTRAINT;

Expand Down Expand Up @@ -1608,7 +1608,7 @@ private static Rule parseRule(String description) throws ParseException {
}

description = description.substring(x+1).trim();
String[] constraintOrSamples = AT_SEPARATED.split(description);
String[] constraintOrSamples = AT_SEPARATED.split(description, 0);
boolean sampleFailure = false;
FixedDecimalSamples integerSamples = null, decimalSamples = null;
switch (constraintOrSamples.length) {
Expand Down Expand Up @@ -1662,7 +1662,7 @@ private static RuleList parseRuleChain(String description)
if (description.endsWith(";")) {
description = description.substring(0,description.length()-1);
}
String[] rules = SEMI_SEPARATED.split(description);
String[] rules = SEMI_SEPARATED.split(description, 0);
for (int i = 0; i < rules.length; ++i) {
Rule rule = parseRule(rules[i].trim());
result.hasExplicitBoundingInfo |= rule.integerSamples != null || rule.decimalSamples != null;
Expand Down

0 comments on commit 0eecb25

Please sign in to comment.