Skip to content

Commit 495322a

Browse files
committed
Implement native numeric scalar properties
1 parent 6136ead commit 495322a

File tree

10 files changed

+4020
-643
lines changed

10 files changed

+4020
-643
lines changed

stdlib/public/SwiftShims/UnicodeData.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ __swift_uint8_t _swift_stdlib_getGraphemeBreakProperty(__swift_uint32_t scalar);
6767
SWIFT_RUNTIME_STDLIB_INTERNAL
6868
__swift_uint64_t _swift_stdlib_getBinaryProperties(__swift_uint32_t scalar);
6969

70+
SWIFT_RUNTIME_STDLIB_INTERNAL
71+
__swift_uint8_t _swift_stdlib_getNumericType(__swift_uint32_t scalar);
72+
73+
SWIFT_RUNTIME_STDLIB_INTERNAL
74+
double _swift_stdlib_getNumericValue(__swift_uint32_t scalar);
75+
7076
#ifdef __cplusplus
7177
} // extern "C"
7278
#endif

stdlib/public/SwiftShims/UnicodeShims.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,6 @@ typedef enum __swift_stdlib_UCharNameChoice {
264264
#endif
265265
} __swift_stdlib_UCharNameChoice;
266266

267-
typedef enum __swift_stdlib_UNumericType {
268-
__swift_stdlib_U_NT_NONE,
269-
__swift_stdlib_U_NT_DECIMAL,
270-
__swift_stdlib_U_NT_DIGIT,
271-
__swift_stdlib_U_NT_NUMERIC,
272-
#ifndef U_HIDE_DEPRECATED_API
273-
__swift_stdlib_U_NT_COUNT
274-
#endif
275-
} __swift_stdlib_UNumericType;
276-
277267
typedef struct __swift_stdlib_UBreakIterator __swift_stdlib_UBreakIterator;
278268
typedef struct __swift_stdlib_UText __swift_stdlib_UText;
279269
typedef __swift_int8_t __swift_stdlib_UBool;
@@ -325,9 +315,6 @@ __swift_int32_t __swift_stdlib_u_strToUpper(
325315
const __swift_stdlib_UChar *src, __swift_int32_t srcLength,
326316
const char *_Nullable locale, __swift_stdlib_UErrorCode *pErrorCode);
327317

328-
SWIFT_RUNTIME_STDLIB_API
329-
double __swift_stdlib_u_getNumericValue(__swift_stdlib_UChar32 c);
330-
331318

332319
#ifdef __cplusplus
333320
} // extern "C"

stdlib/public/core/UnicodeScalarProperties.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,16 +1437,6 @@ extension Unicode {
14371437
/// meet the requirements of `decimal` will have numeric type `numeric`,
14381438
/// and programs can treat `digit` and `numeric` equivalently.
14391439
case numeric
1440-
1441-
internal init?(rawValue: __swift_stdlib_UNumericType) {
1442-
switch rawValue {
1443-
case __swift_stdlib_U_NT_NONE: return nil
1444-
case __swift_stdlib_U_NT_DECIMAL: self = .decimal
1445-
case __swift_stdlib_U_NT_DIGIT: self = .digit
1446-
case __swift_stdlib_U_NT_NUMERIC: self = .numeric
1447-
default: fatalError("Unknown numeric type \(rawValue)")
1448-
}
1449-
}
14501440
}
14511441
}
14521442

@@ -1470,11 +1460,18 @@ extension Unicode.Scalar.Properties {
14701460
/// This property corresponds to the "Numeric_Type" property in the
14711461
/// [Unicode Standard](http://www.unicode.org/versions/latest/).
14721462
public var numericType: Unicode.NumericType? {
1473-
let rawValue = __swift_stdlib_UNumericType(
1474-
__swift_stdlib_UNumericType.RawValue(
1475-
__swift_stdlib_u_getIntPropertyValue(
1476-
icuValue, __swift_stdlib_UCHAR_NUMERIC_TYPE)))
1477-
return Unicode.NumericType(rawValue: rawValue)
1463+
let rawValue = _swift_stdlib_getNumericType(_scalar.value)
1464+
1465+
switch rawValue {
1466+
case 0:
1467+
return .numeric
1468+
case 1:
1469+
return .digit
1470+
case 2:
1471+
return .decimal
1472+
default:
1473+
return nil
1474+
}
14781475
}
14791476

14801477
/// The numeric value of the scalar.
@@ -1494,8 +1491,10 @@ extension Unicode.Scalar.Properties {
14941491
/// This property corresponds to the "Numeric_Value" property in the [Unicode
14951492
/// Standard](http://www.unicode.org/versions/latest/).
14961493
public var numericValue: Double? {
1497-
let icuNoNumericValue: Double = -123456789
1498-
let result = __swift_stdlib_u_getNumericValue(icuValue)
1499-
return result != icuNoNumericValue ? result : nil
1494+
guard numericType != nil else {
1495+
return nil
1496+
}
1497+
1498+
return _swift_stdlib_getNumericValue(_scalar.value)
15001499
}
15011500
}

stdlib/public/stubs/UnicodeScalarProps.cpp

Lines changed: 51 additions & 606 deletions
Large diffs are not rendered by default.

stdlib/public/stubs/UnicodeScalarProps.h

Lines changed: 911 additions & 0 deletions
Large diffs are not rendered by default.

stdlib/public/stubs/UnicodeShims.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ int32_t u_strToTitle(UChar *, int32_t, const UChar *, int32_t,
4545
UBreakIterator *, const char *, UErrorCode *);
4646
int32_t u_strToUpper(UChar *, int32_t, const UChar *, int32_t, const char *,
4747
UErrorCode *);
48-
double u_getNumericValue(UChar32);
4948
}
5049

5150
#else
@@ -177,11 +176,6 @@ __swift_int32_t __swift_stdlib_u_strToUpper(
177176
locale, ptr_cast<UErrorCode>(pErrorCode));
178177
}
179178

180-
double __swift_stdlib_u_getNumericValue(__swift_stdlib_UChar32 c) {
181-
return u_getNumericValue(c);
182-
}
183-
184-
185179
// Force an autolink with ICU
186180
#if defined(__MACH__)
187181
asm(".linker_option \"-licucore\"\n");

utils/gen-unicode-data/Data/DerivedNumericType.txt

Lines changed: 278 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)