Issue #13088: Add shared Py_hexdigits constant to format a number into base 16

This commit is contained in:
Victor Stinner 2011-10-14 02:13:11 +02:00
parent e506437b52
commit f5cff56a1b
16 changed files with 118 additions and 135 deletions

View file

@ -162,7 +162,6 @@ static PyObject *
escape_encode(PyObject *self,
PyObject *args)
{
static const char *hexdigits = "0123456789abcdef";
PyObject *str;
Py_ssize_t size;
Py_ssize_t newsize;
@ -205,8 +204,8 @@ escape_encode(PyObject *self,
else if (c < ' ' || c >= 0x7f) {
*p++ = '\\';
*p++ = 'x';
*p++ = hexdigits[(c & 0xf0) >> 4];
*p++ = hexdigits[c & 0xf];
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
*p++ = Py_hexdigits[c & 0xf];
}
else
*p++ = c;

View file

@ -201,13 +201,11 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
/* Make hex version of the digest */
for(i=j=0; i<digest_size; i++) {
char c;
unsigned char c;
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
}
retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
PyMem_Free(hex_digest);

View file

@ -175,18 +175,18 @@ ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Py_UCS4 v = c - 0x10000;
c = 0xd800 | ((v >> 10) & 0x3ff);
output[chars++] = 'u';
output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
output[chars++] = "0123456789abcdef"[(c ) & 0xf];
output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
output[chars++] = Py_hexdigits[(c ) & 0xf];
c = 0xdc00 | (v & 0x3ff);
output[chars++] = '\\';
}
output[chars++] = 'u';
output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
output[chars++] = "0123456789abcdef"[(c ) & 0xf];
output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
output[chars++] = Py_hexdigits[(c ) & 0xf];
}
return chars;
}

View file

@ -1776,7 +1776,6 @@ save_bytes(PicklerObject *self, PyObject *obj)
static PyObject *
raw_unicode_escape(PyObject *obj)
{
static const char *hexdigits = "0123456789abcdef";
PyObject *repr, *result;
char *p;
Py_ssize_t i, size, expandsize;
@ -1809,23 +1808,23 @@ raw_unicode_escape(PyObject *obj)
if (ch >= 0x10000) {
*p++ = '\\';
*p++ = 'U';
*p++ = hexdigits[(ch >> 28) & 0xf];
*p++ = hexdigits[(ch >> 24) & 0xf];
*p++ = hexdigits[(ch >> 20) & 0xf];
*p++ = hexdigits[(ch >> 16) & 0xf];
*p++ = hexdigits[(ch >> 12) & 0xf];
*p++ = hexdigits[(ch >> 8) & 0xf];
*p++ = hexdigits[(ch >> 4) & 0xf];
*p++ = hexdigits[ch & 15];
*p++ = Py_hexdigits[(ch >> 28) & 0xf];
*p++ = Py_hexdigits[(ch >> 24) & 0xf];
*p++ = Py_hexdigits[(ch >> 20) & 0xf];
*p++ = Py_hexdigits[(ch >> 16) & 0xf];
*p++ = Py_hexdigits[(ch >> 12) & 0xf];
*p++ = Py_hexdigits[(ch >> 8) & 0xf];
*p++ = Py_hexdigits[(ch >> 4) & 0xf];
*p++ = Py_hexdigits[ch & 15];
}
/* Map 16-bit characters to '\uxxxx' */
else if (ch >= 256 || ch == '\\' || ch == '\n') {
*p++ = '\\';
*p++ = 'u';
*p++ = hexdigits[(ch >> 12) & 0xf];
*p++ = hexdigits[(ch >> 8) & 0xf];
*p++ = hexdigits[(ch >> 4) & 0xf];
*p++ = hexdigits[ch & 15];
*p++ = Py_hexdigits[(ch >> 12) & 0xf];
*p++ = Py_hexdigits[(ch >> 8) & 0xf];
*p++ = Py_hexdigits[(ch >> 4) & 0xf];
*p++ = Py_hexdigits[ch & 15];
}
/* Copy everything else as-is */
else

View file

@ -1078,13 +1078,11 @@ binascii_hexlify(PyObject *self, PyObject *args)
/* make hex version of string, taken from shamodule.c */
for (i=j=0; i < arglen; i++) {
char c;
unsigned char c;
c = (argbuf[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
retbuf[j++] = c;
retbuf[j++] = Py_hexdigits[c];
c = argbuf[i] & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
retbuf[j++] = c;
retbuf[j++] = Py_hexdigits[c];
}
PyBuffer_Release(&parg);
return retval;

View file

@ -391,13 +391,11 @@ MD5_hexdigest(MD5object *self, PyObject *unused)
/* Make hex version of the digest */
for(i=j=0; i<MD5_DIGESTSIZE; i++) {
char c;
unsigned char c;
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
}
return retval;
}

View file

@ -367,13 +367,11 @@ SHA1_hexdigest(SHA1object *self, PyObject *unused)
/* Make hex version of the digest */
for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
char c;
unsigned char c;
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
}
return retval;
}

View file

@ -460,13 +460,11 @@ SHA256_hexdigest(SHAobject *self, PyObject *unused)
/* Make hex version of the digest */
for(i=j=0; i<self->digestsize; i++) {
char c;
unsigned char c;
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
}
return retval;
}

View file

@ -526,13 +526,11 @@ SHA512_hexdigest(SHAobject *self, PyObject *unused)
/* Make hex version of the digest */
for (i=j=0; i<self->digestsize; i++) {
char c;
unsigned char c;
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
hex_digest[j++] = Py_hexdigits[c];
}
return retval;
}