diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 5463b795..905a5533 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -122,27 +122,45 @@ extension RawSpan { //MARK: Bounds Checking extension RawSpan { + /// Return true if `offset` is a valid offset into this `RawSpan` + /// + /// - Parameters: + /// - position: an index to validate + /// - Returns: true if `offset` is a valid index + @inlinable @inline(__always) + public func validateBounds(_ offset: Int) -> Bool { + 0 <= offset && offset < byteCount + } + /// Traps if `offset` is not a valid offset into this `RawSpan` /// /// - Parameters: - /// - position: an offset to validate + /// - position: an index to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offset: Int) { + public func assertValidity(_ offset: Int) { precondition( - 0 <= offset && offset < byteCount, - "Offset out of bounds" + validateBounds(offset), "Offset out of bounds" ) } - /// Traps if `bounds` is not a valid range of offsets into this `RawSpan` + /// Return true if `offsets` is a valid range of offsets into this `RawSpan` /// /// - Parameters: - /// - offsets: a range of offsets to validate + /// - offsets: a range of indices to validate + /// - Returns: true if `offsets` is a valid range of indices @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offsets: Range) { + public func validateBounds(_ offsets: Range) -> Bool { + 0 <= offsets.lowerBound && offsets.upperBound <= byteCount + } + + /// Traps if `offsets` is not a valid range of offsets into this `RawSpan` + /// + /// - Parameters: + /// - offsets: a range of indices to validate + @inlinable @inline(__always) + public func assertValidity(_ offsets: Range) { precondition( - 0 <= offsets.lowerBound && offsets.upperBound <= byteCount, - "Range of offsets out of bounds" + validateBounds(offsets), "Range of offsets out of bounds" ) } } @@ -165,7 +183,7 @@ extension RawSpan { /// - Complexity: O(1) @inlinable @inline(__always) public func extracting(_ bounds: Range) -> Self { - boundsCheckPrecondition(bounds) + assertValidity(bounds) return extracting(uncheckedBounds: bounds) } @@ -305,7 +323,7 @@ extension RawSpan { public func load( fromByteOffset offset: Int = 0, as: T.Type ) -> T { - boundsCheckPrecondition( + assertValidity( Range(uncheckedBounds: (offset, offset+MemoryLayout.size)) ) return load(fromUncheckedByteOffset: offset, as: T.self) @@ -347,7 +365,7 @@ extension RawSpan { public func loadUnaligned( fromByteOffset offset: Int = 0, as: T.Type ) -> T { - boundsCheckPrecondition( + assertValidity( Range(uncheckedBounds: (offset, offset+MemoryLayout.size)) ) return loadUnaligned(fromUncheckedByteOffset: offset, as: T.self) @@ -507,7 +525,7 @@ extension RawSpan { @inlinable public init(_ base: RawSpan, in range: Range) { - base.boundsCheckPrecondition(range) + base.assertValidity(range) position = 0 self.base = base parseRange = range diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 9e339019..f3dc0326 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -287,27 +287,45 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { //MARK: Bounds Checking extension Span where Element: ~Copyable /*& ~Escapable*/ { + /// Return true if `offset` is a valid offset into this `Span` + /// + /// - Parameters: + /// - position: an index to validate + /// - Returns: true if `offset` is a valid index + @inlinable @inline(__always) + public func validateBounds(_ offset: Int) -> Bool { + 0 <= offset && offset < count + } + /// Traps if `offset` is not a valid offset into this `Span` /// /// - Parameters: - /// - position: an Index to validate + /// - position: an index to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offset: Int) { + public func assertValidity(_ offset: Int) { precondition( - 0 <= offset && offset < count, - "Offset out of bounds" + validateBounds(offset), "Offset out of bounds" ) } + /// Return true if `offsets` is a valid range of offsets into this `Span` + /// + /// - Parameters: + /// - offsets: a range of indices to validate + /// - Returns: true if `offsets` is a valid range of indices + @inlinable @inline(__always) + public func validateBounds(_ offsets: Range) -> Bool { + 0 <= offsets.lowerBound && offsets.upperBound <= count + } + /// Traps if `offsets` is not a valid range of offsets into this `Span` /// /// - Parameters: /// - offsets: a range of indices to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offsets: Range) { + public func assertValidity(_ offsets: Range) { precondition( - 0 <= offsets.lowerBound && offsets.upperBound <= count, - "Range of offsets out of bounds" + validateBounds(offsets), "Range of offsets out of bounds" ) } } @@ -333,7 +351,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { @inlinable @inline(__always) public subscript(_ position: Int) -> Element { _read { - boundsCheckPrecondition(position) + assertValidity(position) yield self[unchecked: position] } } @@ -365,7 +383,7 @@ extension Span where Element: BitwiseCopyable { @inlinable @inline(__always) public subscript(_ position: Int) -> Element { get { - boundsCheckPrecondition(position) + assertValidity(position) return self[unchecked: position] } } @@ -404,7 +422,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Complexity: O(1) @inlinable @inline(__always) public func extracting(_ bounds: Range) -> Self { - boundsCheckPrecondition(bounds) + assertValidity(bounds) return extracting(uncheckedBounds: bounds) } diff --git a/Tests/FutureTests/RawSpanTests.swift b/Tests/FutureTests/RawSpanTests.swift index d8e193a6..2a4e93a7 100644 --- a/Tests/FutureTests/RawSpanTests.swift +++ b/Tests/FutureTests/RawSpanTests.swift @@ -208,8 +208,8 @@ final class RawSpanTests: XCTestCase { let a = Array(0..