Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/chardet/pipeline/ascii.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

10 statements  

1"""Stage 1c: Pure ASCII detection.""" 

2 

3from __future__ import annotations 

4 

5from chardet.pipeline import DetectionResult 

6 

7# Allowed ASCII bytes: tab (0x09), newline (0x0A), carriage return (0x0D), 

8# and printable ASCII (0x20-0x7E). bytes.translate deletes these from the 

9# input; if anything remains, the data is not pure ASCII. 

10_ALLOWED_ASCII: bytes = bytes([0x09, 0x0A, 0x0D, *range(0x20, 0x7F)]) 

11 

12 

13def detect_ascii(data: bytes) -> DetectionResult | None: 

14 """Return an ASCII result if all bytes are printable ASCII plus common whitespace. 

15 

16 :param data: The raw byte data to examine. 

17 :returns: A :class:`DetectionResult` for ASCII, or ``None``. 

18 """ 

19 if not data: 

20 return None 

21 if data.translate(None, _ALLOWED_ASCII): 

22 return None # Non-allowed bytes remain 

23 return DetectionResult(encoding="ascii", confidence=1.0, language=None)