Skip to content

Commit bd8d94f

Browse files
committed
Unicode escape on-demand
1 parent b8c708d commit bd8d94f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

index.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ const FNV_OFFSETS = {
2727
// Legacy implementation for 32-bit + Number types, older systems that don't
2828
// support BigInt
2929
function fnv1a(str) {
30-
// Handle unicode code points > 255
31-
str = unescape(encodeURIComponent(str));
32-
30+
// Handle unicode code points > 0x7f
3331
let hash = Number(FNV_OFFSETS[32]);
32+
let unicoded = false;
3433
for (let i = 0; i < str.length; i++) {
35-
hash ^= str.charCodeAt(i);
34+
let v = str.charCodeAt(i);
35+
// Non-ASCII char triggers unicode escape logic
36+
if (v > 0x7f && !unicoded) {
37+
str = unescape(encodeURIComponent(str));
38+
v = str.charCodeAt(i);
39+
unicoded = true;
40+
}
41+
42+
hash ^= v;
3643
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
3744
}
3845

@@ -51,11 +58,18 @@ function bigInt(str, {size} = {size: 32}) {
5158
let hash = BigInt(FNV_OFFSETS[size]);
5259
const prime = BigInt(FNV_PRIMES[size]);
5360

54-
// Handle unicode code points > 255
55-
str = unescape(encodeURIComponent(str));
56-
61+
// Handle unicode code points > 0x7f
62+
let unicoded = false;
5763
for (let i = 0; i < str.length; i++) {
58-
hash ^= BigInt(str.charCodeAt(i));
64+
let v = str.charCodeAt(i);
65+
// Non-ASCII char triggers unicode escape logic
66+
if (v > 0x7f && !unicoded) {
67+
str = unescape(encodeURIComponent(str));
68+
v = str.charCodeAt(i);
69+
unicoded = true;
70+
}
71+
72+
hash ^= BigInt(v);
5973
hash = BigInt.asUintN(size, hash * prime);
6074
}
6175

0 commit comments

Comments
 (0)