Skip to content

Commit 16ca726

Browse files
committed
SL-18330: Parse notation true/false without using _get_re()
and hence without using LLSDBaseParser._peek().
1 parent 66164b4 commit 16ca726

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

llsd/serde_notation.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
_format_datestr, _parse_datestr, _str_to_bytes, binary, uri)
99

1010
_real_regex = re.compile(br"[-+]?(?:(\d+(\.\d*)?|\d*\.\d+)([eE][-+]?\d+)?)|[-+]?inf|[-+]?nan")
11-
_true_regex = re.compile(br"TRUE|true|\b[Tt]\b")
12-
_false_regex = re.compile(br"FALSE|false|\b[Ff]\b")
1311

1412

1513
class LLSDNotationParser(LLSDBaseParser):
@@ -346,10 +344,50 @@ def _parse_string_raw(self):
346344
raise LLSDParseError(exc)
347345

348346
def _parse_true(self, cc):
349-
return self._get_re(cc, "'true'", _true_regex, True)
347+
# match t, T, true, TRUE -- not mixed-case
348+
try:
349+
rest = {b't': b'rue', b'T': b'RUE'}[cc]
350+
except KeyError:
351+
self._error("Invalid 'true' token")
352+
353+
cc = self._getc(full=False)
354+
# beware, rest is bytes, so bytes[0] is an int!
355+
if cc != rest[:1]:
356+
# just 't' or 'T' is legal, put back cc and carry on
357+
if cc:
358+
self._putback()
359+
return True
360+
361+
# saw 'tr' or 'TR', cc is the 'r'
362+
tail = self._getc(len(rest)-1)
363+
# 'tr' MUST be followed by 'ue'
364+
if tail != rest[1:]:
365+
self._error("Invalid 'true' token")
366+
# good, it is
367+
return True
350368

351369
def _parse_false(self, cc):
352-
return self._get_re(cc, "'false'", _false_regex, False)
370+
# match f, F, false, FALSE -- not mixed-case
371+
try:
372+
rest = {b'f': b'alse', b'F': b'ALSE'}[cc]
373+
except KeyError:
374+
self._error("Invalid 'false' token")
375+
376+
cc = self._getc(full=False)
377+
# beware, rest is bytes, so bytes[0] is an int!
378+
if cc != rest[:1]:
379+
# just 'f' or 'F' is legal, put back cc and carry on
380+
if cc:
381+
self._putback()
382+
return False
383+
384+
# saw 'fa' or 'FA', cc is the 'a'
385+
tail = self._getc(len(rest)-1)
386+
# 'fa' MUST be followed by 'lse'
387+
if tail != rest[1:]:
388+
self._error("Invalid 'false' token")
389+
# good, it is
390+
return False
353391

354392

355393
class LLSDNotationFormatter(LLSDBaseFormatter):

0 commit comments

Comments
 (0)