Browse Source

Revert "Buffer.copy should copy through sourceEnd, as specified."

This reverts commit a2f70da4c9.

Keep tests modifies a few edge checks on Copy()
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
5e86d01385
  1. 4
      doc/api.markdown
  2. 4
      lib/string_decoder.js
  3. 21
      src/node_buffer.cc
  4. 12
      test/simple/test-buffer.js

4
doc/api.markdown

@ -161,7 +161,7 @@ buffer object. It does not change when the contents of the buffer are changed.
Does a memcpy() between buffers.
Example: build two Buffers, then copy `buf1` from byte 16 through byte 20
Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
into `buf2`, starting at the 8th byte in `buf2`.
var Buffer = require('buffer').Buffer,
@ -177,7 +177,7 @@ into `buf2`, starting at the 8th byte in `buf2`.
buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));
// !!!!!!!!qrstu!!!!!!!!!!!!
// !!!!!!!!qrst!!!!!!!!!!!!!
### buffer.slice(start, end)

4
lib/string_decoder.js

@ -25,7 +25,7 @@ StringDecoder.prototype.write = function (buffer) {
: buffer.length;
// add the new bytes to the char buffer
buffer.copy(this.charBuffer, this.charReceived, 0, (i - 1));
buffer.copy(this.charBuffer, this.charReceived, 0, i);
this.charReceived += i;
if (this.charReceived < this.charLength) {
@ -79,7 +79,7 @@ StringDecoder.prototype.write = function (buffer) {
}
// buffer the incomplete character bytes we got
buffer.copy(this.charBuffer, 0, buffer.length - i, (buffer.length - 1));
buffer.copy(this.charBuffer, 0, buffer.length - i, buffer.length);
this.charReceived = i;
if (buffer.length - i > 0) {

21
src/node_buffer.cc

@ -298,7 +298,7 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
ssize_t target_start = args[1]->Int32Value();
ssize_t source_start = args[2]->Int32Value();
ssize_t source_end = args[3]->IsInt32() ? args[3]->Int32Value()
: (source->length() - 1);
: source->length();
if (source_end < source_start) {
return ThrowException(Exception::Error(String::New(
@ -315,24 +315,17 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
"sourceStart out of bounds")));
}
if (source_end < 0 || source_end >= source->length()) {
if (source_end < 0 || source_end > source->length()) {
return ThrowException(Exception::Error(String::New(
"sourceEnd out of bounds")));
}
ssize_t to_copy = MIN( (source_end - source_start + 1),
(target->length() - target_start) );
ssize_t to_copy = MIN(source_end - source_start,
target->length() - target_start);
if (source->handle_->StrictEquals(target->handle_)) {
// need to use slightly slower memmove is the ranges might overlap
memmove((void*)(target->data() + target_start),
(const void*)(source->data() + source_start),
to_copy);
} else {
memcpy((void*)(target->data() + target_start),
(const void*)(source->data() + source_start),
to_copy);
}
memcpy((void*)(target->data() + target_start),
(const void*)(source->data() + source_start),
to_copy);
return scope.Close(Integer::New(to_copy));
}

12
test/simple/test-buffer.js

@ -19,8 +19,8 @@ for (var i = 0; i < 1024; i++) {
var c = new Buffer(512);
// copy 512 bytes, from 0 to 511.
var copied = b.copy(c, 0, 0, 511);
// copy 512 bytes, from 0 to 512.
var copied = b.copy(c, 0, 0, 512);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
@ -30,7 +30,7 @@ for (var i = 0; i < c.length; i++) {
console.log("");
// try to copy 513 bytes, and hope we don't overrun c, which is only 512 long
var copied = b.copy(c, 0, 0, 512);
var copied = b.copy(c, 0, 0, 513);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
@ -46,7 +46,7 @@ for (var i = 0; i < b.length; i++) {
}
// copy 768 bytes from b into b
var copied = b.copy(b, 0, 256, 1023);
var copied = b.copy(b, 0, 256, 1024);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(768, copied);
for (var i = 0; i < c.length; i++) {
@ -104,8 +104,8 @@ assert.strictEqual('sourceStart out of bounds', caught_error.message);
// try to copy ending after the end of b
try {
var copied = b.copy(c, 0, 1023, 1024);
} catch (err) {
var copied = b.copy(c, 0, 1023, 1025);
} catch (err) {
caught_error = err;
}
assert.strictEqual('sourceEnd out of bounds', caught_error.message);

Loading…
Cancel
Save