decode.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable no-bitwise */
  2. const decodeCache = {}
  3. function getDecodeCache (exclude) {
  4. let cache = decodeCache[exclude]
  5. if (cache) { return cache }
  6. cache = decodeCache[exclude] = []
  7. for (let i = 0; i < 128; i++) {
  8. const ch = String.fromCharCode(i)
  9. cache.push(ch)
  10. }
  11. for (let i = 0; i < exclude.length; i++) {
  12. const ch = exclude.charCodeAt(i)
  13. cache[ch] = '%' + ('0' + ch.toString(16).toUpperCase()).slice(-2)
  14. }
  15. return cache
  16. }
  17. // Decode percent-encoded string.
  18. //
  19. function decode (string, exclude) {
  20. if (typeof exclude !== 'string') {
  21. exclude = decode.defaultChars
  22. }
  23. const cache = getDecodeCache(exclude)
  24. return string.replace(/(%[a-f0-9]{2})+/gi, function (seq) {
  25. let result = ''
  26. for (let i = 0, l = seq.length; i < l; i += 3) {
  27. const b1 = parseInt(seq.slice(i + 1, i + 3), 16)
  28. if (b1 < 0x80) {
  29. result += cache[b1]
  30. continue
  31. }
  32. if ((b1 & 0xE0) === 0xC0 && (i + 3 < l)) {
  33. // 110xxxxx 10xxxxxx
  34. const b2 = parseInt(seq.slice(i + 4, i + 6), 16)
  35. if ((b2 & 0xC0) === 0x80) {
  36. const chr = ((b1 << 6) & 0x7C0) | (b2 & 0x3F)
  37. if (chr < 0x80) {
  38. result += '\ufffd\ufffd'
  39. } else {
  40. result += String.fromCharCode(chr)
  41. }
  42. i += 3
  43. continue
  44. }
  45. }
  46. if ((b1 & 0xF0) === 0xE0 && (i + 6 < l)) {
  47. // 1110xxxx 10xxxxxx 10xxxxxx
  48. const b2 = parseInt(seq.slice(i + 4, i + 6), 16)
  49. const b3 = parseInt(seq.slice(i + 7, i + 9), 16)
  50. if ((b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
  51. const chr = ((b1 << 12) & 0xF000) | ((b2 << 6) & 0xFC0) | (b3 & 0x3F)
  52. if (chr < 0x800 || (chr >= 0xD800 && chr <= 0xDFFF)) {
  53. result += '\ufffd\ufffd\ufffd'
  54. } else {
  55. result += String.fromCharCode(chr)
  56. }
  57. i += 6
  58. continue
  59. }
  60. }
  61. if ((b1 & 0xF8) === 0xF0 && (i + 9 < l)) {
  62. // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx
  63. const b2 = parseInt(seq.slice(i + 4, i + 6), 16)
  64. const b3 = parseInt(seq.slice(i + 7, i + 9), 16)
  65. const b4 = parseInt(seq.slice(i + 10, i + 12), 16)
  66. if ((b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80 && (b4 & 0xC0) === 0x80) {
  67. let chr = ((b1 << 18) & 0x1C0000) | ((b2 << 12) & 0x3F000) | ((b3 << 6) & 0xFC0) | (b4 & 0x3F)
  68. if (chr < 0x10000 || chr > 0x10FFFF) {
  69. result += '\ufffd\ufffd\ufffd\ufffd'
  70. } else {
  71. chr -= 0x10000
  72. result += String.fromCharCode(0xD800 + (chr >> 10), 0xDC00 + (chr & 0x3FF))
  73. }
  74. i += 9
  75. continue
  76. }
  77. }
  78. result += '\ufffd'
  79. }
  80. return result
  81. })
  82. }
  83. decode.defaultChars = ';/?:@&=+$,#'
  84. decode.componentChars = ''
  85. export default decode