-
Notifications
You must be signed in to change notification settings - Fork 650
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
Conversation
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.
Thanks for this! I think the API design needs a bit of a tweak.
I also wonder if we can implement an older-Swift fallback using https://developer.apple.com/documentation/swift/string/init(validatingutf8:)-208fn.
The problem with using the above function is it requires the string to be null terminated. I guess I could copy it out to a separate buffer and add the null termination. |
Oh, we could do this one instead: https://developer.apple.com/documentation/swift/string/init(utf8string:)-8qmaq |
Which also requires a null-terminated string |
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.
Fantastic, I'm really happy with this. Thanks @adam-fowler!
Looks like there are a few compile errors in the unit tests on older Swift compilers. |
FWIW, I think it's usually better to put the compiler guards inside the test function, and |
done |
…ring (apple#2973) Add methods to ByteBuffer to read validated UTF8 strings ### Motivation: The current `readString` and `getString` methods of `ByteBuffer` do not verify that the string being read is valid UTF8. The Swift 6 standard library comes with a new initialiser `String(validating:as:)`. This PR adds alternative methods to ByteBuffer which uses this instead of `String(decoding:as:)`. ### Modifications: Added `ByteBuffer.getUTF8ValidatedString(at:length:)` Added `ByteBuffer.readUTF8ValidatedString(length:)` ### Result: You can read strings from a ByteBuffer and be certain they are valid UTF8 --------- Co-authored-by: Cory Benfield <lukasa@apple.com> (cherry picked from commit 74cf44e)
Add methods to ByteBuffer to read validated UTF8 strings
Motivation:
The current
readString
andgetString
methods ofByteBuffer
do not verify that the string being read is valid UTF8. The Swift 6 standard library comes with a new initialiserString(validating:as:)
. This PR adds alternative methods to ByteBuffer which uses this instead ofString(decoding:as:)
.Modifications:
Added
ByteBuffer.getUTF8ValidatedString(at:length:)
Added
ByteBuffer.readUTF8ValidatedString(length:)
Result:
You can read strings from a ByteBuffer and be certain they are valid UTF8