{"thread":{"id":"18437","subject":"[JGIT PATCH] Ensure RawParseUtils.lineMap last element is the buffer end","startedAt":"2009-03-20T16:38:08Z","lastAt":"2009-03-20T16:38:08Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"108732","messageId":"1237567088-31828-1-git-send-email-spearce@spearce.org","threadId":"18437","inReplyTo":null,"subject":"[JGIT PATCH] Ensure RawParseUtils.lineMap last element is the buffer end","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-03-20T16:38:08Z","receivedAt":"2009-03-20T16:38:08Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Application code is easier to write when we can assume that for\nany given source line the last element of the IntList returned\nby lineMap contains the value of the end parameter.  This makes\nit easy to extract any line by saying:\n\n  RawParseUtils.decodeNoFallback(\n    Constants.CHARSET,\n    buf,\n\tlineMap.get(lineNbr),\n    lineMap.get(lineNbr + 1));\n\nwithout needing to worry about bound checks, assuming of course\nthat lineNbr is already bound-checked within the range of the file.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../jgit/util/RawParseUtils_LineMapTest.java       |   16 ++++++++++------\n .../src/org/spearce/jgit/util/RawParseUtils.java   |    4 ++++\n 2 files changed, 14 insertions(+), 6 deletions(-)\n\ndiff --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\nindex 3f562a4..312e3d8 100644\n--- a/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_LineMapTest.java\n@@ -45,44 +45,48 @@\n \tpublic void testEmpty() {\n \t\tfinal IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0);\n \t\tassertNotNull(map);\n-\t\tassertEquals(1, map.size());\n+\t\tassertEquals(2, map.size());\n \t\tassertEquals(Integer.MIN_VALUE, map.get(0));\n+\t\tassertEquals(0, map.get(1));\n \t}\n \n \tpublic void testOneBlankLine() {\n \t\tfinal IntList map = RawParseUtils.lineMap(new byte[] { '\\n' }, 0, 1);\n-\t\tassertEquals(2, map.size());\n+\t\tassertEquals(3, map.size());\n \t\tassertEquals(Integer.MIN_VALUE, map.get(0));\n \t\tassertEquals(0, map.get(1));\n+\t\tassertEquals(1, map.get(2));\n \t}\n \n \tpublic void testTwoLineFooBar() throws UnsupportedEncodingException {\n \t\tfinal byte[] buf = \"foo\\nbar\\n\".getBytes(\"ISO-8859-1\");\n \t\tfinal IntList map = RawParseUtils.lineMap(buf, 0, buf.length);\n-\t\tassertEquals(3, map.size());\n+\t\tassertEquals(4, map.size());\n \t\tassertEquals(Integer.MIN_VALUE, map.get(0));\n \t\tassertEquals(0, map.get(1));\n \t\tassertEquals(4, map.get(2));\n+\t\tassertEquals(buf.length, map.get(3));\n \t}\n \n \tpublic void testTwoLineNoLF() throws UnsupportedEncodingException {\n \t\tfinal byte[] buf = \"foo\\nbar\".getBytes(\"ISO-8859-1\");\n \t\tfinal IntList map = RawParseUtils.lineMap(buf, 0, buf.length);\n-\t\tassertEquals(3, map.size());\n+\t\tassertEquals(4, map.size());\n \t\tassertEquals(Integer.MIN_VALUE, map.get(0));\n \t\tassertEquals(0, map.get(1));\n \t\tassertEquals(4, map.get(2));\n+\t\tassertEquals(buf.length, map.get(3));\n \t}\n \n \tpublic void testFourLineBlanks() throws UnsupportedEncodingException {\n \t\tfinal byte[] buf = \"foo\\n\\n\\nbar\\n\".getBytes(\"ISO-8859-1\");\n \t\tfinal IntList map = RawParseUtils.lineMap(buf, 0, buf.length);\n-\t\tassertEquals(5, map.size());\n+\t\tassertEquals(6, map.size());\n \t\tassertEquals(Integer.MIN_VALUE, map.get(0));\n \t\tassertEquals(0, map.get(1));\n \t\tassertEquals(4, map.get(2));\n \t\tassertEquals(5, map.get(3));\n \t\tassertEquals(6, map.get(4));\n+\t\tassertEquals(buf.length, map.get(5));\n \t}\n-\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\nindex 0735ce6..79ebe41 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\n@@ -329,6 +329,9 @@ public static final int nextLF(final byte[] b, int ptr, final char chrA) {\n \t * Using a 1 indexed list means that line numbers can be directly accessed\n \t * from the list, so <code>list.get(1)</code> (aka get line 1) returns\n \t * <code>ptr</code>.\n+\t * <p>\n+\t * The last element (index <code>map.size()-1</code>) always contains\n+\t * <code>end</code>.\n \t *\n \t * @param buf\n \t *            buffer to scan.\n@@ -348,6 +351,7 @@ public static final IntList lineMap(final byte[] buf, int ptr, int end) {\n \t\tmap.fillTo(1, Integer.MIN_VALUE);\n \t\tfor (; ptr < end; ptr = nextLF(buf, ptr))\n \t\t\tmap.add(ptr);\n+\t\tmap.add(end);\n \t\treturn map;\n \t}\n \n-- \n1.6.2.1.352.gae594\n"}]}