Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexOf that conforms to nodejs 10 tests #59

Merged
merged 2 commits into from Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 91 additions & 3 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 @@ -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