Skip to content

Commit a6fd74c

Browse files
committed
faster sanity checks
1 parent 2ed3c1c commit a6fd74c

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

bin/validate.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import testcase
33
from util import *
44
from enum import Enum
5+
import re
56

67

78
class Mode(Enum):
@@ -358,38 +359,23 @@ def run(
358359

359360

360361
# Checks if byte is printable or whitespace
361-
def _in_invalid_byte(byte, *, other_whitespaces=False):
362-
if other_whitespaces:
363-
if byte == ord('\t'):
364-
return False
365-
if byte == ord('\r'):
366-
return False
367-
if byte == ord('\v'):
368-
return False
369-
if byte == ord('\f'):
370-
return False
371-
if byte == ord('\n'):
372-
return False
373-
if byte >= 0x20 and byte < 0x7F:
374-
return False
375-
return True
362+
INVALID_BYTES_NO_WHITESPACE = re.compile(b'[^\n\x20-\x7E]')
363+
INVALID_BYTES_WHITESPACE = re.compile(b'[^\t\r\v\f\n\x20-\x7E]')
376364

377365

378366
def _has_invalid_byte(bytes, *, other_whitespaces=False):
379-
return any(_in_invalid_byte(b, other_whitespaces=other_whitespaces) for b in bytes)
367+
if other_whitespaces:
368+
return INVALID_BYTES_WHITESPACE.search(bytes) is not None
369+
else:
370+
return INVALID_BYTES_NO_WHITESPACE.search(bytes) is not None
380371

381372

382373
# assumes that the only possible whitespaces are space and newline
383374
# allows \n\n
384375
def _has_consecutive_whitespaces(bytes):
385-
last = -1
386-
for byte in bytes:
387-
cur_whitespace = byte == ord(' ') or byte == ord('\n')
388-
if last == ord(' ') and cur_whitespace:
389-
return True
390-
if last == ord('\n') and byte == ord(' '):
376+
for bad in [b' \n', b' ', b'\n ']:
377+
if bytes.find(bad) >= 0:
391378
return True
392-
last = byte
393379
return False
394380

395381

0 commit comments

Comments
 (0)