Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move fromList onto the Readable class
It is not generally useful anyway.
  • Loading branch information
isaacs committed Oct 4, 2012
1 parent 9a0662b commit 5f3e929
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 63 deletions.
61 changes: 0 additions & 61 deletions from-list.js

This file was deleted.

58 changes: 57 additions & 1 deletion readable.js
Expand Up @@ -4,7 +4,6 @@ module.exports = Readable;

var Stream = require('stream');
var util = require('util');
var fromList = require('./from-list.js');
var assert = require('assert');

util.inherits(Readable, Stream);
Expand Down Expand Up @@ -355,3 +354,60 @@ Readable.prototype.wrap = function(stream) {
return ret;
};
};



// exposed for testing purposes only.
Readable._fromList = fromList;

// Pluck off n bytes from an array of buffers.
// Length is the combined lengths of all the buffers in the list.
function fromList(n, list, length) {
var ret;

// nothing in the list, definitely empty.
if (list.length === 0) {
return null;
}

if (length === 0)
ret = null;
else if (!n || n >= length) {
// read it all, truncate the array.
ret = Buffer.concat(list, length);
list.length = 0;
} else {
// read just some of it.
if (n < list[0].length) {
// just take a part of the first list item.
// slice is the same for buffers and strings.
var buf = list[0];
ret = buf.slice(0, n);
list[0] = buf.slice(n);
} else if (n === list[0].length) {
// first list is a perfect match
ret = list.shift();
} else {
// complex case.
// we have enough to cover it, but it spans past the first buffer.
ret = new Buffer(n);

var c = 0;
for (var i = 0, l = list.length; i < l && c < n; i++) {
var buf = list[0];
var cpy = Math.min(n - c, buf.length);

buf.copy(ret, c, 0, cpy);

if (cpy < buf.length)
list[0] = buf.slice(cpy);
else
list.shift();

c += cpy;
}
}
}

return ret;
}
2 changes: 1 addition & 1 deletion test/from-list.js
@@ -1,5 +1,5 @@
var test = require('tap').test;
var fromList = require('../from-list.js');
var fromList = require('../readable.js')._fromList;

test('with length', function(t) {
// have a length
Expand Down

0 comments on commit 5f3e929

Please sign in to comment.