diff src/org/tmatesoft/hg/core/Nodeid.java @ 262:3dcd3dd90c77

Improve manifest parsing: decode bytes to chars once, minimize arraycopy on String instantiation, keep set of file revisions from previous manifest only
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 18 Aug 2011 03:46:36 +0200
parents 436bb5f65ce1
children 0a2f445de774
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/Nodeid.java	Wed Aug 17 20:30:06 2011 +0200
+++ b/src/org/tmatesoft/hg/core/Nodeid.java	Thu Aug 18 03:46:36 2011 +0200
@@ -163,7 +163,7 @@
 			throw new IllegalArgumentException();
 		}
 		// XXX is better impl for String possible?
-		return fromAscii(asciiRepresentation.getBytes(), 0, 40);
+		return fromAscii(asciiRepresentation.toCharArray(), 0, 40);
 	}
 	
 	/**
@@ -183,7 +183,8 @@
 			if (hiNibble >= 16 || lowNibble >= 16) {
 				throw new IllegalArgumentException(String.format("Characters '%c%c' (%1$d and %2$d) at index %d are not valid hex digits", asciiRepresentation[j-2], asciiRepresentation[j-1], j-2));
 			}
-			data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF);
+			b = (((hiNibble << 4) | lowNibble) & 0xFF);
+			data[i] = (byte) b;
 			zeroBytes = zeroBytes && b == 0;
 		}
 		if (zeroBytes) {
@@ -191,4 +192,12 @@
 		}
 		return new Nodeid(data, false);
 	}
+	
+	public static Nodeid fromAscii(char[] asciiRepresentation, int offset, int length) {
+		byte[] b = new byte[length];
+		for (int i = 0; i < b.length; i++) {
+			b[i] = (byte) asciiRepresentation[offset+i];
+		}
+		return fromAscii(b, 0, b.length);
+	}
 }