allow Buffer to be used as 'a' format pack parameter

This commit is contained in:
sidorares 2011-07-20 14:45:46 +10:00
parent 7a2761b061
commit aa5c4bac1c

View file

@ -244,6 +244,7 @@ UnpackStream.prototype.pack = function(format, arguments)
var n = arguments[arg++];
buf[offset++] = n;
break;
case 's': // TODO: implement signed INT16!!!
case 'S':
var n = arguments[arg++];
buf[offset++] = n & 0xff;
@ -256,11 +257,17 @@ UnpackStream.prototype.pack = function(format, arguments)
buf[offset++] = (n >> 16) & 0xff;
buf[offset++] = (n >> 24) & 0xff;
break;
case 'a': // string
case 'a': // string or buffer
var str = arguments[arg++];
// TODO: buffer.write could be faster
for (var c = 0; c < str.length; ++c)
buf[offset++] = str.charCodeAt(c);
if (Buffer.isBuffer(str))
{
str.copy(buf, offset);
offset += str.length;
} else {
// TODO: buffer.write could be faster
for (var c = 0; c < str.length; ++c)
buf[offset++] = str.charCodeAt(c);
}
break;
case 'p': // padded string
var str = arguments[arg++];