Browse Source

Allow to get end-of-line characters for a RawText

Bug: 370320
Change-Id: I18adc63596f4657516ccc6d704a561924c79d445
Signed-off-by: Kevin Sawicki <kevin@github.com>
stable-2.0
Tomasz Zarna 13 years ago committed by Kevin Sawicki
parent
commit
9c7371a8c4
  1. 41
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java
  2. 17
      org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java

41
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java

@ -46,6 +46,7 @@ package org.eclipse.jgit.diff;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -189,6 +190,46 @@ public class RawTextTest {
assertEquals(new Edit(0, 1, 0, 2), e); assertEquals(new Edit(0, 1, 0, 2), e);
} }
@Test
public void testLineDelimiter() throws Exception {
RawText rt = new RawText(Constants.encodeASCII("foo\n"));
assertEquals("\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo\r\n"));
assertEquals("\r\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo\nbar"));
assertEquals("\n", rt.getLineDelimiter());
assertTrue(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
assertEquals("\r\n", rt.getLineDelimiter());
assertTrue(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
assertEquals("\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
assertEquals("\r\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("foo"));
assertNull(rt.getLineDelimiter());
assertTrue(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII(""));
assertNull(rt.getLineDelimiter());
assertTrue(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("\n"));
assertEquals("\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
rt = new RawText(Constants.encodeASCII("\r\n"));
assertEquals("\r\n", rt.getLineDelimiter());
assertFalse(rt.isMissingNewlineAtEnd());
}
private static RawText t(String text) { private static RawText t(String text) {
StringBuilder r = new StringBuilder(); StringBuilder r = new StringBuilder();
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {

17
org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java

@ -276,4 +276,21 @@ public class RawText extends Sequence {
return false; return false;
} }
/**
* Get the line delimiter for the first line.
*
* @return the line delimiter or <code>null</code>
*/
public String getLineDelimiter() {
if (size() == 0)
return null;
int e = getEnd(0);
if (content[e - 1] != '\n')
return null;
if (content.length > 1 && content[e - 2] == '\r')
return "\r\n";
else
return "\n";
}
} }

Loading…
Cancel
Save