[stdlib] Add "name", "nameAlias" to Unicode.Scalar.Properties

This commit is contained in:
Tony Allevato
2018-03-23 14:31:31 -07:00
parent af798fa972
commit 9858d4edfb
3 changed files with 118 additions and 0 deletions

View File

@@ -424,6 +424,18 @@ typedef enum __swift_stdlib_UCharCategory {
__swift_stdlib_U_CHAR_CATEGORY_COUNT __swift_stdlib_U_CHAR_CATEGORY_COUNT
} __swift_stdlib_UCharCategory; } __swift_stdlib_UCharCategory;
typedef enum __swift_stdlib_UCharNameChoice {
__swift_stdlib_U_UNICODE_CHAR_NAME,
#ifndef U_HIDE_DEPRECATED_API
__swift_stdlib_U_UNICODE_10_CHAR_NAME,
#endif
__swift_stdlib_U_EXTENDED_CHAR_NAME = __swift_stdlib_U_UNICODE_CHAR_NAME + 2,
__swift_stdlib_U_CHAR_NAME_ALIAS,
#ifndef U_HIDE_DEPRECATED_API
__swift_stdlib_U_CHAR_NAME_CHOICE_COUNT
#endif
} __swift_stdlib_UCharNameChoice;
typedef struct __swift_stdlib_UBreakIterator __swift_stdlib_UBreakIterator; typedef struct __swift_stdlib_UBreakIterator __swift_stdlib_UBreakIterator;
typedef struct __swift_stdlib_UNormalizer2 __swift_stdlib_UNormalizer2; typedef struct __swift_stdlib_UNormalizer2 __swift_stdlib_UNormalizer2;
typedef __swift_int8_t __swift_stdlib_UBool; typedef __swift_int8_t __swift_stdlib_UBool;
@@ -504,6 +516,12 @@ __swift_int32_t
__swift_stdlib_u_getIntPropertyValue(__swift_stdlib_UChar32, __swift_stdlib_u_getIntPropertyValue(__swift_stdlib_UChar32,
__swift_stdlib_UProperty); __swift_stdlib_UProperty);
SWIFT_RUNTIME_STDLIB_INTERFACE
__swift_int32_t __swift_stdlib_u_charName(
__swift_stdlib_UChar32 code, __swift_stdlib_UCharNameChoice nameChoice,
char *buffer, __swift_int32_t bufferLength,
__swift_stdlib_UErrorCode *pErrorCode);
#ifdef __cplusplus #ifdef __cplusplus
}} // extern "C", namespace swift }} // extern "C", namespace swift

View File

@@ -956,3 +956,92 @@ extension Unicode.Scalar.Properties {
return Unicode.GeneralCategory(rawValue: rawValue) return Unicode.GeneralCategory(rawValue: rawValue)
} }
} }
/// Executes a block that will attempt to store text in the memory owned by the
/// given string storage object, returning the length of the text.
///
/// The closure is assumed to return an `Int32` value (the convention of ICU's C
/// functions) that is equal to the length of the string being stored. If the
/// storage object is not large enough to hold the string, it is replaced by one
/// that is exactly the required size and the closure is executed one more time
/// to populate it.
@_inlineable
@_versioned
internal func _expandingStorageIfNeeded<
CodeUnit: UnsignedInteger & FixedWidthInteger
>(
_ storage: inout _SwiftStringStorage<CodeUnit>,
body: (_SwiftStringStorage<CodeUnit>) throws -> Int32
) rethrows -> Int {
let z = try body(storage)
let correctSize = Int(z)
if correctSize > storage.capacity {
// If the buffer wasn't large enough, replace it with one that is the
// correct size and execute the closure again.
storage = _SwiftStringStorage<CodeUnit>.create(
capacity: correctSize,
count: correctSize)
return Int(try body(storage))
} else {
// Otherwise, the buffer has been populated; update its count to the correct
// value.
storage.count = correctSize
return correctSize
}
}
extension Unicode.Scalar.Properties {
internal func _scalarName(
_ choice: __swift_stdlib_UCharNameChoice
) -> String? {
let initialCapacity = 256
var storage = _SwiftStringStorage<UTF8.CodeUnit>.create(
capacity: initialCapacity,
count: 0)
var err = __swift_stdlib_U_ZERO_ERROR
let correctSize = _expandingStorageIfNeeded(&storage) { storage in
return storage.start.withMemoryRebound(
to: Int8.self,
capacity: storage.capacity
) { storagePtr in
err = __swift_stdlib_U_ZERO_ERROR
return __swift_stdlib_u_charName(
_value, choice, storagePtr, Int32(storage.capacity), &err)
}
}
guard err.isSuccess && correctSize > 0 else {
return nil
}
return String(_storage: storage)
}
/// The published name of the scalar.
///
/// Some scalars, such as control characters, do not have a value for this
/// property in the UCD. For such scalars, this property will be nil.
///
/// This property corresponds to the `Name` property in the
/// [Unicode Standard](http://www.unicode.org/versions/latest/).
public var name: String? {
return _scalarName(__swift_stdlib_U_UNICODE_CHAR_NAME)
}
/// The normative formal alias of the scalar, or nil if it has no alias.
///
/// The name of a scalar is immutable and never changed in future versions of
/// the Unicode Standard. The `nameAlias` property is provided to issue
/// corrections if a name was issued erroneously. For example, the `name` of
/// U+FE18 is "PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRAKCET"
/// (note that "BRACKET" is misspelled). The `nameAlias` property then
/// contains the corrected name.
///
/// This property corresponds to the `Name_Alias` property in the
/// [Unicode Standard](http://www.unicode.org/versions/latest/).
public var nameAlias: String? {
return _scalarName(__swift_stdlib_U_CHAR_NAME_ALIAS)
}
}

View File

@@ -28,6 +28,7 @@ typedef struct UBreakIterator UBreakIterator;
typedef struct UBreakIterator UNormalizer2; typedef struct UBreakIterator UNormalizer2;
typedef enum UBreakIteratorType {} UBreakIteratorType; typedef enum UBreakIteratorType {} UBreakIteratorType;
typedef enum UErrorCode {} UErrorCode; typedef enum UErrorCode {} UErrorCode;
typedef enum UCharNameChoice {} UCharNameChoice;
typedef uint16_t UChar; typedef uint16_t UChar;
typedef int32_t UChar32; typedef int32_t UChar32;
typedef int8_t UBool; typedef int8_t UBool;
@@ -55,6 +56,7 @@ UBool u_hasBinaryProperty(UChar32, UProperty);
UBool u_isdefined(UChar32); UBool u_isdefined(UChar32);
void u_charAge(UChar32, UVersionInfo); void u_charAge(UChar32, UVersionInfo);
int32_t u_getIntPropertyValue(UChar32, UProperty); int32_t u_getIntPropertyValue(UChar32, UProperty);
int32_t u_charName(UChar32, UCharNameChoice, char *, int32_t, UErrorCode *);
} }
#else #else
@@ -341,6 +343,15 @@ swift::__swift_stdlib_u_getIntPropertyValue(__swift_stdlib_UChar32 c,
return u_getIntPropertyValue(c, static_cast<UProperty>(p)); return u_getIntPropertyValue(c, static_cast<UProperty>(p));
} }
__swift_int32_t swift::__swift_stdlib_u_charName(
__swift_stdlib_UChar32 code, __swift_stdlib_UCharNameChoice nameChoice,
char *buffer, __swift_int32_t bufferLength,
__swift_stdlib_UErrorCode *pErrorCode) {
return u_charName(code, static_cast<UCharNameChoice>(nameChoice),
buffer, bufferLength,
ptr_cast<UErrorCode>(pErrorCode));
}
// Force an autolink with ICU // Force an autolink with ICU
#if defined(__MACH__) #if defined(__MACH__)