Skip to content

Instantly share code, notes, and snippets.

@scalone
Created December 20, 2017 18:01
Show Gist options
  • Save scalone/39d9fede023ea07979a421d859f2ace5 to your computer and use it in GitHub Desktop.
Save scalone/39d9fede023ea07979a421d859f2ace5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
module Hashname
def self.calc_hashname(name)
name.chars.inject(5381) do |hash, ch|
max_int = (hash << 5) + hash + ch.ord
self.force_overflow_unsigned(max_int)
end
end
def self.force_overflow_unsigned(i)
i % 2**32 # or equivalently: i & 0xffffffff
end
end
if __FILE__ == $0
ARGV.each do |name|
puts "#{name} = #{Hashname.calc_hashname(name)}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment