-
-
Notifications
You must be signed in to change notification settings - Fork 664
fetch: improve regexes in data-uri.js #4483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: tsctx <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves regex patterns in the data-url.js file to address performance issues identified by SonarJS. The changes focus on optimizing regex patterns and refactoring a for loop to a while loop.
- Adds the
u
flag to regex patterns for better Unicode handling and performance - Optimizes character class ordering and grouping in regex patterns
- Refactors a for loop to a while loop in the percentDecode function
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
@@ -86,7 +86,7 @@ function dataURLProcessor (dataURL) { | |||
|
|||
// 5. Remove trailing U+0020 SPACE code points from mimeType, | |||
// if any. | |||
mimeType = mimeType.replace(/(\u0020)+$/, '') | |||
mimeType = mimeType.replace(/(\u0020+)$/u, '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capturing group (\u0020+)
is unnecessary since $1
is not used in the replacement. Use a non-capturing group instead: /(?:\u0020+)$/u
or simply /\u0020+$/u
mimeType = mimeType.replace(/(\u0020+)$/u, '') | |
mimeType = mimeType.replace(/\u0020+$/u, '') |
Copilot uses AI. Check for mistakes.
@@ -572,7 +574,7 @@ function serializeAMimeType (mimeType) { | |||
if (!HTTP_TOKEN_CODEPOINTS.test(value)) { | |||
// 1. Precede each occurrence of U+0022 (") or | |||
// U+005C (\) in value with U+005C (\). | |||
value = value.replace(/(\\|")/g, '\\$1') | |||
value = value.replace(/[\\"]/ug, '\\$&') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The g
flag is redundant when combined with the u
flag for this specific pattern. The u
flag alone is sufficient for Unicode support, and g
should only be used when multiple replacements are needed.
Copilot uses AI. Check for mistakes.
sonarjs was reporting some imperformance in the regexes
This relates to...
Rationale
Changes
Features
Bug Fixes
Breaking Changes and Deprecations
Status