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

Commit 90600d0

Browse files
Merge pull request #2359 from n8sh/issue-19414
Fix Issue 19414 - object.__cmp(T[]) on big-endian architectures can use memcmp for unsigned integers of any size
2 parents af5b01e + 249433a commit 90600d0

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/object.d

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ alias dstring = immutable(dchar)[];
3838

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

41-
int __cmp(T)(const T[] lhs, const T[] rhs) @trusted
41+
int __cmp(T)(scope const T[] lhs, scope const T[] rhs) @trusted
4242
if (__traits(isScalar, T))
4343
{
4444
// Compute U as the implementation type for T
@@ -69,6 +69,22 @@ int __cmp(T)(const T[] lhs, const T[] rhs) @trusted
6969
}
7070
else
7171
{
72+
version (BigEndian)
73+
static if (__traits(isUnsigned, T) ? !is(T == __vector) : is(T : P*, P))
74+
{
75+
if (!__ctfe)
76+
{
77+
import core.stdc.string : memcmp;
78+
int c = memcmp(lhs.ptr, rhs.ptr, (lhs.length <= rhs.length ? lhs.length : rhs.length) * T.sizeof);
79+
if (c)
80+
return c;
81+
static if (size_t.sizeof <= uint.sizeof && T.sizeof >= 2)
82+
return cast(int) lhs.length - cast(int) rhs.length;
83+
else
84+
return int(lhs.length > rhs.length) - int(lhs.length < rhs.length);
85+
}
86+
}
87+
7288
immutable len = lhs.length <= rhs.length ? lhs.length : rhs.length;
7389
foreach (const u; 0 .. len)
7490
{

0 commit comments

Comments
 (0)