comparison 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
comparison
equal deleted inserted replaced
261:436bb5f65ce1 262:3dcd3dd90c77
161 public static Nodeid fromAscii(String asciiRepresentation) { 161 public static Nodeid fromAscii(String asciiRepresentation) {
162 if (asciiRepresentation.length() != 40) { 162 if (asciiRepresentation.length() != 40) {
163 throw new IllegalArgumentException(); 163 throw new IllegalArgumentException();
164 } 164 }
165 // XXX is better impl for String possible? 165 // XXX is better impl for String possible?
166 return fromAscii(asciiRepresentation.getBytes(), 0, 40); 166 return fromAscii(asciiRepresentation.toCharArray(), 0, 40);
167 } 167 }
168 168
169 /** 169 /**
170 * Parse encoded representation. Similar to {@link #fromAscii(String)}. 170 * Parse encoded representation. Similar to {@link #fromAscii(String)}.
171 */ 171 */
181 b = asciiRepresentation[j++] & 0xCF; 181 b = asciiRepresentation[j++] & 0xCF;
182 int lowNibble = b > 64 ? b - 55 : b; 182 int lowNibble = b > 64 ? b - 55 : b;
183 if (hiNibble >= 16 || lowNibble >= 16) { 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)); 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 } 185 }
186 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); 186 b = (((hiNibble << 4) | lowNibble) & 0xFF);
187 data[i] = (byte) b;
187 zeroBytes = zeroBytes && b == 0; 188 zeroBytes = zeroBytes && b == 0;
188 } 189 }
189 if (zeroBytes) { 190 if (zeroBytes) {
190 return NULL; 191 return NULL;
191 } 192 }
192 return new Nodeid(data, false); 193 return new Nodeid(data, false);
193 } 194 }
195
196 public static Nodeid fromAscii(char[] asciiRepresentation, int offset, int length) {
197 byte[] b = new byte[length];
198 for (int i = 0; i < b.length; i++) {
199 b[i] = (byte) asciiRepresentation[offset+i];
200 }
201 return fromAscii(b, 0, b.length);
202 }
194 } 203 }