Prefer using Splitter to String.split
String.split(String) has surprising behaviour [1]:
String[] nothing = "".split(":"); // results in [""]
String[] bunchOfNothing = ":".split(":"); // results in []
More examples:
input | input.split(":") | Splitter.on(':').split(input)
=======|===================|==============================
"" | [""] | [""]
":" | [] | ["", ""]
":::" | [] | ["", "", "", ""]
"a:::" | ["a"] | ["a", "", "", ""]
":::b" | ["", "", "", "b"] | ["", "", "", "b"]
In addition using Splitter makes the code more readable as Splitter has
nicer methods that make it clearer which high-level operation should be
performed. E.g. in some places we can use
Splitter.on(CharMatcher.anyOf(something)) instead of matching on
patterns.
Tests and classes that are used by the GWT UI are not adapted.
[1] http://konigsberg.blogspot.com/2009/11/final-thoughts-java-puzzler-splitting.html
Change-Id: I6f141fb492e2fb94544089d794f245d0885f8649
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -20,7 +20,9 @@ import static com.google.gerrit.reviewdb.client.RefNames.REFS_CONFIG;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.common.FooterConstants;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.common.PageLinks;
|
||||
@@ -334,9 +336,7 @@ public class CommitValidators {
|
||||
sb.append("ERROR: ").append(errMsg);
|
||||
|
||||
if (c.getFullMessage().indexOf(CHANGE_ID_PREFIX) >= 0) {
|
||||
String[] lines = c.getFullMessage().trim().split("\n");
|
||||
String lastLine = lines.length > 0 ? lines[lines.length - 1] : "";
|
||||
|
||||
String lastLine = Iterables.getLast(Splitter.on('\n').split(c.getFullMessage()), "");
|
||||
if (lastLine.indexOf(CHANGE_ID_PREFIX) == -1) {
|
||||
sb.append('\n');
|
||||
sb.append('\n');
|
||||
|
||||
Reference in New Issue
Block a user