Skip to content

Commit 0eec075

Browse files
authored
Merge pull request #42124 from Azoy/spi-unicode (#59108)
[stdlib] Mark NFD and NFC as SPI
1 parent b9f4ae0 commit 0eec075

File tree

7 files changed

+147
-20
lines changed

7 files changed

+147
-20
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ set(SWIFTLIB_ESSENTIAL
183183
UnicodeParser.swift
184184
UnicodeScalarProperties.swift
185185
CharacterProperties.swift # ORDER DEPENDENCY: UnicodeScalarProperties.swift
186+
UnicodeSPI.swift
186187
Unmanaged.swift
187188
UnmanagedOpaqueString.swift
188189
UnmanagedString.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"UnicodeParser.swift",
5151
"UnicodeScalar.swift",
5252
"UnicodeScalarProperties.swift",
53+
"UnicodeSPI.swift",
5354
"UnavailableStringAPIs.swift",
5455
"UnmanagedOpaqueString.swift",
5556
"UnmanagedString.swift",

stdlib/public/core/NFC.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import SwiftShims
1414

1515
extension Unicode {
16-
internal struct _NFC<S: StringProtocol> {
16+
internal struct _InternalNFC<S: StringProtocol> {
1717
let base: S
1818
}
1919
}
2020

21-
extension Unicode._NFC {
21+
extension Unicode._InternalNFC {
2222
internal struct Iterator {
2323
var buffer = Unicode._NormDataBuffer()
2424

@@ -30,11 +30,11 @@ extension Unicode._NFC {
3030
// we continue to try and compose following scalars with this composee.
3131
var composee: Unicode.Scalar? = nil
3232

33-
var iterator: Unicode._NFD<S>.Iterator
33+
var iterator: Unicode._InternalNFD<S>.Iterator
3434
}
3535
}
3636

37-
extension Unicode._NFC.Iterator: IteratorProtocol {
37+
extension Unicode._InternalNFC.Iterator: IteratorProtocol {
3838
internal func compose(
3939
_ x: Unicode.Scalar,
4040
and y: Unicode.Scalar
@@ -210,14 +210,14 @@ extension Unicode._NFC.Iterator: IteratorProtocol {
210210
}
211211
}
212212

213-
extension Unicode._NFC: Sequence {
213+
extension Unicode._InternalNFC: Sequence {
214214
internal func makeIterator() -> Iterator {
215-
Iterator(iterator: base._nfd.makeIterator())
215+
Iterator(iterator: base._internalNFD.makeIterator())
216216
}
217217
}
218218

219219
extension StringProtocol {
220-
internal var _nfc: Unicode._NFC<Self> {
221-
Unicode._NFC(base: self)
220+
internal var _internalNFC: Unicode._InternalNFC<Self> {
221+
Unicode._InternalNFC(base: self)
222222
}
223223
}

stdlib/public/core/NFD.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
extension Unicode {
14-
internal struct _NFD<S: StringProtocol> {
15-
let base: S
14+
internal struct _InternalNFD<S: StringProtocol> {
15+
let base: S.UnicodeScalarView
1616
}
1717
}
1818

19-
extension Unicode._NFD {
19+
extension Unicode._InternalNFD {
2020
internal struct Iterator {
2121
var buffer = Unicode._NormDataBuffer()
2222

@@ -28,7 +28,7 @@ extension Unicode._NFD {
2828
}
2929
}
3030

31-
extension Unicode._NFD.Iterator: IteratorProtocol {
31+
extension Unicode._InternalNFD.Iterator: IteratorProtocol {
3232
internal mutating func decompose(
3333
_ scalar: Unicode.Scalar,
3434
with normData: Unicode._NormData
@@ -165,17 +165,17 @@ extension Unicode._NFD.Iterator: IteratorProtocol {
165165
}
166166
}
167167

168-
extension Unicode._NFD: Sequence {
168+
extension Unicode._InternalNFD: Sequence {
169169
internal func makeIterator() -> Iterator {
170170
Iterator(
171-
index: base.unicodeScalars.startIndex,
172-
unicodeScalars: base.unicodeScalars
171+
index: base.startIndex,
172+
unicodeScalars: base
173173
)
174174
}
175175
}
176176

177177
extension StringProtocol {
178-
internal var _nfd: Unicode._NFD<Self> {
179-
Unicode._NFD(base: self)
178+
internal var _internalNFD: Unicode._InternalNFD<Self> {
179+
Unicode._InternalNFD(base: unicodeScalars)
180180
}
181181
}

stdlib/public/core/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ extension _StringGutsSlice {
988988
}
989989
}
990990

991-
for scalar in substring._nfc {
991+
for scalar in substring._internalNFC {
992992
try scalar.withUTF8CodeUnits {
993993
for byte in $0 {
994994
try f(byte)

stdlib/public/core/StringComparison.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ extension _StringGutsSlice {
333333
with other: _StringGutsSlice,
334334
expecting: _StringComparisonResult
335335
) -> Bool {
336-
var iter1 = Substring(self)._nfc.makeIterator()
337-
var iter2 = Substring(other)._nfc.makeIterator()
336+
var iter1 = Substring(self)._internalNFC.makeIterator()
337+
var iter2 = Substring(other)._internalNFC.makeIterator()
338338

339339
var scalar1: Unicode.Scalar? = nil
340340
var scalar2: Unicode.Scalar? = nil

stdlib/public/core/UnicodeSPI.swift

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
//===----------------------------------------------------------------------===//
14+
// Unicode.NFD
15+
//===----------------------------------------------------------------------===//
16+
17+
extension Unicode {
18+
@_spi(_Unicode)
19+
@available(SwiftStdlib 5.7, *)
20+
public struct _NFD {
21+
let base: Substring.UnicodeScalarView
22+
}
23+
}
24+
25+
@available(SwiftStdlib 5.7, *)
26+
extension Unicode._NFD {
27+
@_spi(_Unicode)
28+
@available(SwiftStdlib 5.7, *)
29+
public struct Iterator {
30+
var base: Unicode._InternalNFD<Substring>.Iterator
31+
}
32+
}
33+
34+
@available(SwiftStdlib 5.7, *)
35+
extension Unicode._NFD.Iterator: IteratorProtocol {
36+
@_spi(_Unicode)
37+
@available(SwiftStdlib 5.7, *)
38+
public mutating func next() -> Unicode.Scalar? {
39+
base.next()?.scalar
40+
}
41+
}
42+
43+
@available(SwiftStdlib 5.7, *)
44+
extension Unicode._NFD: Sequence {
45+
@_spi(_Unicode)
46+
@available(SwiftStdlib 5.7, *)
47+
public func makeIterator() -> Iterator {
48+
Iterator(base: Unicode._InternalNFD(base: base).makeIterator())
49+
}
50+
}
51+
52+
extension String {
53+
@_spi(_Unicode)
54+
@available(SwiftStdlib 5.7, *)
55+
public var _nfd: Unicode._NFD {
56+
Unicode._NFD(base: self[...].unicodeScalars)
57+
}
58+
}
59+
60+
extension Substring {
61+
@_spi(_Unicode)
62+
@available(SwiftStdlib 5.7, *)
63+
public var _nfd: Unicode._NFD {
64+
Unicode._NFD(base: unicodeScalars)
65+
}
66+
}
67+
68+
//===----------------------------------------------------------------------===//
69+
// Unicode.NFC
70+
//===----------------------------------------------------------------------===//
71+
72+
extension Unicode {
73+
@_spi(_Unicode)
74+
@available(SwiftStdlib 5.7, *)
75+
public struct _NFC {
76+
let base: Substring.UnicodeScalarView
77+
}
78+
}
79+
80+
@available(SwiftStdlib 5.7, *)
81+
extension Unicode._NFC {
82+
@_spi(_Unicode)
83+
@available(SwiftStdlib 5.7, *)
84+
public struct Iterator {
85+
var base: Unicode._InternalNFC<Substring>.Iterator
86+
}
87+
}
88+
89+
@available(SwiftStdlib 5.7, *)
90+
extension Unicode._NFC.Iterator: IteratorProtocol {
91+
@_spi(_Unicode)
92+
@available(SwiftStdlib 5.7, *)
93+
public mutating func next() -> Unicode.Scalar? {
94+
base.next()
95+
}
96+
}
97+
98+
@available(SwiftStdlib 5.7, *)
99+
extension Unicode._NFC: Sequence {
100+
@_spi(_Unicode)
101+
@available(SwiftStdlib 5.7, *)
102+
public func makeIterator() -> Iterator {
103+
Iterator(
104+
base: Unicode._InternalNFC<Substring>.Iterator(
105+
iterator: Unicode._InternalNFD<Substring>(base: base).makeIterator()
106+
)
107+
)
108+
}
109+
}
110+
111+
extension String {
112+
@_spi(_Unicode)
113+
@available(SwiftStdlib 5.7, *)
114+
public var _nfc: Unicode._NFC {
115+
Unicode._NFC(base: self[...].unicodeScalars)
116+
}
117+
}
118+
119+
extension Substring {
120+
@_spi(_Unicode)
121+
@available(SwiftStdlib 5.7, *)
122+
public var _nfc: Unicode._NFC {
123+
Unicode._NFC(base: unicodeScalars)
124+
}
125+
}

0 commit comments

Comments
 (0)