@@ -2974,3 +2974,53 @@ extension Data : Codable {
29742974 }
29752975 }
29762976}
2977+
2978+ extension Data {
2979+ /// Returns a boolean value indicating whether this data is identical to
2980+ /// `other`.
2981+ ///
2982+ /// Two data values are identical if there is no way to distinguish between
2983+ /// them.
2984+ ///
2985+ /// Comparing data this way includes comparing (normally) hidden
2986+ /// implementation details such as the memory location of any underlying
2987+ /// data storage object. Therefore, identical data are guaranteed to
2988+ /// compare equal with `==`, but not all equal data are considered
2989+ /// identical.
2990+ ///
2991+ /// - Performance: O(1)
2992+ @_alwaysEmitIntoClient
2993+ public func isIdentical( to other: Self ) -> Bool {
2994+ // See if both are empty
2995+ switch ( self . _representation, other. _representation) {
2996+ case ( . empty, . empty) :
2997+ return true
2998+ case ( . inline, . inline) , ( . slice, . slice) , ( . large, . large) :
2999+ // Continue on to checks below
3000+ break
3001+ default :
3002+ return false
3003+ }
3004+
3005+ let length1 = self . count
3006+ let length2 = other. count
3007+
3008+ // Unequal length data can never be equal
3009+ guard length1 == length2 else {
3010+ return false
3011+ }
3012+
3013+ if length1 > 0 {
3014+ return self . withUnsafeBytes { ( b1: UnsafeRawBufferPointer ) in
3015+ return other. withUnsafeBytes { ( b2: UnsafeRawBufferPointer ) in
3016+ // If they have the same base address and same count, it is equal
3017+ let b1Address = b1. baseAddress!
3018+ let b2Address = b2. baseAddress!
3019+
3020+ return b1Address == b2Address
3021+ }
3022+ }
3023+ }
3024+ return true
3025+ }
3026+ }
0 commit comments