xref: /third_party/python/Objects/stringlib/eq.h (revision 7db96d56)
1/* Fast unicode equal function optimized for dictobject.c and setobject.c */
2
3/* Return 1 if two unicode objects are equal, 0 if not.
4 * unicode_eq() is called when the hash of two unicode objects is equal.
5 */
6Py_LOCAL_INLINE(int)
7unicode_eq(PyObject *aa, PyObject *bb)
8{
9    assert(PyUnicode_Check(aa));
10    assert(PyUnicode_Check(bb));
11    assert(PyUnicode_IS_READY(aa));
12    assert(PyUnicode_IS_READY(bb));
13
14    PyUnicodeObject *a = (PyUnicodeObject *)aa;
15    PyUnicodeObject *b = (PyUnicodeObject *)bb;
16
17    if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
18        return 0;
19    if (PyUnicode_GET_LENGTH(a) == 0)
20        return 1;
21    if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
22        return 0;
23    return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
24                  PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0;
25}
26