您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

string-literals.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.parseString = parseString;
  6. // string literal characters cannot contain control codes
  7. var CONTROL_CODES = [0, // null
  8. 7, // bell
  9. 8, // backspace
  10. 9, // horizontal
  11. 10, // line feed
  12. 11, // vertical tab
  13. 12, // form feed
  14. 13, // carriage return
  15. 26, // Control-Z
  16. 27, // escape
  17. 127 // delete
  18. ]; // escaped sequences can either be a two character hex value, or one of the
  19. // following single character codes
  20. function decodeControlCharacter(char) {
  21. switch (char) {
  22. case "t":
  23. return 0x09;
  24. case "n":
  25. return 0x0a;
  26. case "r":
  27. return 0x0d;
  28. case '"':
  29. return 0x22;
  30. case "′":
  31. return 0x27;
  32. case "\\":
  33. return 0x5c;
  34. }
  35. return -1;
  36. }
  37. var ESCAPE_CHAR = 92; // backslash
  38. var QUOTE_CHAR = 34; // backslash
  39. // parse string as per the spec:
  40. // https://webassembly.github.io/spec/core/multipage/text/values.html#text-string
  41. function parseString(value) {
  42. var byteArray = [];
  43. var index = 0;
  44. while (index < value.length) {
  45. var charCode = value.charCodeAt(index);
  46. if (CONTROL_CODES.indexOf(charCode) !== -1) {
  47. throw new Error("ASCII control characters are not permitted within string literals");
  48. }
  49. if (charCode === QUOTE_CHAR) {
  50. throw new Error("quotes are not permitted within string literals");
  51. }
  52. if (charCode === ESCAPE_CHAR) {
  53. var firstChar = value.substr(index + 1, 1);
  54. var decodedControlChar = decodeControlCharacter(firstChar);
  55. if (decodedControlChar !== -1) {
  56. // single character escaped values, e.g. \r
  57. byteArray.push(decodedControlChar);
  58. index += 2;
  59. } else {
  60. // hex escaped values, e.g. \2a
  61. var hexValue = value.substr(index + 1, 2);
  62. if (!/^[0-9A-F]{2}$/i.test(hexValue)) {
  63. throw new Error("invalid character encoding");
  64. }
  65. byteArray.push(parseInt(hexValue, 16));
  66. index += 3;
  67. }
  68. } else {
  69. // ASCII encoded values
  70. byteArray.push(charCode);
  71. index++;
  72. }
  73. }
  74. return byteArray;
  75. }