@@ -24,18 +24,9 @@ extension Unicode.Scalar {
24
24
// We convert the value to the underlying UChar32 type here and store it
25
25
// in that form to make calling the ICU APIs cleaner below.
26
26
self . _value = __swift_stdlib_UChar32 ( bitPattern: _scalar. _value)
27
-
28
- // Likewise, we cache the UTF-16 encoding of the scalar for a few of the
29
- // properties that require treating it like a string (e.g., case
30
- // mappings).
31
- let utf16 = _scalar. utf16
32
- self . _utf16Length = utf16. count
33
- self . _utf16 = ( utf16 [ 0 ] , _utf16Length > 1 ? utf16 [ 1 ] : 0 )
34
27
}
35
28
36
29
internal var _value : __swift_stdlib_UChar32
37
- internal var _utf16 : ( UTF16 . CodeUnit , UTF16 . CodeUnit )
38
- internal var _utf16Length : Int
39
30
}
40
31
41
32
/// A value that provides access to properties of the Unicode scalar that are
@@ -1036,6 +1027,15 @@ extension Unicode.Scalar.Properties {
1036
1027
1037
1028
extension Unicode . Scalar . Properties {
1038
1029
1030
+ /// The UTF-16 encoding of the scalar, represented as a tuple of 2 elements.
1031
+ ///
1032
+ /// If the scalar only encodes to one code unit, the second element is zero.
1033
+ @_transparent
1034
+ internal var _utf16CodeUnits : ( UTF16 . CodeUnit , UTF16 . CodeUnit ) {
1035
+ let utf16 = UnicodeScalar ( UInt32 ( _value) ) !. utf16
1036
+ return ( utf16 [ 0 ] , utf16. count > 1 ? utf16 [ 1 ] : 0 )
1037
+ }
1038
+
1039
1039
// The type of ICU case conversion functions.
1040
1040
internal typealias _U_StrToX = (
1041
1041
/* dest */ UnsafeMutablePointer < __swift_stdlib_UChar > ,
@@ -1053,16 +1053,17 @@ extension Unicode.Scalar.Properties {
1053
1053
/// all current case mappings. In the event more space is needed, it will be
1054
1054
/// allocated on the heap.
1055
1055
internal func _applyMapping( _ u_strTo: _U_StrToX ) -> String {
1056
+ let utf16Length = UnicodeScalar ( UInt32 ( _value) ) !. utf16. count
1057
+ var utf16 = _utf16CodeUnits
1056
1058
var scratchBuffer = _Normalization. _SegmentOutputBuffer ( allZeros: ( ) )
1057
1059
let count = scratchBuffer. withUnsafeMutableBufferPointer { bufPtr -> Int in
1058
- var utf16 = _utf16
1059
1060
return withUnsafePointer ( to: & utf16. 0 ) { utf16Pointer in
1060
1061
var err = __swift_stdlib_U_ZERO_ERROR
1061
1062
let correctSize = u_strTo (
1062
1063
bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
1063
1064
Int32 ( bufPtr. count) ,
1064
1065
utf16Pointer,
1065
- Int32 ( _utf16Length ) ,
1066
+ Int32 ( utf16Length ) ,
1066
1067
" " ,
1067
1068
& err)
1068
1069
guard err. isSuccess ||
@@ -1078,14 +1079,13 @@ extension Unicode.Scalar.Properties {
1078
1079
}
1079
1080
var array = Array < UInt16 > ( repeating: 0 , count: count)
1080
1081
array. withUnsafeMutableBufferPointer { bufPtr in
1081
- var utf16 = _utf16
1082
1082
withUnsafePointer ( to: & utf16. 0 ) { utf16Pointer in
1083
1083
var err = __swift_stdlib_U_ZERO_ERROR
1084
1084
let correctSize = u_strTo (
1085
1085
bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
1086
1086
Int32 ( bufPtr. count) ,
1087
1087
utf16Pointer,
1088
- Int32 ( _utf16Length ) ,
1088
+ Int32 ( utf16Length ) ,
1089
1089
" " ,
1090
1090
& err)
1091
1091
guard err. isSuccess else {
0 commit comments