Skip to content

Commit d1ae693

Browse files
authored
Merge pull request #32312 from valeriyvan/RemoveRedundantBufferZeroingUnicodeScalarProperties
[stdlib] Eliminates redundant buffer zeroing in UnicodeScalarProperties
2 parents 059344d + dd237b9 commit d1ae693

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

stdlib/public/core/UnicodeScalarProperties.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,27 +1113,29 @@ extension Unicode.Scalar.Properties {
11131113
internal func _scalarName(
11141114
_ choice: __swift_stdlib_UCharNameChoice
11151115
) -> String? {
1116-
var err = __swift_stdlib_U_ZERO_ERROR
1117-
let count = Int(__swift_stdlib_u_charName(icuValue, choice, nil, 0, &err))
1116+
var error = __swift_stdlib_U_ZERO_ERROR
1117+
let count = Int(__swift_stdlib_u_charName(icuValue, choice, nil, 0, &error))
11181118
guard count > 0 else { return nil }
11191119

11201120
// ICU writes a trailing null, so we have to save room for it as well.
1121-
var array = Array<UInt8>(repeating: 0, count: count + 1)
1122-
return array.withUnsafeMutableBufferPointer { bufPtr in
1123-
var err = __swift_stdlib_U_ZERO_ERROR
1121+
let array = Array<UInt8>(unsafeUninitializedCapacity: count + 1) {
1122+
buffer, initializedCount in
1123+
var error = __swift_stdlib_U_ZERO_ERROR
11241124
let correctSize = __swift_stdlib_u_charName(
11251125
icuValue,
11261126
choice,
1127-
UnsafeMutableRawPointer(bufPtr.baseAddress._unsafelyUnwrappedUnchecked)
1127+
UnsafeMutableRawPointer(buffer.baseAddress._unsafelyUnwrappedUnchecked)
11281128
.assumingMemoryBound(to: Int8.self),
1129-
Int32(bufPtr.count),
1130-
&err)
1131-
guard err.isSuccess else {
1129+
Int32(buffer.count),
1130+
&error)
1131+
guard error.isSuccess else {
11321132
fatalError("Unexpected error case-converting Unicode scalar.")
11331133
}
11341134
_internalInvariant(count == correctSize, "inconsistent ICU behavior")
1135-
return String._fromASCII(
1136-
UnsafeBufferPointer(rebasing: bufPtr[..<count]))
1135+
initializedCount = count + 1
1136+
}
1137+
return array.withUnsafeBufferPointer { buffer in
1138+
String._fromASCII(UnsafeBufferPointer(rebasing: buffer[..<count]))
11371139
}
11381140
}
11391141

0 commit comments

Comments
 (0)