Skip to content

Commit c9115e1

Browse files
committed
Create wrapper types with availability NFD and NFC
Swap the bases to unicodeScalarView
1 parent eacab52 commit c9115e1

File tree

9 files changed

+155
-47
lines changed

9 files changed

+155
-47
lines changed

stdlib/public/core/CMakeLists.txt

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

stdlib/public/core/GroupInfo.json

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

stdlib/public/core/NFC.swift

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

1515
extension Unicode {
16-
@_spi(_Unicode)
17-
public struct _NFC<S: StringProtocol> {
16+
internal struct _InternalNFC<S: StringProtocol> {
1817
let base: S
1918
}
2019
}
2120

22-
extension Unicode._NFC {
23-
@_spi(_Unicode)
24-
public struct Iterator {
21+
extension Unicode._InternalNFC {
22+
internal struct Iterator {
2523
var buffer = Unicode._NormDataBuffer()
2624

2725
// This is our starter that is currently being composed with other scalars
@@ -32,11 +30,11 @@ extension Unicode._NFC {
3230
// we continue to try and compose following scalars with this composee.
3331
var composee: Unicode.Scalar? = nil
3432

35-
var iterator: Unicode._NFD<S>.Iterator
33+
var iterator: Unicode._InternalNFD<S>.Iterator
3634
}
3735
}
3836

39-
extension Unicode._NFC.Iterator: IteratorProtocol {
37+
extension Unicode._InternalNFC.Iterator: IteratorProtocol {
4038
internal func compose(
4139
_ x: Unicode.Scalar,
4240
and y: Unicode.Scalar
@@ -100,8 +98,7 @@ extension Unicode._NFC.Iterator: IteratorProtocol {
10098
}
10199
}
102100

103-
@_spi(_Unicode)
104-
public mutating func next() -> Unicode.Scalar? {
101+
internal mutating func next() -> Unicode.Scalar? {
105102
// Empty out our buffer before attempting to compose anything with our new
106103
// composee.
107104
if let nextBuffered = buffer.next() {
@@ -213,16 +210,14 @@ extension Unicode._NFC.Iterator: IteratorProtocol {
213210
}
214211
}
215212

216-
extension Unicode._NFC: Sequence {
217-
@_spi(_Unicode)
218-
public func makeIterator() -> Iterator {
219-
Iterator(iterator: base._nfd.makeIterator())
213+
extension Unicode._InternalNFC: Sequence {
214+
internal func makeIterator() -> Iterator {
215+
Iterator(iterator: base._internalNFD.makeIterator())
220216
}
221217
}
222218

223219
extension StringProtocol {
224-
@_spi(_Unicode)
225-
public var _nfc: Unicode._NFC<Self> {
226-
Unicode._NFC(base: self)
220+
internal var _internalNFC: Unicode._InternalNFC<Self> {
221+
Unicode._InternalNFC(base: self)
227222
}
228223
}

stdlib/public/core/NFD.swift

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

1313
extension Unicode {
14-
@_spi(_Unicode)
15-
public struct _NFD<S: StringProtocol> {
16-
let base: S
14+
internal struct _InternalNFD<S: StringProtocol> {
15+
let base: S.UnicodeScalarView
1716
}
1817
}
1918

20-
extension Unicode._NFD {
21-
@_spi(_Unicode)
22-
public struct Iterator {
19+
extension Unicode._InternalNFD {
20+
internal struct Iterator {
2321
var buffer = Unicode._NormDataBuffer()
2422

2523
// This index always points at the next starter of a normalization segment.
@@ -30,7 +28,7 @@ extension Unicode._NFD {
3028
}
3129
}
3230

33-
extension Unicode._NFD.Iterator: IteratorProtocol {
31+
extension Unicode._InternalNFD.Iterator: IteratorProtocol {
3432
internal mutating func decompose(
3533
_ scalar: Unicode.Scalar,
3634
with normData: Unicode._NormData
@@ -126,8 +124,7 @@ extension Unicode._NFD.Iterator: IteratorProtocol {
126124
}
127125
}
128126

129-
@_spi(_Unicode)
130-
public mutating func next() -> ScalarAndNormData? {
127+
internal mutating func next() -> ScalarAndNormData? {
131128
// Empty out our buffer before attempting to decompose the next
132129
// normalization segment.
133130
if let nextBuffered = buffer.next() {
@@ -168,19 +165,17 @@ extension Unicode._NFD.Iterator: IteratorProtocol {
168165
}
169166
}
170167

171-
extension Unicode._NFD: Sequence {
172-
@_spi(_Unicode)
173-
public func makeIterator() -> Iterator {
168+
extension Unicode._InternalNFD: Sequence {
169+
internal func makeIterator() -> Iterator {
174170
Iterator(
175-
index: base.unicodeScalars.startIndex,
176-
unicodeScalars: base.unicodeScalars
171+
index: base.startIndex,
172+
unicodeScalars: base
177173
)
178174
}
179175
}
180176

181177
extension StringProtocol {
182-
@_spi(_Unicode)
183-
public var _nfd: Unicode._NFD<Self> {
184-
Unicode._NFD(base: self)
178+
internal var _internalNFD: Unicode._InternalNFD<Self> {
179+
Unicode._InternalNFD(base: unicodeScalars)
185180
}
186181
}

stdlib/public/core/String.swift

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

990-
for scalar in String(_guts)._nfc {
990+
for scalar in String(_guts)._internalNFC {
991991
try scalar.withUTF8CodeUnits {
992992
for byte in $0 {
993993
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/UnicodeData.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
import SwiftShims
1414

15-
@_spi(_Unicode)
16-
public typealias ScalarAndNormData = (
15+
internal typealias ScalarAndNormData = (
1716
scalar: Unicode.Scalar,
1817
normData: Unicode._NormData
1918
)
@@ -46,8 +45,7 @@ extension Unicode {
4645
// a scalar with a CCC value of 100, unless there are normalization
4746
// boundaries between them.
4847
//
49-
@_spi(_Unicode)
50-
public struct _NormData {
48+
internal struct _NormData {
5149
var rawValue: UInt16
5250

5351
var ccc: UInt8 {

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+
}

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,4 @@ Func UnsafeMutableBufferPointer.withMemoryRebound(to:_:) has been removed
9090
Func UnsafeMutablePointer.withMemoryRebound(to:capacity:_:) has been removed
9191
Func UnsafePointer.withMemoryRebound(to:capacity:_:) has been removed
9292

93-
// These APIs have been marked @_spi(_Unicode) and SPI is not part of our ABI.
94-
Struct Unicode._NFC is a new API without @available attribute
95-
Struct Unicode._NFD is a new API without @available attribute
96-
Struct Unicode._NormData is a new API without @available attribute
97-
Var StringProtocol._nfc is a new API without @available attribute
98-
Var StringProtocol._nfd is a new API without @available attribute
99-
10093
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

0 commit comments

Comments
 (0)