From aa99a372c97779605c6eb3d465e56097296bbcfc Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Thu, 29 Jun 2023 15:04:18 -0700 Subject: [PATCH] Added check methods for strings --- README.md | 1 + Sources/Bytes/Bytes.docc/Bytes.md | 1 + Sources/Bytes/String.swift | 76 +++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/README.md b/README.md index f63afe4..148b8e1 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Sometimes, while reading a sequence of bytes you just want to verify that the ne 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 ``` ### Complex Example diff --git a/Sources/Bytes/Bytes.docc/Bytes.md b/Sources/Bytes/Bytes.docc/Bytes.md index d1e41e2..a9d3054 100644 --- a/Sources/Bytes/Bytes.docc/Bytes.md +++ b/Sources/Bytes/Bytes.docc/Bytes.md @@ -54,6 +54,7 @@ Sometimes, while reading a sequence of bytes you just want to verify that the ne 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 ``` ### Complex Example diff --git a/Sources/Bytes/String.swift b/Sources/Bytes/String.swift index 080befa..3742768 100644 --- a/Sources/Bytes/String.swift +++ b/Sources/Bytes/String.swift @@ -94,6 +94,44 @@ extension IteratorProtocol where Element == Byte { public mutating func nextIfPresent(utf8 type: String.Type, min minCount: Int = 0, max maxCount: Int) throws -> String? { try nextIfPresent(Bytes.self, min: minCount, max: maxCount).map { String(utf8Bytes: $0) } } + + /// Advances by the specified UTF-8 encoded String if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a 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: String + ) throws { + try check(string.utf8Bytes) + } + + /// Advances by the specified UTF-8 encoded 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 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: String + ) throws -> Bool { + try checkIfPresent(string.utf8Bytes) + } } @@ -152,6 +190,44 @@ extension AsyncIteratorProtocol where Element == Byte { public mutating func nextIfPresent(utf8 type: String.Type, min minCount: Int = 0, max maxCount: Int) async throws -> String? { try await nextIfPresent(Bytes.self, min: minCount, max: maxCount).map { String(utf8Bytes: $0) } } + + /// Asynchronously advances by the specified UTF-8 encoded String if found, or throws if the next bytes in the iterator do not match. + /// + /// Use this method when you expect a 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: String + ) async throws { + try await check(string.utf8Bytes) + } + + /// Asynchronously advances by the specified UTF-8 encoded 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 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: String + ) async throws -> Bool { + try await checkIfPresent(string.utf8Bytes) + } } #endif