Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60e1ce5

Browse files
committed
Simplify definition of mathtext symbols.
Use a single regex that handles both single_symbol (a single character) and symbol_name (`\knowntexsymbolname`), and also slightly simplify the "end-of-symbol-name" regex. This parsing element comes up extremely often, and removing one indirection layers shaves off ~3-4% off drawing all the current mathtext tests, i.e. ``` MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)' ```
1 parent 7c19d85 commit 60e1ce5

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import functools
99
import logging
1010
import os
11+
import re
1112
import types
1213
import unicodedata
1314

@@ -1710,15 +1711,13 @@ def set_names_and_parse_actions():
17101711
p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
17111712
p.space = oneOf(self._space_widths)("space")
17121713

1713-
p.single_symbol = Regex(
1714-
r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
1715-
"\U00000080-\U0001ffff" # unicode range
1716-
)("sym")
17171714
p.accentprefixed = "\\" + oneOf(self._accentprefixed)("sym")
1718-
p.symbol_name = (
1719-
oneOf([rf"\{sym}" for sym in tex2uni])("sym")
1720-
+ Regex("(?=[^A-Za-z]|$)").leaveWhitespace())
1721-
p.symbol = (p.single_symbol | p.symbol_name).leaveWhitespace()
1715+
p.symbol = Regex(
1716+
r"[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]"
1717+
r"|\\[%${}\[\]_|]"
1718+
+ r"|\\(?:{})(?![A-Za-z])".format(
1719+
"|".join(map(re.escape, tex2uni)))
1720+
)("sym").leaveWhitespace()
17221721
p.unknown_symbol = Regex(r"\\[A-Za-z]*")("name")
17231722

17241723
p.font = "\\" + oneOf(self._fontnames)("font")

0 commit comments

Comments
 (0)