Use iterator to read chars

This commit is contained in:
Florian Bruhin 2014-11-04 20:41:29 +01:00
parent 409eba76ca
commit 84916448a9

View File

@ -19,8 +19,6 @@
"""Our own fork of shlex.split with some added and removed features.""" """Our own fork of shlex.split with some added and removed features."""
from io import StringIO
from qutebrowser.utils import log from qutebrowser.utils import log
@ -36,7 +34,7 @@ class ShellLexer:
""" """
def __init__(self, s): def __init__(self, s):
self.instream = StringIO(s) self.iterator = iter(s)
self.whitespace = ' \t\r\n' self.whitespace = ' \t\r\n'
self.quotes = '\'"' self.quotes = '\'"'
self.escape = '\\' self.escape = '\\'
@ -49,14 +47,17 @@ class ShellLexer:
quoted = False quoted = False
escapedstate = ' ' escapedstate = ' '
while True: while True:
nextchar = self.instream.read(1) try:
nextchar = next(self.iterator)
except StopIteration:
nextchar = None
log.shlexer.vdebug("in state {!r} I see character: {!r}".format( log.shlexer.vdebug("in state {!r} I see character: {!r}".format(
self.state, nextchar)) self.state, nextchar))
if self.state is None: if self.state is None:
self.token = None # past end of file self.token = None # past end of file
break break
elif self.state == ' ': elif self.state == ' ':
if not nextchar: if nextchar is None:
self.state = None # end of file self.state = None # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
@ -75,7 +76,7 @@ class ShellLexer:
self.state = 'a' self.state = 'a'
elif self.state in self.quotes: elif self.state in self.quotes:
quoted = True quoted = True
if not nextchar: # end of file if nextchar is None: # end of file
log.shlexer.vdebug("I see EOF in quotes state") log.shlexer.vdebug("I see EOF in quotes state")
self.state = None self.state = None
break break
@ -88,7 +89,7 @@ class ShellLexer:
else: else:
self.token = self.token + nextchar self.token = self.token + nextchar
elif self.state in self.escape: elif self.state in self.escape:
if not nextchar: # end of file if nextchar is None: # end of file
log.shlexer.vdebug("I see EOF in escape state") log.shlexer.vdebug("I see EOF in escape state")
self.token += self.state self.token += self.state
self.state = None self.state = None
@ -101,7 +102,7 @@ class ShellLexer:
self.token = self.token + nextchar self.token = self.token + nextchar
self.state = escapedstate self.state = escapedstate
elif self.state == 'a': elif self.state == 'a':
if not nextchar: if nextchar is None:
self.state = None # end of file self.state = None # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace: