MCPcopy Create free account
hub / github.com/violentmonkey/violentmonkey / buffer2string

Function buffer2string

src/common/util.js:81–95  ·  view source on GitHub ↗
(buf, offset = 0, length = 1e99)

Source from the content-addressed store, hash-verified

79 * @return {string} a binary string i.e. one byte per character
80 */
81export function buffer2string(buf, offset = 0, length = 1e99) {
82 // The max number of arguments varies between JS engines but it's >32k so we're safe
83 const sliceSize = 8192;
84 const slices = [];
85 const arrayLen = buf.length; // present on Uint8Array/Array
86 const end = Math.min(arrayLen || buf.byteLength, offset + length);
87 const needsSlicing = arrayLen == null || offset || end > sliceSize;
88 for (; offset < end; offset += sliceSize) {
89 slices.push(String.fromCharCode.apply(null,
90 needsSlicing
91 ? new Uint8Array(buf, offset, Math.min(sliceSize, end - offset))
92 : buf));
93 }
94 return slices.join('');
95}
96
97/**
98 * Faster than buffer2string+btoa: 2x in Chrome, 10x in FF

Callers 4

stringAsBase64Function · 0.90
encodeWebRequestHeaderFunction · 0.90
string2byteStringFunction · 0.90
sha256b64urlFunction · 0.90

Calls 1

applyMethod · 0.45

Tested by 1

stringAsBase64Function · 0.72