Source: lib/util/string_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.StringUtils');
  18. goog.require('shaka.log');
  19. goog.require('shaka.util.Error');
  20. /**
  21. * @namespace shaka.util.StringUtils
  22. * @summary A set of string utility functions.
  23. * @exportDoc
  24. */
  25. /**
  26. * Creates a string from the given buffer as UTF-8 encoding.
  27. *
  28. * @param {?BufferSource} data
  29. * @return {string}
  30. * @throws {shaka.util.Error}
  31. * @export
  32. */
  33. shaka.util.StringUtils.fromUTF8 = function(data) {
  34. if (!data) return '';
  35. var uint8 = new Uint8Array(data);
  36. // If present, strip off the UTF-8 BOM.
  37. if (uint8[0] == 0xef && uint8[1] == 0xbb && uint8[2] == 0xbf) {
  38. uint8 = uint8.subarray(3);
  39. }
  40. // http://stackoverflow.com/a/13691499
  41. var utf8 = shaka.util.StringUtils.fromCharCode_(uint8);
  42. // This converts each character in the string to an escape sequence. If the
  43. // character is in the ASCII range, it is not converted; otherwise it is
  44. // converted to a URI escape sequence.
  45. // Example: '\x67\x35\xe3\x82\xac' -> 'g#%E3%82%AC'
  46. var escaped = escape(utf8);
  47. // Decode the escaped sequence. This will interpret UTF-8 sequences into the
  48. // correct character.
  49. // Example: 'g#%E3%82%AC' -> 'g#€'
  50. try {
  51. return decodeURIComponent(escaped);
  52. } catch (e) {
  53. throw new shaka.util.Error(
  54. shaka.util.Error.Severity.CRITICAL, shaka.util.Error.Category.TEXT,
  55. shaka.util.Error.Code.BAD_ENCODING);
  56. }
  57. };
  58. /**
  59. * Creates a string from the given buffer as UTF-16 encoding.
  60. *
  61. * @param {?BufferSource} data
  62. * @param {boolean} littleEndian true to read little endian, false to read big.
  63. * @return {string}
  64. * @throws {shaka.util.Error}
  65. * @export
  66. */
  67. shaka.util.StringUtils.fromUTF16 = function(data, littleEndian) {
  68. if (!data) return '';
  69. if (data.byteLength % 2 != 0) {
  70. shaka.log.error('Data has an incorrect length, must be even.');
  71. throw new shaka.util.Error(
  72. shaka.util.Error.Severity.CRITICAL, shaka.util.Error.Category.TEXT,
  73. shaka.util.Error.Code.BAD_ENCODING);
  74. }
  75. /** @type {ArrayBuffer} */
  76. var buffer;
  77. if (data instanceof ArrayBuffer) {
  78. buffer = data;
  79. } else {
  80. // Have to create a new buffer because the argument may be a smaller
  81. // view on a larger ArrayBuffer. We cannot use an ArrayBufferView in
  82. // a DataView.
  83. var temp = new Uint8Array(data.byteLength);
  84. temp.set(new Uint8Array(data));
  85. buffer = temp.buffer;
  86. }
  87. // Use a DataView to ensure correct endianness.
  88. var length = data.byteLength / 2;
  89. var arr = new Uint16Array(length);
  90. var dataView = new DataView(buffer);
  91. for (var i = 0; i < length; i++) {
  92. arr[i] = dataView.getUint16(i * 2, littleEndian);
  93. }
  94. return shaka.util.StringUtils.fromCharCode_(arr);
  95. };
  96. /**
  97. * Creates a string from the given buffer, auto-detecting the encoding that is
  98. * being used. If it cannot detect the encoding, it will throw an exception.
  99. *
  100. * @param {?BufferSource} data
  101. * @return {string}
  102. * @throws {shaka.util.Error}
  103. * @export
  104. */
  105. shaka.util.StringUtils.fromBytesAutoDetect = function(data) {
  106. var StringUtils = shaka.util.StringUtils;
  107. var uint8 = new Uint8Array(data);
  108. if (uint8[0] == 0xef && uint8[1] == 0xbb && uint8[2] == 0xbf)
  109. return StringUtils.fromUTF8(uint8);
  110. else if (uint8[0] == 0xfe && uint8[1] == 0xff)
  111. return StringUtils.fromUTF16(uint8.subarray(2), false /* littleEndian */);
  112. else if (uint8[0] == 0xff && uint8[1] == 0xfe)
  113. return StringUtils.fromUTF16(uint8.subarray(2), true /* littleEndian */);
  114. var isAscii = (function(arr, i) {
  115. // arr[i] >= ' ' && arr[i] <= '~';
  116. return arr.byteLength <= i || (arr[i] >= 0x20 && arr[i] <= 0x7e);
  117. }.bind(null, uint8));
  118. shaka.log.debug('Unable to find byte-order-mark, making an educated guess.');
  119. if (uint8[0] == 0 && uint8[2] == 0)
  120. return StringUtils.fromUTF16(data, false /* littleEndian */);
  121. else if (uint8[1] == 0 && uint8[3] == 0)
  122. return StringUtils.fromUTF16(data, true /* littleEndian */);
  123. else if (isAscii(0) && isAscii(1) && isAscii(2) && isAscii(3))
  124. return StringUtils.fromUTF8(data);
  125. throw new shaka.util.Error(
  126. shaka.util.Error.Severity.CRITICAL,
  127. shaka.util.Error.Category.TEXT,
  128. shaka.util.Error.Code.UNABLE_TO_DETECT_ENCODING);
  129. };
  130. /**
  131. * Creates a ArrayBuffer from the given string, converting to UTF-8 encoding.
  132. *
  133. * @param {string} str
  134. * @return {!ArrayBuffer}
  135. * @export
  136. */
  137. shaka.util.StringUtils.toUTF8 = function(str) {
  138. // http://stackoverflow.com/a/13691499
  139. // Converts the given string to a URI encoded string. If a character falls
  140. // in the ASCII range, it is not converted; otherwise it will be converted to
  141. // a series of URI escape sequences according to UTF-8.
  142. // Example: 'g#€' -> 'g#%E3%82%AC'
  143. var encoded = encodeURIComponent(str);
  144. // Convert each escape sequence individually into a character. Each escape
  145. // sequence is interpreted as a code-point, so if an escape sequence happens
  146. // to be part of a multi-byte sequence, each byte will be converted to a
  147. // single character.
  148. // Example: 'g#%E3%82%AC' -> '\x67\x35\xe3\x82\xac'
  149. var utf8 = unescape(encoded);
  150. var result = new Uint8Array(utf8.length);
  151. for (var i = 0; i < utf8.length; ++i) {
  152. result[i] = utf8.charCodeAt(i);
  153. }
  154. return result.buffer;
  155. };
  156. /**
  157. * Creates a new string from the given array of char codes.
  158. *
  159. * @param {!TypedArray} args
  160. * @return {string}
  161. * @private
  162. */
  163. shaka.util.StringUtils.fromCharCode_ = function(args) {
  164. var max = 16000;
  165. var ret = '';
  166. for (var i = 0; i < args.length; i += max) {
  167. var subArray = args.subarray(i, i + max);
  168. ret += String.fromCharCode.apply(null, subArray);
  169. }
  170. return ret;
  171. };