rearrage_stuff
This commit is contained in:
7
node_modules/buffer-builder/LICENSE.txt
generated
vendored
Normal file
7
node_modules/buffer-builder/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright (C) 2012 Peter Reid
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
97
node_modules/buffer-builder/ReadMe.md
generated
vendored
Normal file
97
node_modules/buffer-builder/ReadMe.md
generated
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
# buffer-builder.js
|
||||
|
||||
BufferBuilder accumulates pieces of data into a buffer, appending each onto the end. The data can be Buffers, strings, a repetition of a byte, or any of the types such as UInt32LE or FloatBE that can be written into Buffers.
|
||||
|
||||
If you are thinking about using this, you should probably have considered streaming your data instead of putting it into a buffer.
|
||||
|
||||
## Usage
|
||||
|
||||
``` js
|
||||
var BufferBuilder = require('buffer-builder');
|
||||
var helloWorld = new BufferBuilder();
|
||||
|
||||
// Append a string, utf8 encoded by default.
|
||||
helloWorld.appendString('hello');
|
||||
|
||||
// Append any type that Buffer has read and write functions for.
|
||||
helloWorld.appendUInt16LE(0x7720);
|
||||
|
||||
// Append a buffer
|
||||
helloWorld.appendBuffer(new Buffer([111, 114, 108, 100]));
|
||||
|
||||
// Appended a repetition of a byte
|
||||
helloWorld.appendFill(33, 3);
|
||||
|
||||
// Convert to an ordinary buffer
|
||||
var buffer = helloWorld.get();
|
||||
|
||||
buffer.toString(); // hello world!!!
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### new BufferBuilder([initialCapacity])
|
||||
Allocate an empty BufferBuilder. If you know approximately what size the Buffer will end up being and are trying to squeeze out more performance, you can set the initial size of the backing buffer.
|
||||
|
||||
### appendBuffer(source)
|
||||
Append a buffer. Use [slice](http://nodejs.org/docs/latest/api/buffers.html#buffer.slice) if you want to append just part of one.
|
||||
|
||||
### appendString(string, [encoding])
|
||||
Append a string, encoded by utf8 by default. No trailing 0 is appended.
|
||||
|
||||
### appendStringZero(string, [encoding])
|
||||
Append a null-terminated string, encoded by utf8 by default.
|
||||
|
||||
### appendUInt8(value)
|
||||
Append 8-bit unsigned integer.
|
||||
|
||||
### appendUInt16LE(value)
|
||||
Append 16-bit unsigned integer, little endian. 1 is encoded as 01 00.
|
||||
|
||||
### appendUInt16BE(value)
|
||||
Append 16-bit unsigned integer, big endian. 1 is encoded as 00 01.
|
||||
|
||||
### appendUInt32LE(value)
|
||||
Append 32-bit unsigned integer, little endian. 1 is encoded as 01 00 00 00.
|
||||
|
||||
### appendUInt32BE(value)
|
||||
Append 32-bit unsigned integer, big endian. 1 is encoded as 00 00 00 01.
|
||||
|
||||
### appendInt8(value)
|
||||
Append 8-bit signed integer.
|
||||
|
||||
### appendInt16LE(value)
|
||||
Append 16-bit signed integer, little endian. 1 is encoded as 01 00.
|
||||
|
||||
### appendInt16BE(value)
|
||||
Append 16-bit signed integer, big endian. 1 is encoded as 00 01.
|
||||
|
||||
### appendInt32LE(value)
|
||||
Append 32-bit signed integer, little endian. 1 is encoded as 01 00 00 00.
|
||||
|
||||
### appendInt32BE(value)
|
||||
Append 32-bit signed integer, big endian. 1 is encoded as 00 00 00 01.
|
||||
|
||||
### appendFloatLE(value)
|
||||
Little-endian float. Occupies 4 bytes.
|
||||
|
||||
### appendFloatBE(value)
|
||||
Big-endian float. Occupies 4 bytes.
|
||||
|
||||
### appendDoubleLE(value)
|
||||
Little-endian double. Occupies 8 bytes.
|
||||
|
||||
### appendDoubleBE(value)
|
||||
Big-endian double. Occupies 8 bytes.
|
||||
|
||||
### appendFill(value, count)
|
||||
Append _count_ repetitions of _value_ (a byte).
|
||||
|
||||
### get()
|
||||
Convert to a buffer. This is a deep copy; modifications to the returned buffer will not affect the BufferBuilder.
|
||||
|
||||
### copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])
|
||||
Copy bytes from the BufferBuilder into _targetBuffer_. _targetStart_ and _sourceStart_ default to 0. _sourceEnd_ defaults to the BufferBuilder's length.
|
||||
|
||||
### length
|
||||
Number of bytes appended so far.
|
||||
175
node_modules/buffer-builder/buffer-builder.js
generated
vendored
Normal file
175
node_modules/buffer-builder/buffer-builder.js
generated
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
module.exports = BufferBuilder;
|
||||
|
||||
function BufferBuilder(initialCapacity) {
|
||||
var buffer = Buffer.isBuffer(initialCapacity) ? initialCapacity : new Buffer(initialCapacity || 512);
|
||||
this.buffers = [buffer];
|
||||
|
||||
this.writeIndex = 0;
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
/* Append a (subsequence of a) Buffer */
|
||||
BufferBuilder.prototype.appendBuffer = function(source) {
|
||||
if (source.length === 0) return this;
|
||||
|
||||
var tail = this.buffers[this.buffers.length-1];
|
||||
|
||||
var spaceInCurrent = tail.length - this.writeIndex;
|
||||
if (source.length <= spaceInCurrent) {
|
||||
// We can fit the whole thing in the current buffer
|
||||
source.copy(tail, this.writeIndex);
|
||||
this.writeIndex += source.length;
|
||||
} else {
|
||||
// Copy as much as we can into the current buffer
|
||||
if (spaceInCurrent) { // Buffer.copy does not handle the degenerate case well
|
||||
source.copy(tail, this.writeIndex);//, start, start + spaceInCurrent);
|
||||
}
|
||||
// Fit the rest into a new buffer. Make sure it is at least as big as
|
||||
// what we're being asked to add, and also follow our double-previous-buffer pattern.
|
||||
var newBuf = new Buffer(Math.max(tail.length*2, source.length));
|
||||
|
||||
this.buffers.push(newBuf);
|
||||
this.writeIndex = source.copy(newBuf, 0, spaceInCurrent);
|
||||
}
|
||||
|
||||
this.length += source.length;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
function makeAppender(encoder, size) {
|
||||
return function(x) {
|
||||
var buf = this.buffers[this.buffers.length-1];
|
||||
if (this.writeIndex + size <= buf.length) {
|
||||
encoder.call(buf, x, this.writeIndex, true);
|
||||
this.writeIndex += size;
|
||||
this.length += size;
|
||||
} else {
|
||||
var scratchBuffer = new Buffer(size);
|
||||
encoder.call(scratchBuffer, x, 0, true);
|
||||
this.appendBuffer(scratchBuffer);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
BufferBuilder.prototype.appendUInt8 = makeAppender(Buffer.prototype.writeUInt8, 1);
|
||||
BufferBuilder.prototype.appendUInt16LE = makeAppender(Buffer.prototype.writeUInt16LE, 2);
|
||||
BufferBuilder.prototype.appendUInt16BE = makeAppender(Buffer.prototype.writeUInt16BE, 2);
|
||||
BufferBuilder.prototype.appendUInt32LE = makeAppender(Buffer.prototype.writeUInt32LE, 4);
|
||||
BufferBuilder.prototype.appendUInt32BE = makeAppender(Buffer.prototype.writeUInt32BE, 4);
|
||||
BufferBuilder.prototype.appendInt8 = makeAppender(Buffer.prototype.writeInt8, 1);
|
||||
BufferBuilder.prototype.appendInt16LE = makeAppender(Buffer.prototype.writeInt16LE, 2);
|
||||
BufferBuilder.prototype.appendInt16BE = makeAppender(Buffer.prototype.writeInt16BE, 2);
|
||||
BufferBuilder.prototype.appendInt32LE = makeAppender(Buffer.prototype.writeInt32LE, 4);
|
||||
BufferBuilder.prototype.appendInt32BE = makeAppender(Buffer.prototype.writeInt32BE, 4);
|
||||
BufferBuilder.prototype.appendFloatLE = makeAppender(Buffer.prototype.writeFloatLE, 4);
|
||||
BufferBuilder.prototype.appendFloatBE = makeAppender(Buffer.prototype.writeFloatBE, 4);
|
||||
BufferBuilder.prototype.appendDoubleLE = makeAppender(Buffer.prototype.writeDoubleLE, 8);
|
||||
BufferBuilder.prototype.appendDoubleBE = makeAppender(Buffer.prototype.writeDoubleBE, 8);
|
||||
|
||||
BufferBuilder.prototype.appendString = function(str, encoding) {
|
||||
return this.appendBuffer(new Buffer(str, encoding));
|
||||
};
|
||||
|
||||
BufferBuilder.prototype.appendStringZero = function(str, encoding) {
|
||||
return this.appendString(str + '\0', encoding);
|
||||
}
|
||||
|
||||
BufferBuilder.prototype.appendFill = function(value, count) {
|
||||
if (!count) return;
|
||||
|
||||
var tail = this.buffers[this.buffers.length-1];
|
||||
|
||||
var spaceInCurrent = tail.length - this.writeIndex;
|
||||
if (count <= spaceInCurrent) {
|
||||
// We can fit the whole thing in the current buffer
|
||||
tail.fill(value, this.writeIndex, this.writeIndex + count);
|
||||
this.writeIndex += count;
|
||||
} else {
|
||||
// Copy as much as we can into the current buffer
|
||||
if (spaceInCurrent) { // does not handle the degenerate case well
|
||||
tail.fill(value, this.writeIndex);
|
||||
}
|
||||
// Fit the rest into a new buffer. Make sure it is at least as big as
|
||||
// what we're being asked to add, and also follow our double-previous-buffer pattern.
|
||||
var newBuf = new Buffer(Math.max(tail.length*2, count));
|
||||
var couldNotFit = count - spaceInCurrent;
|
||||
newBuf.fill(value, 0, couldNotFit);
|
||||
this.buffers.push(newBuf);
|
||||
this.writeIndex = couldNotFit;
|
||||
}
|
||||
|
||||
this.length += count;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/* Convert to a plain Buffer */
|
||||
BufferBuilder.prototype.get = function() {
|
||||
var concatted = new Buffer(this.length);
|
||||
this.copy(concatted);
|
||||
return concatted;
|
||||
};
|
||||
|
||||
/* Copy into targetBuffer */
|
||||
BufferBuilder.prototype.copy = function(targetBuffer, targetStart, sourceStart, sourceEnd) {
|
||||
targetStart || (targetStart = 0);
|
||||
sourceStart || (sourceStart = 0);
|
||||
sourceEnd !== undefined || (sourceEnd = this.length);
|
||||
|
||||
// Validation. Besides making us fail nicely, this makes it so we can skip checks below.
|
||||
if (targetStart < 0 || (targetStart>0 && targetStart >= targetBuffer.length)) {
|
||||
throw new Error('targetStart is out of bounds');
|
||||
}
|
||||
if (sourceEnd < sourceStart) {
|
||||
throw new Error('sourceEnd < sourceStart');
|
||||
}
|
||||
if (sourceStart < 0 || (sourceStart>0 && sourceStart >= this.length)) {
|
||||
throw new Error('sourceStart is out of bounds');
|
||||
}
|
||||
if (sourceEnd > this.length) {
|
||||
throw new Error('sourceEnd out of bounds');
|
||||
}
|
||||
|
||||
sourceEnd = Math.min(sourceEnd, sourceStart + (targetBuffer.length-targetStart));
|
||||
var targetWriteIdx = targetStart;
|
||||
var readBuffer = 0;
|
||||
|
||||
// Skip through our buffers until we get to where the copying should start.
|
||||
var copyLength = sourceEnd - sourceStart;
|
||||
var skipped = 0;
|
||||
while (skipped < sourceStart) {
|
||||
var buffer = this.buffers[readBuffer];
|
||||
if (buffer.length + skipped < targetStart) {
|
||||
skipped += buffer.length;
|
||||
} else {
|
||||
// Do the first copy. This one is different from the others in that it
|
||||
// does not start from the beginning of one of our internal buffers.
|
||||
var copyStart = sourceStart - skipped;
|
||||
var inThisBuffer = Math.min(copyLength, buffer.length - copyStart);
|
||||
|
||||
buffer.copy(targetBuffer, targetWriteIdx, copyStart, copyStart + inThisBuffer);
|
||||
targetWriteIdx += inThisBuffer;
|
||||
copyLength -= inThisBuffer;
|
||||
readBuffer++;
|
||||
break;
|
||||
}
|
||||
readBuffer++;
|
||||
}
|
||||
|
||||
// Copy the rest. Note that we can't run off of our end because we validated the range up above
|
||||
while (copyLength > 0) {
|
||||
var buffer = this.buffers[readBuffer];
|
||||
var toCopy = Math.min(buffer.length, copyLength);
|
||||
|
||||
buffer.copy(targetBuffer, targetWriteIdx, 0, toCopy);
|
||||
copyLength -= toCopy;
|
||||
targetWriteIdx += toCopy;
|
||||
readBuffer++;
|
||||
}
|
||||
|
||||
// Return how many bytes were copied
|
||||
return sourceEnd - sourceStart;
|
||||
};
|
||||
21
node_modules/buffer-builder/package.json
generated
vendored
Normal file
21
node_modules/buffer-builder/package.json
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name" : "buffer-builder",
|
||||
"version" : "0.2.0",
|
||||
"description" : "Build a buffer without knowing its size beforehand",
|
||||
"main" : "buffer-builder",
|
||||
"repository" : {
|
||||
"type" : "git",
|
||||
"url" : "http://github.com/PeterReid/node-buffer-builder.git"
|
||||
},
|
||||
"keywords" : [
|
||||
"buffer-builder", "buffer"
|
||||
],
|
||||
"author" : {
|
||||
"name" : "Peter Reid",
|
||||
"email" : "peter.d.reid@gmail.com"
|
||||
},
|
||||
"license" : "MIT/X11",
|
||||
"engine" : {
|
||||
"node" : ">=0.6.0"
|
||||
}
|
||||
}
|
||||
294
node_modules/buffer-builder/test/buffer-builder-test.js
generated
vendored
Normal file
294
node_modules/buffer-builder/test/buffer-builder-test.js
generated
vendored
Normal file
@@ -0,0 +1,294 @@
|
||||
var assert = require('assert');
|
||||
var BufferBuilder = require('../buffer-builder');
|
||||
|
||||
/* Build 1 2 3 3 4 5 4 5 6 7 ... and make sure it comes out right */
|
||||
(function() {
|
||||
var builder = new BufferBuilder(7);
|
||||
var top = 100;
|
||||
for (var base = 1; base < top; base++) {
|
||||
var xs = [];
|
||||
for (var offset = 0; offset < base; offset++) {
|
||||
xs.push(base + offset);
|
||||
}
|
||||
builder.appendBuffer(new Buffer(xs));
|
||||
}
|
||||
|
||||
var result = builder.get();
|
||||
var idx = 0;
|
||||
for (var base = 1; base < top; base++) {
|
||||
for (var offset = 0; offset < base; offset++) {
|
||||
assert.equal(result[idx++], base+offset);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
/* Build 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 ... and make sure it comes out right. */
|
||||
(function() {
|
||||
var builder = new BufferBuilder(3);
|
||||
var top = 20;
|
||||
for (var i = 1; i < top; i++) {
|
||||
builder.appendFill(i, i);
|
||||
}
|
||||
|
||||
var result = builder.get();
|
||||
var idx = 0;
|
||||
for (var i = 1; i < top; i++) {
|
||||
for (var j = 0; j < i; j++) {
|
||||
assert.equal(result[idx], i);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
/* Numeric types come out like they went in */
|
||||
(function() {
|
||||
var builder = new BufferBuilder();
|
||||
var n = 500;
|
||||
|
||||
var buildFns = BufferBuilder.prototype;
|
||||
var readFns = Buffer.prototype;
|
||||
var entries = [];
|
||||
|
||||
function Entry(buildFn, readFn, length, value) {
|
||||
this.buildFn = buildFn;
|
||||
this.readFn = readFn;
|
||||
this.length = length;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
for (var i = -n; i <= n; i++) {
|
||||
entries.push(new Entry(buildFns.appendInt32LE, readFns.readInt32LE, 4, i));
|
||||
entries.push(new Entry(buildFns.appendInt32BE, readFns.readInt32BE, 4, i));
|
||||
entries.push(new Entry(buildFns.appendUInt32LE, readFns.readUInt32LE, 4, Math.abs(i)));
|
||||
entries.push(new Entry(buildFns.appendUInt32BE, readFns.readUInt32BE, 4, Math.abs(i)));
|
||||
entries.push(new Entry(buildFns.appendInt16LE, readFns.readInt16LE, 2, i));
|
||||
entries.push(new Entry(buildFns.appendInt16BE, readFns.readInt16BE, 2, i));
|
||||
entries.push(new Entry(buildFns.appendUInt16LE, readFns.readUInt16LE, 2, Math.abs(i)));
|
||||
entries.push(new Entry(buildFns.appendUInt16BE, readFns.readUInt16BE, 2, Math.abs(i)));
|
||||
entries.push(new Entry(buildFns.appendInt8, readFns.readInt8, 1, Math.round(i/50)));
|
||||
entries.push(new Entry(buildFns.appendUInt8, readFns.readUInt8, 1, Math.abs(Math.round(i/50))));
|
||||
entries.push(new Entry(buildFns.appendFloatLE, readFns.readFloatLE, 4, i));
|
||||
entries.push(new Entry(buildFns.appendFloatBE, readFns.readFloatBE, 4, i));
|
||||
entries.push(new Entry(buildFns.appendDoubleLE, readFns.readDoubleLE, 8, i));
|
||||
entries.push(new Entry(buildFns.appendDoubleBE, readFns.readDoubleBE, 8, i));
|
||||
}
|
||||
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var entry = entries[i];
|
||||
entry.buildFn.call(builder, entry.value);
|
||||
}
|
||||
|
||||
var result = builder.get();
|
||||
var idx = 0;
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var entry = entries[i];
|
||||
var read = entry.readFn.call(result, idx);
|
||||
assert.equal(read, entry.value);
|
||||
idx += entry.length;
|
||||
}
|
||||
})();
|
||||
|
||||
/* Newly-created buffer should be empty */
|
||||
(function() {
|
||||
assert.equal(new BufferBuilder().get().length, 0)
|
||||
})();
|
||||
|
||||
/* It should concatenating strings. */
|
||||
(function() {
|
||||
var words = ['This', 'is', 'a', 'test'];
|
||||
var builder = new BufferBuilder();
|
||||
for (var i = 0; i < words.length; i++) {
|
||||
builder.appendString(words[i]);
|
||||
}
|
||||
|
||||
assert.equal(builder.get().toString(), words.join(''));
|
||||
})();
|
||||
|
||||
/* Test appendStringZero */
|
||||
(function() {
|
||||
var words = ['alpha', 'bravo', 'charlie', 'delta'];
|
||||
var builder = new BufferBuilder();
|
||||
for (var i = 0; i < words.length; i++) {
|
||||
builder.appendStringZero(words[i]);
|
||||
}
|
||||
|
||||
function appendZero(str) { return str + '\0'; }
|
||||
assert.equal(builder.get().toString() , words.map(appendZero).join(''));
|
||||
})();
|
||||
|
||||
/* copy() sanity checks */
|
||||
(function() {
|
||||
var check = function(low, high, targetStart, sourceStart, sourceEnd) {
|
||||
var source = new BufferBuilder();
|
||||
source.appendFill(1, 10);
|
||||
var dest = new Buffer(20);
|
||||
dest.fill(0);
|
||||
|
||||
|
||||
source.copy(dest, targetStart, sourceStart, sourceEnd);
|
||||
for (var i = 0; i < 20; i++) {
|
||||
assert.equal(dest[i], low<=i && i<high ? 1 : 0);
|
||||
}
|
||||
};
|
||||
|
||||
check(0, 10);
|
||||
check(3, 13, 3);
|
||||
check(3, 10, 3, 3);
|
||||
check(10, 14, 10, 0, 4);
|
||||
})();
|
||||
|
||||
/* copy() applied over ranges */
|
||||
(function() {
|
||||
var source = new BufferBuilder(3);
|
||||
for (var i = 1; i < 100; i++) {
|
||||
source.appendUInt8(i);
|
||||
}
|
||||
|
||||
for (var i = 0; i < 20; i++) {
|
||||
var sourceStart = i*3;
|
||||
var sourceEnd = source.length - i*5;
|
||||
var targetStart = i*20;
|
||||
|
||||
var target = new Buffer(1000);
|
||||
target.fill(0);
|
||||
source.copy(target, targetStart, sourceStart, sourceEnd);
|
||||
|
||||
for (var i = 0; i < target.length; i++) {
|
||||
var expected;
|
||||
if (i >= targetStart && i < targetStart + sourceEnd - sourceStart) {
|
||||
expected = i - targetStart + sourceStart + 1;
|
||||
} else {
|
||||
expected = 0;
|
||||
}
|
||||
assert.equal(expected, target[i]);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
/* Overlap small chunks over one large copy, making sure that does not change anything. */
|
||||
(function() {
|
||||
var n = 1000;
|
||||
var builder = new BufferBuilder(10);
|
||||
var x = .4;
|
||||
for (var i = 0; i < n; i++) {
|
||||
builder.appendDoubleLE(x);
|
||||
x = x * (1-x) * 4;
|
||||
}
|
||||
|
||||
var buffersMatchOneOff = function(b1, b2) {
|
||||
// Why one off? Because then sourceStart != targetStart, so I can't have mixed those up in copy().
|
||||
assert.equal(b1.length+1, b2.length);
|
||||
for (var i = 0; i < b1.length; i++) {
|
||||
assert.equal(b1[i], b2[i+1], 'byte ' + i + ' mismatch');
|
||||
}
|
||||
}
|
||||
|
||||
var original = builder.get();
|
||||
var copyTarget = new Buffer(original.length+1);
|
||||
original.copy(copyTarget, 1);
|
||||
|
||||
buffersMatchOneOff(original, copyTarget);
|
||||
var copyLength = 105;
|
||||
var copyStart = 4;
|
||||
var copyOffset = 143;
|
||||
for (var i = copyStart; i+copyLength < builder.length; i += copyOffset) {
|
||||
builder.copy(copyTarget, i+1, i, i + copyLength);
|
||||
//console.log(copyTarget);
|
||||
buffersMatchOneOff(original, copyTarget);
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
|
||||
/* Try a copy that starts right on a buffer boundary */
|
||||
(function() {
|
||||
var builder = new BufferBuilder(3);
|
||||
for (var i = 0; i < 10; i++) {
|
||||
builder.appendUInt8(i);
|
||||
}
|
||||
|
||||
var dest = new Buffer(7);
|
||||
builder.copy(dest, 0, 3);
|
||||
for (var i = 0; i < 7; i++) {
|
||||
assert.equal(dest[i], i+3);
|
||||
}
|
||||
})();
|
||||
|
||||
/* BufferBuilder.copy() should behave just like Buffer.copy(), including the exceptions thrown */
|
||||
(function() {
|
||||
var builder = new BufferBuilder(5);
|
||||
for (var i = 0; i < 100; i++) {
|
||||
builder.appendUInt8(i);
|
||||
}
|
||||
var buffer = builder.get();
|
||||
|
||||
var dest = new Buffer(50);
|
||||
|
||||
var behavesSame = function(f) {
|
||||
var bufferException, bufferResult, builderException, builderResult;
|
||||
try {
|
||||
bufferResult = f(buffer);
|
||||
} catch (e) {
|
||||
bufferException = e;
|
||||
}
|
||||
try {
|
||||
builderResult = f(buffer);
|
||||
} catch (e) {
|
||||
builderException = e;
|
||||
}
|
||||
|
||||
assert.equal(bufferResult, builderResult);
|
||||
assert.equal(''+bufferException, ''+builderException);
|
||||
};
|
||||
|
||||
// copies that get cut off because target is small
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50), 20) });
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50)) });
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50)) });
|
||||
|
||||
// targetStart too low
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, -1)) });
|
||||
// targetStart too high
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 50)) });
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 51)) });
|
||||
|
||||
// sourceStart too low
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, -4)) });
|
||||
// sourceStart too high
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 130)) });
|
||||
|
||||
// copy cut off by end of source
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 99)) });
|
||||
|
||||
// degenerate copy
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 99, 99)) });
|
||||
|
||||
// sourceEnd after sourceStart
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 99, 90)) });
|
||||
|
||||
// sourceEnd too high
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 99, 150)) });
|
||||
|
||||
// sourceStart and sourceEnd too high
|
||||
behavesSame(function(b) { return b.copy(new Buffer(50, 0, 120, 150)) });
|
||||
|
||||
})();
|
||||
|
||||
/* Make sure functions are chainable */
|
||||
(function() {
|
||||
var x = new BufferBuilder();
|
||||
assert.equal(x, x.appendString('hello'));
|
||||
assert.equal(x, x.appendUInt16LE(0x7720));
|
||||
assert.equal(x, x.appendBuffer(new Buffer([111, 114, 108, 100])));
|
||||
assert.equal(x, x.appendFill(33, 3));
|
||||
})();
|
||||
|
||||
/* Test the degenerate copy */
|
||||
(function() {
|
||||
var x = new BufferBuilder();
|
||||
x.appendBuffer(new Buffer([2,4,6,8]));
|
||||
var b = new Buffer(7);
|
||||
b.fill(0);
|
||||
x.copy(b, 0,0,0);
|
||||
assert.equal(b[0], 0);
|
||||
})();
|
||||
Reference in New Issue
Block a user