[PATCH v11 09/29] string: Improve generic strncmp
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Thu Feb 2 12:01:35 GMT 2023
On 01/02/23 16:42, Noah Goldstein wrote:
>> +/* Compare no more than N characters of S1 and S2,
>> + returning less than, equal to or greater than zero
>> + if S1 is lexicographically less than, equal to or
>> + greater than S2. */
>> +int
>> +STRNCMP (const char *p1, const char *p2, size_t n)
>> +{
>> + /* Handle the unaligned bytes of p1 first. */
>> + uintptr_t a = MIN (-(uintptr_t)p1 % sizeof(op_t), n);
>> + int diff = 0;
>> + for (int i = 0; i < a; ++i)
>> + {
>> + unsigned char c1 = *p1++;
>> + unsigned char c2 = *p2++;
>> + diff = c1 - c2;
>> + if (c1 == '\0' || diff != 0)
>> + return diff;
>> + }
>> + if (a == n)
>> + return diff;
>
> NIT: return 0 IMO
Ack.
More information about the Libc-alpha
mailing list