'When creating a new ANSIString, you may use a custom parser that has
the same attributes as the standard one, and you may declare the
string to be handled as already decoded. It is important not to double
decode strings, as escapes can only be respected once.
Internally, ANSIString can also passes itself precached code/character
indexes and clean strings to avoid doing extra work when combining
ANSIStrings.'

def __new__(cls, *args, **kwargs):

    string = args[0]
    if (not isinstance(string, basestring)):
        string = to_str(string, force_string=True)
    parser = kwargs.get('parser', ANSI_PARSER)
    decoded = (kwargs.get('decoded', False) or hasattr(string, '_raw_string'))
    code_indexes = kwargs.pop('code_indexes', None)
    char_indexes = kwargs.pop('char_indexes', None)
    clean_string = kwargs.pop('clean_string', None)
    checks = [(x is None) for x in [code_indexes, char_indexes, clean_string]]
    if (not (len(set(checks)) == 1)):
        raise ValueError('You    must    specify    code_indexes,    char_indexes,    and    clean_string    together,    or    not    at    all.')
    if (not all(checks)):
        decoded = True
    if (not decoded):
        clean_string = to_unicode(parser.parse_ansi(string, strip_ansi=True, mxp=True))
        string = parser.parse_ansi(string, xterm256=True, mxp=True)
    elif (clean_string is not None):
        pass
    elif hasattr(string, '_clean_string'):
        clean_string = string._clean_string
        code_indexes = string._code_indexes
        char_indexes = string._char_indexes
        string = string._raw_string
    else:
        clean_string = parser.strip_raw_codes(string)
    if (not isinstance(string, unicode)):
        string = string.decode('utf-8')
    ansi_string = super(ANSIString, cls).__new__(ANSIString, to_str(clean_string), 'utf-8')
    ansi_string._raw_string = string
    ansi_string._clean_string = clean_string
    ansi_string._code_indexes = code_indexes
    ansi_string._char_indexes = char_indexes
    return ansi_string
