@@ -27,12 +27,19 @@ const FNV_OFFSETS = {
27
27
// Legacy implementation for 32-bit + Number types, older systems that don't
28
28
// support BigInt
29
29
function fnv1a ( str ) {
30
- // Handle unicode code points > 255
31
- str = unescape ( encodeURIComponent ( str ) ) ;
32
-
30
+ // Handle unicode code points > 0x7f
33
31
let hash = Number ( FNV_OFFSETS [ 32 ] ) ;
32
+ let unicoded = false ;
34
33
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 ;
36
43
hash += ( hash << 1 ) + ( hash << 4 ) + ( hash << 7 ) + ( hash << 8 ) + ( hash << 24 ) ;
37
44
}
38
45
@@ -51,11 +58,18 @@ function bigInt(str, {size} = {size: 32}) {
51
58
let hash = BigInt ( FNV_OFFSETS [ size ] ) ;
52
59
const prime = BigInt ( FNV_PRIMES [ size ] ) ;
53
60
54
- // Handle unicode code points > 255
55
- str = unescape ( encodeURIComponent ( str ) ) ;
56
-
61
+ // Handle unicode code points > 0x7f
62
+ let unicoded = false ;
57
63
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 ) ;
59
73
hash = BigInt . asUintN ( size , hash * prime ) ;
60
74
}
61
75
0 commit comments