##// END OF EJS Templates
make parsestring work with strings that do not have quotes.
Vadim Gelfer -
r1902:1cc5f256 default
parent child Browse files
Show More
@@ -11,19 +11,21 b' esctable = {'
11 11 'v': '\v',
12 12 }
13 13
14 def parsestring(s):
14 def parsestring(s, quoted=True):
15 15 fp = cStringIO.StringIO()
16 if quoted:
17 first = s[0]
18 if len(s) < 2: raise SyntaxError(_('string too short'))
19 if first not in "'\"": raise SyntaxError(_('invalid quote'))
20 if s[-1] != first: raise SyntaxError(_('unmatched quotes'))
21 s = s[1:-1]
16 22 escape = False
17 first = s[0]
18 if len(s) < 2: raise SyntaxError(_('string too short'))
19 if first not in "'\"": raise SyntaxError(_('invalid quote'))
20 if s[-1] != first: raise SyntaxError(_('unmatched quotes'))
21 for c in s[1:-1]:
23 for c in s:
22 24 if escape:
23 25 fp.write(esctable.get(c, c))
24 26 escape = False
25 27 elif c == '\\': escape = True
26 elif c == first: raise SyntaxError(_('string ends early'))
28 elif quoted and c == first: raise SyntaxError(_('string ends early'))
27 29 else: fp.write(c)
28 30 if escape: raise SyntaxError(_('unterminated escape'))
29 31 return fp.getvalue()
General Comments 0
You need to be logged in to leave comments. Login now