blob: dd67128a85cc0492c09a4414a709e3372b5d9574 [file] [log] [blame]
Christian Heimes0ded5b52007-12-10 15:50:56 +00001/* 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009 register PyUnicodeObject *a = (PyUnicodeObject *)aa;
10 register PyUnicodeObject *b = (PyUnicodeObject *)bb;
Christian Heimes0ded5b52007-12-10 15:50:56 +000011
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
13 assert(0 && "unicode_eq ready fail");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020015 }
16
17 if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020019 if (PyUnicode_GET_LENGTH(a) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 return 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020021 if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
22 return 0;
23 /* Just comparing the first byte is enough to see if a and b differ.
24 * If they are 2 byte or 4 byte character most differences will happen in
25 * the lower bytes anyways.
26 */
27 if (PyUnicode_1BYTE_DATA(a)[0] != PyUnicode_1BYTE_DATA(b)[0])
28 return 0;
29 if (PyUnicode_KIND(a) == PyUnicode_1BYTE_KIND &&
30 PyUnicode_GET_LENGTH(a) == 1)
31 return 1;
32 return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
33 PyUnicode_GET_LENGTH(a) * PyUnicode_CHARACTER_SIZE(a)) == 0;
Christian Heimes0ded5b52007-12-10 15:50:56 +000034}