Skip to content

Make the number pattern regular expression more efficient#1213

Merged
akx merged 1 commit intopython-babel:masterfrom
akx:more-efficient-number-pattern-re
Jul 3, 2025
Merged

Make the number pattern regular expression more efficient#1213
akx merged 1 commit intopython-babel:masterfrom
akx:more-efficient-number-pattern-re

Conversation

@akx
Copy link
Member

@akx akx commented Jun 24, 2025

I verified that all patterns parsed for importing CLDR data are parsed equivalently using the new regular expression.

The inefficient regular expression was brought to our attention by GitHub user @s-sanskar – thanks!

@akx akx requested a review from Copilot June 24, 2025 11:52
Copy link

Copilot AI left a 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 makes the regular expression used for parsing number patterns more efficient while preserving equivalent parsing behavior for CLDR data.

  • Consolidates multiple regex components into a single, more efficient pattern.
  • Updates the matching in parse_pattern to use the new regex.
Comments suppressed due to low confidence (2)

babel/numbers.py:1204

  • Consider adding a comment explaining the rationale behind the new regex pattern to clarify its intended improvements in efficiency and maintainability.
_number_pattern_re = re.compile(

babel/numbers.py:1239

  • Ensure that the change from number_re to _number_pattern_re is intentional and thoroughly tested, as per the pull request description.
        rv = _number_pattern_re.search(pattern)

@codecov
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.97%. Comparing base (de44e18) to head (019ba6c).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1213      +/-   ##
==========================================
- Coverage   91.98%   91.97%   -0.01%     
==========================================
  Files          27       27              
  Lines        4693     4688       -5     
==========================================
- Hits         4317     4312       -5     
  Misses        376      376              
Flag Coverage Δ
macos-14-3.10 91.01% <100.00%> (-0.01%) ⬇️
macos-14-3.11 90.95% <100.00%> (-0.01%) ⬇️
macos-14-3.12 91.16% <100.00%> (-0.01%) ⬇️
macos-14-3.13 91.16% <100.00%> (-0.01%) ⬇️
macos-14-3.8 90.88% <100.00%> (-0.01%) ⬇️
macos-14-3.9 90.94% <100.00%> (-0.01%) ⬇️
macos-14-pypy3.10 91.01% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.10 91.04% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.11 90.97% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.12 91.19% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.13 91.19% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.8 90.90% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-3.9 90.97% <100.00%> (-0.01%) ⬇️
ubuntu-24.04-pypy3.10 91.04% <100.00%> (-0.01%) ⬇️
windows-2022-3.10 91.03% <100.00%> (-0.01%) ⬇️
windows-2022-3.11 90.96% <100.00%> (-0.01%) ⬇️
windows-2022-3.12 91.18% <100.00%> (-0.01%) ⬇️
windows-2022-3.13 91.18% <100.00%> (-0.01%) ⬇️
windows-2022-3.8 91.00% <100.00%> (-0.01%) ⬇️
windows-2022-3.9 90.96% <100.00%> (-0.01%) ⬇️
windows-2022-pypy3.10 91.03% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

I verified that all patterns parsed for importing CLDR data
are parsed equivalently using the new regular expression.

The inefficient regular expression was brought to our attention
by GitHub user s-sanskar – thanks!

Co-authored-by: s-sanskar <sanskarpok11@gmail.com>
@akx akx force-pushed the more-efficient-number-pattern-re branch from c0f4caa to 019ba6c Compare June 24, 2025 11:56
@akx akx requested a review from tomasr8 June 24, 2025 11:56
@akx akx marked this pull request as ready for review June 24, 2025 12:03
Copy link
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the two subpatterns in PREFIX_PATTERN are swapped? Really hard to visually compare two long regexes 😄

@akx akx merged commit 69c5db8 into python-babel:master Jul 3, 2025
26 checks passed
@akx akx added this to the Babel 2.18 milestone Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants