Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80547 views
1
// String encode/decode helpers
2
'use strict';
3
4
5
var utils = require('./common');
6
7
8
// Quick check if we can use fast array to bin string conversion
9
//
10
// - apply(Array) can fail on Android 2.2
11
// - apply(Uint8Array) can fail on iOS 5.1 Safary
12
//
13
var STR_APPLY_OK = true;
14
var STR_APPLY_UIA_OK = true;
15
16
try { String.fromCharCode.apply(null, [0]); } catch(__) { STR_APPLY_OK = false; }
17
try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch(__) { STR_APPLY_UIA_OK = false; }
18
19
20
// Table with utf8 lengths (calculated by first byte of sequence)
21
// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
22
// because max possible codepoint is 0x10ffff
23
var _utf8len = new utils.Buf8(256);
24
for (var i=0; i<256; i++) {
25
_utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
26
}
27
_utf8len[254]=_utf8len[254]=1; // Invalid sequence start
28
29
30
// convert string to array (typed, when possible)
31
exports.string2buf = function (str) {
32
var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
33
34
// count binary size
35
for (m_pos = 0; m_pos < str_len; m_pos++) {
36
c = str.charCodeAt(m_pos);
37
if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
38
c2 = str.charCodeAt(m_pos+1);
39
if ((c2 & 0xfc00) === 0xdc00) {
40
c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
41
m_pos++;
42
}
43
}
44
buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
45
}
46
47
// allocate buffer
48
buf = new utils.Buf8(buf_len);
49
50
// convert
51
for (i=0, m_pos = 0; i < buf_len; m_pos++) {
52
c = str.charCodeAt(m_pos);
53
if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
54
c2 = str.charCodeAt(m_pos+1);
55
if ((c2 & 0xfc00) === 0xdc00) {
56
c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
57
m_pos++;
58
}
59
}
60
if (c < 0x80) {
61
/* one byte */
62
buf[i++] = c;
63
} else if (c < 0x800) {
64
/* two bytes */
65
buf[i++] = 0xC0 | (c >>> 6);
66
buf[i++] = 0x80 | (c & 0x3f);
67
} else if (c < 0x10000) {
68
/* three bytes */
69
buf[i++] = 0xE0 | (c >>> 12);
70
buf[i++] = 0x80 | (c >>> 6 & 0x3f);
71
buf[i++] = 0x80 | (c & 0x3f);
72
} else {
73
/* four bytes */
74
buf[i++] = 0xf0 | (c >>> 18);
75
buf[i++] = 0x80 | (c >>> 12 & 0x3f);
76
buf[i++] = 0x80 | (c >>> 6 & 0x3f);
77
buf[i++] = 0x80 | (c & 0x3f);
78
}
79
}
80
81
return buf;
82
};
83
84
// Helper (used in 2 places)
85
function buf2binstring(buf, len) {
86
// use fallback for big arrays to avoid stack overflow
87
if (len < 65537) {
88
if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
89
return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
90
}
91
}
92
93
var result = '';
94
for(var i=0; i < len; i++) {
95
result += String.fromCharCode(buf[i]);
96
}
97
return result;
98
}
99
100
101
// Convert byte array to binary string
102
exports.buf2binstring = function(buf) {
103
return buf2binstring(buf, buf.length);
104
};
105
106
107
// Convert binary string (typed, when possible)
108
exports.binstring2buf = function(str) {
109
var buf = new utils.Buf8(str.length);
110
for(var i=0, len=buf.length; i < len; i++) {
111
buf[i] = str.charCodeAt(i);
112
}
113
return buf;
114
};
115
116
117
// convert array to string
118
exports.buf2string = function (buf, max) {
119
var i, out, c, c_len;
120
var len = max || buf.length;
121
122
// Reserve max possible length (2 words per char)
123
// NB: by unknown reasons, Array is significantly faster for
124
// String.fromCharCode.apply than Uint16Array.
125
var utf16buf = new Array(len*2);
126
127
for (out=0, i=0; i<len;) {
128
c = buf[i++];
129
// quick process ascii
130
if (c < 0x80) { utf16buf[out++] = c; continue; }
131
132
c_len = _utf8len[c];
133
// skip 5 & 6 byte codes
134
if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
135
136
// apply mask on first byte
137
c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
138
// join the rest
139
while (c_len > 1 && i < len) {
140
c = (c << 6) | (buf[i++] & 0x3f);
141
c_len--;
142
}
143
144
// terminated by end of string?
145
if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
146
147
if (c < 0x10000) {
148
utf16buf[out++] = c;
149
} else {
150
c -= 0x10000;
151
utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
152
utf16buf[out++] = 0xdc00 | (c & 0x3ff);
153
}
154
}
155
156
return buf2binstring(utf16buf, out);
157
};
158
159
160
// Calculate max possible position in utf8 buffer,
161
// that will not break sequence. If that's not possible
162
// - (very small limits) return max size as is.
163
//
164
// buf[] - utf8 bytes array
165
// max - length limit (mandatory);
166
exports.utf8border = function(buf, max) {
167
var pos;
168
169
max = max || buf.length;
170
if (max > buf.length) { max = buf.length; }
171
172
// go back from last position, until start of sequence found
173
pos = max-1;
174
while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
175
176
// Fuckup - very small and broken sequence,
177
// return max, because we should return something anyway.
178
if (pos < 0) { return max; }
179
180
// If we came to start of buffer - that means vuffer is too small,
181
// return max too.
182
if (pos === 0) { return max; }
183
184
return (pos + _utf8len[buf[pos]] > max) ? pos : max;
185
};
186
187