Skip to content

Commit ed1c27f

Browse files
committed
Use a deque for the character queue. Patch by shawn.hsiao
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%401010
1 parent d548509 commit ed1c27f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/html5lib/inputstream.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
from constants import EOF, spaceCharacters, asciiLetters, asciiUppercase
66
from constants import encodings
77
from utils import MethodDispatcher
8+
from collections import deque
89

10+
try:
11+
from collections import deque
12+
except ImportError:
13+
from utils import deque
14+
915
class HTMLInputStream(object):
1016
"""Provides a unicode stream of characters to the HTMLTokenizer.
1117
@@ -55,7 +61,7 @@ def __init__(self, source, encoding=None, parseMeta=True, chardet=True):
5561
self.dataStream = codecs.getreader(self.charEncoding)(self.rawStream,
5662
'replace')
5763

58-
self.queue = []
64+
self.queue = deque([])
5965
self.errors = []
6066

6167
self.line = self.col = 0
@@ -212,7 +218,7 @@ def char(self):
212218
if not self.queue:
213219
return EOF
214220

215-
char = self.queue.pop(0)
221+
char = self.queue.popleft()
216222

217223
# update position in stream
218224
if char == '\n':
@@ -277,8 +283,7 @@ def charsUntil(self, characters, opposite = False):
277283
else:
278284
self.col += 1
279285

280-
rv = u"".join(self.queue[:i])
281-
self.queue = self.queue[i:]
286+
rv = u"".join([ self.queue.popleft() for c in range(i) ])
282287

283288
#Calculate where we now are in the stream
284289
#One possible optimisation would be to store all read characters and
@@ -313,7 +318,9 @@ def charsUntil(self, characters, opposite = False):
313318

314319
def unget(self, chars):
315320
if chars:
316-
self.queue = list(chars) + self.queue
321+
l = list(chars)
322+
l.reverse()
323+
self.queue.extendleft(l)
317324
#Alter the current line, col position
318325
for c in chars[::-1]:
319326
if c == '\n':

0 commit comments

Comments
 (0)