@@ -838,31 +838,32 @@ extension String {
838
838
// make UTF-16 array beforehand
839
839
let codeUnits = Array ( self . utf16) . withUnsafeBufferPointer {
840
840
( uChars: UnsafeBufferPointer < UInt16 > ) -> Array < UInt16 > in
841
- var result = Array < UInt16 > ( repeating : 0 , count : uChars . count )
842
- let len = result . withUnsafeMutableBufferPointer {
843
- ( output ) -> Int in
844
- var err = __swift_stdlib_U_ZERO_ERROR
845
- return Int ( truncatingIfNeeded:
841
+ var length : Int = 0
842
+ let result = Array < UInt16 > ( unsafeUninitializedCapacity : uChars . count ) {
843
+ buffer , initializedCount in
844
+ var error = __swift_stdlib_U_ZERO_ERROR
845
+ length = Int ( truncatingIfNeeded:
846
846
__swift_stdlib_u_strToLower (
847
- output . baseAddress. _unsafelyUnwrappedUnchecked,
848
- Int32 ( output . count) ,
847
+ buffer . baseAddress. _unsafelyUnwrappedUnchecked,
848
+ Int32 ( buffer . count) ,
849
849
uChars. baseAddress. _unsafelyUnwrappedUnchecked,
850
850
Int32 ( uChars. count) ,
851
851
" " ,
852
- & err) )
852
+ & error) )
853
+ initializedCount = min ( length, uChars. count)
853
854
}
854
- if len > uChars. count {
855
- var err = __swift_stdlib_U_ZERO_ERROR
856
- result = Array < UInt16 > ( repeating: 0 , count: len)
857
- result. withUnsafeMutableBufferPointer {
858
- output -> Void in
855
+ if length > uChars. count {
856
+ var error = __swift_stdlib_U_ZERO_ERROR
857
+ return Array < UInt16 > ( unsafeUninitializedCapacity: length) {
858
+ buffer, initializedCount in
859
859
__swift_stdlib_u_strToLower (
860
- output . baseAddress. _unsafelyUnwrappedUnchecked,
861
- Int32 ( output . count) ,
860
+ buffer . baseAddress. _unsafelyUnwrappedUnchecked,
861
+ Int32 ( buffer . count) ,
862
862
uChars. baseAddress. _unsafelyUnwrappedUnchecked,
863
863
Int32 ( uChars. count) ,
864
864
" " ,
865
- & err)
865
+ & error)
866
+ initializedCount = length
866
867
}
867
868
}
868
869
return result
@@ -898,31 +899,32 @@ extension String {
898
899
// make UTF-16 array beforehand
899
900
let codeUnits = Array ( self . utf16) . withUnsafeBufferPointer {
900
901
( uChars: UnsafeBufferPointer < UInt16 > ) -> Array < UInt16 > in
901
- var result = Array < UInt16 > ( repeating : 0 , count : uChars . count )
902
- let len = result . withUnsafeMutableBufferPointer {
903
- ( output ) -> Int in
902
+ var length : Int = 0
903
+ let result = Array < UInt16 > ( unsafeUninitializedCapacity : uChars . count ) {
904
+ buffer , initializedCount in
904
905
var err = __swift_stdlib_U_ZERO_ERROR
905
- return Int ( truncatingIfNeeded:
906
+ length = Int ( truncatingIfNeeded:
906
907
__swift_stdlib_u_strToUpper (
907
- output . baseAddress. _unsafelyUnwrappedUnchecked,
908
- Int32 ( output . count) ,
908
+ buffer . baseAddress. _unsafelyUnwrappedUnchecked,
909
+ Int32 ( buffer . count) ,
909
910
uChars. baseAddress. _unsafelyUnwrappedUnchecked,
910
911
Int32 ( uChars. count) ,
911
912
" " ,
912
913
& err) )
914
+ initializedCount = min ( length, uChars. count)
913
915
}
914
- if len > uChars. count {
916
+ if length > uChars. count {
915
917
var err = __swift_stdlib_U_ZERO_ERROR
916
- result = Array < UInt16 > ( repeating: 0 , count: len)
917
- result. withUnsafeMutableBufferPointer {
918
- output -> Void in
918
+ return Array < UInt16 > ( unsafeUninitializedCapacity: length) {
919
+ buffer, initializedCount in
919
920
__swift_stdlib_u_strToUpper (
920
- output . baseAddress. _unsafelyUnwrappedUnchecked,
921
- Int32 ( output . count) ,
921
+ buffer . baseAddress. _unsafelyUnwrappedUnchecked,
922
+ Int32 ( buffer . count) ,
922
923
uChars. baseAddress. _unsafelyUnwrappedUnchecked,
923
924
Int32 ( uChars. count) ,
924
925
" " ,
925
926
& err)
927
+ initializedCount = length
926
928
}
927
929
}
928
930
return result
0 commit comments