@@ -112,7 +112,7 @@ def fill(strings, linelen=75):
112
112
result .append (' ' .join (strings [lasti :]))
113
113
return '\n ' .join (result )
114
114
115
-
115
+ _string_escape_regex = re . compile ( r'([\\()])' )
116
116
def pdfRepr (obj ):
117
117
"""Map Python objects to PDF syntax."""
118
118
@@ -138,7 +138,7 @@ def pdfRepr(obj):
138
138
# simpler to escape them all. TODO: cut long strings into lines;
139
139
# I believe there is some maximum line length in PDF.
140
140
elif is_string_like (obj ):
141
- return '(' + re .sub (r'([\\()])' , r'\\\1' , obj ) + ')'
141
+ return '(' + _string_escape_regex .sub (r'\\\1' , obj ) + ')'
142
142
143
143
# Dictionaries. The keys must be PDF names, so if we find strings
144
144
# there, we make Name objects from them. The values may be
@@ -207,12 +207,13 @@ def write(self, contents, file):
207
207
208
208
class Name :
209
209
"""PDF name object."""
210
+ _regex = re .compile (r'[^!-~]' )
210
211
211
212
def __init__ (self , name ):
212
213
if isinstance (name , Name ):
213
214
self .name = name .name
214
215
else :
215
- self .name = re . sub (r'[^!-~]' , Name .hexify , name )
216
+ self .name = self . _regex . sub (Name .hexify , name )
216
217
217
218
def __repr__ (self ):
218
219
return "<Name %s>" % self .name
0 commit comments