git.vger.kernel.org archive mirror
 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 1/6] Simplify RawParseUtils.nextLF invocations
Date: Wed, 10 Dec 2008 14:05:46 -0800	[thread overview]
Message-ID: <1228946751-12708-2-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1228946751-12708-1-git-send-email-spearce@spearce.org>

Most of the time when we call next('\n') or nextLF('\n') we really
meant to just say nextLF(), which is logically identical to next()
but could be micro-optimized for the LF byte.

This refactoring shifts the calls to use the new nextLF wrapper for
next('\n'), so we can later chose to make this optimization, or to
leave the code as-is.  But either way the call sites are now much
clearer to read.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/lib/ObjectChecker.java    |    4 +-
 .../src/org/spearce/jgit/revwalk/RevTag.java       |    2 +-
 .../src/org/spearce/jgit/util/RawParseUtils.java   |   25 ++++++++++++++++----
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectChecker.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectChecker.java
index b303d6f..75e3c77 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectChecker.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectChecker.java
@@ -205,11 +205,11 @@ public void checkTag(final byte[] raw) throws CorruptObjectException {
 
 		if ((ptr = match(raw, ptr, type)) < 0)
 			throw new CorruptObjectException("no type header");
-		ptr = nextLF(raw, ptr, '\n');
+		ptr = nextLF(raw, ptr);
 
 		if ((ptr = match(raw, ptr, tag)) < 0)
 			throw new CorruptObjectException("no tag header");
-		ptr = nextLF(raw, ptr, '\n');
+		ptr = nextLF(raw, ptr);
 
 		if ((ptr = match(raw, ptr, tagger)) < 0)
 			throw new CorruptObjectException("no tagger header");
diff --git a/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevTag.java b/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevTag.java
index bbb18ee..77a55cd 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevTag.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevTag.java
@@ -90,7 +90,7 @@ void parseCanonical(final RevWalk walk, final byte[] rawTag)
 		object = walk.lookupAny(walk.idBuffer, oType);
 
 		int p = pos.value += 4; // "tag "
-		final int nameEnd = RawParseUtils.next(rawTag, p, '\n') - 1;
+		final int nameEnd = RawParseUtils.nextLF(rawTag, p) - 1;
 		name = RawParseUtils.decode(Constants.CHARSET, rawTag, p, nameEnd);
 		buffer = rawTag;
 		flags |= PARSED;
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 4b96439..10c2239 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
@@ -230,6 +230,21 @@ public static final int next(final byte[] b, int ptr, final char chrA) {
 	}
 
 	/**
+	 * Locate the first position after the next LF.
+	 * <p>
+	 * This method stops on the first '\n' it finds.
+	 * 
+	 * @param b
+	 *            buffer to scan.
+	 * @param ptr
+	 *            position within buffer to start looking for LF at.
+	 * @return new position just after the first LF found.
+	 */
+	public static final int nextLF(final byte[] b, int ptr) {
+		return next(b, ptr, '\n');
+	}
+
+	/**
 	 * Locate the first position after either the given character or LF.
 	 * <p>
 	 * This method stops on the first match it finds from either chrA or '\n'.
@@ -296,7 +311,7 @@ public static final int committer(final byte[] b, int ptr) {
 		while (ptr < sz && b[ptr] == 'p')
 			ptr += 48; // skip this parent.
 		if (ptr < sz && b[ptr] == 'a')
-			ptr = next(b, ptr, '\n');
+			ptr = nextLF(b, ptr);
 		return match(b, ptr, committer);
 	}
 
@@ -320,7 +335,7 @@ public static final int encoding(final byte[] b, int ptr) {
 				return -1;
 			if (b[ptr] == 'e')
 				break;
-			ptr = next(b, ptr, '\n');
+			ptr = nextLF(b, ptr);
 		}
 		return match(b, ptr, encoding);
 	}
@@ -342,7 +357,7 @@ public static Charset parseEncoding(final byte[] b) {
 		final int enc = encoding(b, 0);
 		if (enc < 0)
 			return Constants.CHARSET;
-		final int lf = next(b, enc, '\n');
+		final int lf = nextLF(b, enc);
 		return Charset.forName(decode(Constants.CHARSET, b, enc, lf - 1));
 	}
 
@@ -505,7 +520,7 @@ public static final int commitMessage(final byte[] b, int ptr) {
 		// header line type is.
 		//
 		while (ptr < sz && b[ptr] != '\n')
-			ptr = next(b, ptr, '\n');
+			ptr = nextLF(b, ptr);
 		if (ptr < sz && b[ptr] == '\n')
 			return ptr + 1;
 		return -1;
@@ -529,7 +544,7 @@ public static final int endOfParagraph(final byte[] b, final int start) {
 		int ptr = start;
 		final int sz = b.length;
 		while (ptr < sz && b[ptr] != '\n')
-			ptr = next(b, ptr, '\n');
+			ptr = nextLF(b, ptr);
 		while (0 < ptr && start < ptr && b[ptr - 1] == '\n')
 			ptr--;
 		return ptr;
-- 
1.6.1.rc2.299.gead4c

  reply	other threads:[~2008-12-10 22:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-10 22:05 [JGIT PATCH 0/6] RawParseUtil improvements Shawn O. Pearce
2008-12-10 22:05 ` Shawn O. Pearce [this message]
2008-12-10 22:05   ` [JGIT PATCH 2/6] Simplify RawParseUtils next and nextLF loops Shawn O. Pearce
2008-12-10 22:05     ` [JGIT PATCH 3/6] Correct Javadoc of RawParseUtils next and nextLF methods Shawn O. Pearce
2008-12-10 22:05       ` [JGIT PATCH 4/6] Add QuotedString class to handle C-style quoting rules Shawn O. Pearce
2008-12-10 22:05         ` [JGIT PATCH 5/6] Add Bourne style quoting for TransportGitSsh Shawn O. Pearce
2008-12-10 22:05           ` [JGIT PATCH 6/6] Add ~user friendly " Shawn O. Pearce
2008-12-10 23:22         ` [JGIT PATCH 4/6] Add QuotedString class to handle C-style quoting rules Robin Rosenberg
2008-12-10 23:41           ` Shawn O. Pearce
2008-12-11  0:33             ` Robin Rosenberg
2008-12-11  0:57               ` [JGIT PATCH 4/6 v3] Add QuotedString class to handle Git path style " Shawn O. Pearce

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=1228946751-12708-2-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;
as well as URLs for NNTP newsgroup(s).