Skip to content

Commit

Permalink
Added check methods for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 29, 2023
1 parent 9728214 commit aa99a37
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Sources/Bytes/Bytes.docc/Bytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions Sources/Bytes/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String: StringProtocol>(
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<String: StringProtocol>(
utf8 string: String
) throws -> Bool {
try checkIfPresent(string.utf8Bytes)
}
}


Expand Down Expand Up @@ -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<String: StringProtocol>(
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<String: StringProtocol>(
utf8 string: String
) async throws -> Bool {
try await checkIfPresent(string.utf8Bytes)
}
}

#endif

0 comments on commit aa99a37

Please # to comment.