Avoid compiling "the same" regex multiple times

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2020-05-21 13:00:47 +00:00
parent 045f1f0ad5
commit fd2daf44b4
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC

View file

@ -80,6 +80,9 @@ XML_ENTS_ESCAPE_APOS["'"] = '&apos;'
XML_ENTS_ESCAPE_QUOT = XML_ENTS_NO_QUOTES.copy() XML_ENTS_ESCAPE_QUOT = XML_ENTS_NO_QUOTES.copy()
XML_ENTS_ESCAPE_QUOT['"'] = '&quot;' XML_ENTS_ESCAPE_QUOT['"'] = '&quot;'
# Used to split values where "x y" or "x,y" or a mix of the two is allowed
RE_COMMA_WSP = re.compile(r"\s*[\s,]\s*")
NS = {'SVG': 'http://www.w3.org/2000/svg', NS = {'SVG': 'http://www.w3.org/2000/svg',
'XLINK': 'http://www.w3.org/1999/xlink', 'XLINK': 'http://www.w3.org/1999/xlink',
'SODIPODI': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd', 'SODIPODI': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
@ -2738,7 +2741,7 @@ def parseListOfPoints(s):
# coordinate-pair = coordinate comma-or-wsp coordinate # coordinate-pair = coordinate comma-or-wsp coordinate
# coordinate = sign? integer # coordinate = sign? integer
# comma-wsp: (wsp+ comma? wsp*) | (comma wsp*) # comma-wsp: (wsp+ comma? wsp*) | (comma wsp*)
ws_nums = re.split(r"\s*[\s,]\s*", s.strip()) ws_nums = RE_COMMA_WSP.split(s.strip())
nums = [] nums = []
# also, if 100-100 is found, split it into two also # also, if 100-100 is found, split it into two also
@ -3351,7 +3354,7 @@ def properlySizeDoc(docElement, options):
# else we have a statically sized image and we should try to remedy that # else we have a statically sized image and we should try to remedy that
# parse viewBox attribute # parse viewBox attribute
vbSep = re.split('[, ]+', docElement.getAttribute('viewBox')) vbSep = RE_COMMA_WSP.split(docElement.getAttribute('viewBox'))
# if we have a valid viewBox we need to check it # if we have a valid viewBox we need to check it
vbWidth, vbHeight = 0, 0 vbWidth, vbHeight = 0, 0
if len(vbSep) == 4: if len(vbSep) == 4:
@ -3810,7 +3813,7 @@ def scourString(in_string, options=None):
elem.setAttribute(attr, scourLength(elem.getAttribute(attr))) elem.setAttribute(attr, scourLength(elem.getAttribute(attr)))
viewBox = doc.documentElement.getAttribute('viewBox') viewBox = doc.documentElement.getAttribute('viewBox')
if viewBox: if viewBox:
lengths = re.split('[, ]+', viewBox) lengths = RE_COMMA_WSP.split(viewBox)
lengths = [scourUnitlessLength(length) for length in lengths] lengths = [scourUnitlessLength(length) for length in lengths]
doc.documentElement.setAttribute('viewBox', ' '.join(lengths)) doc.documentElement.setAttribute('viewBox', ' '.join(lengths))