From 8ccab81c10509c1a68986f63c053d491fc9bd77c Mon Sep 17 00:00:00 2001 From: Lee Treveil Date: Mon, 13 Jan 2014 00:58:12 +0000 Subject: [PATCH] count number of buffer bytes copied manually --- lib/strtok.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/strtok.js b/lib/strtok.js index 9f43168..281fa7b 100644 --- a/lib/strtok.js +++ b/lib/strtok.js @@ -313,26 +313,27 @@ var parse = function(s, cb) { var bytesCopied = 0; while (bytesCopied < type.len && bufs.length > 0) { var bb = bufs[0]; + var copyLength = Math.min(type.len - bytesCopied, bb.length - bufOffset); // TODO: Manually copy bytes if we don't need many of them. // Bouncing down into C++ land to invoke // Buffer.copy() is expensive enough that we // shouldnt' do it unless we have a lot of dato to // copy. - var copied = bb.copy( + bb.copy( b, bytesCopied, bufOffset, - bufOffset + Math.min(type.len - bytesCopied, bb.length - bufOffset) + bufOffset + copyLength ); - bytesCopied += copied; + bytesCopied += copyLength; - if (copied < (bb.length - bufOffset)) { + if (copyLength < (bb.length - bufOffset)) { assert.equal(bytesCopied, type.len); - bufOffset += copied; + bufOffset += copyLength; } else { - assert.equal(bufOffset + copied, bb.length); + assert.equal(bufOffset + copyLength, bb.length); bufs.shift(); bufOffset = 0; }