Skip to content

Commit bfb1824

Browse files
Max Moiseevmoiseev
authored andcommitted
[stdlib] Adding the StringProtocol
1 parent bba33f3 commit bfb1824

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

stdlib/public/core/String.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,46 @@
1212

1313
import SwiftShims
1414

15+
public protocol StringProtocol
16+
: RangeReplaceableCollection, BidirectionalCollection,
17+
CustomStringConvertible, CustomDebugStringConvertible,
18+
CustomReflectable, CustomPlaygroundQuickLookable,
19+
TextOutputStream, TextOutputStreamable,
20+
LosslessStringConvertible, ExpressibleByStringLiteral,
21+
Hashable
22+
where Iterator.Element == Character {
23+
24+
// this should be just <T : StringProtocol>
25+
init<
26+
T : LosslessStringConvertible & Sequence
27+
>(_ other: T) where T.Iterator.Element == Character
28+
29+
associatedtype UTF8Index
30+
var utf8: String.UTF8View { get }
31+
associatedtype UTF16Index
32+
var utf16: String.UTF16View { get }
33+
associatedtype UnicodeScalarIndex
34+
var unicodeScalars: String.UnicodeScalarView { get }
35+
/*associatedtype CharacterIndex*/
36+
var characters: String.CharacterView { get }
37+
38+
#if _runtime(_ObjC)
39+
func hasPrefix(_ prefix: String) -> Bool
40+
func hasSuffix(_ prefix: String) -> Bool
41+
#endif
42+
43+
func lowercased() -> String
44+
func uppercased() -> String
45+
}
46+
47+
extension StringProtocol {
48+
public init<
49+
T : LosslessStringConvertible & Sequence
50+
>(_ other: T) where T.Iterator.Element == Character {
51+
self.init(other.description.characters)
52+
}
53+
}
54+
1555
// FIXME: complexity documentation for most of methods on String is ought to be
1656
// qualified with "amortized" at least, as Characters are variable-length.
1757

stdlib/public/core/StringRangeReplaceableCollection.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
extension String : RangeReplaceableCollection, BidirectionalCollection {
13+
extension String : StringProtocol {
1414
/// The index type for subscripting a string.
1515
public typealias Index = CharacterView.Index
1616

stdlib/public/core/Substring.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension String {
1717
}
1818
}
1919

20-
public struct Substring : RangeReplaceableCollection, BidirectionalCollection {
20+
public struct Substring : StringProtocol {
2121
public typealias Index = String.Index
2222
public typealias IndexDistance = String.IndexDistance
2323
public typealias SubSequence = Substring

0 commit comments

Comments
 (0)