MCPcopy Create free account
hub / github.com/witheve/Eve / decode

Function decode

src/commonmark.js:3093–3177  ·  view source on GitHub ↗
(string, exclude)

Source from the content-addressed store, hash-verified

3091// Decode percent-encoded string.
3092//
3093function decode(string, exclude) {
3094 var cache;
3095
3096 if (typeof exclude !== 'string') {
3097 exclude = decode.defaultChars;
3098 }
3099
3100 cache = getDecodeCache(exclude);
3101
3102 return string.replace(/(%[a-f0-9]{2})+/gi, function(seq) {
3103 var i, l, b1, b2, b3, b4, chr,
3104 result = '';
3105
3106 for (i = 0, l = seq.length; i < l; i += 3) {
3107 b1 = parseInt(seq.slice(i + 1, i + 3), 16);
3108
3109 if (b1 < 0x80) {
3110 result += cache[b1];
3111 continue;
3112 }
3113
3114 if ((b1 & 0xE0) === 0xC0 && (i + 3 < l)) {
3115 // 110xxxxx 10xxxxxx
3116 b2 = parseInt(seq.slice(i + 4, i + 6), 16);
3117
3118 if ((b2 & 0xC0) === 0x80) {
3119 chr = ((b1 << 6) & 0x7C0) | (b2 & 0x3F);
3120
3121 if (chr < 0x80) {
3122 result += '\ufffd\ufffd';
3123 } else {
3124 result += String.fromCharCode(chr);
3125 }
3126
3127 i += 3;
3128 continue;
3129 }
3130 }
3131
3132 if ((b1 & 0xF0) === 0xE0 && (i + 6 < l)) {
3133 // 1110xxxx 10xxxxxx 10xxxxxx
3134 b2 = parseInt(seq.slice(i + 4, i + 6), 16);
3135 b3 = parseInt(seq.slice(i + 7, i + 9), 16);
3136
3137 if ((b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
3138 chr = ((b1 << 12) & 0xF000) | ((b2 << 6) & 0xFC0) | (b3 & 0x3F);
3139
3140 if (chr < 0x800 || (chr >= 0xD800 && chr <= 0xDFFF)) {
3141 result += '\ufffd\ufffd\ufffd';
3142 } else {
3143 result += String.fromCharCode(chr);
3144 }
3145
3146 i += 6;
3147 continue;
3148 }
3149 }
3150

Callers 1

normalizeURIFunction · 0.85

Calls 1

getDecodeCacheFunction · 0.85

Tested by

no test coverage detected