threads / patch / 18437

patchEnsure RawParseUtils.lineMap last element is the buffer end

Subject: [JGIT PATCH] Ensure RawParseUtils.lineMap last element is the buffer end

## tl;dr

One message between Mar 20, 2009 and Mar 20, 2009. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Shawn O. Pearce· Mar 20, 2009, 16:38 UTC · lore

Application code is easier to write when we can assume that for any given source line the last element of the IntList returned by lineMap contains the value of the end parameter. This makes it easy to extract any line by saying:

  RawParseUtils.decodeNoFallback(
    Constants.CHARSET,
    buf,
	lineMap.get(lineNbr),
    lineMap.get(lineNbr + 1));

without needing to worry about bound checks, assuming of course that lineNbr is already bound-checked within the range of the file.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../jgit/util/RawParseUtils_LineMapTest.java       |   16 ++++++++++------
 .../src/org/spearce/jgit/util/RawParseUtils.java   |    4 ++++
 2 files changed, 14 insertions(+), 6 deletions(-)
Show changes to 2 files +14 −6

org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java, org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java
index 3f562a4..312e3d8 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java
@@ -45,44 +45,48 @@
 	public void testEmpty() {
 		final IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0);
 		assertNotNull(map);
-		assertEquals(1, map.size());
+		assertEquals(2, map.size());
 		assertEquals(Integer.MIN_VALUE, map.get(0));
+		assertEquals(0, map.get(1));
 	}
 
 	public void testOneBlankLine() {
 		final IntList map = RawParseUtils.lineMap(new byte[] { '\n' }, 0, 1);
-		assertEquals(2, map.size());
+		assertEquals(3, map.size());
 		assertEquals(Integer.MIN_VALUE, map.get(0));
 		assertEquals(0, map.get(1));
+		assertEquals(1, map.get(2));
 	}
 
 	public void testTwoLineFooBar() throws UnsupportedEncodingException {
 		final byte[] buf = "foo\nbar\n".getBytes("ISO-8859-1");
 		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
-		assertEquals(3, map.size());
+		assertEquals(4, map.size());
 		assertEquals(Integer.MIN_VALUE, map.get(0));
 		assertEquals(0, map.get(1));
 		assertEquals(4, map.get(2));
+		assertEquals(buf.length, map.get(3));
 	}
 
 	public void testTwoLineNoLF() throws UnsupportedEncodingException {
 		final byte[] buf = "foo\nbar".getBytes("ISO-8859-1");
 		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
-		assertEquals(3, map.size());
+		assertEquals(4, map.size());
 		assertEquals(Integer.MIN_VALUE, map.get(0));
 		assertEquals(0, map.get(1));
 		assertEquals(4, map.get(2));
+		assertEquals(buf.length, map.get(3));
 	}
 
 	public void testFourLineBlanks() throws UnsupportedEncodingException {
 		final byte[] buf = "foo\n\n\nbar\n".getBytes("ISO-8859-1");
 		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
-		assertEquals(5, map.size());
+		assertEquals(6, map.size());
 		assertEquals(Integer.MIN_VALUE, map.get(0));
 		assertEquals(0, map.get(1));
 		assertEquals(4, map.get(2));
 		assertEquals(5, map.get(3));
 		assertEquals(6, map.get(4));
+		assertEquals(buf.length, map.get(5));
 	}
-
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
index 0735ce6..79ebe41 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
@@ -329,6 +329,9 @@ public static final int nextLF(final byte[] b, int ptr, final char chrA) {
 	 * Using a 1 indexed list means that line numbers can be directly accessed
 	 * from the list, so <code>list.get(1)</code> (aka get line 1) returns
 	 * <code>ptr</code>.
+	 * <p>
+	 * The last element (index <code>map.size()-1</code>) always contains
+	 * <code>end</code>.
 	 *
 	 * @param buf
 	 *            buffer to scan.
@@ -348,6 +351,7 @@ public static final IntList lineMap(final byte[] buf, int ptr, int end) {
 		map.fillTo(1, Integer.MIN_VALUE);
 		for (; ptr < end; ptr = nextLF(buf, ptr))
 			map.add(ptr);
+		map.add(end);
 		return map;
 	}
 
-- 
1.6.2.1.352.gae594

← back to recent threads