def detectencoding_str(input, final=False):
    """
    Detect the encoding of the byte string ``input``, which contains the
    beginning of a CSS file. This function returns the detected encoding (or
    ``None`` if it hasn't got enough data), and a flag that indicates whether
    that encoding has been detected explicitely or implicitely. To detect the
    encoding the first few bytes are used (or if ``input`` is ASCII compatible
    and starts with a charset rule the encoding name from the rule). "Explicit"
    detection means that the bytes start with a BOM or a charset rule.

    If the encoding can't be detected yet, ``None`` is returned as the encoding.
    ``final`` specifies whether more data will be available in later calls or
    not. If ``final`` is true, ``detectencoding_str()`` will never return
    ``None`` as the encoding.
    """
    CANDIDATE_UTF_8_SIG = 1
    CANDIDATE_UTF_16_AS_LE = 2
    CANDIDATE_UTF_16_AS_BE = 4
    CANDIDATE_UTF_16_LE = 8
    CANDIDATE_UTF_16_BE = 16
    CANDIDATE_UTF_32_AS_LE = 32
    CANDIDATE_UTF_32_AS_BE = 64
    CANDIDATE_UTF_32_LE = 128
    CANDIDATE_UTF_32_BE = 256
    CANDIDATE_CHARSET = 512
    candidates = 1023
    li = len(input)
    if li >= 1:
        c = input[0]
        if c != 'ï':
            candidates &= ~CANDIDATE_UTF_8_SIG
        if c != 'ÿ':
            candidates &= ~(CANDIDATE_UTF_32_AS_LE | CANDIDATE_UTF_16_AS_LE)
        if c != 'þ':
            candidates &= ~CANDIDATE_UTF_16_AS_BE
        if c != '@':
            candidates &= ~(CANDIDATE_UTF_32_LE | CANDIDATE_UTF_16_LE |
                CANDIDATE_CHARSET)
        if c != '\x00':
            candidates &= ~(CANDIDATE_UTF_32_AS_BE | CANDIDATE_UTF_32_BE |
                CANDIDATE_UTF_16_BE)
        if li >= 2:
            c = input[1]
            if c != '»':
                candidates &= ~CANDIDATE_UTF_8_SIG
            if c != 'þ':
                candidates &= ~(CANDIDATE_UTF_16_AS_LE | CANDIDATE_UTF_32_AS_LE
                    )
            if c != 'ÿ':
                candidates &= ~CANDIDATE_UTF_16_AS_BE
            if c != '\x00':
                candidates &= ~(CANDIDATE_UTF_16_LE |
                    CANDIDATE_UTF_32_AS_BE | CANDIDATE_UTF_32_LE |
                    CANDIDATE_UTF_32_BE)
            if c != '@':
                candidates &= ~CANDIDATE_UTF_16_BE
            if c != 'c':
                candidates &= ~CANDIDATE_CHARSET
            if li >= 3:
                c = input[2]
                if c != '¿':
                    candidates &= ~CANDIDATE_UTF_8_SIG
                if c != 'c':
                    candidates &= ~CANDIDATE_UTF_16_LE
                if c != '\x00':
                    candidates &= ~(CANDIDATE_UTF_32_AS_LE |
                        CANDIDATE_UTF_32_LE | CANDIDATE_UTF_32_BE)
                if c != 'þ':
                    candidates &= ~CANDIDATE_UTF_32_AS_BE
                if c != 'h':
                    candidates &= ~CANDIDATE_CHARSET
                if li >= 4:
                    c = input[3]
                    if input[2:4] == '\x00\x00':
                        candidates &= ~CANDIDATE_UTF_16_AS_LE
                    if c != '\x00':
                        candidates &= ~(CANDIDATE_UTF_16_LE |
                            CANDIDATE_UTF_32_AS_LE | CANDIDATE_UTF_32_LE)
                    if c != 'ÿ':
                        candidates &= ~CANDIDATE_UTF_32_AS_BE
                    if c != '@':
                        candidates &= ~CANDIDATE_UTF_32_BE
                    if c != 'a':
                        candidates &= ~CANDIDATE_CHARSET
    if candidates == 0:
        return 'utf-8', False
    if not candidates & candidates - 1:
        if candidates == CANDIDATE_UTF_8_SIG and li >= 3:
            return 'utf-8-sig', True
        elif candidates == CANDIDATE_UTF_16_AS_LE and li >= 2:
            return 'utf-16', True
        elif candidates == CANDIDATE_UTF_16_AS_BE and li >= 2:
            return 'utf-16', True
        elif candidates == CANDIDATE_UTF_16_LE and li >= 4:
            return 'utf-16-le', False
        elif candidates == CANDIDATE_UTF_16_BE and li >= 2:
            return 'utf-16-be', False
        elif candidates == CANDIDATE_UTF_32_AS_LE and li >= 4:
            return 'utf-32', True
        elif candidates == CANDIDATE_UTF_32_AS_BE and li >= 4:
            return 'utf-32', True
        elif candidates == CANDIDATE_UTF_32_LE and li >= 4:
            return 'utf-32-le', False
        elif candidates == CANDIDATE_UTF_32_BE and li >= 4:
            return 'utf-32-be', False
        elif candidates == CANDIDATE_CHARSET and li >= 4:
            prefix = '@charset "'
            if input[:len(prefix)] == prefix:
                pos = input.find('"', len(prefix))
                if pos >= 0:
                    return input[len(prefix):pos], True
    if final:
        return 'utf-8', False
    return None, False
