Simplify code for PyUnicode (thx Serhiy)

This commit is contained in:
Christian Heimes 2013-10-03 22:26:22 +02:00
parent 139d3ebea9
commit 64c6e9cd8d
1 changed files with 5 additions and 22 deletions

View File

@ -430,34 +430,17 @@ has to support hashing of unaligned memory segments in the future.
unicode_hash (Objects/unicodeobject.c)
--------------------------------------
``bytes_hash`` provides the tp_hash slot function for unicode. Right now it
``unicode_hash`` provides the tp_hash slot function for unicode. Right now it
implements the FNV algorithm three times for ``unsigned char*``, ``Py_UCS2``
and ``Py_UCS4``. A reimplementation of the function must take care to use the
correct length. Since the macro ``PyUnicode_GET_LENGTH`` returns the length
of the unicode string and not its size in octets, the length must be
multiplied with the size of the internal unicode kind::
Py_ssize_t len;
Py_uhash_t x;
len = PyUnicode_GET_LENGTH(self);
switch (PyUnicode_KIND(self)) {
case PyUnicode_1BYTE_KIND: {
const Py_UCS1 *c = PyUnicode_1BYTE_DATA(self);
x = _PyHash_Func->hashfunc(c, len * sizeof(Py_UCS1));
break;
}
case PyUnicode_2BYTE_KIND: {
const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
x = _PyHash_Func->hashfunc(s, len * sizeof(Py_UCS2));
break;
}
case PyUnicode_4BYTE_KIND: {
const Py_UCS4 *l = PyUnicode_4BYTE_DATA(self);
x = _PyHash_Func->hashfunc(l, len * sizeof(Py_UCS4));
break;
}
}
if (PyUnicode_READY(u) == -1)
return -1;
x = _PyHash_Func->hashfunc(PyUnicode_DATA(u),
PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u));
generic_hash (Modules/_datetimemodule.c)