Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Fix Issue 19414 - object.__cmp(T[]) on big-endian architectures can u…
Browse files Browse the repository at this point in the history
…se memcmp for unsigned integers of any size
  • Loading branch information
n8sh committed Nov 19, 2018
1 parent 4978a08 commit 249433a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/object.d
Expand Up @@ -38,7 +38,7 @@ alias dstring = immutable(dchar)[];

version (D_ObjectiveC) public import core.attribute : selector;

int __cmp(T)(const T[] lhs, const T[] rhs) @trusted
int __cmp(T)(scope const T[] lhs, scope const T[] rhs) @trusted
if (__traits(isScalar, T))
{
// Compute U as the implementation type for T
Expand Down Expand Up @@ -69,6 +69,22 @@ int __cmp(T)(const T[] lhs, const T[] rhs) @trusted
}
else
{
version (BigEndian)
static if (__traits(isUnsigned, T) ? !is(T == __vector) : is(T : P*, P))
{
if (!__ctfe)
{
import core.stdc.string : memcmp;
int c = memcmp(lhs.ptr, rhs.ptr, (lhs.length <= rhs.length ? lhs.length : rhs.length) * T.sizeof);
if (c)
return c;
static if (size_t.sizeof <= uint.sizeof && T.sizeof >= 2)
return cast(int) lhs.length - cast(int) rhs.length;
else
return int(lhs.length > rhs.length) - int(lhs.length < rhs.length);
}
}

immutable len = lhs.length <= rhs.length ? lhs.length : rhs.length;
foreach (const u; 0 .. len)
{
Expand Down

0 comments on commit 249433a

Please sign in to comment.