Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,17 +1428,17 @@ 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)
}
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.
Expand Down
9 changes: 9 additions & 0 deletions test/stdlib/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data.Index>(4..<5))
}
}

#if !FOUNDATION_XCTEST
Expand Down Expand Up @@ -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") {
Expand Down