Skip to content
everloot

Lore archive · Warcraft Wiki, 2023 dump

StringHash

Last wiki revision 2008-07-09

StringHash - by -

Return a fair-quality 32-bit hash of a string

hashVal = StringHash("string")

> print(StringHash("")) 1 > print(StringHash("ab")) 3458343178 > print(StringHash("ba")) 3466747145 > print(StringHash("AB")) 2653593770 > print(StringHash("BA")) 2661997737 > print(StringHash("The quick brown fox jumps over the lazy dog")) 3402772626

  • This algorithm is ~30% faster than a Lua implementation of the Java String.hashCode library call
  • The bit transmutation patterns and resulting collision rates are MUCH better than the results from the Java hash
  • See StringHash/Analysis

local function StringHash(text) local counter = 1 local len = string.len(text) for i = 1, len, 3 do counter = math.fmod(counter*8161, 4294967279) + -- 2^32 - 17: Prime! (string.byte(text,i)*16776193) + ((string.byte(text,i+1) or (len-i+256))*8372226) + ((string.byte(text,i+2) or (len-i+256))*3932164) end return math.fmod(counter, 4294967291) -- 2^32 - 5: Prime (and different from the prime in the loop) end

Categories

  • User defined functions

Source

https://warcraft.wiki.gg/wiki/StringHash · retrieved 2023-11-13 · CC BY-SA 4.0

Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.

This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.