-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
perf(): rm runtime RegExp usages #9802
Changes from 2 commits
787eec9
821278f
3790f13
2a97de8
3019c8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/** | ||
* Do not use in runtime | ||
*/ | ||
export function getSvgRegex(arr: string[]) { | ||
return new RegExp('^(' + arr.join('|') + ')\\b', 'i'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { reNum } from '../../parser/constants'; | ||
|
||
const regex = new RegExp(`(${reNum})`, 'gi'); | ||
|
||
export const cleanupSvgAttribute = (attributeValue: string) => | ||
attributeValue | ||
.replace(new RegExp(`(${reNum})`, 'gi'), ' $1 ') | ||
.replace(regex, ' $1 ') | ||
// replace annoying commas and arbitrary whitespace with single spaces | ||
.replace(/,/gi, ' ') | ||
.replace(/\s+/gi, ' '); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -829,6 +829,9 @@ export const getPointOnPath = ( | |
} | ||
}; | ||
|
||
const rePathCmdAll = new RegExp(rePathCommand, 'gi'); | ||
const rePathCmd = new RegExp(rePathCommand, 'i'); | ||
|
||
/** | ||
* | ||
* @param {string} pathString | ||
|
@@ -847,41 +850,31 @@ export const parsePath = (pathString: string): TComplexPathData => { | |
pathString = cleanupSvgAttribute(pathString); | ||
|
||
const res: TComplexPathData = []; | ||
for (const match of pathString.matchAll(new RegExp(rePathCommand, 'gi'))) { | ||
let matchStr = match[0]; | ||
for (let [match] of pathString.matchAll(rePathCmdAll)) { | ||
const chain: TComplexPathData = []; | ||
let paramArr: RegExpExecArray | null; | ||
do { | ||
paramArr = new RegExp(rePathCommand, 'i').exec(matchStr); | ||
paramArr = rePathCmd.exec(match); | ||
if (!paramArr) { | ||
break; | ||
} | ||
// ignore undefined match groups | ||
const filteredGroups = paramArr.filter((g) => g); | ||
const filteredGroups = paramArr.filter(Boolean); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This equal to call Boolean as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd avoid |
||
// remove the first element from the match array since it's just the whole command | ||
filteredGroups.shift(); | ||
const value = filteredGroups.shift(); | ||
// if we can't parse the number, just interpret it as a string | ||
// (since it's probably the path command) | ||
const command = filteredGroups.map((g) => { | ||
const command: (string | number)[] = []; | ||
for (const g of filteredGroups) { | ||
const numParse = Number.parseFloat(g); | ||
if (Number.isNaN(numParse)) { | ||
return g; | ||
} else { | ||
return numParse; | ||
} | ||
}); | ||
chain.push(command as any); | ||
command.push(Number.isNaN(numParse) ? g : numParse); | ||
} | ||
chain.push(command as TComplexParsedCommand); | ||
// stop now if it's a z command | ||
if (filteredGroups.length <= 1) { | ||
break; | ||
} | ||
// remove the last part of the chained command | ||
filteredGroups.shift(); | ||
// ` ?` is to support commands with optional spaces between flags | ||
matchStr = matchStr.replace( | ||
new RegExp(`${filteredGroups.join(' ?')} ?$`), | ||
'' | ||
); | ||
match = match.slice(value ? value.length : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the changes to the path parsing code please either add a very detailed explanation or let's not do them. |
||
} while (paramArr); | ||
// add the chain, convert multiple m's to l's in the process | ||
chain.reverse().forEach((c, idx) => { | ||
|
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.
This function is already used at runtime during svg parsing.
Which is the concept you wanted to add here? to do not add it in loops?