From: Nolan Lawson Date: Sat, 15 Aug 2015 15:11:02 +0000 (-0400) Subject: fix call stack exceeded in #69 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=c5a9012b09d8e32217619362134e8a6871c307b0;p=buffer.git fix call stack exceeded in #69 --- diff --git a/index.js b/index.js index 86ef1a3..ec7b75d 100644 --- a/index.js +++ b/index.js @@ -622,6 +622,28 @@ function base64Slice (buf, start, end) { } } +function decodeLargeCodePointsArray (array) { + var res = '' + var i + var end = array.length + + for (i = 0; i < end; i++) { + res += String.fromCharCode(array[i]) + } + + return res +} + +function decodeCodePointsArray (array) { + if (array.length < 0x10000) { + return String.fromCharCode.apply(String, array) + } + // Decode using string concatenation to avoid "call stack size exceeded". + // Based on http://stackoverflow.com/a/22747272/680742, the browser with + // the lowest limit is Chrome, with 0x10000 args. + return decodeLargeCodePointsArray(array) +} + function utf8Slice (buf, start, end) { end = Math.min(buf.length, end) var firstByte @@ -700,7 +722,7 @@ function utf8Slice (buf, start, end) { res.push(codePoint) } - return String.fromCharCode.apply(String, res) + return decodeCodePointsArray(res) } function asciiSlice (buf, start, end) {