```python
from __future__ import annotations
import sys
from pathlib import Path

# Byte-order marks, longest first: the UTF-32 LE BOM ("ff fe 00 00") starts with
# the UTF-16 LE BOM ("ff fe"), so UTF-32 must be checked before UTF-16.
_BOMS = (
    (b"\xef\xbb\xbf", "utf-8-sig"),
    (b"\xff\xfe\x00\x00", "utf-32"),
    (b"\x00\x00\xfe\xff", "utf-32"),
    (b"\xff\xfe", "utf-16"),
    (b"\xfe\xff", "utf-16"),
)


def read_text_file(path: str) -> str | None:
    try:
        data = Path(path).read_bytes()
    except Exception as e:
        print(f"  [warn] read_text_file failed: {type(e).__name__}: {e}", file=sys.stderr)
        return None

    # Decode by BOM when present (the utf-16/utf-32 codecs strip the BOM and
    # auto-select byte order).
    for bom, encoding in _BOMS:
        if data.startswith(bom):
            try:
                return data.decode(encoding)
            except (UnicodeDecodeError, LookupError):
                # The BOM is an explicit encoding declaration. Trying cp1252 or
                # latin-1 here would silently accept damaged data as mojibake.
                print(f"  [warn] read_text_file: invalid {encoding} data after BOM", file=sys.stderr)
                return None

    # No BOM: fall back to the prior chain for BOM-less files.
    for encoding in ("utf-8", "cp1252", "latin-1"):
        try:
            return data.decode(encoding)
        except UnicodeDecodeError:
            continue
    return None
```

---

Copyright (c) 2025 virgiliojr94 License files are attached. [MIT](https://knowen.md/f/9833e77c-92ec-431d-8fba-7b3beff917f7)

### MIT notice

````text
MIT License

Copyright (c) 2025 virgiliojr94

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
````