1 |
3a515b92
|
cagy
|
//boyer-moore?
|
2 |
|
|
module.exports = function bm(buf,search,offset){
|
3 |
|
|
var m = 0, j = 0
|
4 |
|
|
var table = []
|
5 |
|
|
|
6 |
|
|
var ret = -1;
|
7 |
|
|
for(var i=offset||0;i<buf.length;++i){
|
8 |
|
|
console.log('i',i)
|
9 |
|
|
|
10 |
|
|
table[i] = [[i,0]]
|
11 |
|
|
if(buf[i] === search[0]) {
|
12 |
|
|
for(j = search.length-1;j>0;--j){
|
13 |
|
|
table[i].push([i+j,j])
|
14 |
|
|
console.log('j',j)
|
15 |
|
|
if(buf[i+j] !== search[j]) {
|
16 |
|
|
|
17 |
|
|
//i += j
|
18 |
|
|
j = -1
|
19 |
|
|
break
|
20 |
|
|
}
|
21 |
|
|
}
|
22 |
|
|
if(j === 0) {
|
23 |
|
|
ret = i
|
24 |
|
|
break
|
25 |
|
|
}
|
26 |
|
|
}
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
console.log(table)
|
30 |
|
|
renderTable(table,buf,search)
|
31 |
|
|
return ret
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
var chalk = require('chalk')
|
36 |
|
|
function renderTable(table,buf,search){
|
37 |
|
|
var s = ''
|
38 |
|
|
|
39 |
|
|
console.log('-----')
|
40 |
|
|
console.log('search:',search)
|
41 |
|
|
console.log('-----')
|
42 |
|
|
console.log(buf+'')
|
43 |
|
|
|
44 |
|
|
table.forEach(function(a){
|
45 |
|
|
if(!a) return;// console.log('')
|
46 |
|
|
a.forEach(function(v){
|
47 |
|
|
if(!v) return;
|
48 |
|
|
var pad = ''
|
49 |
|
|
while(pad.length < v[0]){
|
50 |
|
|
pad += ' '
|
51 |
|
|
}
|
52 |
|
|
if(search[v[1]] === buf[v[0]]) console.log(pad+chalk.green(search[v[1]]))
|
53 |
|
|
else console.log(pad+chalk.red(search[v[1]]))
|
54 |
|
|
|
55 |
|
|
})
|
56 |
|
|
})
|
57 |
|
|
console.log('-----')
|
58 |
|
|
}
|