import re


def parse_int(to_convert:str):
    """Converts a string to an integer with the
    given base. If base is None and the string starts
    with a 0, automatically resolve the given base.

    Supported bases are:
    - 0x: Hexadecimal
    - 0o or 0: Octal

    The rest is interpreted as base 10.

    Args:
        to_convert (string): The string to convert to an int.
        base (int, optional): Assume to_convert is in this base. If none, get the base from the string. Defaults to None.

    Raises:
        ValueError: If to_convert cannot be converted.

    Returns:
        int: The converted integer.
    """
    
    to_convert = to_convert.lower()
    
    # Determine which system this uses
    if re.match("0x.*", to_convert):
        base = 16

    elif re.match("0b.*", to_convert):
        base = 2
    
    elif re.match("0o.*", to_convert):
        base = 8
    
    else:
        base = 10

    # Try to convert the number.
    return int(to_convert, base)