-
Notifications
You must be signed in to change notification settings - Fork 651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ByteBuffer methods getUTF8ValidatedString and readUTF8ValidatedString #2973
Open
adam-fowler
wants to merge
7
commits into
apple:main
Choose a base branch
from
adam-fowler:read-validated-string
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−0
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7e4353
Add ByteBuffer methods getValidatedString and readValidatedString
adam-fowler 1c15fd9
swift-format
adam-fowler 6ac9150
Swift 6 only
adam-fowler e4a4d1f
swift-format
adam-fowler 712ee77
Throw error when string is not valid UTF8
adam-fowler 88d6b80
Make ReadUTF8ValidationError Equatable
adam-fowler 926b53e
Use slice, add test for getUTF8ValidatedString
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -902,3 +902,48 @@ extension Optional where Wrapped == ByteBuffer { | |
} | ||
} | ||
} | ||
|
||
#if compiler(>=6) | ||
extension ByteBuffer { | ||
/// Get the string at `index` from this `ByteBuffer` decoding using the UTF-8 encoding. Does not move the reader index. | ||
/// The selected bytes must be readable or else `nil` will be returned. | ||
/// | ||
/// This is an alternative to `ByteBuffer.getString(at:length:)` which ensures the returned string is valid UTF8 | ||
/// | ||
/// - Parameters: | ||
/// - index: The starting index into `ByteBuffer` containing the string of interest. | ||
/// - length: The number of bytes making up the string. | ||
/// - Returns: A `String` value containing the UTF-8 decoded selected bytes from this `ByteBuffer` or `nil` if | ||
/// the requested bytes are not readable. | ||
@inlinable | ||
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) | ||
public func getUTF8ValidatedString(at index: Int, length: Int) -> String? { | ||
guard let range = self.rangeWithinReadableBytes(index: index, length: length) else { | ||
return nil | ||
} | ||
return self.withUnsafeReadableBytes { pointer in | ||
assert(range.lowerBound >= 0 && (range.upperBound - range.lowerBound) <= pointer.count) | ||
return String(validating: UnsafeRawBufferPointer(fastRebase: pointer[range]), as: Unicode.UTF8.self) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can make this method less-scary by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
/// Read `length` bytes off this `ByteBuffer`, decoding it as `String` using the UTF-8 encoding. Move the reader index | ||
/// forward by `length`. | ||
/// | ||
/// This is an alternative to `ByteBuffer.readString(length:)` which ensures the returned string is valid UTF8. If the | ||
/// string is not valid UTF8 then the reader index is not advanced. | ||
/// | ||
/// - Parameters: | ||
/// - length: The number of bytes making up the string. | ||
/// - Returns: A `String` value deserialized from this `ByteBuffer` or `nil` if there aren't at least `length` bytes readable. | ||
@inlinable | ||
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) | ||
public mutating func readUTF8ValidatedString(length: Int) -> String? { | ||
guard let result = self.getUTF8ValidatedString(at: self.readerIndex, length: length) else { | ||
return nil | ||
} | ||
self.moveReaderIndex(forwardBy: length) | ||
return result | ||
} | ||
} | ||
#endif // compiler(>=6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not the spelling we want. Right now it's not possible to tell if this API call fails because there aren't enough readable bytes or because the string is not valid UTF8. We need to distinguish the two cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could throw an error for invalid strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that might be appropriate, yeah.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok it is now throwing an error on invalid UTF8