Skip to content

Commit 68089b7

Browse files
committed
v1.0.0
1 parent 02c4c10 commit 68089b7

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.*.swp
3+
.DS_Store
4+
*.log

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1-
# knuth-morris-pratt
1+
knuth-morris-pratt
2+
==================
3+
4+
An implementation of the
5+
[Knuth-Morris-Pratt](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm)
6+
string searching algorithm in JavaScript (it works with strings ***and*** arrays!).
7+
8+
License
9+
-------
10+
11+
Copyright (c) 2015 Julian Fleischer
12+
13+
Permission is hereby granted, free of charge, to any
14+
person obtaining a copy of this software and associated
15+
documentation files (the "Software"), to deal in the
16+
Software without restriction, including without
17+
limitation the rights to use, copy, modify, merge,
18+
publish, distribute, sublicense, and/or sell copies of
19+
the Software, and to permit persons to whom the Software
20+
is furnished to do so, subject to the following
21+
conditions:
22+
23+
The above copyright notice and this permission notice
24+
shall be included in all copies or substantial portions
25+
of the Software.
26+
27+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
28+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
29+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35+
IN THE SOFTWARE.
36+

index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* vim: set et sw=2 ts=2: */
2+
3+
/**
4+
* `indexOf` is an implementation of the Knuth-Morrison-Pratt
5+
* Algorithm for finding the index of a given subsequence
6+
* (identified as `word` here) within a sequence (identified as
7+
* `string` here). Thanks to JavaScript's dynamic nature it
8+
* works equally well with strings and arrays.
9+
*/
10+
module.exports = function indexOf(word, string) {
11+
'use strict';
12+
13+
var m = 0;
14+
var i = 0;
15+
var table = [];
16+
17+
var pos = 2;
18+
var cnd = 0;
19+
20+
table[0] = -1;
21+
table[1] = 0;
22+
23+
// build the table for KMP. This takes `O(word.length)` steps.
24+
while (pos < word.length) {
25+
if (word[pos - 1] == word[cnd]) {
26+
cnd = cnd + 1;
27+
table[pos] = cnd;
28+
pos = pos + 1;
29+
} else if (cnd > 0) {
30+
cnd = table[cnd];
31+
} else {
32+
table[pos] = 0;
33+
pos = pos + 1;
34+
}
35+
}
36+
37+
// scan the string. This takes `O(string.length)` steps.
38+
while (m + i < string.length) {
39+
if (word[i] == string[m + i]) {
40+
if (i == word.length - 1) {
41+
return m;
42+
}
43+
i = i + 1;
44+
} else {
45+
if (table[i] > -1) {
46+
m = m + i - table[i];
47+
i = table[i];
48+
} else {
49+
i = 0;
50+
m = m + 1;
51+
}
52+
}
53+
}
54+
// Returns -1 if the subsequence was not found in the sequence.
55+
return -1;
56+
};

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "An implementation of the Knuth-Morris-Pratt string searching algorithm.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jshint index.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -21,9 +21,12 @@
2121
"kmp"
2222
],
2323
"author": "Julian Alexander Fleischer <tirednesscankill@warhog.net>",
24-
"license": "ISC",
24+
"license": "MIT",
2525
"bugs": {
2626
"url": "https://github.com/nodash/knuth-morris-pratt/issues"
2727
},
28-
"homepage": "https://github.com/nodash/knuth-morris-pratt"
28+
"homepage": "https://github.com/nodash/knuth-morris-pratt",
29+
"devDependencies": {
30+
"jshint": "^2.8.0"
31+
}
2932
}

0 commit comments

Comments
 (0)