Git development
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH] Ensure RawParseUtils.lineMap last element is the buffer end
Date: Fri, 20 Mar 2009 09:38:08 -0700	[thread overview]
Message-ID: <1237567088-31828-1-git-send-email-spearce@spearce.org> (raw)

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

                 reply	other threads:[~2009-03-20 16:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1237567088-31828-1-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox