4 Copyright: Denis Shelomovskij 2013
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 Authors: Denis Shelomovskij
7 Source: $(DRUNTIMESRC src/rt/util/_array.d)
12 import core
.internal
.string
;
13 import core
.stdc
.stdint
;
16 @safe /* pure dmd @@@BUG11461@@@ */ nothrow:
18 void enforceTypedArraysConformable(T
)(const char[] action
,
19 const T
[] a1
, const T
[] a2
, in bool allowOverlap
= false)
21 _enforceSameLength(action
, a1
.length
, a2
.length
);
23 _enforceNoOverlap(action
, arrayToPtr(a1
), arrayToPtr(a2
), T
.sizeof
* a1
.length
);
26 void enforceRawArraysConformable(const char[] action
, in size_t elementSize
,
27 const void[] a1
, const void[] a2
, in bool allowOverlap
= false)
29 _enforceSameLength(action
, a1
.length
, a2
.length
);
31 _enforceNoOverlap(action
, arrayToPtr(a1
), arrayToPtr(a2
), elementSize
* a1
.length
);
34 private void _enforceSameLength(const char[] action
,
35 in size_t length1
, in size_t length2
)
37 if (length1
== length2
)
40 UnsignedStringBuf tmpBuff
= void;
41 string msg
= "Array lengths don't match for ";
44 msg
~= length1
.unsignedToTempString(tmpBuff
, 10);
46 msg
~= length2
.unsignedToTempString(tmpBuff
, 10);
50 private void _enforceNoOverlap(const char[] action
,
51 uintptr_t ptr1
, uintptr_t ptr2
, in size_t bytes
)
53 const d
= ptr1
> ptr2 ? ptr1
- ptr2
: ptr2
- ptr1
;
56 const overlappedBytes
= bytes
- d
;
58 UnsignedStringBuf tmpBuff
= void;
59 string msg
= "Overlapping arrays in ";
62 msg
~= overlappedBytes
.unsignedToTempString(tmpBuff
, 10);
63 msg
~= " byte(s) overlap of ";
64 msg
~= bytes
.unsignedToTempString(tmpBuff
, 10);
68 private uintptr_t
arrayToPtr(const void[] array
) @trusted
70 // Ok because the user will never dereference the pointer
71 return cast(uintptr_t
)array
.ptr
;