(value)
| 371 | // |
| 372 | // It is a single left-to-right pass, so the cost is linear in the length of the value. It replaces `decode-uri-component`, whose fallback for a string that `decodeURIComponent()` rejects collected every distinct percent-encoded run into a replacement map and then replaced each entry across the whole string, so a value with N distinct runs cost N passes over N bytes. |
| 373 | function decodeComponent(value) { |
| 374 | try { |
| 375 | // Almost every value is valid percent-encoded UTF-8, and the built-in decoder handles those in one pass in C++, which is far faster than the step at a time above. It gives the same answer, so this only decides which of the two paths runs. |
| 376 | return decodeURIComponent(value); |
| 377 | } catch { |
| 378 | // One byte that cannot be decoded makes the built-in decoder throw away the whole value, so the value is decoded the lenient way instead. |
| 379 | } |
| 380 | |
| 381 | percentSign.lastIndex = 0; |
| 382 | |
| 383 | let output = ''; |
| 384 | let position = 0; |
| 385 | let match = percentSign.exec(value); |
| 386 | |
| 387 | while (match !== null) { |
| 388 | // Everything between the last escape and this one is copied as it is. |
| 389 | output += value.slice(position, match.index); |
| 390 | |
| 391 | const [decoded, nextPosition] = decodeSequence(value, match.index); |
| 392 | output += decoded; |
| 393 | position = nextPosition; |
| 394 | |
| 395 | percentSign.lastIndex = position; |
| 396 | match = percentSign.exec(value); |
| 397 | } |
| 398 | |
| 399 | return output + value.slice(position); |
| 400 | } |
| 401 | |
| 402 | function decode(value, options) { |
| 403 | return options.decode ? decodeComponent(value) : value; |
no test coverage detected