[swift-evolution] Proposal: Add StaticString.UnicodeScalarView
Kevin Ballard
kevin at sb.org
Sun Dec 6 03:21:36 CST 2015
There's no way to create a substring of `StaticString` that's still
typed as `StaticString`. This is occasionally desirable, for example if
you want to extract the filename from `__FILE__` to pass to another API
that requires a `StaticString`.
I believe the best solution to this is to add a type
`StaticString.UnicodeScalarView`, similar to `String.UnicodeScalarView`.
`StaticString` would also be extended with a property `unicodeScalars`
and two initializers to construct a `StaticString` from a
`String.UnicodeScalarView`. The full proposed API looks like:
extension StaticString { /// The value of `self` as a collection of
[Unicode scalar values]
(http://www.unicode.org/glossary/#unicode_scalar_value). public var
unicodeScalars: UnicodeScalarView
/// Construct the `StaticString` corresponding to the given ///
`UnicodeScalarView`. public init(_: UnicodeScalarView)
/// Construct the `StaticString` corresponding to the given ///
`UnicodeScalarView` slice. public init(_: Slice<UnicodeScalarView>)
/// A collection of [Unicode scalar values]
(http://www.unicode.org/glossary/#unicode_scalar_value) that /// encode
a `StaticString`. public struct UnicodeScalarView : CollectionType {
init(_: StaticString)
/// A position in a `StaticString.UnicodeScalarView`. public struct
Index : BidirectionalIndexType, Comparable { /// Returns the next
consecutive value after `self`. /// /// - Requires: The next
value is representable. @warn_unused_result public func
successor() -> Index
/// Returns the previous consecutive value before `self`. ///
/// - Requires: The previous value is representable.
@warn_unused_result public func predecessor() -> Index }
/// The position of the first `UnicodeScalar` if the `StaticString` is
/// non-empty; identical to `endIndex` otherwise. public var
startIndex: Index
/// The "past the end" position. /// /// `endIndex` is not a valid
argument to `subscript`, and is always /// reachable from
`startIndex` by zero or more applications of /// `successor()`.
public var endIndex: Index
/// Returns `true` iff `self` is empty. public var isEmpty: Bool
public subscript(position: Index) -> UnicodeScalar } }
An alternative would be to make StaticString itself conform to
CollectionType, but this is a bad idea for the same reasons that String
doesn't conform to CollectionType.
-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/2f52b014/attachment.html>
More information about the swift-evolution
mailing list