• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Bnaya / objectbuffer / 1cc46d885c9a8c4464d8420dc6a68afe83ac9811

pending completion
1cc46d885c9a8c4464d8420dc6a68afe83ac9811

push

github

Bnaya Peretz
wip

458 of 552 branches covered (82.97%)

Branch coverage included in aggregate %.

1249 of 1361 relevant lines covered (91.77%)

277.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.25
/src/internal/hashmap/hashFunctionsStuff.ts
1
export function hashCodeExternalValue(value: string | number): number {
2
  if (typeof value === "number") {
1,307✔
3
    return hashNumber(value);
4
  }
5

6
  return hashString(value);
7
}
8

9
/**
10
 * We may replace that with code that knows to break down float into bytes
11
 */
12
const helperFloatArray = new Float64Array(1);
13
const helperUint8Array = new Uint8Array(helperFloatArray.buffer);
14

15
export function hashNumber(num: number) {
16
  helperFloatArray[0] = num;
17

18
  return hashUint8CodeInPlace(helperUint8Array, 0, helperUint8Array.byteLength);
19
}
20

21
export function hashUint8CodeInPlace(
22
  uint8: Uint8Array,
23
  keyStart: number,
24
  keyBytesLength: number
25
): number {
26
  let h = 0 | 0;
27

28
  // const hashed: number[] = [];
29

30
  for (let i = 0; i < keyBytesLength; i++) {
31
    // h = (Math.imul(31, h) + uint8[i + keyStart]) | 0;
32
    h = hashStep(h, uint8[i + keyStart]);
33
  }
34

35
  // console.log(hashed);
36

37
  return Math.abs(h);
38
}
39

40
function hashStep(h: number, v: number) {
41
  // console.log({ h, v });
42
  return (Math.imul(31, h) + v) | 0;
43
}
44

45
export function hashString(str: string) {
46
  const strLen = str.length;
47
  let h = 0 | 0;
48

49
  for (let point = 0, nextCode = 0, i = 0; i !== strLen; ) {
50
    (point = str.charCodeAt(i)), (i += 1);
51

52
    if (point >= 0xd800 && point <= 0xdbff) {
22,198✔
53
      if (i === strLen) {
16!
54
        h = hashStep(h, 0xef) /*0b11101111*/;
55
        h = hashStep(h, 0xbf) /*0b10111111*/;
56
        h = hashStep(h, 0xbd) /*0b10111101*/;
57
        break;
58
      }
59
      // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
60
      nextCode = str.charCodeAt(i);
61
      if (nextCode >= 0xdc00 && nextCode <= 0xdfff) {
48!
62
        point = (point - 0xd800) * 0x400 + nextCode - 0xdc00 + 0x10000;
63
        i += 1;
64
        if (point > 0xffff) {
16!
65
          h = hashStep(h, (0x1e /*0b11110*/ << 3) | (point >>> 18));
66
          h = hashStep(
67
            h,
68
            (0x2 /*0b10*/ << 6) | ((point >>> 12) & 0x3f)
69
          ) /*0b00111111*/;
70
          h = hashStep(
71
            h,
72
            (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f)
73
          ) /*0b00111111*/;
74
          h = hashStep(h, (0x2 /*0b10*/ << 6) | (point & 0x3f)) /*0b00111111*/;
75
          continue;
76
        }
77
      } else {
78
        h = hashStep(h, 0xef) /*0b11101111*/;
79
        h = hashStep(h, 0xbf) /*0b10111111*/;
80
        h = hashStep(h, 0xbd) /*0b10111101*/;
81
        continue;
82
      }
83
    }
84
    if (point <= 0x007f) {
11,075!
85
      h = hashStep(h, (0x0 /*0b0*/ << 7) | point);
86
    } else if (point <= 0x07ff) {
×
87
      h = hashStep(h, (0x6 /*0b110*/ << 5) | (point >>> 6));
88
      h = hashStep(h, (0x2 /*0b10*/ << 6) | (point & 0x3f)) /*0b00111111*/;
89
    } else {
90
      h = hashStep(h, (0xe /*0b1110*/ << 4) | (point >>> 12));
91
      h = hashStep(
92
        h,
93
        (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f)
94
      ) /*0b00111111*/;
95
      h = hashStep(h, (0x2 /*0b10*/ << 6) | (point & 0x3f)) /*0b00111111*/;
96
    }
97
  }
98

99
  return Math.abs(h);
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc