Skip to content

Removes redundant buffer zeroing in lowercased() and uppercased() by using `init(unsafeUninitializedCapacity:initializingWith:) #30895

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

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -838,31 +838,32 @@ extension String {
// make UTF-16 array beforehand
let codeUnits = Array(self.utf16).withUnsafeBufferPointer {
(uChars: UnsafeBufferPointer<UInt16>) -> Array<UInt16> in
var result = Array<UInt16>(repeating: 0, count: uChars.count)
let len = result.withUnsafeMutableBufferPointer {
(output) -> Int in
var err = __swift_stdlib_U_ZERO_ERROR
return Int(truncatingIfNeeded:
var length: Int = 0
let result = Array<UInt16>(unsafeUninitializedCapacity: uChars.count) {
buffer, initializedCount in
var error = __swift_stdlib_U_ZERO_ERROR
length = Int(truncatingIfNeeded:
__swift_stdlib_u_strToLower(
output.baseAddress._unsafelyUnwrappedUnchecked,
Int32(output.count),
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err))
&error))
initializedCount = min(length, uChars.count)
}
if len > uChars.count {
var err = __swift_stdlib_U_ZERO_ERROR
result = Array<UInt16>(repeating: 0, count: len)
result.withUnsafeMutableBufferPointer {
output -> Void in
if length > uChars.count {
var error = __swift_stdlib_U_ZERO_ERROR
return Array<UInt16>(unsafeUninitializedCapacity: length) {
buffer, initializedCount in
__swift_stdlib_u_strToLower(
output.baseAddress._unsafelyUnwrappedUnchecked,
Int32(output.count),
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err)
&error)
initializedCount = length
}
}
return result
Expand Down Expand Up @@ -898,31 +899,32 @@ extension String {
// make UTF-16 array beforehand
let codeUnits = Array(self.utf16).withUnsafeBufferPointer {
(uChars: UnsafeBufferPointer<UInt16>) -> Array<UInt16> in
var result = Array<UInt16>(repeating: 0, count: uChars.count)
let len = result.withUnsafeMutableBufferPointer {
(output) -> Int in
var length: Int = 0
let result = Array<UInt16>(unsafeUninitializedCapacity: uChars.count) {
buffer, initializedCount in
var err = __swift_stdlib_U_ZERO_ERROR
return Int(truncatingIfNeeded:
length = Int(truncatingIfNeeded:
__swift_stdlib_u_strToUpper(
output.baseAddress._unsafelyUnwrappedUnchecked,
Int32(output.count),
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err))
initializedCount = min(length, uChars.count)
}
if len > uChars.count {
if length > uChars.count {
var err = __swift_stdlib_U_ZERO_ERROR
result = Array<UInt16>(repeating: 0, count: len)
result.withUnsafeMutableBufferPointer {
output -> Void in
return Array<UInt16>(unsafeUninitializedCapacity: length) {
buffer, initializedCount in
__swift_stdlib_u_strToUpper(
output.baseAddress._unsafelyUnwrappedUnchecked,
Int32(output.count),
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err)
initializedCount = length
}
}
return result
Expand Down