1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Threshold for switching from 32-bit to 64-bit hashing. This is selected to ensure that the bias towards lower modulo results when using 32-bit hashing is <0.5%.
|
---|
10 | * @type {number}
|
---|
11 | */
|
---|
12 | const FNV_64_THRESHOLD = 1 << 24;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * The FNV-1a offset basis for 32-bit hash values.
|
---|
16 | * @type {number}
|
---|
17 | */
|
---|
18 | const FNV_OFFSET_32 = 2166136261;
|
---|
19 | /**
|
---|
20 | * The FNV-1a prime for 32-bit hash values.
|
---|
21 | * @type {number}
|
---|
22 | */
|
---|
23 | const FNV_PRIME_32 = 16777619;
|
---|
24 | /**
|
---|
25 | * The mask for a positive 32-bit signed integer.
|
---|
26 | * @type {number}
|
---|
27 | */
|
---|
28 | const MASK_31 = 0x7fffffff;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The FNV-1a offset basis for 64-bit hash values.
|
---|
32 | * @type {bigint}
|
---|
33 | */
|
---|
34 | const FNV_OFFSET_64 = BigInt("0xCBF29CE484222325");
|
---|
35 | /**
|
---|
36 | * The FNV-1a prime for 64-bit hash values.
|
---|
37 | * @type {bigint}
|
---|
38 | */
|
---|
39 | const FNV_PRIME_64 = BigInt("0x100000001B3");
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Computes a 32-bit FNV-1a hash value for the given string.
|
---|
43 | * See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
---|
44 | * @param {string} str The input string to hash
|
---|
45 | * @returns {number} - The computed hash value.
|
---|
46 | */
|
---|
47 | function fnv1a32(str) {
|
---|
48 | let hash = FNV_OFFSET_32;
|
---|
49 | for (let i = 0, len = str.length; i < len; i++) {
|
---|
50 | hash ^= str.charCodeAt(i);
|
---|
51 | // Use Math.imul to do c-style 32-bit multiplication and keep only the 32 least significant bits
|
---|
52 | hash = Math.imul(hash, FNV_PRIME_32);
|
---|
53 | }
|
---|
54 | // Force the result to be positive
|
---|
55 | return hash & MASK_31;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Computes a 64-bit FNV-1a hash value for the given string.
|
---|
60 | * See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
---|
61 | * @param {string} str The input string to hash
|
---|
62 | * @returns {bigint} - The computed hash value.
|
---|
63 | */
|
---|
64 | function fnv1a64(str) {
|
---|
65 | let hash = FNV_OFFSET_64;
|
---|
66 | for (let i = 0, len = str.length; i < len; i++) {
|
---|
67 | hash ^= BigInt(str.charCodeAt(i));
|
---|
68 | hash = BigInt.asUintN(64, hash * FNV_PRIME_64);
|
---|
69 | }
|
---|
70 | return hash;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Computes a hash value for the given string and range. This hashing algorithm is a modified
|
---|
75 | * version of the [FNV-1a algorithm](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
|
---|
76 | * It is optimized for speed and does **not** generate a cryptographic hash value.
|
---|
77 | *
|
---|
78 | * We use `numberHash` in `lib/ids/IdHelpers.js` to generate hash values for the module identifier. The generated
|
---|
79 | * hash is used as a prefix for the module id's to avoid collisions with other modules.
|
---|
80 | * @param {string} str The input string to hash.
|
---|
81 | * @param {number} range The range of the hash value (0 to range-1).
|
---|
82 | * @returns {number} - The computed hash value.
|
---|
83 | * @example
|
---|
84 | * ```js
|
---|
85 | * const numberHash = require("webpack/lib/util/numberHash");
|
---|
86 | * numberHash("hello", 1000); // 73
|
---|
87 | * numberHash("hello world"); // 72
|
---|
88 | * ```
|
---|
89 | */
|
---|
90 | module.exports = (str, range) => {
|
---|
91 | if (range < FNV_64_THRESHOLD) {
|
---|
92 | return fnv1a32(str) % range;
|
---|
93 | }
|
---|
94 | return Number(fnv1a64(str) % BigInt(range));
|
---|
95 | };
|
---|