@@ -3020,3 +3020,53 @@ internal func _overrideLifetime<
3020
3020
) -> T {
3021
3021
dependent
3022
3022
}
3023
+
3024
+ extension Data {
3025
+ /// Returns a boolean value indicating whether this data is identical to
3026
+ /// `other`.
3027
+ ///
3028
+ /// Two data values are identical if there is no way to distinguish between
3029
+ /// them.
3030
+ ///
3031
+ /// Comparing data this way includes comparing (normally) hidden
3032
+ /// implementation details such as the memory location of any underlying
3033
+ /// data storage object. Therefore, identical data are guaranteed to
3034
+ /// compare equal with `==`, but not all equal data are considered
3035
+ /// identical.
3036
+ ///
3037
+ /// - Performance: O(1)
3038
+ @_alwaysEmitIntoClient
3039
+ public func isIdentical( to other: Self ) -> Bool {
3040
+ // See if both are empty
3041
+ switch ( self . _representation, other. _representation) {
3042
+ case ( . empty, . empty) :
3043
+ return true
3044
+ case ( . inline, . inline) , ( . slice, . slice) , ( . large, . large) :
3045
+ // Continue on to checks below
3046
+ break
3047
+ default :
3048
+ return false
3049
+ }
3050
+
3051
+ let length1 = self . count
3052
+ let length2 = other. count
3053
+
3054
+ // Unequal length data can never be equal
3055
+ guard length1 == length2 else {
3056
+ return false
3057
+ }
3058
+
3059
+ if length1 > 0 {
3060
+ return self . withUnsafeBytes { ( b1: UnsafeRawBufferPointer ) in
3061
+ return other. withUnsafeBytes { ( b2: UnsafeRawBufferPointer ) in
3062
+ // If they have the same base address and same count, it is equal
3063
+ let b1Address = b1. baseAddress!
3064
+ let b2Address = b2. baseAddress!
3065
+
3066
+ return b1Address == b2Address
3067
+ }
3068
+ }
3069
+ }
3070
+ return true
3071
+ }
3072
+ }
0 commit comments