Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bugs related to unicode support in polynomial-hash functions #304

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for unicode in PolynomialHash.roll
dubzzz committed Feb 14, 2019
commit 944c63cfcd8689bfa743a9e7592d3b19d3d7964a
28 changes: 5 additions & 23 deletions src/algorithms/cryptography/polynomial-hash/PolynomialHash.js
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ export default class PolynomialHash {
* @return {number}
*/
hash(word) {
const charCodes = Array.from(word).map(char => this.charToNumber(char));
const charCodes = Array.from(word).map(char => char.codePointAt(0));

let hash = 0;
for (let charIndex = 0; charIndex < charCodes.length; charIndex += 1) {
@@ -49,11 +49,12 @@ export default class PolynomialHash {
roll(prevHash, prevWord, newWord) {
let hash = prevHash;

const prevValue = this.charToNumber(prevWord[0]);
const newValue = this.charToNumber(newWord[newWord.length - 1]);
const prevValue = prevWord.codePointAt(0);
const newWordChars = Array.from(newWord);
const newValue = newWordChars[newWordChars.length - 1].codePointAt(0);

let prevValueMultiplier = 1;
for (let i = 1; i < prevWord.length; i += 1) {
for (let i = 1; i < newWordChars.length; i += 1) {
prevValueMultiplier *= this.base;
prevValueMultiplier %= this.modulus;
}
@@ -67,23 +68,4 @@ export default class PolynomialHash {

return hash;
}

/**
* Converts char to number.
*
* @param {string} char
* @return {number}
*/
charToNumber(char) {
let charCode = char.codePointAt(0);

// Check if character has surrogate pair.
const surrogate = char.codePointAt(1);
if (surrogate !== undefined) {
const surrogateShift = 2 ** 16;
charCode += surrogate * surrogateShift;
}

return charCode;
}
}
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ describe('PolynomialHash', () => {
fc.property(
fc.constantFrom(3, 79, 101, 3251, 13229, 122743, 3583213),
fc.integer(2, 0x7fffffff),
fc.string(0, 50),
fc.char(),
fc.char(),
fc.fullUnicodeString(0, 50),
fc.fullUnicode(),
fc.fullUnicode(),
(base, modulus, commonWord, previousChar, newChar) => {
fc.pre(base * modulus + 0x10ffff < 0x7fffffff); // avoid overflows

@@ -41,6 +41,6 @@ describe('PolynomialHash', () => {
expect(polynomialHash.hash('ab')).toBe(87);

// @TODO: Provide Unicode support.
expect(polynomialHash.hash('\u{20000}')).toBe(92);
expect(polynomialHash.hash('\u{20000}')).toBe(72);
});
});