Don't crash on windows line endings in javadoc
MOE_MIGRATED_REVID=127627837
diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java
index e229c93..a0c5bc8 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java
@@ -53,9 +53,9 @@
import com.google.common.collect.PeekingIterator;
import com.google.googlejavaformat.java.javadoc.Token.Type;
import java.util.ArrayDeque;
+import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
-import java.util.ArrayList;
import java.util.regex.Pattern;
/** Lexer for the Javadoc formatter. */
@@ -67,9 +67,19 @@
* original form). This would mean mean everything from an encoded ∕✱✱ to an encoded <pre> tag,
* so we'll probably never bother.
*/
- return new JavadocLexer(new CharStream(stripJavadocBeginAndEnd(input))).generateTokens();
+ input = stripJavadocBeginAndEnd(input);
+ input = normalizeLineEndings(input);
+ return new JavadocLexer(new CharStream(input)).generateTokens();
}
+ /** The lexer crashes on windows line endings, so for now just normalize to `\n`. */
+ // TODO(cushon): use the platform line separator for output
+ private static String normalizeLineEndings(String input) {
+ return NON_UNIX_LINE_ENDING.matcher(input).replaceAll("\n");
+ }
+
+ private static final Pattern NON_UNIX_LINE_ENDING = Pattern.compile("\r\n?");
+
private static String stripJavadocBeginAndEnd(String input) {
/*
* We do this ahead of time so that the main part of the lexer need not say things like
diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java
index bf88986..31d5e7b 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java
@@ -19,6 +19,7 @@
import com.google.common.base.Joiner;
import com.google.common.io.ByteStreams;
+import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -1270,4 +1271,15 @@
throw new AssertionError(e);
}
}
+
+ @Test
+ public void windowsLineSeparator() throws FormatterException {
+ String[] input = {
+ "/**", " * hello", " *", " * <p>world", " */", "class Test {}",
+ };
+ for (String separator : Arrays.asList("\r", "\r\n")) {
+ String actual = formatter.formatSource(Joiner.on(separator).join(input));
+ assertThat(actual).isEqualTo(Joiner.on('\n').join(input) + "\n");
+ }
+ }
}