Idea: Bytes Literal

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.

  • String would replace invalid UTF-8 bytes with U+FFFD.

  • StaticString might 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).
  • Byte string interpolation would use the existing protocols, with their StringLiteralType: _ExpressibleByBuiltinStringLiteral associated 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