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"]
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: Ib9534727a9fe1840eecb86e4141ca3fe9d997936
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
index 0a7070f..7981e18 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
@@ -13,6 +13,7 @@
// limitations under the License.
package com.googlesource.gerrit.plugins.validators;
+import com.google.common.base.Splitter;
import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.events.CommitReceivedEvent;
@@ -91,7 +92,7 @@
int longLineCnt = 0;
int nonEmptyCnt = 0;
- for (String line : commit.getFullMessage().split("\n")) {
+ for (String line : Splitter.on('\n').split(commit.getFullMessage())) {
if (!line.trim().isEmpty()) {
nonEmptyCnt++;
}