From 34488b61354aa12ddff8a8291d9cd2662c1402de Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 9 Mar 2015 15:28:41 -0700 Subject: [PATCH] buffer: add indexOf() method Add Buffer#indexOf(). Support strings, numbers and other Buffers. Supported in iojs@1.5.0, see: https://github.com/iojs/io.js/commit/f212ae702e57f6d6548972e10d4374a000e 53e19 --- index.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/index.js b/index.js index e859ac3..8ef7e82 100644 --- a/index.js +++ b/index.js @@ -308,6 +308,47 @@ Buffer.prototype.compare = function compare (b) { return Buffer.compare(this, b) } +Buffer.prototype.indexOf = function indexOf (val, byteOffset) { + if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff + else if (byteOffset < -0x80000000) byteOffset = -0x80000000 + byteOffset >>= 0 + + if (this.length === 0) return -1 + if (byteOffset >= this.length) return -1 + + // Negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0) + + if (typeof val === 'string') { + if (val.length === 0) return -1 // special case: looking for empty string always fails + return String.prototype.indexOf.call(this, val, byteOffset) + } + if (Buffer.isBuffer(val)) { + return arrayIndexOf(this, val, byteOffset) + } + if (typeof val === 'number') { + if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') { + return Uint8Array.prototype.indexOf.call(this, val, byteOffset) + } + return arrayIndexOf(this, [ val ], byteOffset) + } + + function arrayIndexOf (arr, val, byteOffset) { + var foundIndex = -1 + for (var i = 0; byteOffset + i < arr.length; i++) { + if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) { + if (foundIndex === -1) foundIndex = i + if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex + } else { + foundIndex = -1 + } + } + return -1 + } + + throw new TypeError('val must be string, number or Buffer') +} + // `get` will be removed in Node 0.13+ Buffer.prototype.get = function get (offset) { console.log('.get() is deprecated. Access using array indexes instead.') @@ -1092,6 +1133,7 @@ Buffer._augment = function _augment (arr) { arr.toJSON = BP.toJSON arr.equals = BP.equals arr.compare = BP.compare + arr.indexOf = BP.indexOf arr.copy = BP.copy arr.slice = BP.slice arr.readUIntLE = BP.readUIntLE -- 2.34.1