QString: use memcmp() in ucstrncmp() where it's acceptable

ucstrncmp() exists because memcmp() can't be used to sort UTF-16 code
units in little-endian platforms. But it can be used in big endian
platforms and when sorting isn't necessary.

Change-Id: I0e5f6bec596a4a78bd3bfffd16c908c46cc9af2d
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Thiago Macieira 2022-01-10 14:18:40 -08:00
parent 339aa99fec
commit 33e682f9af

View File

@ -1145,6 +1145,10 @@ extern "C" int qt_ucstrncmp_mips_dsp_asm(const char16_t *a,
template <StringComparisonMode Mode>
static int ucstrncmp(const char16_t *a, const char16_t *b, size_t l)
{
// This function isn't memcmp() because that can return the wrong sorting
// result in little-endian architectures: 0x00ff must sort before 0x0100,
// but the bytes in memory are FF 00 and 00 01.
#ifndef __OPTIMIZE_SIZE__
# if defined(__mips_dsp)
static_assert(sizeof(uint) == sizeof(size_t));
@ -1254,6 +1258,9 @@ static int ucstrncmp(const char16_t *a, const char16_t *b, size_t l)
# endif // MIPS DSP or __SSE2__ or __ARM_NEON__
#endif // __OPTIMIZE_SIZE__
if (Mode == CompareStringsForEquality || QSysInfo::ByteOrder == QSysInfo::BigEndian)
return memcmp(a, b, l * sizeof(char16_t));
for (size_t i = 0; i < l; ++i) {
if (int diff = a[i] - b[i])
return diff;