diff --git a/Sources/BinaryParsing/Operations/OptionalOperations.swift b/Sources/BinaryParsing/Operations/OptionalOperations.swift index 35713f7..0abd8f2 100644 --- a/Sources/BinaryParsing/Operations/OptionalOperations.swift +++ b/Sources/BinaryParsing/Operations/OptionalOperations.swift @@ -17,6 +17,14 @@ extension Collection { } return self[i] } + + @inlinable + public subscript(ifInBounds range: Range) -> SubSequence? { + let bounds = startIndex...endIndex + guard range.lowerBound >= startIndex, range.upperBound <= endIndex + else { return nil } + return self[range] + } } extension Collection where Index == Int { @@ -27,4 +35,14 @@ extension Collection where Index == Int { } return self[i] } + + @_alwaysEmitIntoClient + public subscript(ifInBounds bounds: Range) -> SubSequence? { + guard let low = Int(exactly: bounds.lowerBound), + let high = Int(exactly: bounds.upperBound), + low >= startIndex, high <= endIndex else { + return nil + } + return self[low..) -> SubSequence { + get throws(ParsingError) { + guard bounds.lowerBound >= startIndex && bounds.upperBound <= endIndex + else { throw ParsingError(statusOnly: .invalidValue) } + return self[bounds] + } + } } extension Optional { diff --git a/Sources/BinaryParsing/Parser Types/Endianness.swift b/Sources/BinaryParsing/Parser Types/Endianness.swift index 22ee730..aaab23e 100644 --- a/Sources/BinaryParsing/Parser Types/Endianness.swift +++ b/Sources/BinaryParsing/Parser Types/Endianness.swift @@ -29,4 +29,8 @@ extension Endianness { public var isBigEndian: Bool { _isBigEndian } + + public var isLittleEndian: Bool { + !_isBigEndian + } } diff --git a/Sources/BinaryParsing/Parser Types/ParserRange.swift b/Sources/BinaryParsing/Parser Types/ParserRange.swift index 85f77b5..04b141f 100644 --- a/Sources/BinaryParsing/Parser Types/ParserRange.swift +++ b/Sources/BinaryParsing/Parser Types/ParserRange.swift @@ -34,20 +34,6 @@ public struct ParserRange: Hashable, Sendable { } } -extension ParserRange { - public func slicing>(_ coll: C) throws(ParsingError) - -> C.SubSequence - where C.Index == Int { - let validRange = coll.startIndex...coll.endIndex - guard validRange.contains(range.lowerBound), - validRange.contains(range.upperBound) - else { - throw ParsingError(status: .invalidValue, location: range.lowerBound) - } - return coll[range] - } -} - extension RandomAccessCollection where Index == Int { public subscript(_ range: ParserRange) -> SubSequence { get throws(ParsingError) { diff --git a/Sources/BinaryParsing/Parser Types/Seeking.swift b/Sources/BinaryParsing/Parser Types/Seeking.swift index c757b5f..cc52009 100644 --- a/Sources/BinaryParsing/Parser Types/Seeking.swift +++ b/Sources/BinaryParsing/Parser Types/Seeking.swift @@ -70,7 +70,7 @@ extension ParserSpan { throws(ParsingError) { guard let offset = Int(exactly: offset), - (-startPosition...count).contains(offset) + (0...count).contains(offset) else { throw ParsingError(status: .invalidValue, location: startPosition) } @@ -97,7 +97,7 @@ extension ParserSpan { throws(ParsingError) { guard let offset = Int(exactly: offset), - (0...endPosition).contains(offset) + (0...count).contains(offset) else { throw ParsingError(status: .invalidValue, location: startPosition) } diff --git a/Tests/BinaryParsingTests/EndiannessTests.swift b/Tests/BinaryParsingTests/EndiannessTests.swift new file mode 100644 index 0000000..785c5e2 --- /dev/null +++ b/Tests/BinaryParsingTests/EndiannessTests.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Binary Parsing open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import BinaryParsing +import Testing + +struct EndiannessTests { + @Test(arguments: [false, true]) + func endianness(isBigEndian: Bool) { + let endianness = Endianness(isBigEndian: isBigEndian) + #expect(endianness.isBigEndian == isBigEndian) + #expect(endianness.isLittleEndian == !isBigEndian) + + let endianness2: Endianness = isBigEndian ? .big : .little + #expect(endianness == endianness2) + } +} diff --git a/Tests/BinaryParsingTests/OptionalOperationsTests.swift b/Tests/BinaryParsingTests/OptionalOperationsTests.swift index 7291782..2c41dc6 100644 --- a/Tests/BinaryParsingTests/OptionalOperationsTests.swift +++ b/Tests/BinaryParsingTests/OptionalOperationsTests.swift @@ -140,29 +140,48 @@ struct OptionalOperationsTests { @Test func collectionIfInBounds() throws { let str = "Hello, world!" let substr = str.dropFirst(5).dropLast() - - var i = str.startIndex - while true { - if substr.indices.contains(i) { + let allIndices = str.indices + [str.endIndex] + let validIndices = substr.startIndex..