Skip to content

Commit

Permalink
indexOf that conforms to nodejs 10 tests
Browse files Browse the repository at this point in the history
This is a JS implementation of indexOf for searching for BufferLists, Buffers, strings and numbers inside a BufferList. It passes the node 10 buffer indexof test suite and works on node 4+.

There are quite of a few performance improvements possible, single byte search values can use the native Buffer.indexOf command. Multibyte buffer search values could too with some extra work. I don't believe searching for another buffer list can be made any faster.

This takes largely from two places and brings them together
- The nodejs buffer index of tests https://github.com/nodejs/node/blob/5d8373a498a50b1387464391402ef22636439303/test/parallel/test-buffer-indexof.js
- from @soldair's node-buffer-indexof https://www.npmjs.com/package/buffer-indexof
  • Loading branch information
reconbot committed Oct 3, 2018
1 parent f13bdda commit 71fff46
Show file tree
Hide file tree
Showing 2 changed files with 511 additions and 0 deletions.
61 changes: 61 additions & 0 deletions bl.js
@@ -1,3 +1,4 @@
'use strict'
var DuplexStream = require('readable-stream').Duplex
, util = require('util')
, Buffer = require('safe-buffer').Buffer
Expand Down Expand Up @@ -251,6 +252,66 @@ BufferList.prototype.destroy = function destroy () {
this.push(null)
}

BufferList.prototype.indexOf = function (search, offset, encoding) {
if (encoding === undefined && typeof offset === 'string') {
encoding = offset;
offset = undefined;
}
if (typeof search === 'function' || Array.isArray(search)) {
throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
} else if (typeof search === 'number') {
search = Buffer.from([search])
} else if (typeof search === 'string') {
search = Buffer.from(search, encoding)
} else if (!search instanceof BufferList && !Buffer.isBuffer(search)) {
search = Buffer.from(search)
}
search = new BufferList(search)

offset = Number(offset || 0)
if (isNaN(offset)) {
offset = 0
}

if (offset < 0) {
offset = this.length + offset
}

if (offset < 0) {
offset = 0
}

if (search.length === 0) {
return offset > this.length ? this.length : offset
}

let searchOffset = 0;
let searchPosition = -1;

for (let blSearchOffset = offset; blSearchOffset < this.length ; ++blSearchOffset) {
if(this.get(blSearchOffset) != search.get(searchOffset)){
searchPosition = -1
blSearchOffset -= searchOffset-1
searchOffset = 0
}

if(this.get(blSearchOffset) == search.get(searchOffset)) {
if(searchPosition == -1) {
searchPosition = blSearchOffset
}
++searchOffset
if(searchOffset == search.length) {
break;
}
}
}

if (searchPosition > -1 && this.length - searchPosition < search.length) {
return -1
}
return searchPosition
}


;(function () {
var methods = {
Expand Down

0 comments on commit 71fff46

Please sign in to comment.