git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Mar 20, 2009, 16:38 UTC
Message-ID
<1237567088-31828-1-git-send-email-spearce@spearce.org>

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(-)
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
Message 1 of 1 in “Ensure RawParseUtils.lineMap last element is the buffer end”
  1. Ensure RawParseUtils.lineMap last element is the buffer endShawn O. Pearce, Mar 20, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.