diff --git a/stdlib/public/core/String.swift b/stdlib/public/core/String.swift index 07e1d9caef8..69219c0e9d7 100644 --- a/stdlib/public/core/String.swift +++ b/stdlib/public/core/String.swift @@ -12,6 +12,46 @@ import SwiftShims +public protocol StringProtocol + : RangeReplaceableCollection, BidirectionalCollection, + CustomStringConvertible, CustomDebugStringConvertible, + CustomReflectable, CustomPlaygroundQuickLookable, + TextOutputStream, TextOutputStreamable, + LosslessStringConvertible, ExpressibleByStringLiteral, + Hashable + where Iterator.Element == Character { + + // this should be just + init< + T : LosslessStringConvertible & Sequence + >(_ other: T) where T.Iterator.Element == Character + + associatedtype UTF8Index + var utf8: String.UTF8View { get } + associatedtype UTF16Index + var utf16: String.UTF16View { get } + associatedtype UnicodeScalarIndex + var unicodeScalars: String.UnicodeScalarView { get } + /*associatedtype CharacterIndex*/ + var characters: String.CharacterView { get } + +#if _runtime(_ObjC) + func hasPrefix(_ prefix: String) -> Bool + func hasSuffix(_ prefix: String) -> Bool +#endif + + func lowercased() -> String + func uppercased() -> String +} + +extension StringProtocol { + public init< + T : LosslessStringConvertible & Sequence + >(_ other: T) where T.Iterator.Element == Character { + self.init(other.description.characters) + } +} + // FIXME: complexity documentation for most of methods on String is ought to be // qualified with "amortized" at least, as Characters are variable-length. diff --git a/stdlib/public/core/StringRangeReplaceableCollection.swift.gyb b/stdlib/public/core/StringRangeReplaceableCollection.swift.gyb index 659f1e37794..abec71213e2 100644 --- a/stdlib/public/core/StringRangeReplaceableCollection.swift.gyb +++ b/stdlib/public/core/StringRangeReplaceableCollection.swift.gyb @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -extension String : RangeReplaceableCollection, BidirectionalCollection { +extension String : StringProtocol { /// The index type for subscripting a string. public typealias Index = CharacterView.Index diff --git a/stdlib/public/core/Substring.swift.gyb b/stdlib/public/core/Substring.swift.gyb index b42fbe6afec..aadd9a62c84 100644 --- a/stdlib/public/core/Substring.swift.gyb +++ b/stdlib/public/core/Substring.swift.gyb @@ -17,7 +17,7 @@ extension String { } } -public struct Substring : RangeReplaceableCollection, BidirectionalCollection { +public struct Substring : StringProtocol { public typealias Index = String.Index public typealias IndexDistance = String.IndexDistance public typealias SubSequence = Substring