Skip to content

Commit a578ee3

Browse files
Dave Abrahamsairspeedswift
authored andcommitted
String Index Interchange, etc. (Changes for SE-0180 => swift-4.0-branch) (#10852)
* [stdlib] Index interchange, part I * [stdlib] String index interchange, part II (UTF16) * [stdlib] String index interchange, part III (UTF8) * [stdlib] Bring back some SPI that Foundation-on-linux uses. * [stdlib] Minimal docs for the new string index * [stdlib] UTF8View Iterator and count specialization * [stdlib] Foundation SPI fixup * [stdlib] Fix a test: String.UTF8View has its own Iterator now * [stdlib] Small correction to UTF8 Iterator This works either way I suppose, but the code is clearer now. * [stdlib] UTF8View: count by iterating The fancy code didn't turn out to be any faster. * [stdlib] Speed String.UTF8View.count even more * [stdlib] Rebuild String.Index for UTF8View * [stdlib] Drop a couple bogus duplicate tests Only the last of each set of dups gets run anyway. See https://bugs.swift.org/browse/SR-5362 * [stdlib] Extend String index conversion tests for interchange test the new APIs as well as the legacy failing conversions. * [stdlib] Repair some doc comments Drive-by fix while cherry-picking.
1 parent dd1614c commit a578ee3

27 files changed

+917
-1415
lines changed

stdlib/public/SDK/Foundation/ExtraStringAPIs.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// Random access for String.UTF16View, only when Foundation is
14-
// imported. Making this API dependent on Foundation decouples the
15-
// Swift core from a UTF16 representation.
16-
extension String.UTF16View.Index : Strideable {
13+
extension String.UTF16View.Index {
1714
/// Construct from an integer offset.
15+
@available(swift, deprecated: 3.2)
16+
@available(swift, obsoleted: 4.0)
1817
public init(_ offset: Int) {
1918
_precondition(offset >= 0, "Negative UTF16 index offset not allowed")
2019
self.init(_offset: offset)
2120
}
2221

22+
@available(swift, deprecated: 3.2)
23+
@available(swift, obsoleted: 4.0)
2324
public func distance(to other: String.UTF16View.Index) -> Int {
2425
return _offset.distance(to: other._offset)
2526
}
2627

28+
@available(swift, deprecated: 3.2)
29+
@available(swift, obsoleted: 4.0)
2730
public func advanced(by n: Int) -> String.UTF16View.Index {
2831
return String.UTF16View.Index(_offset.advanced(by: n))
2932
}
3033
}
31-
32-
extension String.UTF16View : RandomAccessCollection {}
33-
extension String.UTF16View.Indices : RandomAccessCollection {}
34-

stdlib/public/SDK/Foundation/NSStringAPI.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func _toNSArray<T, U : AnyObject>(_ a: [T], f: (T) -> U) -> NSArray {
3232

3333
func _toNSRange(_ r: Range<String.Index>) -> NSRange {
3434
return NSRange(
35-
location: r.lowerBound._utf16Index,
36-
length: r.upperBound._utf16Index - r.lowerBound._utf16Index)
35+
location: r.lowerBound.encodedOffset,
36+
length: r.upperBound.encodedOffset - r.lowerBound.encodedOffset)
3737
}
3838

3939
// We only need this for UnsafeMutablePointer, but there's not currently a way
@@ -72,10 +72,7 @@ extension String {
7272
/// Return an `Index` corresponding to the given offset in our UTF-16
7373
/// representation.
7474
func _index(_ utf16Index: Int) -> Index {
75-
return Index(
76-
_base: String.UnicodeScalarView.Index(_position: utf16Index),
77-
in: characters
78-
)
75+
return Index(encodedOffset: utf16Index)
7976
}
8077

8178
/// Return a `Range<Index>` corresponding to the given `NSRange` of
@@ -1260,7 +1257,7 @@ extension String {
12601257
public
12611258
func rangeOfComposedCharacterSequence(at anIndex: Index) -> Range<Index> {
12621259
return _range(
1263-
_ns.rangeOfComposedCharacterSequence(at: anIndex._utf16Index))
1260+
_ns.rangeOfComposedCharacterSequence(at: anIndex.encodedOffset))
12641261
}
12651262

12661263
// - (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range
@@ -1610,15 +1607,15 @@ extension String {
16101607
/// Returns a new string containing the characters of the
16111608
/// `String` from the one at a given index to the end.
16121609
public func substring(from index: Index) -> String {
1613-
return _ns.substring(from: index._utf16Index)
1610+
return _ns.substring(from: index.encodedOffset)
16141611
}
16151612

16161613
// - (NSString *)substringToIndex:(NSUInteger)anIndex
16171614

16181615
/// Returns a new string containing the characters of the
16191616
/// `String` up to, but not including, the one at a given index.
16201617
public func substring(to index: Index) -> String {
1621-
return _ns.substring(to: index._utf16Index)
1618+
return _ns.substring(to: index.encodedOffset)
16221619
}
16231620

16241621
// - (NSString *)substringWithRange:(NSRange)aRange

stdlib/public/SDK/Foundation/URLComponents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public struct URLComponents : ReferenceConvertible, Hashable, Equatable, _Mutabl
194194
private func _toStringRange(_ r : NSRange) -> Range<String.Index>? {
195195
guard r.location != NSNotFound else { return nil }
196196

197-
let utf16Start = String.UTF16View.Index(_offset: r.location)
198-
let utf16End = String.UTF16View.Index(_offset: r.location + r.length)
197+
let utf16Start = String.UTF16View.Index(encodedOffset: r.location)
198+
let utf16End = String.UTF16View.Index(encodedOffset: r.location + r.length)
199199

200200
guard let s = self.string else { return nil }
201201
guard let start = String.Index(utf16Start, within: s) else { return nil }

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ set(SWIFTLIB_ESSENTIAL
124124
StringBuffer.swift
125125
StringComparable.swift
126126
StringCore.swift
127+
StringIndex.swift
127128
StringInterpolation.swift
128129
StringLegacy.swift
129130
StringRangeReplaceableCollection.swift.gyb

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"StringComparable.swift",
1818
"StringCore.swift",
1919
"StringHashable.swift",
20+
"StringIndex.swift",
2021
"StringIndexConversions.swift",
2122
"StringInterpolation.swift",
2223
"StringLegacy.swift",

stdlib/public/core/Sequence.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,11 @@ public protocol IteratorProtocol {
323323
/// traverse a sequence should be considered O(*n*) unless documented
324324
/// otherwise.
325325
public protocol Sequence {
326+
/// A type representing the sequence's elements.
327+
associatedtype Element
328+
326329
/// A type that provides the sequence's iteration interface and
327330
/// encapsulates its iteration state.
328-
associatedtype Element
329331
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
330332

331333
/// A type that represents a subsequence of some of the sequence's elements.

0 commit comments

Comments
 (0)