How isTextOrBinary determines if a file is text or binary
masterThe library uses a two-step detection process to ensure high accuracy and performance:
- Extension Check: If a filename is provided, the library checks its extensions from right to left against known text and binary extension lists. This allows for accurate detection of files with multiple extensions (e.g.,
app.x.y). - Contents Check: If no filename is provided, or if the extension check is indeterminate, the library inspects the buffer contents. It checks 24 bytes at the start, middle, and end of the buffer. This three-location check is designed to be more accurate than single-location checks while remaining more performant than scanning large chunks of data.
Note: The combination of extension and content checks is necessary because UTF-16 characters can be indistinguishable from binary data if only content is checked.