Browse Source

string.length !== Buffer.byteLength(string)

v0.7.4-release
Tim-Smart 15 years ago
committed by Ryan Dahl
parent
commit
6ea99721f1
  1. 10
      lib/buffer.js

10
lib/buffer.js

@ -111,6 +111,9 @@ function Buffer (subject, encoding, legacy, slice_legacy) {
break; break;
case 'string': case 'string':
length = Buffer.byteLength(subject);
break;
case 'object': // Assume object is an array case 'object': // Assume object is an array
length = subject.length; length = subject.length;
break; 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) { Buffer.prototype.write = function write (string, offset, encoding) {
if (!isFinite(offset)) { if (!isFinite(offset)) {
var swap = encoding; var swap = encoding;
@ -195,11 +198,12 @@ Buffer.prototype.write = function write (string, offset, encoding) {
} }
offset || (offset = 0); offset || (offset = 0);
encoding || (encoding = 'uft8'); encoding || (encoding = 'utf8');
// Make sure we are not going to overflow // Make sure we are not going to overflow
var max_length = this.length - offset; 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); string = string.slice(0, max_length);
} }

Loading…
Cancel
Save