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

Skip to content

Commit cf40c9f

Browse files
committed
Replace re.sub by the faster str.translate.
1 parent 047254d commit cf40c9f

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import logging
1414
import math
1515
import os
16-
import re
1716
import string
1817
import struct
1918
import sys
@@ -119,25 +118,6 @@ def _fill(strings, linelen=75):
119118
result.append(b' '.join(strings[lasti:]))
120119
return b'\n'.join(result)
121120

122-
# PDF strings are supposed to be able to include any eight-bit data,
123-
# except that unbalanced parens and backslashes must be escaped by a
124-
# backslash. However, sf bug #2708559 shows that the carriage return
125-
# character may get read as a newline; these characters correspond to
126-
# \gamma and \Omega in TeX's math font encoding. Escaping them fixes
127-
# the bug.
128-
_string_escape_regex = re.compile(br'([\\()\r\n])')
129-
130-
131-
def _string_escape(match):
132-
m = match.group(0)
133-
if m in br'\()':
134-
return b'\\' + m
135-
elif m == b'\n':
136-
return br'\n'
137-
elif m == b'\r':
138-
return br'\r'
139-
assert False
140-
141121

142122
def _create_pdf_info_dict(backend, metadata):
143123
"""
@@ -250,6 +230,15 @@ def _datetime_to_pdf(d):
250230
return r
251231

252232

233+
# PDF strings are supposed to be able to include any eight-bit data, except
234+
# that unbalanced parens and backslashes must be escaped by a backslash.
235+
# However, sf bug #2708559 shows that the carriage return character may get
236+
# read as a newline; these characters correspond to \gamma and \Omega in TeX's
237+
# math font encoding. Escaping them fixes the bug.
238+
_str_escapes = str.maketrans({
239+
'\\': '\\\\', '(': '\\(', ')': '\\)', '\n': '\\n', '\r': '\\r'})
240+
241+
253242
def pdfRepr(obj):
254243
"""Map Python objects to PDF syntax."""
255244

@@ -289,8 +278,12 @@ def pdfRepr(obj):
289278
# escaped. Actually balanced parens are allowed, but it is
290279
# simpler to escape them all. TODO: cut long strings into lines;
291280
# I believe there is some maximum line length in PDF.
281+
# Despite the extra decode/encode, translate is faster than regex.
292282
elif isinstance(obj, bytes):
293-
return b'(' + _string_escape_regex.sub(_string_escape, obj) + b')'
283+
return (
284+
b'(' +
285+
obj.decode('latin-1').translate(_str_escapes).encode('latin-1')
286+
+ b')')
294287

295288
# Dictionaries. The keys must be PDF names, so if we find strings
296289
# there, we make Name objects from them. The values may be

0 commit comments

Comments
 (0)