<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>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`.<br></div>
<div>&nbsp;</div>
<div>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:<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">extension StaticString {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// The value of `self` as a collection of [Unicode scalar values](http://www.unicode.org/glossary/#unicode_scalar_value).</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; public var unicodeScalars: UnicodeScalarView</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// Construct the `StaticString` corresponding to the given</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// `UnicodeScalarView`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; public init(_: UnicodeScalarView)</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// Construct the `StaticString` corresponding to the given</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// `UnicodeScalarView` slice.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; public init(_: Slice&lt;UnicodeScalarView&gt;)</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// A collection of [Unicode scalar values](http://www.unicode.org/glossary/#unicode_scalar_value) that</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; /// encode a `StaticString`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; public struct UnicodeScalarView : CollectionType {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; init(_: StaticString)</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// A position in a `StaticString.UnicodeScalarView`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; public struct Index : BidirectionalIndexType, Comparable {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// Returns the next consecutive value after `self`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// - Requires: The next value is representable.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @warn_unused_result</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public func successor() -&gt; Index</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// Returns the previous consecutive value before `self`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// - Requires: The previous value is representable.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @warn_unused_result</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public func predecessor() -&gt; Index</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// The position of the first `UnicodeScalar` if the `StaticString` is</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// non-empty; identical to `endIndex` otherwise.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; public var startIndex: Index</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// The "past the end" position.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; ///</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// `endIndex` is not a valid argument to `subscript`, and is always</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// reachable from `startIndex` by zero or more applications of</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// `successor()`.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; public var endIndex: Index</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; /// Returns `true` iff `self` is empty.</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; public var isEmpty: Bool</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; public subscript(position: Index) -&gt; UnicodeScalar</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><br></div>
<div>&nbsp;</div>
<div>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.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard<br></div>
</body>
</html>