diff --git a/stdlib/public/SDK/Foundation/Data.swift b/stdlib/public/SDK/Foundation/Data.swift index 6fe3b2962257c..859249cf525ad 100644 --- a/stdlib/public/SDK/Foundation/Data.swift +++ b/stdlib/public/SDK/Foundation/Data.swift @@ -1428,9 +1428,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl let nsRange : NSRange if let r = range { _validateRange(r) - nsRange = NSMakeRange(r.lowerBound, r.upperBound - r.lowerBound) + nsRange = NSRange(location: r.lowerBound - startIndex, length: r.upperBound - r.lowerBound) } else { - nsRange = NSMakeRange(0, _backing.length) + nsRange = NSRange(location: 0, length: count) } let result = _backing.withInteriorPointerReference(_sliceRange) { $0.range(of: dataToFind, options: options, in: nsRange) @@ -1438,7 +1438,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl if result.location == NSNotFound { return nil } - return result.location..<(result.location + result.length) + return (result.location + startIndex)..<((result.location + startIndex) + result.length) } /// Enumerate the contents of the data. diff --git a/test/stdlib/TestData.swift b/test/stdlib/TestData.swift index 117b4320855e9..5cb720537ca5e 100644 --- a/test/stdlib/TestData.swift +++ b/test/stdlib/TestData.swift @@ -3741,6 +3741,14 @@ class TestData : TestDataSuper { expectEqual(Data(bytes: [4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]), t) } + + func test_rangeOfSlice() { + let data = "FooBar".data(using: .ascii)! + let slice = data[3...] // Bar + + let range = slice.range(of: "a".data(using: .ascii)!) + expectEqual(range, Range(4..<5)) + } } #if !FOUNDATION_XCTEST @@ -4058,6 +4066,7 @@ DataTests.test("test_validateMutation_slice_mutableBacking_withUnsafeMutableByte DataTests.test("test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound") { TestData().test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() } DataTests.test("test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound") { TestData().test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() } DataTests.test("test_byte_access_of_discontiguousData") { TestData().test_byte_access_of_discontiguousData() } +DataTests.test("test_rangeOfSlice") { TestData().test_rangeOfSlice() } // XCTest does not have a crash detection, whereas lit does DataTests.test("bounding failure subdata") {