Skip to content

[stdlib] Make the rest of the scalar properties native #40233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions stdlib/public/SwiftShims/UnicodeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
extern "C" {
#endif

#define SWIFT_STDLIB_LARGEST_NAME_COUNT 88

//===----------------------------------------------------------------------===//
// Utilities
//===----------------------------------------------------------------------===//

__swift_intptr_t _swift_stdlib_getMphIdx(__swift_uint32_t scalar,
__swift_intptr_t levels,
const __swift_uint64_t * const *keys,
const __swift_uint16_t * const *ranks,
const __swift_uint16_t * const sizes);

__swift_intptr_t _swift_stdlib_getScalarBitArrayIdx(__swift_uint32_t scalar,
const __swift_uint64_t *bitArrays,
const __swift_uint16_t *ranks);

//===----------------------------------------------------------------------===//
// Normalization
//===----------------------------------------------------------------------===//

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint16_t _swift_stdlib_getNormData(__swift_uint32_t scalar);

Expand All @@ -34,19 +54,50 @@ SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint32_t _swift_stdlib_getComposition(__swift_uint32_t x,
__swift_uint32_t y);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_intptr_t _swift_stdlib_getMphIdx(__swift_uint32_t scalar,
__swift_intptr_t levels,
const __swift_uint64_t * const *keys,
const __swift_uint16_t * const *ranks,
const __swift_uint16_t * const sizes);
//===----------------------------------------------------------------------===//
// Grapheme Breaking
//===----------------------------------------------------------------------===//


SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint8_t _swift_stdlib_getGraphemeBreakProperty(__swift_uint32_t scalar);

//===----------------------------------------------------------------------===//
// Unicode.Scalar.Properties
//===----------------------------------------------------------------------===//

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint64_t _swift_stdlib_getBinaryProperties(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint8_t _swift_stdlib_getNumericType(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
double _swift_stdlib_getNumericValue(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
const char *_swift_stdlib_getNameAlias(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_int32_t _swift_stdlib_getMapping(__swift_uint32_t scalar,
__swift_uint8_t mapping);

SWIFT_RUNTIME_STDLIB_INTERNAL
const __swift_uint8_t *_swift_stdlib_getSpecialMapping(__swift_uint32_t scalar,
__swift_uint8_t mapping,
__swift_intptr_t *length);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_intptr_t _swift_stdlib_getScalarName(__swift_uint32_t scalar,
__swift_uint8_t *buffer,
__swift_intptr_t capacity);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint16_t _swift_stdlib_getAge(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint8_t _swift_stdlib_getGeneralCategory(__swift_uint32_t scalar);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
58 changes: 0 additions & 58 deletions stdlib/public/SwiftShims/UnicodeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@
extern "C" {
#endif

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t _swift_stdlib_unicode_strToUpper(
__swift_uint16_t *Destination, __swift_int32_t DestinationCapacity,
const __swift_uint16_t *Source, __swift_int32_t SourceLength);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t _swift_stdlib_unicode_strToLower(
__swift_uint16_t *Destination, __swift_int32_t DestinationCapacity,
const __swift_uint16_t *Source, __swift_int32_t SourceLength);

typedef enum __swift_stdlib_UProperty {
__swift_stdlib_UCHAR_GENERAL_CATEGORY = 0x1005,
__swift_stdlib_UCHAR_NUMERIC_TYPE = 0x1009,
Expand Down Expand Up @@ -264,16 +254,6 @@ typedef enum __swift_stdlib_UCharNameChoice {
#endif
} __swift_stdlib_UCharNameChoice;

typedef enum __swift_stdlib_UNumericType {
__swift_stdlib_U_NT_NONE,
__swift_stdlib_U_NT_DECIMAL,
__swift_stdlib_U_NT_DIGIT,
__swift_stdlib_U_NT_NUMERIC,
#ifndef U_HIDE_DEPRECATED_API
__swift_stdlib_U_NT_COUNT
#endif
} __swift_stdlib_UNumericType;

typedef struct __swift_stdlib_UBreakIterator __swift_stdlib_UBreakIterator;
typedef struct __swift_stdlib_UText __swift_stdlib_UText;
typedef __swift_int8_t __swift_stdlib_UBool;
Expand All @@ -291,44 +271,6 @@ typedef __swift_uint16_t __swift_stdlib_UChar;
typedef __swift_uint8_t
__swift_stdlib_UVersionInfo[__SWIFT_STDLIB_U_MAX_VERSION_LENGTH];

SWIFT_RUNTIME_STDLIB_API
void __swift_stdlib_u_charAge(
__swift_stdlib_UChar32, __swift_stdlib_UVersionInfo _Nonnull);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t
__swift_stdlib_u_getIntPropertyValue(__swift_stdlib_UChar32,
__swift_stdlib_UProperty);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_u_charName(
__swift_stdlib_UChar32 code, __swift_stdlib_UCharNameChoice nameChoice,
char *_Nullable buffer, __swift_int32_t bufferLength,
__swift_stdlib_UErrorCode *pErrorCode);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_u_strToLower(
__swift_stdlib_UChar *dest, __swift_int32_t destCapacity,
const __swift_stdlib_UChar *src, __swift_int32_t srcLength,
const char *_Nullable locale, __swift_stdlib_UErrorCode *pErrorCode);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_u_strToTitle(
__swift_stdlib_UChar *dest, __swift_int32_t destCapacity,
const __swift_stdlib_UChar *src, __swift_int32_t srcLength,
__swift_stdlib_UBreakIterator *_Nullable titleIter,
const char *_Nullable locale, __swift_stdlib_UErrorCode *pErrorCode);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_u_strToUpper(
__swift_stdlib_UChar *dest, __swift_int32_t destCapacity,
const __swift_stdlib_UChar *src, __swift_int32_t srcLength,
const char *_Nullable locale, __swift_stdlib_UErrorCode *pErrorCode);

SWIFT_RUNTIME_STDLIB_API
double __swift_stdlib_u_getNumericValue(__swift_stdlib_UChar32 c);


#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
82 changes: 14 additions & 68 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -849,41 +849,14 @@ extension String {
}
}

// TODO(String performance): Try out incremental case-conversion rather than
// make UTF-16 array beforehand
let codeUnits = Array(self.utf16).withUnsafeBufferPointer {
(uChars: UnsafeBufferPointer<UInt16>) -> Array<UInt16> in
var length: Int = 0
let result = Array<UInt16>(unsafeUninitializedCapacity: uChars.count) {
buffer, initializedCount in
var error = __swift_stdlib_U_ZERO_ERROR
length = Int(truncatingIfNeeded:
__swift_stdlib_u_strToLower(
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&error))
initializedCount = min(length, uChars.count)
}
if length > uChars.count {
var error = __swift_stdlib_U_ZERO_ERROR
return Array<UInt16>(unsafeUninitializedCapacity: length) {
buffer, initializedCount in
__swift_stdlib_u_strToLower(
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&error)
initializedCount = length
}
}
return result
var result = ""
result.reserveCapacity(utf8.count)

for scalar in unicodeScalars {
result += scalar.properties.lowercaseMapping
}
return codeUnits.withUnsafeBufferPointer { String._uncheckedFromUTF16($0) }

return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭 (out of joy)

}

/// Returns an uppercase version of the string.
Expand All @@ -910,41 +883,14 @@ extension String {
}
}

// TODO(String performance): Try out incremental case-conversion rather than
// make UTF-16 array beforehand
let codeUnits = Array(self.utf16).withUnsafeBufferPointer {
(uChars: UnsafeBufferPointer<UInt16>) -> Array<UInt16> in
var length: Int = 0
let result = Array<UInt16>(unsafeUninitializedCapacity: uChars.count) {
buffer, initializedCount in
var err = __swift_stdlib_U_ZERO_ERROR
length = Int(truncatingIfNeeded:
__swift_stdlib_u_strToUpper(
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err))
initializedCount = min(length, uChars.count)
}
if length > uChars.count {
var err = __swift_stdlib_U_ZERO_ERROR
return Array<UInt16>(unsafeUninitializedCapacity: length) {
buffer, initializedCount in
__swift_stdlib_u_strToUpper(
buffer.baseAddress._unsafelyUnwrappedUnchecked,
Int32(buffer.count),
uChars.baseAddress._unsafelyUnwrappedUnchecked,
Int32(uChars.count),
"",
&err)
initializedCount = length
}
}
return result
var result = ""
result.reserveCapacity(utf8.count)

for scalar in unicodeScalars {
result += scalar.properties.uppercaseMapping
}
return codeUnits.withUnsafeBufferPointer { String._uncheckedFromUTF16($0) }

return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we have good benchmark coverage for this. This could be a nice improvement.

}

/// Creates an instance from the description of a given
Expand Down
Loading