From 3ea0699c2050cca1c7e2eda11089bd6e1d103708 Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 21 Jan 2025 07:35:20 +0800 Subject: [PATCH] core: optimize regex for isValidPid --- packages/hydrooj/src/model/problem.ts | 2 +- packages/import-hoj/index.ts | 2 +- packages/import-qduoj/index.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 9d1dc3242..a4028c94f 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -487,7 +487,7 @@ export class ProblemModel { let overridePid = null; const isValidPid = async (id: string) => { - if (!(/^[A-Za-z]+[0-9A-Za-z]*$/.test(id))) return false; + if (!(/^[A-Za-z][0-9A-Za-z]*$/.test(id))) return false; const doc = await ProblemModel.get(domainId, id); if (doc) { if (!override) return false; diff --git a/packages/import-hoj/index.ts b/packages/import-hoj/index.ts index 973fce63c..405ab9844 100644 --- a/packages/import-hoj/index.ts +++ b/packages/import-hoj/index.ts @@ -46,7 +46,7 @@ class ImportHojHandler extends Handler { content.samples = examples.map((sample) => ([sample.input, sample.output])); } const isValidPid = async (id: string) => { - if (!(/^[A-Za-z]+[0-9A-Za-z]*$/.test(id))) return false; + if (!(/^[A-Za-z][0-9A-Za-z]*$/.test(id))) return false; if (await ProblemModel.get(domainId, id)) return false; return true; }; diff --git a/packages/import-qduoj/index.ts b/packages/import-qduoj/index.ts index 7abca129d..047deecb7 100644 --- a/packages/import-qduoj/index.ts +++ b/packages/import-qduoj/index.ts @@ -68,7 +68,7 @@ class ImportQduojHandler extends Handler { }, 'html'); if (+pdoc.display_id) pdoc.display_id = `P${pdoc.display_id}`; const isValidPid = async (id: string) => { - if (!(/^[A-Za-z]+[0-9A-Za-z]*$/.test(id))) return false; + if (!(/^[A-Za-z][0-9A-Za-z]*$/.test(id))) return false; if (await ProblemModel.get(domainId, id)) return false; return true; };