Mercurial > hg4j
comparison src/org/tmatesoft/hg/core/Nodeid.java @ 266:0a2f445de774
Improve manifest parsing: reduce number of arrays instantiated for Nodeid
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 19 Aug 2011 04:59:32 +0200 |
parents | 3dcd3dd90c77 |
children | 88c58edc0857 |
comparison
equal
deleted
inserted
replaced
265:3dd953c65619 | 266:0a2f445de774 |
---|---|
17 package org.tmatesoft.hg.core; | 17 package org.tmatesoft.hg.core; |
18 | 18 |
19 import static org.tmatesoft.hg.internal.DigestHelper.toHexString; | 19 import static org.tmatesoft.hg.internal.DigestHelper.toHexString; |
20 | 20 |
21 import java.util.Arrays; | 21 import java.util.Arrays; |
22 | |
23 import org.tmatesoft.hg.internal.DigestHelper; | |
22 | 24 |
23 | 25 |
24 | 26 |
25 /** | 27 /** |
26 * A 20-bytes (40 characters) long hash value to identify a revision. | 28 * A 20-bytes (40 characters) long hash value to identify a revision. |
172 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { | 174 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { |
173 if (length != 40) { | 175 if (length != 40) { |
174 throw new IllegalArgumentException(); | 176 throw new IllegalArgumentException(); |
175 } | 177 } |
176 byte[] data = new byte[20]; | 178 byte[] data = new byte[20]; |
177 boolean zeroBytes = true; | 179 boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data); |
178 for (int i = 0, j = offset; i < data.length; i++) { | |
179 int b = asciiRepresentation[j++] & 0xCF; // -0x30 to get decimal digit out from their char, and to uppercase if a letter | |
180 int hiNibble = b > 64 ? b - 55 : b; | |
181 b = asciiRepresentation[j++] & 0xCF; | |
182 int lowNibble = b > 64 ? b - 55 : b; | |
183 if (hiNibble >= 16 || lowNibble >= 16) { | |
184 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)); | |
185 } | |
186 b = (((hiNibble << 4) | lowNibble) & 0xFF); | |
187 data[i] = (byte) b; | |
188 zeroBytes = zeroBytes && b == 0; | |
189 } | |
190 if (zeroBytes) { | 180 if (zeroBytes) { |
191 return NULL; | 181 return NULL; |
192 } | 182 } |
193 return new Nodeid(data, false); | 183 return new Nodeid(data, false); |
194 } | 184 } |