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

Skip to content

Commit 8fa303f

Browse files
committed
Simplify enc file parsing
Use re.findall, and open the file as binary.
1 parent 119934a commit 8fa303f

1 file changed

Lines changed: 16 additions & 29 deletions

File tree

lib/matplotlib/dviread.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ class Encoding(object):
965965
__slots__ = ('encoding',)
966966

967967
def __init__(self, filename):
968-
with open(filename, 'rt') as file:
968+
with open(filename, 'rb') as file:
969969
matplotlib.verbose.report('Parsing TeX encoding ' + filename,
970970
'debug-annoying')
971971
self.encoding = self._parse(file)
@@ -979,34 +979,21 @@ def __iter__(self):
979979
def _parse(self, file):
980980
result = []
981981

982-
state = 0
983-
for line in file:
984-
line = six.b(line)
985-
comment_start = line.find(b'%')
986-
if comment_start > -1:
987-
line = line[:comment_start]
988-
line = line.strip()
989-
990-
if state == 0:
991-
# Expecting something like /FooEncoding [
992-
if b'[' in line:
993-
state = 1
994-
line = line[line.index(b'[')+1:].strip()
995-
996-
if state == 1:
997-
if b']' in line: # ] def
998-
line = line[:line.index(b']')]
999-
state = 2
1000-
words = line.split()
1001-
for w in words:
1002-
if w.startswith(b'/'):
1003-
# Allow for /abc/def/ghi
1004-
subwords = w.split(b'/')
1005-
result.extend(subwords[1:])
1006-
else:
1007-
raise ValueError("Broken name in encoding file: " + w)
1008-
1009-
return result
982+
lines = (line[:line.find(b'%')] if b'%' in line else line.strip()
983+
for line in file)
984+
data = b''.join(lines)
985+
match = re.search(six.b(r'\['), data)
986+
if not match:
987+
raise ValueError("Cannot locate beginning of encoding in {}"
988+
.format(file))
989+
data = data[match.span()[1]:]
990+
match = re.search(six.b(r'\]'), data)
991+
if not match:
992+
raise ValueError("Cannot locate end of encoding in {}"
993+
.format(file))
994+
data = data[:match.span()[0]]
995+
996+
return re.findall(six.b(r'/([^][{}<>\s]+)'), data)
1010997

1011998

1012999
def find_tex_file(filename, format=None):

0 commit comments

Comments
 (0)