You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB

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