You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2 KiB

  1. /* Fast unicode equal function optimized for dictobject.c and setobject.c */
  2. /* Return 1 if two unicode objects are equal, 0 if not.
  3. * unicode_eq() is called when the hash of two unicode objects is equal.
  4. */
  5. Py_LOCAL_INLINE(int)
  6. unicode_eq(PyObject *aa, PyObject *bb)
  7. {
  8. register PyUnicodeObject *a = (PyUnicodeObject *)aa;
  9. register PyUnicodeObject *b = (PyUnicodeObject *)bb;
  10. if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
  11. assert(0 && "unicode_eq ready fail");
  12. return 0;
  13. }
  14. if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
  15. return 0;
  16. if (PyUnicode_GET_LENGTH(a) == 0)
  17. return 1;
  18. if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
  19. return 0;
  20. /* Just comparing the first byte is enough to see if a and b differ.
  21. * If they are 2 byte or 4 byte character most differences will happen in
  22. * the lower bytes anyways.
  23. */
  24. if (PyUnicode_1BYTE_DATA(a)[0] != PyUnicode_1BYTE_DATA(b)[0])
  25. return 0;
  26. if (PyUnicode_KIND(a) == PyUnicode_1BYTE_KIND &&
  27. PyUnicode_GET_LENGTH(a) == 1)
  28. return 1;
  29. return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
  30. PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0;
  31. }