Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: deprecate legacy code
Browse files Browse the repository at this point in the history
Several things are now no longer necessary. These have been deprecated,
and will be removed in v0.13.
  • Loading branch information
trevnorris committed Jun 18, 2013
1 parent 56869d9 commit f489649
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
var smalloc = process.binding('smalloc');
var buffer = process.binding('buffer');
var assert = require('assert');
var util = require('util');
var alloc = smalloc.alloc;
var sliceOnto = smalloc.sliceOnto;
var kMaxLength = smalloc.kMaxLength;
Expand Down Expand Up @@ -245,57 +246,69 @@ Buffer.prototype.inspect = function inspect() {
};


// TODO(trevnorris): DEPRECATE
Buffer.prototype.get = function get(offset) {
// XXX remove in v0.13
Buffer.prototype.get = util.deprecate(function get(offset) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
return this[offset];
};
}, '.get() is deprecated. Access using array indexes instead.');


// TODO(trevnorris): DEPRECATE
Buffer.prototype.set = function set(offset, v) {
// XXX remove in v0.13
Buffer.prototype.set = util.deprecate(function set(offset, v) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
return this[offset] = v;
};
}, '.set() is deprecated. Set using array indexes instead.');


// TODO(trevnorris): fix these checks to follow new standard
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
var writeWarned = false;
var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
' Use write(string, offset, length, encoding) instead.';
Buffer.prototype.write = function(string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
if (isFinite(offset)) {
if (!isFinite(length)) {
// allow write(string, encoding)
if (typeof offset === 'string' && typeof length === 'undefined') {
encoding = offset;
offset = 0;
length = undefined;

// allow write(string, offset[, length], encoding)
} else if (isFinite(offset)) {
offset = ~~offset;
if (isFinite(length)) {
length = ~~length;
} else {
encoding = length;
length = undefined;
}
// TODO(trevnorris): DEPRECATE
} else { // legacy

// XXX legacy write(string, encoding, offset, length) - remove in v0.13
} else {
if (!writeWarned) {
if (process.throwDeprecation)
throw new Error(writeMsg);
else if (process.traceDeprecation)
console.trace(writeMsg);
else
console.error(writeMsg);
writeWarned = true;
}

var swap = encoding;
encoding = offset;
offset = length;
offset = ~~length;
length = swap;
}

offset = +offset || 0;
var remaining = this.length - offset;
if (!length) {
if (typeof length === 'undefined' || length > remaining)
length = remaining;
} else {
length = +length;
if (length > remaining) {
length = remaining;
}
}

if (typeof encoding === 'undefined')
encoding = 'utf8';
else
encoding = (encoding + '').toLowerCase();
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';

if (string.length > 0 && (length < 0 || offset < 0))
throw new RangeError('attempt to write beyond buffer bounds');
Expand Down

0 comments on commit f489649

Please sign in to comment.