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

Skip to content

Commit 56a5153

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 dab648a commit 56a5153

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

@@ -1723,15 +1724,13 @@ def set_names_and_parse_actions():
17231724
p.style_literal = oneOf(
17241725
[str(e.value) for e in self._MathStyle])("style_literal")
17251726

1726-
p.single_symbol = Regex(
1727-
r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
1728-
"\U00000080-\U0001ffff" # unicode range
1729-
)("sym")
17301727
p.accentprefixed = "\\" + oneOf(self._accentprefixed)("sym")
1731-
p.symbol_name = (
1732-
oneOf([rf"\{sym}" for sym in tex2uni])("sym")
1733-
+ Regex("(?=[^A-Za-z]|$)").leaveWhitespace())
1734-
p.symbol = (p.single_symbol | p.symbol_name).leaveWhitespace()
1728+
p.symbol = Regex(
1729+
r"[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]"
1730+
r"|\\[%${}\[\]_|]"
1731+
+ r"|\\(?:{})(?![A-Za-z])".format(
1732+
"|".join(map(re.escape, tex2uni)))
1733+
)("sym").leaveWhitespace()
17351734
p.unknown_symbol = Regex(r"\\[A-Za-z]*")("name")
17361735

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

0 commit comments

Comments
 (0)