MCPcopy Index your code
hub / github.com/antlr/antlr4 / stringHashCode

Function stringHashCode

runtime/JavaScript/src/antlr4/utils/stringHashCode.js:8–70  ·  view source on GitHub ↗
(value)

Source from the content-addressed store, hash-verified

6export const StringSeedHashCode = Math.round(Math.random() * Math.pow(2, 32));
7
8export function stringHashCode (value) {
9 if (!value) {
10 return 0;
11 }
12 const type = typeof value;
13 const key = type === 'string' ? value : type === 'object' && value.toString ? value.toString() : false;
14 if (!key) {
15 return 0;
16 }
17 let h1b, k1;
18
19 const remainder = key.length & 3; // key.length % 4
20 const bytes = key.length - remainder;
21 let h1 = StringSeedHashCode;
22 const c1 = 0xcc9e2d51;
23 const c2 = 0x1b873593;
24 let i = 0;
25
26 while (i < bytes) {
27 k1 =
28 ((key.charCodeAt(i) & 0xff)) |
29 ((key.charCodeAt(++i) & 0xff) << 8) |
30 ((key.charCodeAt(++i) & 0xff) << 16) |
31 ((key.charCodeAt(++i) & 0xff) << 24);
32 ++i;
33
34 k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
35 k1 = (k1 << 15) | (k1 >>> 17);
36 k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
37
38 h1 ^= k1;
39 h1 = (h1 << 13) | (h1 >>> 19);
40 h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
41 h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
42 }
43
44 k1 = 0;
45
46 switch (remainder) {
47 case 3:
48 k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
49 // no-break
50 case 2:
51 k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
52 // no-break
53 case 1:
54 k1 ^= (key.charCodeAt(i) & 0xff);
55 k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
56 k1 = (k1 << 15) | (k1 >>> 17);
57 k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
58 h1 ^= k1;
59 }
60
61 h1 ^= key.length;
62
63 h1 ^= h1 >>> 16;
64 h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
65 h1 ^= h1 >>> 13;

Callers 2

standardHashCodeFunctionFunction · 0.90
updateMethod · 0.90

Calls 1

toStringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…