Skip to content

Instantly share code, notes, and snippets.

@paincompiler
Last active February 26, 2016 09:30
Show Gist options
  • Save paincompiler/e204d180794fd5836579 to your computer and use it in GitHub Desktop.
Save paincompiler/e204d180794fd5836579 to your computer and use it in GitHub Desktop.

Revisions

  1. paincompiler revised this gist Feb 26, 2016. No changes.
  2. paincompiler created this gist Feb 26, 2016.
    17 changes: 17 additions & 0 deletions python_string_built_in_hash.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import "math/big"

    // simulate python 2.7 built-in __hash__ of string object
    func hash(s string) *big.Int {
    if len(s) == 0 {
    return big.NewInt(0)
    }
    value := big.NewInt(int64([]byte(s)[0]) << 7)
    for _, v := range []byte(s) {
    value = value.Xor(value.Mul(value, big.NewInt(1000003)), big.NewInt(int64(v)))
    }
    value = value.Xor(value, big.NewInt(int64(len(s))))
    if value.Int64() == -1 {
    value.SetInt64(-2)
    }
    return value
    }