Instead of adding new protocols (for byte strings and interpolation), could we simply add another initializer to the existing _ExpressibleByBuiltinStringLiteral protocol?
-
The new initializer would only be used when a string literal contains
\x{...}. -
A default implementation (if needed) would forward to the existing initializer.
-
Stringwould replace invalid UTF-8 bytes with U+FFFD. -
StaticStringmight allow invalid UTF-8 bytes, by updating preconditions, and adding APIs:- e.g.
public var isUTF8: Bool(using a spare bit in_flags). - e.g.
public var bytes: UnsafeRawBufferPointer(can be empty).
- e.g.
-
Byte string interpolation would use the existing protocols, with their
StringLiteralType: _ExpressibleByBuiltinStringLiteralassociated types.
Off-topic
Could the previous Unicode scalar literals pitch be solved by having a shorter type name?
public typealias Rune = Unicode.Scalar
extension StringProtocol {
public typealias RuneView = Self.UnicodeScalarView
public var runes: RuneView { self.unicodeScalars }
}
let rune = Rune("\u{1F600}")
rune.properties.isEmoji //-> true
rune.properties.name //-> "GRINNING FACE"
rune.utf8.count //-> 4
let runes = "ABC".runes
runes.allSatisfy(\.isASCII) //-> true
runes.count //-> 3