|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +/// A `ByteString` represents a sequence of bytes. |
| 12 | +/// |
| 13 | +/// This struct provides useful operations for working with buffers of |
| 14 | +/// bytes. Conceptually it is just a contiguous array of bytes (UInt8), but it |
| 15 | +/// contains methods and default behavor suitable for common operations done |
| 16 | +/// using bytes strings. |
| 17 | +/// |
| 18 | +/// This struct *is not* intended to be used for significant mutation of byte |
| 19 | +/// strings, we wish to retain the flexibility to micro-optimize the memory |
| 20 | +/// allocation of the storage (for example, by inlining the storage for small |
| 21 | +/// strings or and by eliminating wasted space in growable arrays). For |
| 22 | +/// construction of byte arrays, clients should use the `OutputByteStream` class |
| 23 | +/// and then convert to a `ByteString` when complete. |
| 24 | +public struct ByteString: ExpressibleByArrayLiteral, Hashable { |
| 25 | + /// The buffer contents. |
| 26 | + @usableFromInline |
| 27 | + internal var _bytes: [UInt8] |
| 28 | + |
| 29 | + /// Create an empty byte string. |
| 30 | + @inlinable |
| 31 | + public init() { |
| 32 | + _bytes = [] |
| 33 | + } |
| 34 | + |
| 35 | + /// Create a byte string from a byte array literal. |
| 36 | + @inlinable |
| 37 | + public init(arrayLiteral contents: UInt8...) { |
| 38 | + _bytes = contents |
| 39 | + } |
| 40 | + |
| 41 | + /// Create a byte string from an array of bytes. |
| 42 | + @inlinable |
| 43 | + public init(_ contents: [UInt8]) { |
| 44 | + _bytes = contents |
| 45 | + } |
| 46 | + |
| 47 | + /// Create a byte string from an array slice. |
| 48 | + @inlinable |
| 49 | + public init(_ contents: ArraySlice<UInt8>) { |
| 50 | + _bytes = Array(contents) |
| 51 | + } |
| 52 | + |
| 53 | + /// Create a byte string from an byte buffer. |
| 54 | + @inlinable |
| 55 | + public init<S: Sequence> (_ contents: S) where S.Iterator.Element == UInt8 { |
| 56 | + _bytes = [UInt8](contents) |
| 57 | + } |
| 58 | + |
| 59 | + /// Create a byte string from the UTF8 encoding of a string. |
| 60 | + @inlinable |
| 61 | + public init(encodingAsUTF8 string: String) { |
| 62 | + _bytes = [UInt8](string.utf8) |
| 63 | + } |
| 64 | + |
| 65 | + /// Access the byte string contents as an array. |
| 66 | + @inlinable |
| 67 | + public var contents: [UInt8] { |
| 68 | + return _bytes |
| 69 | + } |
| 70 | + |
| 71 | + /// Return the byte string size. |
| 72 | + @inlinable |
| 73 | + public var count: Int { |
| 74 | + return _bytes.count |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Conform to CustomDebugStringConvertible. |
| 79 | +extension ByteString: CustomStringConvertible { |
| 80 | + /// Return the string decoded as a UTF8 sequence, or traps if not possible. |
| 81 | + public var description: String { |
| 82 | + guard let description = validDescription else { |
| 83 | + fatalError("invalid byte string: \(cString)") |
| 84 | + } |
| 85 | + |
| 86 | + return description |
| 87 | + } |
| 88 | + |
| 89 | + /// Return the string decoded as a UTF8 sequence, if possible. |
| 90 | + @inlinable |
| 91 | + public var validDescription: String? { |
| 92 | + // FIXME: This is very inefficient, we need a way to pass a buffer. It |
| 93 | + // is also wrong if the string contains embedded '\0' characters. |
| 94 | + let tmp = _bytes + [UInt8(0)] |
| 95 | + return tmp.withUnsafeBufferPointer { ptr in |
| 96 | + return String(validatingUTF8: unsafeBitCast(ptr.baseAddress, to: UnsafePointer<CChar>.self)) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// Return the string decoded as a UTF8 sequence, substituting replacement |
| 101 | + /// characters for ill-formed UTF8 sequences. |
| 102 | + @inlinable |
| 103 | + public var cString: String { |
| 104 | + return String(decoding: _bytes, as: Unicode.UTF8.self) |
| 105 | + } |
| 106 | + |
| 107 | + @available(*, deprecated, message: "use description or validDescription instead") |
| 108 | + public var asString: String? { |
| 109 | + return validDescription |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// ByteStreamable conformance for a ByteString. |
| 114 | +extension ByteString: ByteStreamable { |
| 115 | + @inlinable |
| 116 | + public func write(to stream: OutputByteStream) { |
| 117 | + stream.write(_bytes) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// StringLiteralConvertable conformance for a ByteString. |
| 122 | +extension ByteString: ExpressibleByStringLiteral { |
| 123 | + public typealias UnicodeScalarLiteralType = StringLiteralType |
| 124 | + public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType |
| 125 | + |
| 126 | + public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { |
| 127 | + _bytes = [UInt8](value.utf8) |
| 128 | + } |
| 129 | + public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { |
| 130 | + _bytes = [UInt8](value.utf8) |
| 131 | + } |
| 132 | + public init(stringLiteral value: StringLiteralType) { |
| 133 | + _bytes = [UInt8](value.utf8) |
| 134 | + } |
| 135 | +} |
0 commit comments