StringHash.h 522 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. * Copyright (c) 2018-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. namespace AK {
  9. constexpr u32 string_hash(char const* characters, size_t length, u32 seed = 0)
  10. {
  11. u32 hash = seed;
  12. for (size_t i = 0; i < length; ++i) {
  13. hash += (u32)characters[i];
  14. hash += (hash << 10);
  15. hash ^= (hash >> 6);
  16. }
  17. hash += hash << 3;
  18. hash ^= hash >> 11;
  19. hash += hash << 15;
  20. return hash;
  21. }
  22. }
  23. using AK::string_hash;