import re


def pascal_to_snake_case(s: str) -> str:
    """Convert a string from PascalCase to snake_case"""
    if not s:
        return s
    # Interpret sequences of 2+ uppercase chars as acronyms to be wordified. e.g. HTML -> Html
    result = re.sub(r"([A-Z])([A-Z]+)", lambda m: m.group(1) + m.group(2).lower(), s)
    # Find any uppercase character(s) following a lowercase character, insert an underscore and
    # convert. E.g. aA -> a_a, MyHtmlThing -> my_html_thing
    # Replace any aA combo with a_a:
    result = re.sub(
        r"([^A-Z])([A-Z]+)",
        lambda m: "_".join((m.group(1), m.group(2).lower())),
        result,
    )
    # Force lowercase first char:
    return result[0].lower() + result[1:]