MCPcopy Create free account
hub / github.com/Stuk/jszip / buf2string

Function buf2string

lib/utf8.js:108–157  ·  view source on GitHub ↗
(buf)

Source from the content-addressed store, hash-verified

106
107// convert array to string
108var buf2string = function (buf) {
109 var i, out, c, c_len;
110 var len = buf.length;
111
112 // Reserve max possible length (2 words per char)
113 // NB: by unknown reasons, Array is significantly faster for
114 // String.fromCharCode.apply than Uint16Array.
115 var utf16buf = new Array(len*2);
116
117 for (out=0, i=0; i<len;) {
118 c = buf[i++];
119 // quick process ascii
120 if (c < 0x80) { utf16buf[out++] = c; continue; }
121
122 c_len = _utf8len[c];
123 // skip 5 & 6 byte codes
124 if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
125
126 // apply mask on first byte
127 c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
128 // join the rest
129 while (c_len > 1 && i < len) {
130 c = (c << 6) | (buf[i++] & 0x3f);
131 c_len--;
132 }
133
134 // terminated by end of string?
135 if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
136
137 if (c < 0x10000) {
138 utf16buf[out++] = c;
139 } else {
140 c -= 0x10000;
141 utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
142 utf16buf[out++] = 0xdc00 | (c & 0x3ff);
143 }
144 }
145
146 // shrinkBuf(utf16buf, out)
147 if (utf16buf.length !== out) {
148 if(utf16buf.subarray) {
149 utf16buf = utf16buf.subarray(0, out);
150 } else {
151 utf16buf.length = out;
152 }
153 }
154
155 // return String.fromCharCode.apply(null, utf16buf);
156 return utils.applyFromCharCode(utf16buf);
157};
158
159
160// That's all for the pako functions.

Callers 1

utf8.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected