MCPcopy Create free account
hub / github.com/TruthHun/BookStack / string2buf

Function string2buf

static/word2md/mammoth.browser.js:13764–13819  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

13762
13763// convert string to array (typed, when possible)
13764var string2buf = function (str) {
13765 var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
13766
13767 // count binary size
13768 for (m_pos = 0; m_pos < str_len; m_pos++) {
13769 c = str.charCodeAt(m_pos);
13770 if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
13771 c2 = str.charCodeAt(m_pos+1);
13772 if ((c2 & 0xfc00) === 0xdc00) {
13773 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
13774 m_pos++;
13775 }
13776 }
13777 buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
13778 }
13779
13780 // allocate buffer
13781 if (support.uint8array) {
13782 buf = new Uint8Array(buf_len);
13783 } else {
13784 buf = new Array(buf_len);
13785 }
13786
13787 // convert
13788 for (i=0, m_pos = 0; i < buf_len; m_pos++) {
13789 c = str.charCodeAt(m_pos);
13790 if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
13791 c2 = str.charCodeAt(m_pos+1);
13792 if ((c2 & 0xfc00) === 0xdc00) {
13793 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
13794 m_pos++;
13795 }
13796 }
13797 if (c < 0x80) {
13798 /* one byte */
13799 buf[i++] = c;
13800 } else if (c < 0x800) {
13801 /* two bytes */
13802 buf[i++] = 0xC0 | (c >>> 6);
13803 buf[i++] = 0x80 | (c & 0x3f);
13804 } else if (c < 0x10000) {
13805 /* three bytes */
13806 buf[i++] = 0xE0 | (c >>> 12);
13807 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
13808 buf[i++] = 0x80 | (c & 0x3f);
13809 } else {
13810 /* four bytes */
13811 buf[i++] = 0xf0 | (c >>> 18);
13812 buf[i++] = 0x80 | (c >>> 12 & 0x3f);
13813 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
13814 buf[i++] = 0x80 | (c & 0x3f);
13815 }
13816 }
13817
13818 return buf;
13819};
13820
13821// Calculate max possible position in utf8 buffer,

Callers 1

mammoth.browser.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected