Skip to content

Commit 3d68242

Browse files
authored
Merge pull request #9554 from apple/stringprotocol-4
Implement SE-0163
2 parents 06c1295 + 886c980 commit 3d68242

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2239
-1388
lines changed

stdlib/private/StdlibUnicodeUnittest/Collation.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import StdlibUnittest
1414

1515
struct CollationTableEntry {
16-
let scalars: [UnicodeScalar]
16+
let scalars: [Unicode.Scalar]
1717
let collationElements: [UInt64]
1818
let comment: String
1919

@@ -22,7 +22,7 @@ struct CollationTableEntry {
2222
_ collationElements: [UInt64],
2323
_ comment: String
2424
) {
25-
self.scalars = scalars.map { UnicodeScalar($0)! }
25+
self.scalars = scalars.map { Unicode.Scalar($0)! }
2626
self.collationElements = collationElements
2727
self.comment = comment
2828
}
@@ -212,9 +212,9 @@ extension HashableArray : ExpressibleByArrayLiteral {
212212
}
213213
}
214214

215-
let ducetExtract: [HashableArray<UnicodeScalar> : CollationTableEntry] = {
215+
let ducetExtract: [HashableArray<Unicode.Scalar> : CollationTableEntry] = {
216216
() in
217-
var result: [HashableArray<UnicodeScalar> : CollationTableEntry] = [:]
217+
var result: [HashableArray<Unicode.Scalar> : CollationTableEntry] = [:]
218218
for entry in ducetExtractData {
219219
result[HashableArray(entry.scalars)] = entry
220220
}
@@ -232,7 +232,7 @@ extension String {
232232
internal var _collationElements: [UInt64] {
233233
var result: [UInt64] = []
234234
for us in self.unicodeScalars {
235-
let scalars: HashableArray<UnicodeScalar> = [us]
235+
let scalars: HashableArray<Unicode.Scalar> = [us]
236236
let collationElements = ducetExtract[scalars]!.collationElements
237237
if collationElements[0] != 0 {
238238
result += collationElements

stdlib/private/StdlibUnicodeUnittest/StdlibUnicodeUnittest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public struct UTFTest {
2727
public let string: String
2828
public let utf8: [UInt8]
2929
public let utf16: [UInt16]
30-
public let unicodeScalars: [UnicodeScalar]
31-
public let unicodeScalarsRepairedTail: [UnicodeScalar]
30+
public let unicodeScalars: [Unicode.Scalar]
31+
public let unicodeScalarsRepairedTail: [Unicode.Scalar]
3232
public let flags: Flags
3333
public let loc: SourceLoc
3434

@@ -52,9 +52,9 @@ public struct UTFTest {
5252
self.string = string
5353
self.utf8 = utf8
5454
self.utf16 = utf16
55-
self.unicodeScalars = scalars.map { UnicodeScalar($0)! }
55+
self.unicodeScalars = scalars.map { Unicode.Scalar($0)! }
5656
self.unicodeScalarsRepairedTail =
57-
scalarsRepairedTail.map { UnicodeScalar($0)! }
57+
scalarsRepairedTail.map { Unicode.Scalar($0)! }
5858
self.flags = flags
5959
self.loc = SourceLoc(file, line, comment: "test data")
6060
}

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func _stdlib_getline() -> String? {
589589
}
590590
return String._fromWellFormedCodeUnitSequence(UTF8.self, input: result)
591591
}
592-
if c == CInt(UnicodeScalar("\n").value) {
592+
if c == CInt(Unicode.Scalar("\n").value) {
593593
return String._fromWellFormedCodeUnitSequence(UTF8.self, input: result)
594594
}
595595
result.append(UInt8(c))

stdlib/private/SwiftPrivate/IO.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct _FDInputStream {
2525

2626
public mutating func getline() -> String? {
2727
if let newlineIndex =
28-
_buffer[0..<_bufferUsed].index(of: UInt8(UnicodeScalar("\n").value)) {
28+
_buffer[0..<_bufferUsed].index(of: UInt8(Unicode.Scalar("\n").value)) {
2929
let result = String._fromWellFormedCodeUnitSequence(
3030
UTF8.self, input: _buffer[0..<newlineIndex])
3131
_buffer.removeSubrange(0...newlineIndex)

stdlib/public/SDK/Foundation/CharacterSet.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import CoreFoundation
1515
import _SwiftCoreFoundationOverlayShims
1616

17-
private func _utfRangeToCFRange(_ inRange : Range<UnicodeScalar>) -> CFRange {
17+
private func _utfRangeToCFRange(_ inRange : Range<Unicode.Scalar>) -> CFRange {
1818
return CFRange(
1919
location: Int(inRange.lowerBound.value),
2020
length: Int(inRange.upperBound.value - inRange.lowerBound.value))
2121
}
2222

23-
private func _utfRangeToCFRange(_ inRange : ClosedRange<UnicodeScalar>) -> CFRange {
23+
private func _utfRangeToCFRange(_ inRange : ClosedRange<Unicode.Scalar>) -> CFRange {
2424
return CFRange(
2525
location: Int(inRange.lowerBound.value),
2626
length: Int(inRange.upperBound.value - inRange.lowerBound.value + 1))
@@ -104,7 +104,7 @@ fileprivate final class _CharacterSetStorage : Hashable {
104104

105105
// MARK: Mutable functions
106106

107-
fileprivate func insert(charactersIn range: Range<UnicodeScalar>) {
107+
fileprivate func insert(charactersIn range: Range<Unicode.Scalar>) {
108108
switch _backing {
109109
case .immutable(let cs):
110110
let r = CFCharacterSetCreateMutableCopy(nil, cs)!
@@ -115,7 +115,7 @@ fileprivate final class _CharacterSetStorage : Hashable {
115115
}
116116
}
117117

118-
fileprivate func insert(charactersIn range: ClosedRange<UnicodeScalar>) {
118+
fileprivate func insert(charactersIn range: ClosedRange<Unicode.Scalar>) {
119119
switch _backing {
120120
case .immutable(let cs):
121121
let r = CFCharacterSetCreateMutableCopy(nil, cs)!
@@ -126,7 +126,7 @@ fileprivate final class _CharacterSetStorage : Hashable {
126126
}
127127
}
128128

129-
fileprivate func remove(charactersIn range: Range<UnicodeScalar>) {
129+
fileprivate func remove(charactersIn range: Range<Unicode.Scalar>) {
130130
switch _backing {
131131
case .immutable(let cs):
132132
let r = CFCharacterSetCreateMutableCopy(nil, cs)!
@@ -137,7 +137,7 @@ fileprivate final class _CharacterSetStorage : Hashable {
137137
}
138138
}
139139

140-
fileprivate func remove(charactersIn range: ClosedRange<UnicodeScalar>) {
140+
fileprivate func remove(charactersIn range: ClosedRange<Unicode.Scalar>) {
141141
switch _backing {
142142
case .immutable(let cs):
143143
let r = CFCharacterSetCreateMutableCopy(nil, cs)!
@@ -186,28 +186,28 @@ fileprivate final class _CharacterSetStorage : Hashable {
186186
// MARK: SetAlgebraType
187187

188188
@discardableResult
189-
fileprivate func insert(_ character: UnicodeScalar) -> (inserted: Bool, memberAfterInsert: UnicodeScalar) {
190-
insert(charactersIn: character..<UnicodeScalar(character.value + 1)!)
189+
fileprivate func insert(_ character: Unicode.Scalar) -> (inserted: Bool, memberAfterInsert: Unicode.Scalar) {
190+
insert(charactersIn: character..<Unicode.Scalar(character.value + 1)!)
191191
// TODO: This should probably return the truth, but figuring it out requires two calls into NSCharacterSet
192192
return (true, character)
193193
}
194194

195195
@discardableResult
196-
fileprivate func update(with character: UnicodeScalar) -> UnicodeScalar? {
196+
fileprivate func update(with character: Unicode.Scalar) -> Unicode.Scalar? {
197197
insert(character)
198198
// TODO: This should probably return the truth, but figuring it out requires two calls into NSCharacterSet
199199
return character
200200
}
201201

202202
@discardableResult
203-
fileprivate func remove(_ character: UnicodeScalar) -> UnicodeScalar? {
203+
fileprivate func remove(_ character: Unicode.Scalar) -> Unicode.Scalar? {
204204
// TODO: Add method to CFCharacterSet to do this in one call
205-
let result : UnicodeScalar? = contains(character) ? character : nil
206-
remove(charactersIn: character..<UnicodeScalar(character.value + 1)!)
205+
let result : Unicode.Scalar? = contains(character) ? character : nil
206+
remove(charactersIn: character..<Unicode.Scalar(character.value + 1)!)
207207
return result
208208
}
209209

210-
fileprivate func contains(_ member: UnicodeScalar) -> Bool {
210+
fileprivate func contains(_ member: Unicode.Scalar) -> Bool {
211211
switch _backing {
212212
case .immutable(let cs):
213213
return CFCharacterSetIsLongCharacterMember(cs, member.value)
@@ -374,15 +374,15 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
374374

375375
/// Initialize with a range of integers.
376376
///
377-
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
378-
public init(charactersIn range: Range<UnicodeScalar>) {
377+
/// It is the caller's responsibility to ensure that the values represent valid `Unicode.Scalar` values, if that is what is desired.
378+
public init(charactersIn range: Range<Unicode.Scalar>) {
379379
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetCreateWithCharactersInRange(nil, _utfRangeToCFRange(range)))
380380
}
381381

382382
/// Initialize with a closed range of integers.
383383
///
384-
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
385-
public init(charactersIn range: ClosedRange<UnicodeScalar>) {
384+
/// It is the caller's responsibility to ensure that the values represent valid `Unicode.Scalar` values, if that is what is desired.
385+
public init(charactersIn range: ClosedRange<Unicode.Scalar>) {
386386
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetCreateWithCharactersInRange(nil, _utfRangeToCFRange(range)))
387387
}
388388

@@ -569,8 +569,8 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
569569

570570
/// Insert a range of integer values in the `CharacterSet`.
571571
///
572-
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
573-
public mutating func insert(charactersIn range: Range<UnicodeScalar>) {
572+
/// It is the caller's responsibility to ensure that the values represent valid `Unicode.Scalar` values, if that is what is desired.
573+
public mutating func insert(charactersIn range: Range<Unicode.Scalar>) {
574574
if !isKnownUniquelyReferenced(&_storage) {
575575
_storage = _storage.mutableCopy()
576576
}
@@ -579,24 +579,24 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
579579

580580
/// Insert a closed range of integer values in the `CharacterSet`.
581581
///
582-
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
583-
public mutating func insert(charactersIn range: ClosedRange<UnicodeScalar>) {
582+
/// It is the caller's responsibility to ensure that the values represent valid `Unicode.Scalar` values, if that is what is desired.
583+
public mutating func insert(charactersIn range: ClosedRange<Unicode.Scalar>) {
584584
if !isKnownUniquelyReferenced(&_storage) {
585585
_storage = _storage.mutableCopy()
586586
}
587587
_storage.insert(charactersIn: range)
588588
}
589589

590590
/// Remove a range of integer values from the `CharacterSet`.
591-
public mutating func remove(charactersIn range: Range<UnicodeScalar>) {
591+
public mutating func remove(charactersIn range: Range<Unicode.Scalar>) {
592592
if !isKnownUniquelyReferenced(&_storage) {
593593
_storage = _storage.mutableCopy()
594594
}
595595
_storage.remove(charactersIn: range)
596596
}
597597

598598
/// Remove a closed range of integer values from the `CharacterSet`.
599-
public mutating func remove(charactersIn range: ClosedRange<UnicodeScalar>) {
599+
public mutating func remove(charactersIn range: ClosedRange<Unicode.Scalar>) {
600600
if !isKnownUniquelyReferenced(&_storage) {
601601
_storage = _storage.mutableCopy()
602602
}
@@ -631,42 +631,42 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
631631
// MARK: -
632632
// MARK: SetAlgebraType
633633

634-
/// Insert a `UnicodeScalar` representation of a character into the `CharacterSet`.
634+
/// Insert a `Unicode.Scalar` representation of a character into the `CharacterSet`.
635635
///
636-
/// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`.
636+
/// `Unicode.Scalar` values are available on `Swift.String.UnicodeScalarView`.
637637
@discardableResult
638-
public mutating func insert(_ character: UnicodeScalar) -> (inserted: Bool, memberAfterInsert: UnicodeScalar) {
638+
public mutating func insert(_ character: Unicode.Scalar) -> (inserted: Bool, memberAfterInsert: Unicode.Scalar) {
639639
if !isKnownUniquelyReferenced(&_storage) {
640640
_storage = _storage.mutableCopy()
641641
}
642642
return _storage.insert(character)
643643
}
644644

645-
/// Insert a `UnicodeScalar` representation of a character into the `CharacterSet`.
645+
/// Insert a `Unicode.Scalar` representation of a character into the `CharacterSet`.
646646
///
647-
/// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`.
647+
/// `Unicode.Scalar` values are available on `Swift.String.UnicodeScalarView`.
648648
@discardableResult
649-
public mutating func update(with character: UnicodeScalar) -> UnicodeScalar? {
649+
public mutating func update(with character: Unicode.Scalar) -> Unicode.Scalar? {
650650
if !isKnownUniquelyReferenced(&_storage) {
651651
_storage = _storage.mutableCopy()
652652
}
653653
return _storage.update(with: character)
654654
}
655655

656656

657-
/// Remove a `UnicodeScalar` representation of a character from the `CharacterSet`.
657+
/// Remove a `Unicode.Scalar` representation of a character from the `CharacterSet`.
658658
///
659-
/// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`.
659+
/// `Unicode.Scalar` values are available on `Swift.String.UnicodeScalarView`.
660660
@discardableResult
661-
public mutating func remove(_ character: UnicodeScalar) -> UnicodeScalar? {
661+
public mutating func remove(_ character: Unicode.Scalar) -> Unicode.Scalar? {
662662
if !isKnownUniquelyReferenced(&_storage) {
663663
_storage = _storage.mutableCopy()
664664
}
665665
return _storage.remove(character)
666666
}
667667

668-
/// Test for membership of a particular `UnicodeScalar` in the `CharacterSet`.
669-
public func contains(_ member: UnicodeScalar) -> Bool {
668+
/// Test for membership of a particular `Unicode.Scalar` in the `CharacterSet`.
669+
public func contains(_ member: Unicode.Scalar) -> Bool {
670670
return _storage.contains(member)
671671
}
672672

stdlib/public/SwiftOnoneSupport/SwiftOnoneSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct _Prespecialize {
147147
// Force pre-specialization of arrays with elements of different
148148
// character and unicode scalar types.
149149
_createArrayUser("a" as Character)
150-
_createArrayUser("a" as UnicodeScalar)
150+
_createArrayUser("a" as Unicode.Scalar)
151151
_createArrayUserWithoutSorting("a".utf8)
152152
_createArrayUserWithoutSorting("a".utf16)
153153
_createArrayUserWithoutSorting("a".unicodeScalars)

stdlib/public/core/ASCII.swift

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===--- ASCII.swift ------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
extension Unicode {
13+
@_fixed_layout
14+
public enum ASCII {}
15+
}
16+
17+
extension Unicode.ASCII : Unicode.Encoding {
18+
public typealias CodeUnit = UInt8
19+
public typealias EncodedScalar = CollectionOfOne<CodeUnit>
20+
21+
public static var encodedReplacementCharacter : EncodedScalar {
22+
return EncodedScalar(0x1a) // U+001A SUBSTITUTE; best we can do for ASCII
23+
}
24+
25+
@inline(__always)
26+
@_inlineable
27+
public static func _isScalar(_ x: CodeUnit) -> Bool {
28+
return true
29+
}
30+
31+
@inline(__always)
32+
@_inlineable
33+
public static func decode(_ source: EncodedScalar) -> Unicode.Scalar {
34+
return Unicode.Scalar(_unchecked: UInt32(
35+
source.first._unsafelyUnwrappedUnchecked))
36+
}
37+
38+
@inline(__always)
39+
@_inlineable
40+
public static func encode(
41+
_ source: Unicode.Scalar
42+
) -> EncodedScalar? {
43+
guard source.value < (1&<<7) else { return nil }
44+
return EncodedScalar(UInt8(extendingOrTruncating: source.value))
45+
}
46+
47+
@inline(__always)
48+
public static func transcode<FromEncoding : Unicode.Encoding>(
49+
_ content: FromEncoding.EncodedScalar, from _: FromEncoding.Type
50+
) -> EncodedScalar? {
51+
if _fastPath(FromEncoding.self == UTF16.self) {
52+
let c = unsafeBitCast(content, to: UTF16.EncodedScalar.self)
53+
guard (c._storage & 0xFF80 == 0) else { return nil }
54+
return EncodedScalar(CodeUnit(c._storage & 0x7f))
55+
}
56+
else if _fastPath(FromEncoding.self == UTF8.self) {
57+
let c = unsafeBitCast(content, to: UTF8.EncodedScalar.self)
58+
guard (c._storage & 0x80 == 0) else { return nil }
59+
return EncodedScalar(CodeUnit(c._storage & 0x7f))
60+
}
61+
return encode(FromEncoding.decode(content))
62+
}
63+
64+
public struct Parser {
65+
public init() { }
66+
}
67+
68+
public typealias ForwardParser = Parser
69+
public typealias ReverseParser = Parser
70+
}
71+
72+
extension Unicode.ASCII.Parser : Unicode.Parser {
73+
public typealias Encoding = Unicode.ASCII
74+
75+
/// Parses a single Unicode scalar value from `input`.
76+
public mutating func parseScalar<I : IteratorProtocol>(
77+
from input: inout I
78+
) -> Unicode.ParseResult<Encoding.EncodedScalar>
79+
where I.Element == Encoding.CodeUnit {
80+
let n = input.next()
81+
if _fastPath(n != nil), let x = n {
82+
guard _fastPath(Int8(extendingOrTruncating: x) >= 0)
83+
else { return .error(length: 1) }
84+
return .valid(Unicode.ASCII.EncodedScalar(x))
85+
}
86+
return .emptyInput
87+
}
88+
}

0 commit comments

Comments
 (0)