strings.js 5.0 KB

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