Skip to content
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
1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(sources
System.h
Target.h
ThreadLocalStorage.h
UnicodeData.h
UnicodeShims.h
Visibility.h
_SwiftConcurrency.h
Expand Down
47 changes: 47 additions & 0 deletions stdlib/public/SwiftShims/UnicodeData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_STDLIB_SHIMS_UNICODEDATA_H
#define SWIFT_STDLIB_SHIMS_UNICODEDATA_H

#include "SwiftStdint.h"
#include "Visibility.h"

#ifdef __cplusplus
extern "C" {
#endif

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint16_t _swift_stdlib_getNormData(__swift_uint32_t scalar);

SWIFT_RUNTIME_STDLIB_INTERNAL
const __swift_uint8_t * const _swift_stdlib_nfd_decompositions;

SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_uint32_t _swift_stdlib_getDecompositionEntry(__swift_uint32_t scalar);

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);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // SWIFT_STDLIB_SHIMS_UNICODEDATA_H
22 changes: 0 additions & 22 deletions stdlib/public/SwiftShims/UnicodeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ typedef enum __swift_stdlib_UNumericType {

typedef struct __swift_stdlib_UBreakIterator __swift_stdlib_UBreakIterator;
typedef struct __swift_stdlib_UText __swift_stdlib_UText;
typedef struct __swift_stdlib_UNormalizer2 __swift_stdlib_UNormalizer2;
typedef __swift_int8_t __swift_stdlib_UBool;
typedef __swift_int32_t __swift_stdlib_UChar32;
#if defined(__APPLE__)
Expand Down Expand Up @@ -489,27 +488,6 @@ SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_ubrk_following(__swift_stdlib_UBreakIterator *bi,
__swift_int32_t offset);

SWIFT_RUNTIME_STDLIB_API
__swift_stdlib_UBool
__swift_stdlib_unorm2_hasBoundaryBefore(const __swift_stdlib_UNormalizer2 *,
__swift_stdlib_UChar32);

SWIFT_RUNTIME_STDLIB_API
const __swift_stdlib_UNormalizer2 *
__swift_stdlib_unorm2_getNFCInstance(__swift_stdlib_UErrorCode *);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t
__swift_stdlib_unorm2_normalize(const __swift_stdlib_UNormalizer2 *,
const __swift_stdlib_UChar *, __swift_int32_t,
__swift_stdlib_UChar *, __swift_int32_t,
__swift_stdlib_UErrorCode *);

SWIFT_RUNTIME_STDLIB_API
__swift_int32_t __swift_stdlib_unorm2_spanQuickCheckYes(
const __swift_stdlib_UNormalizer2 *, const __swift_stdlib_UChar *,
__swift_int32_t, __swift_stdlib_UErrorCode *);

SWIFT_RUNTIME_STDLIB_API
__swift_stdlib_UBool
__swift_stdlib_u_hasBinaryProperty(__swift_stdlib_UChar32,
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module SwiftShims {
header "SwiftStdint.h"
header "System.h"
header "ThreadLocalStorage.h"
header "UnicodeData.h"
header "UnicodeShims.h"
header "Visibility.h"
export *
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ set(SWIFTLIB_ESSENTIAL
NativeDictionary.swift
NativeSet.swift
NewtypeWrapper.swift
NFC.swift
NFD.swift
ObjectIdentifier.swift
Optional.swift
OptionSet.swift
Expand Down Expand Up @@ -172,6 +174,7 @@ set(SWIFTLIB_ESSENTIAL
ThreadLocalStorage.swift
UIntBuffer.swift
UnavailableStringAPIs.swift
UnicodeData.swift
UnicodeEncoding.swift
UnicodeHelpers.swift
UnicodeParser.swift
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"Character.swift",
"CharacterProperties.swift",
"ICU.swift",
"NormalizedCodeUnitIterator.swift",
"NFC.swift",
"NFD.swift",
"SmallString.swift",
"StaticString.swift",
"String.swift",
Expand Down Expand Up @@ -42,6 +43,7 @@
"StringUnicodeScalarView.swift",
"Substring.swift",
"Unicode.swift",
"UnicodeData.swift",
"UnicodeEncoding.swift",
"UnicodeHelpers.swift",
"UnicodeParser.swift",
Expand Down
223 changes: 223 additions & 0 deletions stdlib/public/core/NFC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftShims

extension Unicode {
internal struct _NFC<S: StringProtocol> {
let base: S
}
}

extension Unicode._NFC {
internal struct Iterator {
var buffer = Unicode._NormDataBuffer()

// This is our starter that is currently being composed with other scalars
// into new scalars. For example, "e\u{301}", here our first scalar is 'e',
// which is a starter, thus we assign composee to this 'e' and move to the
// next scalar. We attempt to compose our composee, 'e', with '\u{301}' and
// find that there is a composition. Thus our new composee is now 'é' and
// we continue to try and compose following scalars with this composee.
var composee: Unicode.Scalar? = nil

var iterator: Unicode._NFD<S>.Iterator
}
}

extension Unicode._NFC.Iterator: IteratorProtocol {
internal func compose(
_ x: Unicode.Scalar,
and y: Unicode.Scalar
) -> Unicode.Scalar? {
// Fast path: ASCII and some latiny scalars never compose when they're on
// the rhs.
if _fastPath(y.value < 0x300) {
return nil
}

if let hangul = composeHangul(x, and: y) {
return hangul
}

// Otherwise, lookup the composition.
let composition = _swift_stdlib_getComposition(x.value, y.value)

guard composition != .max else {
return nil
}

return Unicode.Scalar(_value: composition)
}

@inline(never)
internal func composeHangul(
_ x: Unicode.Scalar,
and y: Unicode.Scalar
) -> Unicode.Scalar? {
// L = Hangul leading consonants
let L: (base: UInt32, count: UInt32) = (base: 0x1100, count: 19)
// V = Hangul vowels
let V: (base: UInt32, count: UInt32) = (base: 0x1161, count: 21)
// T = Hangul tail consonants
let T: (base: UInt32, count: UInt32) = (base: 0x11A7, count: 28)
// N = Number of precomposed Hangul syllables that start with the same
// leading consonant. (There is no base for N).
let N: (base: UInt32, count: UInt32) = (base: 0x0, count: 588)
// S = Hangul precomposed syllables
let S: (base: UInt32, count: UInt32) = (base: 0xAC00, count: 11172)

switch (x.value, y.value) {
// Check for Hangul (L, V) -> LV compositions.
case (L.base ..< L.base &+ L.count, V.base ..< V.base &+ V.count):
let lIdx = x.value &- L.base
let vIdx = y.value &- V.base
let lvIdx = lIdx &* N.count &+ vIdx &* T.count
let s = S.base &+ lvIdx
return Unicode.Scalar(_value: s)

// Check for Hangul (LV, T) -> LVT compositions.
case (S.base ..< S.base &+ S.count, T.base &+ 1 ..< T.base &+ T.count):
if (x.value &- S.base) % T.count == 0 {
return Unicode.Scalar(_value: x.value &+ y.value &- T.base)
} else {
fallthrough
}

default:
return nil
}
}

internal mutating func next() -> Unicode.Scalar? {
// Empty out our buffer before attempting to compose anything with our new
// composee.
if let nextBuffered = buffer.next() {
return nextBuffered.scalar
}

while let current = iterator.next() {
guard let currentComposee = composee else {
// If we don't have a composee at this point, we're most likely looking
Copy link
Member

Choose a reason for hiding this comment

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

"most likely"? When are we not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the start of the string begins with say 50 non starters, we just emit those and do no normalization work, but the 51st scalar is a starter so technically we're not doing any work until that scalar.

// at the start of a string. If our class is 0, then attempt to compose
// the following scalars with this one. Otherwise, it's a one off scalar
// that needs to be emitted.
if current.normData.ccc == 0 {
composee = current.scalar
continue
} else {
return current.scalar
}
}

// If we have any scalars in the buffer, it means those scalars couldn't
// compose with our composee to form a new scalar. However, scalars
// following them may still compose with our composee, so take the last
// scalar in the buffer and get its normalization data so that we can
// perform the check underneath this one about whether this current scalar
// is "blocked". We get the last scalar because the scalars we receive are
// already NFD, so the last scalar in the buffer will have the highest
// CCC value in this normalization segment.
guard let lastBufferedNormData = buffer.last?.normData else {
Copy link
Member

Choose a reason for hiding this comment

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

It took me a few readings to figure it out, but we're really checking the prior scalar's CCC, right? So maybe:

Suggested change
guard let lastBufferedNormData = buffer.last?.normData else {
guard let priorCCC = buffer.last?.normData.ccc else {

And update the comment appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not necessarily the prior scalar's CCC, no. I can say something like lastBufferedCCC however and add the .ccc here.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't the prior CCC what's required by Unicode? When is it a different scalar?

"last" sounds like it's the last scalar in the current normalization segment, and thus would probably appear after current.

// If we do not any have scalars in our buffer yet, then this step is
// trivial. Attempt to compose our current scalar with whatever composee
// we're currently building up.

// If our right hand side scalar IS NFC_QC, then that means it can
// never compose with any scalars previous to it. So, if our current
// scalar is NFC_QC, then we have no composition.
guard !current.normData.isNFCQC,
let composed = compose(currentComposee, and: current.scalar) else {
// We did not find a composition between the two. If our current class
// is 0, then set that as the new composee and return whatever built
// up scalar we have. Otherwise, add our current scalar to the buffer
// for eventual removal!

if current.normData.ccc == 0 {
composee = current.scalar
return currentComposee
}

buffer.append(current)
continue
}

// We found a composition! Record it as our new composee and repeat the
// process.
composee = composed
continue
}

// Check if our current scalar is not blocked from our current composee.
// In this case blocked means there is some scalar whose class
// (lastBufferedNormData.ccc) is either == 0 or >= current.normData.ccc.
//
// Example:
//
// "z\u{0335}\u{0327}\u{0324}\u{0301}"
//
// In this example, there are several combining marks following a 'z', but
// none of them actually compose with the composee 'z'. However, the last
// scalar U+0301 does actually compose. So this check makes sure that the
// last scalar doesn't have any scalar in between it and the composee that
// would otherwise "block" it from composing.
guard lastBufferedNormData.ccc < current.normData.ccc else {
// We had a scalar block it. That means our current scalar is either a
// starter or has a same class (preserve ordering).

// Starters are the "start" of a new normalization segment. Set it as
// the new composee and return our current composee. This will trigger
// any other scalars in the buffer to be emitted before we handle
// normalizing this new segment.
if current.normData.ccc == 0 {
composee = current.scalar
return currentComposee
}

_internalInvariant(current.normData.ccc == lastBufferedNormData.ccc)
buffer.append(current)
continue
}

// There were no blockers! Attempt to compose the two! (Again, if our rhs
// scalar IS NFC_QC, then it can never compose with anything previous to
// it).
guard !current.normData.isNFCQC,
let composed = compose(currentComposee, and: current.scalar) else {
// No composition found. Stick it at the end of the buffer with the rest
// of non-composed scalars.

buffer.append(current)
continue
}

// They composed! Assign the composition as our new composee and iterate
// to the next scalar.
composee = composed
}

// If we have a leftover composee, make sure to return it.
return composee._take()
}
}

extension Unicode._NFC: Sequence {
internal func makeIterator() -> Iterator {
Iterator(iterator: base._nfd.makeIterator())
}
}

extension StringProtocol {
internal var _nfc: Unicode._NFC<Self> {
Unicode._NFC(base: self)
}
}
Loading