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.

87 lines
2.3 KiB

  1. Description
  2. ===========
  3. streamsearch is a module for [node.js](http://nodejs.org/) that allows searching a stream using the Boyer-Moore-Horspool algorithm.
  4. This module is based heavily on the Streaming Boyer-Moore-Horspool C++ implementation by Hongli Lai [here](https://github.com/FooBarWidget/boyer-moore-horspool).
  5. Requirements
  6. ============
  7. * [node.js](http://nodejs.org/) -- v0.8.0 or newer
  8. Installation
  9. ============
  10. npm install streamsearch
  11. Example
  12. =======
  13. ```javascript
  14. var StreamSearch = require('streamsearch'),
  15. inspect = require('util').inspect;
  16. var needle = new Buffer([13, 10]), // CRLF
  17. s = new StreamSearch(needle),
  18. chunks = [
  19. new Buffer('foo'),
  20. new Buffer(' bar'),
  21. new Buffer('\r'),
  22. new Buffer('\n'),
  23. new Buffer('baz, hello\r'),
  24. new Buffer('\n world.'),
  25. new Buffer('\r\n Node.JS rules!!\r\n\r\n')
  26. ];
  27. s.on('info', function(isMatch, data, start, end) {
  28. if (data)
  29. console.log('data: ' + inspect(data.toString('ascii', start, end)));
  30. if (isMatch)
  31. console.log('match!');
  32. });
  33. for (var i = 0, len = chunks.length; i < len; ++i)
  34. s.push(chunks[i]);
  35. // output:
  36. //
  37. // data: 'foo'
  38. // data: ' bar'
  39. // match!
  40. // data: 'baz, hello'
  41. // match!
  42. // data: ' world.'
  43. // match!
  44. // data: ' Node.JS rules!!'
  45. // match!
  46. // data: ''
  47. // match!
  48. ```
  49. API
  50. ===
  51. Events
  52. ------
  53. * **info**(< _boolean_ >isMatch[, < _Buffer_ >chunk, < _integer_ >start, < _integer_ >end]) - A match _may_ or _may not_ have been made. In either case, a preceding `chunk` of data _may_ be available that did not match the needle. Data (if available) is in `chunk` between `start` (inclusive) and `end` (exclusive).
  54. Properties
  55. ----------
  56. * **maxMatches** - < _integer_ > - The maximum number of matches. Defaults to Infinity.
  57. * **matches** - < _integer_ > - The current match count.
  58. Functions
  59. ---------
  60. * **(constructor)**(< _mixed_ >needle) - Creates and returns a new instance for searching for a _Buffer_ or _string_ `needle`.
  61. * **push**(< _Buffer_ >chunk) - _integer_ - Processes `chunk`. The return value is the last processed index in `chunk` + 1.
  62. * **reset**() - _(void)_ - Resets internal state. Useful for when you wish to start searching a new/different stream for example.