File tree Expand file tree Collapse file tree 1 file changed +9
-23
lines changed Expand file tree Collapse file tree 1 file changed +9
-23
lines changed Original file line number Diff line number Diff line change 2
2
import testcase
3
3
from util import *
4
4
from enum import Enum
5
+ import re
5
6
6
7
7
8
class Mode (Enum ):
@@ -358,38 +359,23 @@ def run(
358
359
359
360
360
361
# 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 ]' )
376
364
377
365
378
366
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
380
371
381
372
382
373
# assumes that the only possible whitespaces are space and newline
383
374
# allows \n\n
384
375
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 :
391
378
return True
392
- last = byte
393
379
return False
394
380
395
381
You can’t perform that action at this time.
0 commit comments