summaryrefslogtreecommitdiff
path: root/lib/buffer.js
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2017-04-10 05:05:58 -0400
committerAnna Henningsen <anna@addaleax.net>2017-04-14 22:36:42 +0200
commit3ee4a1a281d6bd4bd7be0577aed41a0f1c9bb1a0 (patch)
tree53264bacba482729377d4d9ed4925908324c51e3 /lib/buffer.js
parent4a86803f6005cc0b67a093fb852109b343cb8654 (diff)
downloadandroid-node-v8-3ee4a1a281d6bd4bd7be0577aed41a0f1c9bb1a0.tar.gz
android-node-v8-3ee4a1a281d6bd4bd7be0577aed41a0f1c9bb1a0.tar.bz2
android-node-v8-3ee4a1a281d6bd4bd7be0577aed41a0f1c9bb1a0.zip
buffer: optimize toString()
PR-URL: https://github.com/nodejs/node/pull/12361 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/buffer.js')
-rw-r--r--lib/buffer.js129
1 files changed, 66 insertions, 63 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index 0469f4147e..1e3478909b 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -483,82 +483,85 @@ Object.defineProperty(Buffer.prototype, 'offset', {
});
-function slowToString(buf, encoding, start, end) {
- var loweredCase = false;
-
- // No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
- // property of a typed array.
-
- // This behaves neither like String nor Uint8Array in that we set start/end
- // to their upper/lower bounds if the value passed is out of range.
- // undefined is handled specially as per ECMA-262 6th Edition,
- // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
- if (start === undefined || start < 0)
- start = 0;
- // Return early if start > buf.length. Done here to prevent potential uint32
- // coercion fail below.
- if (start > buf.length)
- return '';
-
- if (end === undefined || end > buf.length)
- end = buf.length;
-
- if (end <= 0)
- return '';
-
- // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
- end >>>= 0;
- start >>>= 0;
-
- if (end <= start)
- return '';
-
- if (encoding === undefined) encoding = 'utf8';
-
- while (true) {
- switch (encoding) {
- case 'hex':
- return buf.hexSlice(start, end);
-
- case 'utf8':
- case 'utf-8':
- return buf.utf8Slice(start, end);
-
- case 'ascii':
- return buf.asciiSlice(start, end);
-
- case 'latin1':
- case 'binary':
+function stringSlice(buf, encoding, start, end) {
+ if (encoding === undefined) return buf.utf8Slice(start, end);
+ encoding += '';
+ switch (encoding.length) {
+ case 4:
+ if (encoding === 'utf8') return buf.utf8Slice(start, end);
+ if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
+ encoding = encoding.toLowerCase();
+ if (encoding === 'utf8') return buf.utf8Slice(start, end);
+ if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
+ break;
+ case 5:
+ if (encoding === 'utf-8') return buf.utf8Slice(start, end);
+ if (encoding === 'ascii') return buf.asciiSlice(start, end);
+ if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
+ encoding = encoding.toLowerCase();
+ if (encoding === 'utf-8') return buf.utf8Slice(start, end);
+ if (encoding === 'ascii') return buf.asciiSlice(start, end);
+ if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
+ break;
+ case 6:
+ if (encoding === 'latin1' || encoding === 'binary')
return buf.latin1Slice(start, end);
-
- case 'base64':
- return buf.base64Slice(start, end);
-
- case 'ucs2':
- case 'ucs-2':
- case 'utf16le':
- case 'utf-16le':
+ if (encoding === 'base64') return buf.base64Slice(start, end);
+ encoding = encoding.toLowerCase();
+ if (encoding === 'latin1' || encoding === 'binary')
+ return buf.latin1Slice(start, end);
+ if (encoding === 'base64') return buf.base64Slice(start, end);
+ break;
+ case 3:
+ if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
+ return buf.hexSlice(start, end);
+ break;
+ case 7:
+ if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
return buf.ucs2Slice(start, end);
-
- default:
- if (loweredCase)
- throw new TypeError('Unknown encoding: ' + encoding);
- encoding = (encoding + '').toLowerCase();
- loweredCase = true;
- }
+ break;
+ case 8:
+ if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
+ return buf.ucs2Slice(start, end);
+ break;
}
+ throw new TypeError('Unknown encoding: ' + encoding);
}
+
Buffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) {
return binding.copy(this, target, targetStart, sourceStart, sourceEnd);
};
+// No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
+// property of a typed array.
+// This behaves neither like String nor Uint8Array in that we set start/end
+// to their upper/lower bounds if the value passed is out of range.
Buffer.prototype.toString = function(encoding, start, end) {
- let result;
+ var result;
if (arguments.length === 0) {
result = this.utf8Slice(0, this.length);
} else {
- result = slowToString(this, encoding, start, end);
+ const len = this.length;
+ if (len === 0)
+ return '';
+
+ if (!start || start < 0)
+ start = 0;
+ else if (start >= len)
+ return '';
+
+ if (end === undefined || end > len)
+ end = len;
+ else if (end <= 0)
+ return '';
+
+ start |= 0;
+ end |= 0;
+
+ if (end <= start)
+ return '';
+ result = stringSlice(this, encoding, start, end);
}
if (result === undefined)
throw new Error('"toString()" failed');