1
- /*
1
+ /*
2
2
*
3
3
*
4
4
*Implementation of the Boyer-Moore String Search Algo.
11
11
*
12
12
**/
13
13
const buildBadMatchTable = ( str ) => {
14
- let tableObj = { } ;
15
- let strLength = str . length ;
16
- for ( var i = 0 ; i < strLength - 1 ; i ++ ) {
17
- tableObj [ str [ i ] ] = strLength - 1 - i ;
14
+ const tableObj = { } ;
15
+ const strLength = str . length ;
16
+ for ( let i = 0 ; i < strLength - 1 ; i ++ ) {
17
+ tableObj [ str [ i ] ] = strLength - 1 - i ;
18
18
}
19
- if ( tableObj [ str [ strLength - 1 ] ] == undefined ) {
20
- tableObj [ str [ strLength - 1 ] ] = strLength ;
19
+ if ( tableObj [ str [ strLength - 1 ] ] = == undefined ) {
20
+ tableObj [ str [ strLength - 1 ] ] = strLength ;
21
21
}
22
22
return tableObj ;
23
23
}
24
24
25
25
26
- let boyerMoore = ( str , pattern ) => {
27
- var badMatchTable = buildBadMatchTable ( pattern ) ,
28
- offset = 0 ,
29
- patternLastIndex = pattern . length - 1 ,
30
- scanIndex = patternLastIndex ,
31
- maxOffset = str . length - pattern . length ;
26
+ const boyerMoore = ( str , pattern ) => {
27
+ let badMatchTable = buildBadMatchTable ( pattern ) ;
28
+ let offset = 0 ;
29
+ let patternLastIndex = pattern . length - 1 ;
30
+ let scanIndex = patternLastIndex ;
31
+ let maxOffset = str . length - pattern . length ;
32
32
// if the offset is bigger than maxOffset, cannot be found
33
33
while ( offset <= maxOffset ) {
34
34
scanIndex = 0 ;
@@ -39,7 +39,7 @@ let boyerMoore = (str, pattern)=> {
39
39
}
40
40
scanIndex ++ ;
41
41
}
42
- var badMatchString = str [ offset + patternLastIndex ] ;
42
+ const badMatchString = str [ offset + patternLastIndex ] ;
43
43
if ( badMatchTable [ badMatchString ] ) {
44
44
// increase the offset if it exists
45
45
offset += badMatchTable [ badMatchString ]
@@ -49,10 +49,4 @@ let boyerMoore = (str, pattern)=> {
49
49
}
50
50
return - 1 ;
51
51
}
52
- export { boyerMoore }
53
-
54
-
55
-
56
-
57
-
58
-
52
+ export { boyerMoore }
0 commit comments