2019-02-20 18:32:12 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-04-27 05:30:57 +08:00
|
|
|
/**
|
|
|
|
* The maximum safe integer value for 32-bit integers.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
const SAFE_LIMIT = 0x80000000;
|
2023-04-27 05:30:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum safe integer value for 32-bit integers minus one. This is used
|
|
|
|
* in the algorithm to ensure that intermediate hash values do not exceed the
|
|
|
|
* 32-bit integer limit.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
const SAFE_PART = SAFE_LIMIT - 1;
|
2023-04-27 05:30:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of 32-bit integers used to store intermediate hash values.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
const COUNT = 4;
|
2023-04-27 05:30:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array used to store intermediate hash values during the calculation.
|
|
|
|
* @type {number[]}
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
const arr = [0, 0, 0, 0, 0];
|
2023-04-27 05:30:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of prime numbers used in the hash calculation.
|
|
|
|
* @type {number[]}
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
const primes = [3, 7, 17, 19];
|
|
|
|
|
2023-04-27 05:30:57 +08:00
|
|
|
/**
|
|
|
|
* Computes a hash value for the given string and range. This hashing algorithm is a modified
|
|
|
|
* version of the [FNV-1a algorithm](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
|
|
|
|
* It is optimized for speed and does **not** generate a cryptographic hash value.
|
|
|
|
*
|
|
|
|
* We use `numberHash` in `lib/ids/IdHelpers.js` to generate hash values for the module identifier. The generated
|
|
|
|
* hash is used as a prefix for the module id's to avoid collisions with other modules.
|
|
|
|
*
|
|
|
|
* @param {string} str The input string to hash.
|
|
|
|
* @param {number} range The range of the hash value (0 to range-1).
|
|
|
|
* @returns {number} - The computed hash value.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* const numberHash = require("webpack/lib/util/numberHash");
|
|
|
|
* numberHash("hello", 1000); // 57
|
|
|
|
* numberHash("hello world"); // 990
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
module.exports = (str, range) => {
|
2023-04-27 05:30:57 +08:00
|
|
|
/**
|
|
|
|
* Initialize the array with zeros before it is used
|
|
|
|
* to store intermediate hash values.
|
|
|
|
*/
|
2019-02-20 18:32:12 +08:00
|
|
|
arr.fill(0);
|
2023-04-27 05:30:57 +08:00
|
|
|
// For each character in the string
|
2019-02-20 18:32:12 +08:00
|
|
|
for (let i = 0; i < str.length; i++) {
|
2023-04-27 05:30:57 +08:00
|
|
|
// Get the character code.
|
2019-02-20 18:32:12 +08:00
|
|
|
const c = str.charCodeAt(i);
|
2023-04-27 05:30:57 +08:00
|
|
|
// For each 32-bit integer used to store the hash value
|
2019-02-20 18:32:12 +08:00
|
|
|
for (let j = 0; j < COUNT; j++) {
|
2023-04-27 05:30:57 +08:00
|
|
|
// Get the index of the previous 32-bit integer.
|
2019-02-20 18:32:12 +08:00
|
|
|
const p = (j + COUNT - 1) % COUNT;
|
2023-04-27 05:30:57 +08:00
|
|
|
// Add the character code to the current hash value and multiply by the prime number.
|
2019-02-20 18:32:12 +08:00
|
|
|
arr[j] = (arr[j] + c * primes[j] + arr[p]) & SAFE_PART;
|
|
|
|
}
|
2023-04-27 05:30:57 +08:00
|
|
|
|
|
|
|
// For each 32-bit integer used to store the hash value
|
2019-02-20 18:32:12 +08:00
|
|
|
for (let j = 0; j < COUNT; j++) {
|
2023-04-27 05:30:57 +08:00
|
|
|
// Get the index of the next 32-bit integer.
|
2019-02-20 18:32:12 +08:00
|
|
|
const q = arr[j] % COUNT;
|
2023-04-27 05:30:57 +08:00
|
|
|
// XOR the current hash value with the value of the next 32-bit integer.
|
2019-02-20 18:32:12 +08:00
|
|
|
arr[j] = arr[j] ^ (arr[q] >> 1);
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 05:30:57 +08:00
|
|
|
|
2019-02-20 18:32:12 +08:00
|
|
|
if (range <= SAFE_PART) {
|
|
|
|
let sum = 0;
|
2023-04-27 05:30:57 +08:00
|
|
|
// For each 32-bit integer used to store the hash value
|
2019-02-20 18:32:12 +08:00
|
|
|
for (let j = 0; j < COUNT; j++) {
|
2023-04-27 05:30:57 +08:00
|
|
|
// Add the value of the current 32-bit integer to the sum.
|
2019-02-20 18:32:12 +08:00
|
|
|
sum = (sum + arr[j]) % range;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
} else {
|
|
|
|
let sum1 = 0;
|
|
|
|
let sum2 = 0;
|
2023-04-27 05:30:57 +08:00
|
|
|
// Calculate the range extension.
|
2019-02-20 18:32:12 +08:00
|
|
|
const rangeExt = Math.floor(range / SAFE_LIMIT);
|
|
|
|
for (let j = 0; j < COUNT; j += 2) {
|
|
|
|
sum1 = (sum1 + arr[j]) & SAFE_PART;
|
|
|
|
}
|
|
|
|
for (let j = 1; j < COUNT; j += 2) {
|
|
|
|
sum2 = (sum2 + arr[j]) % rangeExt;
|
|
|
|
}
|
|
|
|
return (sum2 * SAFE_LIMIT + sum1) % range;
|
|
|
|
}
|
|
|
|
};
|