Skip to content

Instantly share code, notes, and snippets.

@dpk dpk/gist:4223533

Created Dec 6, 2012
What would you like to do?
Apple's CFHash algorithm for strings in Ruby
# Why? Why not!
def cfhash string
result = string.length
max = 2 ** 32
string.each_codepoint do |cp|
result = ((result * 257) % max) + cp
return (result + (result << (string.length & 31))) % max

This comment has been minimized.

Copy link
Owner Author

dpk commented Dec 6, 2012

This still isn't quite right. For some strings it's correct, for others it's wrong.

(Notably, it will be wrong for strings longer than 96 characters, because that's special-cased in the original algorithm. But I don't need to cover that case for what I'm using this for.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.