##// END OF EJS Templates
parsers: speed up hex decoding for manifests
Matt Mackall -
r7092:fb3fc276 default
parent child Browse files
Show More
@@ -15,14 +15,13 b' static int hexdigit(char c)'
15 {
15 {
16 if (c >= '0' && c <= '9')
16 if (c >= '0' && c <= '9')
17 return c - '0';
17 return c - '0';
18
18 if (c >= 'a' && c <= 'f')
19 return c - 'a' + 10;
19 if (c >= 'A' && c <= 'F')
20 if (c >= 'A' && c <= 'F')
20 return c - 'A' + 10;
21 return c - 'A' + 10;
21
22
22 if (c >= 'a' && c <= 'f')
23 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
23 return c - 'a' + 10;
24 return 0;
24
25 return -1;
26 }
25 }
27
26
28 /*
27 /*
@@ -30,43 +29,21 b' static int hexdigit(char c)'
30 */
29 */
31 static PyObject *unhexlify(const char *str, int len)
30 static PyObject *unhexlify(const char *str, int len)
32 {
31 {
33 PyObject *ret = NULL;
32 PyObject *ret;
34 const char *c;
33 const char *c;
35 char *d;
34 char *d;
36
35
37 if (len % 2) {
38 PyErr_SetString(PyExc_ValueError,
39 "input is not even in length");
40 goto bail;
41 }
42
43 ret = PyString_FromStringAndSize(NULL, len / 2);
36 ret = PyString_FromStringAndSize(NULL, len / 2);
44 if (!ret)
37 if (!ret)
45 goto bail;
38 return NULL;
46
39
47 d = PyString_AsString(ret);
40 d = PyString_AS_STRING(ret);
48 if (!d)
49 goto bail;
50
51 for (c = str; c < str + len;) {
41 for (c = str; c < str + len;) {
52 int hi = hexdigit(*c++);
42 int hi = hexdigit(*c++);
53 int lo = hexdigit(*c++);
43 int lo = hexdigit(*c++);
54
55 if (hi == -1 || lo == -1) {
56 PyErr_SetString(PyExc_ValueError,
57 "input contains non-hex character");
58 goto bail;
59 }
60
61 *d++ = (hi << 4) | lo;
44 *d++ = (hi << 4) | lo;
62 }
45 }
63
46
64 goto done;
65
66 bail:
67 Py_XDECREF(ret);
68 ret = NULL;
69 done:
70 return ret;
47 return ret;
71 }
48 }
72
49
General Comments 0
You need to be logged in to leave comments. Login now