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
[stdlib] Fix buffer size of small-capacity strings #67929
Conversation
@swift-ci Please smoke test |
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.
Thank you!
stdlib/public/core/SmallString.swift
Outdated
@@ -243,7 +243,9 @@ extension _SmallString { | |||
fileprivate mutating func withMutableCapacity( | |||
_ f: (UnsafeMutableRawBufferPointer) throws -> Int | |||
) rethrows { | |||
let len = try withUnsafeMutableBytes(of: &_storage, f) | |||
let len = try withUnsafeMutableBytes(of: &_storage) { | |||
try f(.init(rebasing: $0.prefix(_SmallString.capacity))) |
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.
It would be justifiable to use .init(_uncheckedStart:count:)
here: the preconditions are known to be met.
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.
UMRBP doesn't have an unchecked initializer, so I used init(start:count:)
instead.
let smol = try _SmallString(initializingUTF8With: initializer) | ||
let smol = try _SmallString(initializingUTF8With: { | ||
try initializer(.init(rebasing: $0.prefix(capacity))) | ||
}) |
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.
Could also use .init(_uncheckedStart:count:)
here.
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.
The given capacity
could be negative, so I used init(start:count:)
instead.
@swift-ci please test |
Resolves #57911