Skip to content

Commit

Permalink
perf: Various micro optimizations
Browse files Browse the repository at this point in the history
Sort regular expressions to match most common pattern first. Use re.findall() instead of re.finditer(). Reduce allocations for new strings.
  • Loading branch information
defnull committed Oct 18, 2024
1 parent 02e8146 commit babed4d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ def __get__(self, obj, cls):
# Header Parser
# -------------


_special = re.escape('()<>@,;:"\\/[]?={} \t')
_re_special = re.compile(r'[%s]' % _special)
_quoted_string = r'"(?:\\.|[^"])*"' # Quoted string
_value = r'(?:[^%s]+|%s)' % (_special, _quoted_string) # Save or quoted string
_option = r'(?:;|^)\s*([^%s]+)\s*=\s*(%s)' % (_special, _value)
_re_option = re.compile(_option) # key=value part of an Content-Type like header
# ASCII minus control or special chars
_token="[a-zA-Z0-9-!#$%&'*+.^_`|~]+"
_re_istoken = re.compile("^%s$" % _token, re.ASCII)
# A token or quoted-string (simple qs | token | slow qs)
_value = r'"[^\\"]*"|%s|"(?:\\.|[^"])*"' % _token
# A "; key=value" pair from content-disposition header
_option = r'; *(%s) *= *(%s)' % (_token, _value)
_re_option = re.compile(_option)


def header_quote(val):
Expand All @@ -153,7 +154,7 @@ def header_quote(val):
Note: This is NOT the way modern browsers quote field names or filenames
in Content-Disposition headers. See :func:`content_disposition_quote`
"""
if not _re_special.search(val):
if _re_istoken.match(val):
return val

return '"' + val.replace("\\", "\\\\").replace('"', '\\"') + '"'
Expand Down Expand Up @@ -216,17 +217,17 @@ def parse_options_header(header, options=None, unquote=header_unquote):
function. See `content_disposition_unquote`.
"""
value, sep, tail = header.partition(";")
if not sep:

i = header.find(";")
if i < 0:
return header.lower().strip(), {}

options = options or {}
for match in _re_option.finditer(tail):
key, val = match.groups()
for key, val in _re_option.findall(header, i):
key = key.lower()
options[key] = unquote(val, key == "filename")

return value.lower(), options
return header[:i].lower().strip(), options


##############################################################################
Expand Down Expand Up @@ -552,7 +553,7 @@ def _close_headers(self):
raise self._fail("Invalid Content-Disposition segment header: Wrong type")
if "name" not in args and self._parser.strict:
raise self._fail("Invalid Content-Disposition segment header: Missing name option")
self.name = args.get("name", "")
self.name = args.get("name") or ""
self.filename = args.get("filename")
elif h == "Content-Type":
self.content_type, args = parse_options_header(v)
Expand Down

0 comments on commit babed4d

Please sign in to comment.