@@ -20,32 +20,33 @@ extension Unicode.Scalar {
20
20
/// A value that provides access to properties of a Unicode scalar that are
21
21
/// defined by the Unicode standard.
22
22
public struct Properties {
23
- internal init ( _scalar: Unicode . Scalar ) {
24
- // We convert the value to the underlying UChar32 type here and store it
25
- // in that form to make calling the ICU APIs cleaner below.
26
- self . _value = __swift_stdlib_UChar32 ( bitPattern: _scalar. _value)
23
+ @usableFromInline
24
+ internal var _scalar : Unicode . Scalar
25
+
26
+ internal init ( _ scalar: Unicode . Scalar ) {
27
+ self . _scalar = scalar
27
28
}
28
29
29
- @usableFromInline
30
- internal var _value : __swift_stdlib_UChar32
30
+ // Provide the value as UChar32 to make calling the ICU APIs cleaner
31
+ internal var icuValue : __swift_stdlib_UChar32 {
32
+ return __swift_stdlib_UChar32 ( bitPattern: self . _scalar. _value)
33
+ }
31
34
}
32
35
33
36
/// A value that provides access to properties of the Unicode scalar that are
34
37
/// defined by the Unicode standard.
35
38
public var properties : Properties {
36
- return Properties ( _scalar : self )
39
+ return Properties ( self )
37
40
}
38
41
}
39
42
40
43
/// Boolean properties that are defined by the Unicode Standard (i.e., not
41
44
/// ICU-specific).
42
45
extension Unicode . Scalar . Properties {
43
-
44
- @usableFromInline @_transparent
45
46
internal func _hasBinaryProperty(
46
47
_ property: __swift_stdlib_UProperty
47
48
) -> Bool {
48
- return __swift_stdlib_u_hasBinaryProperty ( _value , property) != 0
49
+ return __swift_stdlib_u_hasBinaryProperty ( icuValue , property) != 0
49
50
}
50
51
51
52
/// A Boolean property indicating whether the scalar is alphabetic.
@@ -668,16 +669,6 @@ extension Unicode.Scalar.Properties {
668
669
669
670
/// Case mapping properties.
670
671
extension Unicode . Scalar . Properties {
671
-
672
- /// The UTF-16 encoding of the scalar, represented as a tuple of 2 elements.
673
- ///
674
- /// If the scalar only encodes to one code unit, the second element is zero.
675
- @usableFromInline @_transparent
676
- internal var _utf16CodeUnits : ( UTF16 . CodeUnit , UTF16 . CodeUnit ) {
677
- let utf16 = UnicodeScalar ( UInt32 ( _value) ) !. utf16
678
- return ( utf16 [ 0 ] , utf16. count > 1 ? utf16 [ 1 ] : 0 )
679
- }
680
-
681
672
// The type of ICU case conversion functions.
682
673
internal typealias _U_StrToX = (
683
674
/* dest */ UnsafeMutablePointer < __swift_stdlib_UChar > ,
@@ -695,51 +686,44 @@ extension Unicode.Scalar.Properties {
695
686
/// all current case mappings. In the event more space is needed, it will be
696
687
/// allocated on the heap.
697
688
internal func _applyMapping( _ u_strTo: _U_StrToX ) -> String {
698
- let utf16Length = UnicodeScalar ( UInt32 ( _value) ) !. utf16. count
699
- var utf16 = _utf16CodeUnits
700
689
var scratchBuffer = _Normalization. _SegmentOutputBuffer ( allZeros: ( ) )
701
690
let count = scratchBuffer. withUnsafeMutableBufferPointer { bufPtr -> Int in
702
- return withUnsafePointer ( to: & utf16) { tuplePtr in
703
- return tuplePtr. withMemoryRebound ( to: UInt16 . self, capacity: 2 ) {
704
- utf16Pointer in
705
- var err = __swift_stdlib_U_ZERO_ERROR
706
- let correctSize = u_strTo (
707
- bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
708
- Int32 ( bufPtr. count) ,
709
- utf16Pointer,
710
- Int32 ( utf16Length) ,
711
- " " ,
712
- & err)
713
- guard err. isSuccess ||
714
- err == __swift_stdlib_U_BUFFER_OVERFLOW_ERROR else {
715
- fatalError ( " Unexpected error case-converting Unicode scalar. " )
716
- }
717
- return Int ( correctSize)
691
+ return _scalar. withUTF16CodeUnits { utf16 in
692
+ var err = __swift_stdlib_U_ZERO_ERROR
693
+ let correctSize = u_strTo (
694
+ bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
695
+ Int32 ( bufPtr. count) ,
696
+ utf16. baseAddress. _unsafelyUnwrappedUnchecked,
697
+ Int32 ( utf16. count) ,
698
+ " " ,
699
+ & err)
700
+ guard err. isSuccess ||
701
+ err == __swift_stdlib_U_BUFFER_OVERFLOW_ERROR else {
702
+ fatalError ( " Unexpected error case-converting Unicode scalar. " )
718
703
}
704
+ return Int ( correctSize)
719
705
}
720
706
}
707
+
721
708
if _fastPath ( count <= scratchBuffer. count) {
722
709
scratchBuffer. count = count
723
710
return String . _fromWellFormedUTF16CodeUnits ( scratchBuffer)
724
711
}
725
712
var array = Array < UInt16 > ( repeating: 0 , count: count)
726
713
array. withUnsafeMutableBufferPointer { bufPtr in
727
- withUnsafePointer ( to: & utf16) { tuplePtr in
728
- tuplePtr. withMemoryRebound ( to: UInt16 . self, capacity: 2 ) {
729
- utf16Pointer in
730
- var err = __swift_stdlib_U_ZERO_ERROR
731
- let correctSize = u_strTo (
732
- bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
733
- Int32 ( bufPtr. count) ,
734
- utf16Pointer,
735
- Int32 ( utf16Length) ,
736
- " " ,
737
- & err)
738
- guard err. isSuccess else {
739
- fatalError ( " Unexpected error case-converting Unicode scalar. " )
740
- }
741
- _sanityCheck ( count == correctSize, " inconsistent ICU behavior " )
714
+ return _scalar. withUTF16CodeUnits { utf16 in
715
+ var err = __swift_stdlib_U_ZERO_ERROR
716
+ let correctSize = u_strTo (
717
+ bufPtr. baseAddress. _unsafelyUnwrappedUnchecked,
718
+ Int32 ( bufPtr. count) ,
719
+ utf16. baseAddress. _unsafelyUnwrappedUnchecked,
720
+ Int32 ( utf16. count) ,
721
+ " " ,
722
+ & err)
723
+ guard err. isSuccess else {
724
+ fatalError ( " Unexpected error case-converting Unicode scalar. " )
742
725
}
726
+ _sanityCheck ( count == correctSize, " inconsistent ICU behavior " )
743
727
}
744
728
}
745
729
return String . _fromWellFormedUTF16CodeUnits ( array [ ..< count] )
@@ -811,7 +795,7 @@ extension Unicode.Scalar.Properties {
811
795
withUnsafeMutablePointer ( to: & versionInfo) { tuplePtr in
812
796
tuplePtr. withMemoryRebound ( to: UInt8 . self, capacity: 4 ) {
813
797
versionInfoPtr in
814
- __swift_stdlib_u_charAge ( _value , versionInfoPtr)
798
+ __swift_stdlib_u_charAge ( icuValue , versionInfoPtr)
815
799
}
816
800
}
817
801
guard versionInfo. 0 != 0 else { return nil }
@@ -1087,7 +1071,7 @@ extension Unicode.Scalar.Properties {
1087
1071
public var generalCategory : Unicode . GeneralCategory {
1088
1072
let rawValue = __swift_stdlib_UCharCategory (
1089
1073
UInt32 ( __swift_stdlib_u_getIntPropertyValue (
1090
- _value , __swift_stdlib_UCHAR_GENERAL_CATEGORY) ) )
1074
+ icuValue , __swift_stdlib_UCHAR_GENERAL_CATEGORY) ) )
1091
1075
return Unicode . GeneralCategory ( rawValue: rawValue)
1092
1076
}
1093
1077
}
@@ -1098,15 +1082,15 @@ extension Unicode.Scalar.Properties {
1098
1082
_ choice: __swift_stdlib_UCharNameChoice
1099
1083
) -> String ? {
1100
1084
var err = __swift_stdlib_U_ZERO_ERROR
1101
- let count = Int ( __swift_stdlib_u_charName ( _value , choice, nil , 0 , & err) )
1085
+ let count = Int ( __swift_stdlib_u_charName ( icuValue , choice, nil , 0 , & err) )
1102
1086
guard count > 0 else { return nil }
1103
1087
1104
1088
// ICU writes a trailing null, so we have to save room for it as well.
1105
1089
var array = Array < UInt8 > ( repeating: 0 , count: count + 1 )
1106
1090
return array. withUnsafeMutableBufferPointer { bufPtr in
1107
1091
var err = __swift_stdlib_U_ZERO_ERROR
1108
1092
let correctSize = __swift_stdlib_u_charName (
1109
- _value ,
1093
+ icuValue ,
1110
1094
choice,
1111
1095
UnsafeMutableRawPointer ( bufPtr. baseAddress. _unsafelyUnwrappedUnchecked)
1112
1096
. assumingMemoryBound ( to: Int8 . self) ,
@@ -1282,7 +1266,7 @@ extension Unicode.Scalar.Properties {
1282
1266
/// the [Unicode Standard](http://www.unicode.org/versions/latest/).
1283
1267
public var canonicalCombiningClass : Unicode . CanonicalCombiningClass {
1284
1268
let rawValue = UInt8 ( __swift_stdlib_u_getIntPropertyValue (
1285
- _value , __swift_stdlib_UCHAR_CANONICAL_COMBINING_CLASS) )
1269
+ icuValue , __swift_stdlib_UCHAR_CANONICAL_COMBINING_CLASS) )
1286
1270
return Unicode . CanonicalCombiningClass ( rawValue: rawValue)
1287
1271
}
1288
1272
}
@@ -1364,7 +1348,7 @@ extension Unicode.Scalar.Properties {
1364
1348
public var numericType : Unicode . NumericType ? {
1365
1349
let rawValue = __swift_stdlib_UNumericType (
1366
1350
UInt32 ( __swift_stdlib_u_getIntPropertyValue (
1367
- _value , __swift_stdlib_UCHAR_NUMERIC_TYPE) ) )
1351
+ icuValue , __swift_stdlib_UCHAR_NUMERIC_TYPE) ) )
1368
1352
return Unicode . NumericType ( rawValue: rawValue)
1369
1353
}
1370
1354
@@ -1391,7 +1375,7 @@ extension Unicode.Scalar.Properties {
1391
1375
/// [Unicode Standard](http://www.unicode.org/versions/latest/).
1392
1376
public var numericValue : Double ? {
1393
1377
let icuNoNumericValue : Double = - 123456789
1394
- let result = __swift_stdlib_u_getNumericValue ( _value )
1378
+ let result = __swift_stdlib_u_getNumericValue ( icuValue )
1395
1379
return result != icuNoNumericValue ? result : nil
1396
1380
}
1397
1381
}
0 commit comments