* Transform an array-like object to a string. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result.
(array)
| 14021 | * @return {String} the result. |
| 14022 | */ |
| 14023 | function arrayLikeToString(array) { |
| 14024 | // Performances notes : |
| 14025 | // -------------------- |
| 14026 | // String.fromCharCode.apply(null, array) is the fastest, see |
| 14027 | // see http://jsperf.com/converting-a-uint8array-to-a-string/2 |
| 14028 | // but the stack is limited (and we can get huge arrays !). |
| 14029 | // |
| 14030 | // result += String.fromCharCode(array[i]); generate too many strings ! |
| 14031 | // |
| 14032 | // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2 |
| 14033 | var chunk = 65536; |
| 14034 | var result = [], |
| 14035 | len = array.length, |
| 14036 | type = exports.getTypeOf(array), |
| 14037 | k = 0, |
| 14038 | canUseApply = true; |
| 14039 | try { |
| 14040 | switch(type) { |
| 14041 | case "uint8array": |
| 14042 | String.fromCharCode.apply(null, new Uint8Array(0)); |
| 14043 | break; |
| 14044 | case "nodebuffer": |
| 14045 | String.fromCharCode.apply(null, nodeBuffer(0)); |
| 14046 | break; |
| 14047 | } |
| 14048 | } catch(e) { |
| 14049 | canUseApply = false; |
| 14050 | } |
| 14051 | |
| 14052 | // no apply : slow and painful algorithm |
| 14053 | // default browser on android 4.* |
| 14054 | if (!canUseApply) { |
| 14055 | var resultStr = ""; |
| 14056 | for(var i = 0; i < array.length;i++) { |
| 14057 | resultStr += String.fromCharCode(array[i]); |
| 14058 | } |
| 14059 | return resultStr; |
| 14060 | } |
| 14061 | while (k < len && chunk > 1) { |
| 14062 | try { |
| 14063 | if (type === "array" || type === "nodebuffer") { |
| 14064 | result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len)))); |
| 14065 | } |
| 14066 | else { |
| 14067 | result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len)))); |
| 14068 | } |
| 14069 | k += chunk; |
| 14070 | } |
| 14071 | catch (e) { |
| 14072 | chunk = Math.floor(chunk / 2); |
| 14073 | } |
| 14074 | } |
| 14075 | return result.join(""); |
| 14076 | } |
| 14077 | |
| 14078 | exports.applyFromCharCode = arrayLikeToString; |
| 14079 |