From 7f198827ca9a84ea213ca40d8b8b9ea7d6e9c81c Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Thu, 29 Jun 2023 17:05:15 -0700 Subject: [PATCH] Added check methods for raw representable --- README.md | 1 + Sources/Bytes/Bytes.docc/Bytes.md | 1 + Sources/Bytes/RawRepresentable.swift | 201 +++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) diff --git a/README.md b/README.md index 148b8e1..e3ef1d0 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ try iterator.check(0) // Check for 0x00, throw if not found try iterator.check([0x0d, 0x0a]) // Check for \r\n, throw if not found try iterator.checkIfPresent([0x0d, 0x0a]) // Check for \r\n, return false if iterator is finished, throw if not finished and not found try iterator.checkIfPresent(utf8: "\r\n") // Check for \r\n, return false if iterator is finished, throw if not finished and not found +try iterator.checkIfPresent(utf8: Separators.header) // Check for \r\n\r\n, return false if iterator is finished, throw if not finished and not found ``` ### Complex Example diff --git a/Sources/Bytes/Bytes.docc/Bytes.md b/Sources/Bytes/Bytes.docc/Bytes.md index a9d3054..8c5ab3e 100644 --- a/Sources/Bytes/Bytes.docc/Bytes.md +++ b/Sources/Bytes/Bytes.docc/Bytes.md @@ -55,6 +55,7 @@ try iterator.check(0) // Check for 0x00, throw if not found try iterator.check([0x0d, 0x0a]) // Check for \r\n, throw if not found try iterator.checkIfPresent([0x0d, 0x0a]) // Check for \r\n, return false if iterator is finished, throw if not finished and not found try iterator.checkIfPresent(utf8: "\r\n") // Check for \r\n, return false if iterator is finished, throw if not finished and not found +try iterator.checkIfPresent(utf8: Separators.header) // Check for \r\n\r\n, return false if iterator is finished, throw if not finished and not found ``` ### Complex Example diff --git a/Sources/Bytes/RawRepresentable.swift b/Sources/Bytes/RawRepresentable.swift index 5937c58..961b8df 100644 --- a/Sources/Bytes/RawRepresentable.swift +++ b/Sources/Bytes/RawRepresentable.swift @@ -236,6 +236,36 @@ extension IteratorProtocol where Element == Byte { public mutating func nextIfPresent(raw type: T.Type) throws -> T? { try nextIfPresent(Bytes.self, count: MemoryLayout.size).map { try T(rawBytes: $0) } } + + /// Advances by the specified raw representable value if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter value: The raw value to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + public mutating func check( + raw value: Raw + ) throws { + try check(value.rawBytes) + } + + /// Advances by the specified raw representable value if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter value: The raw value to check for. + /// - Returns: `true` if the string was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + raw value: Raw + ) throws -> Bool { + try checkIfPresent(value.rawBytes) + } } extension IteratorProtocol where Element == Byte { @@ -382,6 +412,76 @@ extension IteratorProtocol where Element == Byte { } } +extension IteratorProtocol where Element == Byte { + /// Advances by the specified UTF-8 encoded raw value Character if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value Character to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter character: The character to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the character could not be identified. + @inlinable + public mutating func check( + utf8 character: RawCharacter + ) throws where RawCharacter.RawValue == Character { + try check(character.utf8Bytes) + } + + /// Advances by the specified UTF-8 encoded raw value String if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value String to be next in the sequence, and it would be an error if something else were encountered. + /// + /// If the String is empty, this method won't do anything. + /// + /// - Note: The string will not check for null termination unless a null character is specified. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter string: The string to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + public mutating func check( + utf8 string: RawString + ) throws where RawString.RawValue: StringProtocol { + try check(string.utf8Bytes) + } + + /// Advances by the specified UTF-8 encoded raw value Character if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value Character to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter character: The character to check for. + /// - Returns: `true` if the character was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the character could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + utf8 character: RawCharacter + ) throws -> Bool where RawCharacter.RawValue == Character { + try checkIfPresent(character.utf8Bytes) + } + + /// Advances by the specified UTF-8 encoded raw value String if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value String to be next in the sequence, and it would be an error if something else were encountered. + /// + /// If the String is empty, this method won't do anything. + /// + /// - Note: The string will not check for null termination unless a null character is specified. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter string: The string to check for. + /// - Returns: `true` if the string was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + utf8 string: RawString + ) throws -> Bool where RawString.RawValue: StringProtocol { + try checkIfPresent(string.utf8Bytes) + } +} + // MARK: - AsyncByteIterator @@ -414,6 +514,36 @@ extension AsyncIteratorProtocol where Element == Byte { public mutating func nextIfPresent(raw type: T.Type) async throws -> T? { try await nextIfPresent(Bytes.self, count: MemoryLayout.size).map { try T(rawBytes: $0) } } + + /// Asynchronously advances by the specified raw representable value if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter value: The raw value to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + public mutating func check( + raw value: Raw + ) async throws { + try await check(value.rawBytes) + } + + /// Asynchronously advances by the specified raw representable value if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter value: The raw value to check for. + /// - Returns: `true` if the string was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + raw value: Raw + ) async throws -> Bool { + try await checkIfPresent(value.rawBytes) + } } @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @@ -561,4 +691,75 @@ extension AsyncIteratorProtocol where Element == Byte { } } +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) +extension AsyncIteratorProtocol where Element == Byte { + /// Asynchronously advances by the specified UTF-8 encoded raw value Character if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value Character to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter character: The character to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the character could not be identified. + @inlinable + public mutating func check( + utf8 character: RawCharacter + ) async throws where RawCharacter.RawValue == Character { + try await check(character.utf8Bytes) + } + + /// Asynchronously advances by the specified UTF-8 encoded raw value String if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a raw value String to be next in the sequence, and it would be an error if something else were encountered. + /// + /// If the String is empty, this method won't do anything. + /// + /// - Note: The string will not check for null termination unless a null character is specified. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter string: The string to check for. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified. + @inlinable + public mutating func check( + utf8 string: RawString + ) async throws where RawString.RawValue: StringProtocol { + try await check(string.utf8Bytes) + } + + /// Asynchronously advances by the specified UTF-8 encoded raw value Character if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value Character to be next in the sequence, and it would be an error if something else were encountered. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter character: The character to check for. + /// - Returns: `true` if the character was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the character could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + utf8 character: RawCharacter + ) async throws -> Bool where RawCharacter.RawValue == Character { + try await checkIfPresent(character.utf8Bytes) + } + + /// Asynchronously advances by the specified UTF-8 encoded raw value String if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended. + /// + /// Use this method when you expect a raw value String to be next in the sequence, and it would be an error if something else were encountered. + /// + /// If the String is empty, this method won't do anything. + /// + /// - Note: The string will not check for null termination unless a null character is specified. + /// + /// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes) + /// - Parameter string: The string to check for. + /// - Returns: `true` if the character was found, or `false` if the sequence finished. + /// - Throws: ``BytesError/checkedSequenceNotFound`` if the character could not be identified. + @inlinable + @discardableResult + public mutating func checkIfPresent( + utf8 string: RawString + ) async throws -> Bool where RawString.RawValue: StringProtocol { + try await checkIfPresent(string.utf8Bytes) + } +} + #endif