|
7 | 7 |
|
8 | 8 | const PATH_QUERY_FRAGMENT_REGEXP =
|
9 | 9 | /^(#?(?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
|
| 10 | +const ZERO_ESCAPE_REGEXP = /\0(.)/g; |
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * @param {string} identifier identifier
|
13 | 14 | * @returns {[string, string, string]|null} parsed identifier
|
14 | 15 | */
|
15 | 16 | function parseIdentifier(identifier) {
|
| 17 | + if (!identifier) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + const firstEscape = identifier.indexOf("\0"); |
| 22 | + if (firstEscape < 0) { |
| 23 | + // Fast path for inputs that don't use \0 escaping. |
| 24 | + const queryStart = identifier.indexOf("?"); |
| 25 | + // Start at index 1 to ignore a possible leading hash. |
| 26 | + const fragmentStart = identifier.indexOf("#", 1); |
| 27 | + |
| 28 | + if (fragmentStart < 0) { |
| 29 | + if (queryStart < 0) { |
| 30 | + // No fragment, no query |
| 31 | + return [identifier, "", ""]; |
| 32 | + } |
| 33 | + // Query, no fragment |
| 34 | + return [ |
| 35 | + identifier.slice(0, queryStart), |
| 36 | + identifier.slice(queryStart), |
| 37 | + "" |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + if (queryStart < 0 || fragmentStart < queryStart) { |
| 42 | + // Fragment, no query |
| 43 | + return [ |
| 44 | + identifier.slice(0, fragmentStart), |
| 45 | + "", |
| 46 | + identifier.slice(fragmentStart) |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + // Query and fragment |
| 51 | + return [ |
| 52 | + identifier.slice(0, queryStart), |
| 53 | + identifier.slice(queryStart, fragmentStart), |
| 54 | + identifier.slice(fragmentStart) |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
16 | 58 | const match = PATH_QUERY_FRAGMENT_REGEXP.exec(identifier);
|
17 | 59 |
|
18 | 60 | if (!match) return null;
|
19 | 61 |
|
20 | 62 | return [
|
21 |
| - match[1].replace(/\0(.)/g, "$1"), |
22 |
| - match[2] ? match[2].replace(/\0(.)/g, "$1") : "", |
| 63 | + match[1].replace(ZERO_ESCAPE_REGEXP, "$1"), |
| 64 | + match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : "", |
23 | 65 | match[3] || ""
|
24 | 66 | ];
|
25 | 67 | }
|
|
0 commit comments