Skip to content

Commit

Permalink
Merge pull request #59 from reconbot/reconbot/indexOf
Browse files Browse the repository at this point in the history
indexOf that conforms to nodejs 10 tests
  • Loading branch information
mcollina authored Oct 4, 2018
2 parents f13bdda + 104b478 commit de8ba93
Show file tree
Hide file tree
Showing 2 changed files with 543 additions and 3 deletions.
94 changes: 91 additions & 3 deletions bl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
var DuplexStream = require('readable-stream').Duplex
, util = require('util')
, Buffer = require('safe-buffer').Buffer
Expand Down Expand Up @@ -43,18 +44,27 @@ BufferList.prototype._offset = function _offset (offset) {
if (offset === 0) return [ 0, 0 ]
for (; i < this._bufs.length; i++) {
_t = tot + this._bufs[i].length
if (offset < _t || i == this._bufs.length - 1)
if (offset < _t || i == this._bufs.length - 1) {
return [ i, offset - tot ]
}
tot = _t
}
}

BufferList.prototype._reverseOffset = function (blOffset) {
const bufferId = blOffset[0]
let offset = blOffset[1]
for (let i = 0; i < bufferId; i++) {
offset += this._bufs[i].length
}
return offset
}

BufferList.prototype.append = function append (buf) {
var i = 0

if (Buffer.isBuffer(buf)) {
this._appendBuffer(buf);
this._appendBuffer(buf)
} else if (Array.isArray(buf)) {
for (; i < buf.length; i++)
this.append(buf[i])
Expand All @@ -68,7 +78,7 @@ BufferList.prototype.append = function append (buf) {
if (typeof buf == 'number')
buf = buf.toString()

this._appendBuffer(Buffer.from(buf));
this._appendBuffer(Buffer.from(buf))
}

return this
Expand Down Expand Up @@ -251,6 +261,84 @@ 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
}

// Use the native buffer indexOf
if (search.length === 1) {
const searchBuffer = search.slice()
const blOffset = this._offset(offset)
let blIndex = blOffset[0]
let buffOffset = blOffset[1]

for (blIndex; blIndex < this._bufs.length; blIndex++) {
let position = this._bufs[blIndex].indexOf(searchBuffer, buffOffset)
if (position !== -1) {
return this._reverseOffset([blIndex, position])
}
buffOffset = 0
}
return -1
}


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
Loading

0 comments on commit de8ba93

Please sign in to comment.