Comparing version 0.0.4 to 0.1.0
89
index.js
@@ -168,26 +168,95 @@ module.exports = Buffers; | ||
Buffers.prototype.get = function get (i) { | ||
Buffers.prototype.pos = function (i) { | ||
if (i < 0 || i >= this.length) throw new Error('oob'); | ||
var l = i, bi = 0, bu = null; | ||
for (;;) { | ||
bu = this.buffers[bi++]; | ||
bu = this.buffers[bi]; | ||
if (l < bu.length) { | ||
return bu.get(l); | ||
return {buf: bi, offset: l}; | ||
} else { | ||
l -= bu.length; | ||
} | ||
bi++; | ||
} | ||
}; | ||
Buffers.prototype.get = function get (i) { | ||
var pos = this.pos(i); | ||
return this.buffers[pos.buf].get(pos.offset); | ||
}; | ||
Buffers.prototype.set = function set (i, b) { | ||
if (i < 0 || i >= this.length) throw new Error('oob'); | ||
var l = i, bi = 0, bu = null; | ||
var pos = this.pos(i); | ||
return this.buffers[pos.buf].set(pos.offset, b); | ||
}; | ||
Buffers.prototype.indexOf = function (needle, offset) { | ||
if ("string" === typeof needle) { | ||
needle = new Buffer(needle); | ||
} else if (needle instanceof Buffer) { | ||
// already a buffer | ||
} else { | ||
throw new Error('Invalid type for a search string'); | ||
} | ||
if (!needle.length) { | ||
return 0; | ||
} | ||
if (!this.length) { | ||
return -1; | ||
} | ||
var i = 0, j = 0, match = 0, mstart, pos = 0; | ||
// start search from a particular point in the virtual buffer | ||
if (offset) { | ||
var p = this.pos(offset); | ||
i = p.buf; | ||
j = p.offset; | ||
pos = offset; | ||
} | ||
// for each character in virtual buffer | ||
for (;;) { | ||
bu = this.buffers[bi++]; | ||
if (l < bu.length) { | ||
return bu.set(l, b); | ||
} else { | ||
l -= bu.length; | ||
while (j >= this.buffers[i].length) { | ||
j = 0; | ||
i++; | ||
if (i >= this.buffers.length) { | ||
// search string not found | ||
return -1; | ||
} | ||
} | ||
var char = this.buffers[i][j]; | ||
if (char == needle[match]) { | ||
// keep track where match started | ||
if (match == 0) { | ||
mstart = { | ||
i: i, | ||
j: j, | ||
pos: pos | ||
}; | ||
} | ||
match++; | ||
if (match == needle.length) { | ||
// full match | ||
return mstart.pos; | ||
} | ||
} else if (match != 0) { | ||
// a partial match ended, go back to match starting position | ||
// this will continue the search at the next character | ||
i = mstart.i; | ||
j = mstart.j; | ||
pos = mstart.pos; | ||
match = 0; | ||
} | ||
j++; | ||
pos++; | ||
} | ||
}; |
{ | ||
"name" : "buffers", | ||
"description" : "Treat a collection of Buffers as a single contiguous partially mutable Buffer.", | ||
"version" : "0.0.4", | ||
"version" : "0.1.0", | ||
"repository" : "http://github.com/substack/node-buffers.git", | ||
@@ -6,0 +6,0 @@ "author" : "James Halliday <mail@substack.net> (http://substack.net)", |
@@ -193,1 +193,18 @@ var assert = require('assert'); | ||
}; | ||
exports.indexOf = function () { | ||
var bufs = Buffers(); | ||
bufs.push(new Buffer("Hel")); | ||
bufs.push(new Buffer("lo,")); | ||
bufs.push(new Buffer(" how are ")); | ||
bufs.push(new Buffer("you")); | ||
bufs.push(new Buffer("?")); | ||
assert.eql( bufs.indexOf("Hello"), 0 ); | ||
assert.eql( bufs.indexOf("Hello", 1), -1 ); | ||
assert.eql( bufs.indexOf("ello"), 1 ); | ||
assert.eql( bufs.indexOf("ello", 1), 1 ); | ||
assert.eql( bufs.indexOf("ello", 2), -1 ); | ||
assert.eql( bufs.indexOf("e"), 1 ); | ||
assert.eql( bufs.indexOf("e", 2), 13 ); | ||
assert.eql( bufs.indexOf(new Buffer([0x65]), 2), 13 ); | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16916
423
112