int32_t str_hashcode_()

in cext/cutils.c [90:113]


int32_t str_hashcode_(PyObject *str, int lower) {
    int32_t res = 0;

    Py_ssize_t sz = PyUnicode_GET_LENGTH(str);
    if (!sz) {
        return res;
    }

    int kind = PyUnicode_KIND(str);
    void* buf = PyUnicode_DATA(str);

    Py_ssize_t i;
    for (i = 0; i < sz; i++) {
        Py_UCS4 ch = PyUnicode_READ(kind, buf, i);

        if (lower) {
            ch = Py_UNICODE_TOLOWER(ch);
        }

        res = 31 * res + ch;
    }

    return res;
}