Speed up the qhash implementation

This commit is contained in:
Kovid Goyal 2013-09-18 21:27:31 +05:30
parent c893f0de7e
commit 3ab5097788

View File

@ -31,10 +31,10 @@ from calibre.utils.config_base import prefs
EPUB_EXT = '.epub' EPUB_EXT = '.epub'
# Implementation of QtQHash for strings. This doesn't seem to be in the Python implemention. # Implementation of QtQHash for strings. This doesn't seem to be in the Python implementation.
def qhash(inputstr): def qhash(inputstr):
instr = "" instr = b""
if isinstance (inputstr, str): if isinstance(inputstr, bytes):
instr = inputstr instr = inputstr
elif isinstance(inputstr, unicode): elif isinstance(inputstr, unicode):
instr = inputstr.encode("utf8") instr = inputstr.encode("utf8")
@ -42,8 +42,8 @@ def qhash (inputstr):
return -1 return -1
h = 0x00000000 h = 0x00000000
for i in range (0, len (instr)): for x in bytearray(instr):
h = (h << 4) + ord(instr[i]) h = (h << 4) + x
h ^= (h & 0xf0000000) >> 23 h ^= (h & 0xf0000000) >> 23
h &= 0x0fffffff h &= 0x0fffffff