[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const SAFE_LIMIT = 0x80000000;
|
---|
| 9 | const SAFE_PART = SAFE_LIMIT - 1;
|
---|
| 10 | const COUNT = 4;
|
---|
| 11 | const arr = [0, 0, 0, 0, 0];
|
---|
| 12 | const primes = [3, 7, 17, 19];
|
---|
| 13 |
|
---|
| 14 | module.exports = (str, range) => {
|
---|
| 15 | arr.fill(0);
|
---|
| 16 | for (let i = 0; i < str.length; i++) {
|
---|
| 17 | const c = str.charCodeAt(i);
|
---|
| 18 | for (let j = 0; j < COUNT; j++) {
|
---|
| 19 | const p = (j + COUNT - 1) % COUNT;
|
---|
| 20 | arr[j] = (arr[j] + c * primes[j] + arr[p]) & SAFE_PART;
|
---|
| 21 | }
|
---|
| 22 | for (let j = 0; j < COUNT; j++) {
|
---|
| 23 | const q = arr[j] % COUNT;
|
---|
| 24 | arr[j] = arr[j] ^ (arr[q] >> 1);
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 | if (range <= SAFE_PART) {
|
---|
| 28 | let sum = 0;
|
---|
| 29 | for (let j = 0; j < COUNT; j++) {
|
---|
| 30 | sum = (sum + arr[j]) % range;
|
---|
| 31 | }
|
---|
| 32 | return sum;
|
---|
| 33 | } else {
|
---|
| 34 | let sum1 = 0;
|
---|
| 35 | let sum2 = 0;
|
---|
| 36 | const rangeExt = Math.floor(range / SAFE_LIMIT);
|
---|
| 37 | for (let j = 0; j < COUNT; j += 2) {
|
---|
| 38 | sum1 = (sum1 + arr[j]) & SAFE_PART;
|
---|
| 39 | }
|
---|
| 40 | for (let j = 1; j < COUNT; j += 2) {
|
---|
| 41 | sum2 = (sum2 + arr[j]) % rangeExt;
|
---|
| 42 | }
|
---|
| 43 | return (sum2 * SAFE_LIMIT + sum1) % range;
|
---|
| 44 | }
|
---|
| 45 | };
|
---|