|
|
@ -17,29 +17,29 @@ SlowBuffer.prototype.inspect = function () { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
SlowBuffer.prototype.toString = function (encoding, start, stop) { |
|
|
|
SlowBuffer.prototype.toString = function (encoding, start, end) { |
|
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
start = +start || 0; |
|
|
|
if (typeof stop == "undefined") stop = this.length; |
|
|
|
if (typeof end == "undefined") end = this.length; |
|
|
|
|
|
|
|
// Fastpath empty strings
|
|
|
|
if (+stop == start) { |
|
|
|
if (+end == start) { |
|
|
|
return ''; |
|
|
|
} |
|
|
|
|
|
|
|
switch (encoding) { |
|
|
|
case 'utf8': |
|
|
|
case 'utf-8': |
|
|
|
return this.utf8Slice(start, stop); |
|
|
|
return this.utf8Slice(start, end); |
|
|
|
|
|
|
|
case 'ascii': |
|
|
|
return this.asciiSlice(start, stop); |
|
|
|
return this.asciiSlice(start, end); |
|
|
|
|
|
|
|
case 'binary': |
|
|
|
return this.binarySlice(start, stop); |
|
|
|
return this.binarySlice(start, end); |
|
|
|
|
|
|
|
case 'base64': |
|
|
|
return this.base64Slice(start, stop); |
|
|
|
return this.base64Slice(start, end); |
|
|
|
|
|
|
|
default: |
|
|
|
throw new Error('Unknown encoding'); |
|
|
@ -212,14 +212,19 @@ Buffer.prototype.write = function write (string, offset, encoding) { |
|
|
|
|
|
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
|
Buffer.prototype.toString = function toString (encoding, start, end) { |
|
|
|
encoding || (encoding = 'utf8'); |
|
|
|
start || (start = 0); |
|
|
|
end || (end = this.length); |
|
|
|
Buffer.prototype.toString = function (encoding, start, end) { |
|
|
|
if (typeof encoding == 'undefined') encoding = 'utf8'; |
|
|
|
|
|
|
|
// Make sure we aren't oob
|
|
|
|
if (end > this.length) { |
|
|
|
if (typeof start == 'undefined' || start < 0) { |
|
|
|
start = 0; |
|
|
|
} else if (start > this.length) { |
|
|
|
start = this.length; |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof end == "undefined" || end > this.length) { |
|
|
|
end = this.length; |
|
|
|
} else if (end < 0) { |
|
|
|
end = 0; |
|
|
|
} |
|
|
|
|
|
|
|
return this.parent.toString(encoding, start + this.offset, end + this.offset); |
|
|
@ -232,14 +237,37 @@ Buffer.byteLength = SlowBuffer.byteLength; |
|
|
|
|
|
|
|
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
|
|
|
|
Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
|
var source = this; |
|
|
|
start || (start = 0); |
|
|
|
end || (end = this.length); |
|
|
|
|
|
|
|
if (end < start) throw new Error("sourceEnd < sourceStart"); |
|
|
|
|
|
|
|
// Copy 0 bytes; we're done
|
|
|
|
if (end === start) return 0; |
|
|
|
if (target.length == 0 || source.length == 0) return 0; |
|
|
|
|
|
|
|
if (target_start < 0 || target_start >= target.length) { |
|
|
|
throw new Error("targetStart out of bounds"); |
|
|
|
} |
|
|
|
|
|
|
|
if (start < 0 || start >= source.length) { |
|
|
|
throw new Error("sourceStart out of bounds"); |
|
|
|
} |
|
|
|
|
|
|
|
if (end < 0 || end > source.length) { |
|
|
|
throw new Error("sourceEnd out of bounds"); |
|
|
|
} |
|
|
|
|
|
|
|
// Are we oob?
|
|
|
|
if (end > this.length) { |
|
|
|
end = this.length; |
|
|
|
} |
|
|
|
|
|
|
|
if (target.length - target_start < end - start) { |
|
|
|
end = target.length - target_start + start; |
|
|
|
} |
|
|
|
|
|
|
|
return this.parent.copy(target.parent, |
|
|
|
target_start + target.offset, |
|
|
|
start + this.offset, |
|
|
|