Skip to content

Commit 502181c

Browse files
♻️ refactor: Use startsWith from @string-searching/brute-force.
Maybe this function should go to a separate package at some point. Note that String.prototype.startsWith could be used if we could restrict ourselves to pi = 0 and pj = p.length.
1 parent 44abd6a commit 502181c

File tree

5 files changed

+13
-28
lines changed

5 files changed

+13
-28
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
5959
"test": "ava"
6060
},
61-
"dependencies": {},
61+
"dependencies": {
62+
"@string-searching/brute-force": "^0.0.1"
63+
},
6264
"devDependencies": {
6365
"@babel/core": "7.14.0",
6466
"@babel/preset-env": "7.14.0",

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export {default as match} from './match.js';
21
export {default as rabinKarp} from './rabinKarp.js';

src/match.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/rabinKarp.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import assert from 'assert';
2-
import match from './match.js';
2+
import {startsWith} from '@string-searching/brute-force';
33

44
/**
5-
* Note that q should be random and about m (log m bits) for best performance.
5+
* Note that q should be random and about m (that is, log m bits) for best
6+
* performance.
67
* Make sure d * (q-1) <= Number.MAX_SAFE_INTEGER
78
* and q + (q-1) <= Number.MAX_SAFE_INTEGER. Otherwise use bigints.
89
*
@@ -53,7 +54,7 @@ const rabinKarp = (code, d, q, one = 1) => {
5354
const j = sj - m;
5455
let i = si;
5556
for (; i < j; ++i) {
56-
if (sh === ph && match(m, s, i, p, pi)) yield i;
57+
if (sh === ph && startsWith(p, pi, pj, s, i)) yield i;
5758

5859
sh -= ((code(s[i]) % q) * of) % q;
5960
sh += q;
@@ -65,7 +66,7 @@ const rabinKarp = (code, d, q, one = 1) => {
6566
console.debug({s: s[i + m], sh, ph, of});
6667
}
6768

68-
if (sh === ph && match(m, s, i, p, pi)) yield i;
69+
if (sh === ph && startsWith(p, pi, pj, s, i)) yield i;
6970
};
7071

7172
return findAll;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,11 @@
13441344
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-4.0.1.tgz#d26729db850fa327b7cacc5522252194404226f5"
13451345
integrity sha512-Qm9hBEBu18wt1PO2flE7LPb30BHMQt1eQgbV76YntdNk73XZGpn3izvGTYxbGgzXKgbCjiia0uxTd3aTNQrY/g==
13461346

1347+
"@string-searching/brute-force@^0.0.1":
1348+
version "0.0.1"
1349+
resolved "https://registry.yarnpkg.com/@string-searching/brute-force/-/brute-force-0.0.1.tgz#d0c0f3b07abf88b73af48b05e0d7e8c3a990b8cf"
1350+
integrity sha512-EcQA4atJJ9YwMSG3DWSvnGSAUMVuby9WjJ+FoF7U4XYpsCu2qyS55IQso6F22wZk6ZwobmwXw3SuQwYUbdwgeQ==
1351+
13471352
"@string-searching/specification@^0.0.1":
13481353
version "0.0.1"
13491354
resolved "https://registry.yarnpkg.com/@string-searching/specification/-/specification-0.0.1.tgz#25d992d1c11b441bbf0b72dc0045a85bc39991f4"

0 commit comments

Comments
 (0)