Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 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 | */ |
| 6 | Py_LOCAL_INLINE(int) |
| 7 | unicode_eq(PyObject *aa, PyObject *bb) |
| 8 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 9 | register PyUnicodeObject *a = (PyUnicodeObject *)aa; |
| 10 | register PyUnicodeObject *b = (PyUnicodeObject *)bb; |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame^] | 12 | if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { |
| 13 | assert(0 && "unicode_eq ready fail"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame^] | 15 | } |
| 16 | |
| 17 | if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame^] | 19 | if (PyUnicode_GET_LENGTH(a) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame^] | 21 | 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 Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 34 | } |