BlobSoyData: Remove special handling of \r
The only benefit of handling \r\n specially is to avoid emitting
"<td>foo\r</td>" for lines ending in \r\n. In practice, browsers don't
render the \r, so this is basically a non-issue.
The major downside of handling \r\n specially is that it disagrees
with the internals of JGit, which always assumes \n line endings.
There was a bug in the existing code that treated bare \r not followed
by \n as a newline, which broke the code in BlameServlet that assumes
the number of lines in the blame (from JGit internals) is the same as
in the BlobSoyData. Rather than fixing that bug by introducing more
complexity, we decided to go with the safest option, which is to match
JGit internals.
Change-Id: I4c96c3dc21260c13c311a6c018302d759c5b6e17
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java b/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
index 45b24fd..41e0701 100644
--- a/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
@@ -46,10 +46,9 @@
private static final Logger log = LoggerFactory.getLogger(BlobSoyData.class);
/**
- * Maximum number of bytes to load from a supposed text file for display.
- * Files larger than this will be displayed as binary files, even if the
- * contents was text. For example really big XML files may be above this limit
- * and will get displayed as binary.
+ * Maximum number of bytes to load from a supposed text file for display. Files larger than this
+ * will be displayed as binary files, even if the contents was text. For example really big XML
+ * files may be above this limit and will get displayed as binary.
*/
private static final int MAX_FILE_SIZE = 10 << 20;
@@ -134,8 +133,7 @@
}
}
- private static void writeResult(
- SoyListData lines, String classes, String s, int start, int end) {
+ private static void writeResult(SoyListData lines, String classes, String s, int start, int end) {
SoyListData line = lines.getListData(lines.length() - 1);
while (true) {
int nl = nextLineBreak(s, start, end);
@@ -144,7 +142,7 @@
}
addSpan(line, classes, s, start, nl);
- start = nl + (isCrNl(s, nl) ? 2 : 1);
+ start = nl + 1;
if (start == s.length()) {
return;
}
@@ -163,17 +161,9 @@
}
}
- private static boolean isCrNl(String s, int n) {
- return s.charAt(n) == '\r' && n != s.length() - 1 && s.charAt(n + 1) == '\n';
- }
-
private static int nextLineBreak(String s, int start, int end) {
- for (int i = start; i < end; i++) {
- if (s.charAt(i) == '\n' || s.charAt(i) == '\r') {
- return i;
- }
- }
- return -1;
+ int n = s.indexOf('\n', start);
+ return n < end ? n : -1;
}
private static String extension(String path, String content) {