3 var utils = require('./utils');
4 var support = require('./support');
5 var nodeBuffer = require('./nodeBuffer');
8 * The following functions come from pako, from pako/lib/utils/strings
9 * released under the MIT license, see pako https://github.com/nodeca/pako/
12 // Table with utf8 lengths (calculated by first byte of sequence)
13 // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
14 // because max possible codepoint is 0x10ffff
15 var _utf8len = new Array(256);
16 for (var i=0; i<256; i++) {
17 _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
19 _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
21 // convert string to array (typed, when possible)
22 var string2buf = function (str) {
23 var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
26 for (m_pos = 0; m_pos < str_len; m_pos++) {
27 c = str.charCodeAt(m_pos);
28 if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
29 c2 = str.charCodeAt(m_pos+1);
30 if ((c2 & 0xfc00) === 0xdc00) {
31 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
35 buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
39 if (support.uint8array) {
40 buf = new Uint8Array(buf_len);
42 buf = new Array(buf_len);
46 for (i=0, m_pos = 0; i < buf_len; m_pos++) {
47 c = str.charCodeAt(m_pos);
48 if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
49 c2 = str.charCodeAt(m_pos+1);
50 if ((c2 & 0xfc00) === 0xdc00) {
51 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
58 } else if (c < 0x800) {
60 buf[i++] = 0xC0 | (c >>> 6);
61 buf[i++] = 0x80 | (c & 0x3f);
62 } else if (c < 0x10000) {
64 buf[i++] = 0xE0 | (c >>> 12);
65 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
66 buf[i++] = 0x80 | (c & 0x3f);
69 buf[i++] = 0xf0 | (c >>> 18);
70 buf[i++] = 0x80 | (c >>> 12 & 0x3f);
71 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
72 buf[i++] = 0x80 | (c & 0x3f);
79 // Calculate max possible position in utf8 buffer,
80 // that will not break sequence. If that's not possible
81 // - (very small limits) return max size as is.
83 // buf[] - utf8 bytes array
84 // max - length limit (mandatory);
85 var utf8border = function(buf, max) {
88 max = max || buf.length;
89 if (max > buf.length) { max = buf.length; }
91 // go back from last position, until start of sequence found
93 while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
95 // Fuckup - very small and broken sequence,
96 // return max, because we should return something anyway.
97 if (pos < 0) { return max; }
99 // If we came to start of buffer - that means vuffer is too small,
101 if (pos === 0) { return max; }
103 return (pos + _utf8len[buf[pos]] > max) ? pos : max;
106 // convert array to string
107 var buf2string = function (buf) {
108 var str, i, out, c, c_len;
109 var len = buf.length;
111 // Reserve max possible length (2 words per char)
112 // NB: by unknown reasons, Array is significantly faster for
113 // String.fromCharCode.apply than Uint16Array.
114 var utf16buf = new Array(len*2);
116 for (out=0, i=0; i<len;) {
118 // quick process ascii
119 if (c < 0x80) { utf16buf[out++] = c; continue; }
122 // skip 5 & 6 byte codes
123 if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
125 // apply mask on first byte
126 c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
128 while (c_len > 1 && i < len) {
129 c = (c << 6) | (buf[i++] & 0x3f);
133 // terminated by end of string?
134 if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
140 utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
141 utf16buf[out++] = 0xdc00 | (c & 0x3ff);
145 // shrinkBuf(utf16buf, out)
146 if (utf16buf.length !== out) {
147 if(utf16buf.subarray) {
148 utf16buf = utf16buf.subarray(0, out);
150 utf16buf.length = out;
154 // return String.fromCharCode.apply(null, utf16buf);
155 return utils.applyFromCharCode(utf16buf);
159 // That's all for the pako functions.
163 * Transform a javascript string into an array (typed if possible) of bytes,
165 * @param {String} str the string to encode
166 * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string.
168 exports.utf8encode = function utf8encode(str) {
169 if (support.nodebuffer) {
170 return nodeBuffer(str, "utf-8");
173 return string2buf(str);
178 * Transform a bytes array (or a representation) representing an UTF-8 encoded
179 * string into a javascript string.
180 * @param {Array|Uint8Array|Buffer} buf the data de decode
181 * @return {String} the decoded string.
183 exports.utf8decode = function utf8decode(buf) {
184 if (support.nodebuffer) {
185 return utils.transformTo("nodebuffer", buf).toString("utf-8");
188 buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf);
190 // return buf2string(buf);
191 // Chrome prefers to work with "small" chunks of data
192 // for the method buf2string.
193 // Firefox and Chrome has their own shortcut, IE doesn't seem to really care.
194 var result = [], k = 0, len = buf.length, chunk = 65536;
196 var nextBoundary = utf8border(buf, Math.min(k + chunk, len));
197 if (support.uint8array) {
198 result.push(buf2string(buf.subarray(k, nextBoundary)));
200 result.push(buf2string(buf.slice(k, nextBoundary)));
204 return result.join("");
207 // vim: set shiftwidth=4 softtabstop=4: