#37082 closed Cleanup/optimization (wontfix)

Improved readability and minor performance of sanitize_strftime_format regex substitution

Reported by: xMintTea Owned by:
Component: Core (Other) Version: 6.0
Severity: Normal Keywords: sanitize_strftime_format, regex, readability, performance
Cc: xMintTea Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The current implementation of sanitize_strftime_format uses an inline lambda with indexed groups (r"%s%%0%s%s" % (m[1], mapping[m[2]], m[2])) and a compact regular expression. While correct, it is not immediately obvious what the replacement does and why the groups are referenced by index.

This patch replaces the lambda with a small helper function _replace_specifier, uses a named group (?P<letter>...) in the regex, enables re.VERBOSE for readability, and switches to an f-string for clarity.

No behavioral changes – the early exit condition and the mapping dictionary remain identical. Equivalence was verified on Windows by mocking the early exit and comparing outputs for a comprehensive set of format strings, including edge cases with escaped percent signs (%%). The regex match results were also confirmed to be the same.

Additionally, the use of an f-string and named group yields a ~12-13% performance improvement over the old %-formatting with m[1]/m[2] access (measured with timeit on 240,000 calls).

Change History (2)

comment:1 by xMintTea, 2 hours ago

Has patch: set

comment:2 by Jacob Walls, 108 minutes ago

Has patch: unset
Resolution: wontfix
Status: newclosed

This function is cached.

Note: See TracTickets for help on using tickets.
Back to Top