parsers: speed up hex decoding for manifests
authorMatt Mackall <mpm@selenic.com>
Sun, 12 Oct 2008 15:21:08 -0500
changeset 7092 fb3fc27617a2
parent 7091 12b35ae03365
child 7093 16bafcebd3d1
parsers: speed up hex decoding for manifests
mercurial/parsers.c
--- a/mercurial/parsers.c	Sun Oct 12 15:21:08 2008 -0500
+++ b/mercurial/parsers.c	Sun Oct 12 15:21:08 2008 -0500
@@ -15,14 +15,13 @@
 {
 	if (c >= '0' && c <= '9')
 		return c - '0';
-
+	if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
 	if (c >= 'A' && c <= 'F')
 		return c - 'A' + 10;
 
-	if (c >= 'a' && c <= 'f')
-		return c - 'a' + 10;
-
-	return -1;
+	PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
+	return 0;
 }
 
 /*
@@ -30,43 +29,21 @@
  */
 static PyObject *unhexlify(const char *str, int len)
 {
-	PyObject *ret = NULL;
+	PyObject *ret;
 	const char *c;
 	char *d;
 
-	if (len % 2) {
-		PyErr_SetString(PyExc_ValueError,
-				"input is not even in length");
-		goto bail;
-	}
-
 	ret = PyString_FromStringAndSize(NULL, len / 2);
 	if (!ret)
-		goto bail;
+		return NULL;
 
-	d = PyString_AsString(ret);
-	if (!d)
-		goto bail;
-
+	d = PyString_AS_STRING(ret);
 	for (c = str; c < str + len;) {
 		int hi = hexdigit(*c++);
 		int lo = hexdigit(*c++);
-
-		if (hi == -1 || lo == -1) {
-			PyErr_SetString(PyExc_ValueError,
-					"input contains non-hex character");
-			goto bail;
-		}
-
 		*d++ = (hi << 4) | lo;
 	}
 
-	goto done;
-
-bail:
-	Py_XDECREF(ret);
-	ret = NULL;
-done:
 	return ret;
 }