import string


def convert_to_int(constant):
    """
    Parse a string representing a constant in the appropriate base.

    Take a string such as "0b01111010", "0173", "124", or "0x7d" and parse it
    into the equivalent value, such as 122, 123, 124, or 125, respectively.
    Python's int(constant, 0) call is generally good at handling this, except
    in the case of octal, in which it assumes that a leading zero on its own
    (that is, not a leading "0b" or "0x") represents a decimal number; the
    octal prefix in Python is "0o".

    It should also be noted that while a leading "0b" can be used to represent
    a binary literal, this is a GCC extension and will produce a corresponding
    warning actual compilation. It should not be relied on for portability.
    However, this function will parse it as a valid constant.

    Args:
        constant: a string representing a constant in binary, octal, decimal,
                  or hex.

    Returns:
        An int representing the equivalent value of the constant.
    """
    if (len(constant) > 1 and constant[0] == '0'
        and constant[1] in string.digits):
        # Parse as octal constant.
        return int(constant, 8)
    return int(constant, 0)