|
44 | 44 | #define XXH128_DIGESTSIZE 16 |
45 | 45 | #define XXH128_BLOCKSIZE 64 |
46 | 46 |
|
| 47 | +static const char _hexdigits[] = "0123456789abcdef"; |
| 48 | + |
47 | 49 |
|
48 | 50 | /* Get a buffer from an object. Rejects str with hashlib-compatible error. */ |
49 | 51 | #ifndef Py_ALWAYS_INLINE |
@@ -217,13 +219,9 @@ static PyObject *xxh32_hexdigest(PyObject *self, PyObject *const *args, |
217 | 219 | char retbuf[XXH32_DIGESTSIZE * 2]; |
218 | 220 | int i, j; |
219 | 221 | for (i = j = 0; i < XXH32_DIGESTSIZE; i++) { |
220 | | - unsigned char c; |
221 | | - c = (digest[i] >> 4) & 0xf; |
222 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
223 | | - retbuf[j++] = c; |
224 | | - c = (digest[i] & 0xf); |
225 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
226 | | - retbuf[j++] = c; |
| 222 | + unsigned char byte = digest[i]; |
| 223 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 224 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
227 | 225 | } |
228 | 226 |
|
229 | 227 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -287,13 +285,9 @@ static PyObject *xxh64_hexdigest(PyObject *self, PyObject *const *args, |
287 | 285 | char retbuf[XXH64_DIGESTSIZE * 2]; |
288 | 286 | int i, j; |
289 | 287 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
290 | | - unsigned char c; |
291 | | - c = (digest[i] >> 4) & 0xf; |
292 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
293 | | - retbuf[j++] = c; |
294 | | - c = (digest[i] & 0xf); |
295 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
296 | | - retbuf[j++] = c; |
| 288 | + unsigned char byte = digest[i]; |
| 289 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 290 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
297 | 291 | } |
298 | 292 |
|
299 | 293 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -357,13 +351,9 @@ static PyObject *xxh3_64_hexdigest(PyObject *self, PyObject *const *args, |
357 | 351 | char retbuf[XXH64_DIGESTSIZE * 2]; |
358 | 352 | int i, j; |
359 | 353 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
360 | | - unsigned char c; |
361 | | - c = (digest[i] >> 4) & 0xf; |
362 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
363 | | - retbuf[j++] = c; |
364 | | - c = (digest[i] & 0xf); |
365 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
366 | | - retbuf[j++] = c; |
| 354 | + unsigned char byte = digest[i]; |
| 355 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 356 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
367 | 357 | } |
368 | 358 |
|
369 | 359 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -442,13 +432,9 @@ static PyObject *xxh3_128_hexdigest(PyObject *self, PyObject *const *args, |
442 | 432 | char retbuf[XXH128_DIGESTSIZE * 2]; |
443 | 433 | int i, j; |
444 | 434 | for (i = j = 0; i < XXH128_DIGESTSIZE; i++) { |
445 | | - unsigned char c; |
446 | | - c = (digest[i] >> 4) & 0xf; |
447 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
448 | | - retbuf[j++] = c; |
449 | | - c = (digest[i] & 0xf); |
450 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
451 | | - retbuf[j++] = c; |
| 435 | + unsigned char byte = digest[i]; |
| 436 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 437 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
452 | 438 | } |
453 | 439 |
|
454 | 440 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -621,13 +607,9 @@ static PyObject *PYXXH32_hexdigest(PYXXH32Object *self) |
621 | 607 | XXH32_canonicalFromHash((XXH32_canonical_t *)digest, intdigest); |
622 | 608 |
|
623 | 609 | for (i = j = 0; i < XXH32_DIGESTSIZE; i++) { |
624 | | - unsigned char c; |
625 | | - c = (digest[i] >> 4) & 0xf; |
626 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
627 | | - retbuf[j++] = c; |
628 | | - c = (digest[i] & 0xf); |
629 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
630 | | - retbuf[j++] = c; |
| 610 | + unsigned char byte = digest[i]; |
| 611 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 612 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
631 | 613 | } |
632 | 614 |
|
633 | 615 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -973,13 +955,9 @@ static PyObject *PYXXH64_hexdigest(PYXXH64Object *self) |
973 | 955 | XXH64_canonicalFromHash((XXH64_canonical_t *)digest, intdigest); |
974 | 956 |
|
975 | 957 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
976 | | - unsigned char c; |
977 | | - c = (digest[i] >> 4) & 0xf; |
978 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
979 | | - retbuf[j++] = c; |
980 | | - c = (digest[i] & 0xf); |
981 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
982 | | - retbuf[j++] = c; |
| 958 | + unsigned char byte = digest[i]; |
| 959 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 960 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
983 | 961 | } |
984 | 962 |
|
985 | 963 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -1326,13 +1304,9 @@ static PyObject *PYXXH3_64_hexdigest(PYXXH3_64Object *self) |
1326 | 1304 | XXH64_canonicalFromHash((XXH64_canonical_t *)digest, intdigest); |
1327 | 1305 |
|
1328 | 1306 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
1329 | | - unsigned char c; |
1330 | | - c = (digest[i] >> 4) & 0xf; |
1331 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
1332 | | - retbuf[j++] = c; |
1333 | | - c = (digest[i] & 0xf); |
1334 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
1335 | | - retbuf[j++] = c; |
| 1307 | + unsigned char byte = digest[i]; |
| 1308 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 1309 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
1336 | 1310 | } |
1337 | 1311 |
|
1338 | 1312 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -1686,13 +1660,9 @@ static PyObject *PYXXH3_128_hexdigest(PYXXH3_128Object *self) |
1686 | 1660 | XXH128_canonicalFromHash((XXH128_canonical_t *)digest, intdigest); |
1687 | 1661 |
|
1688 | 1662 | for (i = j = 0; i < XXH128_DIGESTSIZE; i++) { |
1689 | | - unsigned char c; |
1690 | | - c = (digest[i] >> 4) & 0xf; |
1691 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
1692 | | - retbuf[j++] = c; |
1693 | | - c = (digest[i] & 0xf); |
1694 | | - c = (c > 9) ? c + 'a' - 10 : c + '0'; |
1695 | | - retbuf[j++] = c; |
| 1663 | + unsigned char byte = digest[i]; |
| 1664 | + retbuf[j++] = _hexdigits[byte >> 4]; |
| 1665 | + retbuf[j++] = _hexdigits[byte & 0xf]; |
1696 | 1666 | } |
1697 | 1667 |
|
1698 | 1668 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
|
0 commit comments