Skip to content

Commit e8014bd

Browse files
committed
update hash algorithm to something a bit more standard
1 parent 93c89ce commit e8014bd

6 files changed

Lines changed: 21 additions & 17 deletions

File tree

dist/htmx.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,12 @@ return (function () {
821821
}
822822
}
823823

824+
// based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0,
825+
// derived from Java's string hashcode implementation
824826
function stringHash(string, hash) {
825-
for (var i = 0; i < string.length; i++) {
826-
var char = string.charCodeAt(i);
827-
hash = ((hash << 5) - hash) + char;
828-
hash = hash & hash;
827+
var char = 0;
828+
while (char < string.length){
829+
hash = (hash << 5) - hash + string.charCodeAt(char++) | 0; // bitwise or ensures we have a 32-bit int
829830
}
830831
return hash;
831832
}

dist/htmx.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/htmx.min.js.gz

5 Bytes
Binary file not shown.

src/htmx.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,12 @@ return (function () {
821821
}
822822
}
823823

824+
// based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0,
825+
// derived from Java's string hashcode implementation
824826
function stringHash(string, hash) {
825-
for (var i = 0; i < string.length; i++) {
826-
var char = string.charCodeAt(i);
827-
hash = ((hash << 5) - hash) + char;
828-
hash = hash & hash;
827+
var char = 0;
828+
while (char < string.length){
829+
hash = (hash << 5) - hash + string.charCodeAt(char++) | 0; // bitwise or ensures we have a 32-bit int
829830
}
830831
return hash;
831832
}

www/js/htmx.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,12 @@ return (function () {
821821
}
822822
}
823823

824+
// based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0,
825+
// derived from Java's string hashcode implementation
824826
function stringHash(string, hash) {
825-
for (var i = 0; i < string.length; i++) {
826-
var char = string.charCodeAt(i);
827-
hash = ((hash << 5) - hash) + char;
828-
hash = hash & hash;
827+
var char = 0;
828+
while (char < string.length){
829+
hash = (hash << 5) - hash + string.charCodeAt(char++) | 0; // bitwise or ensures we have a 32-bit int
829830
}
830831
return hash;
831832
}

www/test/1.8.1/src/htmx.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,12 @@ return (function () {
821821
}
822822
}
823823

824+
// based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0,
825+
// derived from Java's string hashcode implementation
824826
function stringHash(string, hash) {
825-
for (var i = 0; i < string.length; i++) {
826-
var char = string.charCodeAt(i);
827-
hash = ((hash << 5) - hash) + char;
828-
hash = hash & hash;
827+
var char = 0;
828+
while (char < string.length){
829+
hash = (hash << 5) - hash + string.charCodeAt(char++) | 0; // bitwise or ensures we have a 32-bit int
829830
}
830831
return hash;
831832
}

0 commit comments

Comments
 (0)