From 6ea99721f12c6dcce4e7f1ca5d5dfcba20891a74 Mon Sep 17 00:00:00 2001 From: Tim-Smart Date: Mon, 6 Sep 2010 01:12:35 +1200 Subject: [PATCH] string.length !== Buffer.byteLength(string) --- lib/buffer.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 72a82c0134..71b5a67128 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -111,6 +111,9 @@ function Buffer (subject, encoding, legacy, slice_legacy) { break; case 'string': + length = Buffer.byteLength(subject); + break; + case 'object': // Assume object is an array length = subject.length; break; @@ -186,7 +189,7 @@ Buffer.prototype.set = function set (i, v) { }; -// write(string, offset = 0, encoding = 'uft8') +// write(string, offset = 0, encoding = 'utf8') Buffer.prototype.write = function write (string, offset, encoding) { if (!isFinite(offset)) { var swap = encoding; @@ -195,11 +198,12 @@ Buffer.prototype.write = function write (string, offset, encoding) { } offset || (offset = 0); - encoding || (encoding = 'uft8'); + encoding || (encoding = 'utf8'); // Make sure we are not going to overflow var max_length = this.length - offset; - if (string.length > max_length) { + if (Buffer.byteLength(string) > max_length) { + // FIXME: Char length !== byte length string = string.slice(0, max_length); }