[Python-ideas] Adding str.isascii() ?

INADA Naoki songofacandy at gmail.com
Fri Jan 26 04:44:31 EST 2018


> +1
>
> Just a note: checking the header in CPython will only give a hint,
> since strings created using higher order kinds can still be 100%
> ASCII.
>

Oh, really?
I think checking header is enough for all ready unicode.
For example, this is _PyUnicode_EqualToASCIIString implementation:

    if (PyUnicode_READY(unicode) == -1) {
        /* Memory error or bad data */
        PyErr_Clear();
        return non_ready_unicode_equal_to_ascii_string(unicode, str);
    }
    if (!PyUnicode_IS_ASCII(unicode))
        return 0;

And I think str.isascii() can be implemented as:

    if (PyUnicode_READY(unicode) == -1) {
        return NULL;
    }
    if (PyUnicode_IS_ASCII(unicode)) {
        Py_RETURN_TRUE;
    }
    else {
        Py_RETURN_FALSE;
    }


More information about the Python-ideas mailing list