Skip to content

Commit 9df2448

Browse files
authored
Remove more bytes from parser (#401)
1 parent 012a54a commit 9df2448

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed

src/index.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ describe("path-to-regexp", () => {
2222
describe("ParseError", () => {
2323
it("should contain original path and debug url", () => {
2424
const error = new PathError(
25-
"Unexpected END at index 7, expected }",
25+
"Unexpected end at index 7, expected }",
2626
"/{:foo,",
2727
);
2828

2929
expect(error).toBeInstanceOf(TypeError);
3030
expect(error.message).toBe(
31-
"Unexpected END at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for info",
31+
"Unexpected end at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for info",
3232
);
3333
expect(error.originalPath).toBe("/{:foo,");
3434
});
3535

3636
it("should omit original url when undefined", () => {
3737
const error = new PathError(
38-
"Unexpected END at index 7, expected }",
38+
"Unexpected end at index 7, expected }",
3939
undefined,
4040
);
4141

4242
expect(error).toBeInstanceOf(TypeError);
4343
expect(error.message).toBe(
44-
"Unexpected END at index 7, expected }; visit https://git.new/pathToRegexpError for info",
44+
"Unexpected end at index 7, expected }; visit https://git.new/pathToRegexpError for info",
4545
);
4646
expect(error.originalPath).toBeUndefined();
4747
});
@@ -50,13 +50,13 @@ describe("path-to-regexp", () => {
5050
describe("parse errors", () => {
5151
it("should throw on unbalanced group", () => {
5252
expect(() => parse("/{:foo,")).toThrow(
53-
new PathError("Unexpected END at index 7, expected }", "/{:foo,"),
53+
new PathError("Unexpected end at index 7, expected }", "/{:foo,"),
5454
);
5555
});
5656

5757
it("should throw on nested unbalanced group", () => {
5858
expect(() => parse("/{:foo/{x,y}")).toThrow(
59-
new PathError("Unexpected END at index 12, expected }", "/{:foo/{x,y}"),
59+
new PathError("Unexpected end at index 12, expected }", "/{:foo/{x,y}"),
6060
);
6161
});
6262

src/index.ts

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ export interface CompileOptions {
6060
type TokenType =
6161
| "{"
6262
| "}"
63-
| "WILDCARD"
64-
| "PARAM"
65-
| "CHAR"
66-
| "ESCAPED"
67-
| "END"
63+
| "wildcard"
64+
| "param"
65+
| "char"
66+
| "escape"
67+
| "end"
6868
// Reserved for use or ambiguous due to past use.
6969
| "("
7070
| ")"
@@ -197,29 +197,27 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
197197
let value = "";
198198

199199
if (ID_START.test(chars[index])) {
200-
value += chars[index];
201-
while (ID_CONTINUE.test(chars[++index])) {
202-
value += chars[index];
203-
}
200+
do {
201+
value += chars[index++];
202+
} while (ID_CONTINUE.test(chars[index]));
204203
} else if (chars[index] === '"') {
205-
let pos = index;
204+
let quoteStart = index;
206205

207-
while (index < chars.length) {
208-
if (chars[++index] === '"') {
206+
while (index++ < chars.length) {
207+
if (chars[index] === '"') {
209208
index++;
210-
pos = 0;
209+
quoteStart = 0;
211210
break;
212211
}
213212

214-
if (chars[index] === "\\") {
215-
value += chars[++index];
216-
} else {
217-
value += chars[index];
218-
}
213+
// Increment over escape characters.
214+
if (chars[index] === "\\") index++;
215+
216+
value += chars[index];
219217
}
220218

221-
if (pos) {
222-
throw new PathError(`Unterminated quote at index ${pos}`, str);
219+
if (quoteStart) {
220+
throw new PathError(`Unterminated quote at index ${quoteStart}`, str);
223221
}
224222
}
225223

@@ -237,54 +235,50 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
237235
if (type) {
238236
tokens.push({ type, index: index++, value });
239237
} else if (value === "\\") {
240-
tokens.push({ type: "ESCAPED", index: index++, value: chars[index++] });
238+
tokens.push({ type: "escape", index: index++, value: chars[index++] });
241239
} else if (value === ":") {
242-
tokens.push({ type: "PARAM", index: index++, value: name() });
240+
tokens.push({ type: "param", index: index++, value: name() });
243241
} else if (value === "*") {
244-
tokens.push({ type: "WILDCARD", index: index++, value: name() });
242+
tokens.push({ type: "wildcard", index: index++, value: name() });
245243
} else {
246-
tokens.push({ type: "CHAR", index: index++, value });
244+
tokens.push({ type: "char", index: index++, value });
247245
}
248246
}
249247

250-
tokens.push({ type: "END", index, value: "" });
248+
tokens.push({ type: "end", index, value: "" });
251249

252250
function consumeUntil(endType: TokenType): Token[] {
253251
const output: Token[] = [];
254252

255253
while (true) {
256-
const { type, value, index } = tokens[pos++];
257-
if (type === endType) break;
258-
259-
if (type === "CHAR" || type === "ESCAPED") {
260-
let path = value;
261-
while (true) {
262-
const next = tokens[pos];
263-
if (next.type !== "CHAR" && next.type !== "ESCAPED") break;
264-
pos++;
265-
path += next.value;
254+
const token = tokens[pos++];
255+
if (token.type === endType) break;
256+
257+
if (token.type === "char" || token.type === "escape") {
258+
let path = token.value;
259+
let cur = tokens[pos];
260+
261+
while (cur.type === "char" || cur.type === "escape") {
262+
path += cur.value;
263+
cur = tokens[++pos];
266264
}
267-
output.push({ type: "text", value: encodePath(path) });
268-
continue;
269-
}
270265

271-
if (type === "PARAM") {
272266
output.push({
273-
type: "param",
274-
name: value,
267+
type: "text",
268+
value: encodePath(path),
275269
});
276270
continue;
277271
}
278272

279-
if (type === "WILDCARD") {
273+
if (token.type === "param" || token.type === "wildcard") {
280274
output.push({
281-
type: "wildcard",
282-
name: value,
275+
type: token.type,
276+
name: token.value,
283277
});
284278
continue;
285279
}
286280

287-
if (type === "{") {
281+
if (token.type === "{") {
288282
output.push({
289283
type: "group",
290284
tokens: consumeUntil("}"),
@@ -293,15 +287,15 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
293287
}
294288

295289
throw new PathError(
296-
`Unexpected ${type} at index ${index}, expected ${endType}`,
290+
`Unexpected ${token.type} at index ${token.index}, expected ${endType}`,
297291
str,
298292
);
299293
}
300294

301295
return output;
302296
}
303297

304-
return new TokenData(consumeUntil("END"), str);
298+
return new TokenData(consumeUntil("end"), str);
305299
}
306300

307301
/**

0 commit comments

Comments
 (0)