-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathStaticString.swift
286 lines (260 loc) · 9.36 KB
/
StaticString.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Implementation Note: Because StaticString is used in the
// implementation of _precondition(), _fatalErrorMessage(), etc., we
// keep it extremely close to the bare metal. In particular, because
// we store only Builtin types, we are guaranteed that no assertions
// are involved in its construction. This feature is crucial for
// preventing infinite recursion even in non-asserting cases.
/// A string type designed to represent text that is known at compile time.
///
/// Instances of the `StaticString` type are immutable. `StaticString` provides
/// limited, pointer-based access to its contents, unlike Swift's more
/// commonly used `String` type. A static string can store its value as a
/// pointer to an ASCII code unit sequence, as a pointer to a UTF-8 code unit
/// sequence, or as a single Unicode scalar value.
@_fixed_layout
public struct StaticString
: _ExpressibleByBuiltinUnicodeScalarLiteral,
_ExpressibleByBuiltinExtendedGraphemeClusterLiteral,
_ExpressibleByBuiltinStringLiteral,
ExpressibleByUnicodeScalarLiteral,
ExpressibleByExtendedGraphemeClusterLiteral,
ExpressibleByStringLiteral,
CustomStringConvertible,
CustomDebugStringConvertible,
CustomReflectable {
/// Either a pointer to the start of UTF-8 data, represented as an integer,
/// or an integer representation of a single Unicode scalar.
@_versioned
internal var _startPtrOrData: Builtin.Word
/// If `_startPtrOrData` is a pointer, contains the length of the UTF-8 data
/// in bytes.
@_versioned
internal var _utf8CodeUnitCount: Builtin.Word
/// Extra flags:
///
/// - bit 0: set to 0 if `_startPtrOrData` is a pointer, or to 1 if it is a
/// Unicode scalar.
///
/// - bit 1: set to 1 if `_startPtrOrData` is a pointer and string data is
/// ASCII.
@_versioned
internal var _flags: Builtin.Int8
/// A pointer to the beginning of the string's UTF-8 encoded representation.
///
/// The static string must store a pointer to either ASCII or UTF-8 code
/// units. Accessing this property when `hasPointerRepresentation` is
/// `false` triggers a runtime error.
@_transparent
public var utf8Start: UnsafePointer<UInt8> {
_precondition(
hasPointerRepresentation,
"StaticString should have pointer representation")
return UnsafePointer(bitPattern: UInt(_startPtrOrData))!
}
/// The stored Unicode scalar value.
///
/// The static string must store a single Unicode scalar value. Accessing
/// this property when `hasPointerRepresentation` is `true` triggers a
/// runtime error.
@_transparent
public var unicodeScalar: Unicode.Scalar {
_precondition(
!hasPointerRepresentation,
"StaticString should have Unicode scalar representation")
return Unicode.Scalar(UInt32(UInt(_startPtrOrData)))!
}
/// The length in bytes of the static string's ASCII or UTF-8 representation.
///
/// - Warning: If the static string stores a single Unicode scalar value, the
/// value of `utf8CodeUnitCount` is unspecified.
@_transparent
public var utf8CodeUnitCount: Int {
_precondition(
hasPointerRepresentation,
"StaticString should have pointer representation")
return Int(_utf8CodeUnitCount)
}
/// A Boolean value indicating whether the static string stores a pointer to
/// ASCII or UTF-8 code units.
@_transparent
public var hasPointerRepresentation: Bool {
return (UInt8(_flags) & 0x1) == 0
}
/// A Boolean value that is `true` if the static string stores a pointer to
/// ASCII code units.
///
/// Use this property in conjunction with `hasPointerRepresentation` to
/// determine whether a static string with pointer representation stores an
/// ASCII or UTF-8 code unit sequence.
///
/// - Warning: If the static string stores a single Unicode scalar value, the
/// value of `isASCII` is unspecified.
@_transparent
public var isASCII: Bool {
return (UInt8(_flags) & 0x2) != 0
}
/// Invokes the given closure with a buffer containing the static string's
/// UTF-8 code unit sequence.
///
/// This method works regardless of whether the static string stores a
/// pointer or a single Unicode scalar value.
///
/// The pointer argument to `body` is valid only for the lifetime of the
/// closure. Do not escape it from the closure for later use.
///
/// - Parameter body: A closure that takes a buffer pointer to the static
/// string's UTF-8 code unit sequence as its sole argument. If the closure
/// has a return value, it is used as the return value of the
/// `withUTF8Buffer(invoke:)` method. The pointer argument is valid only
/// for the duration of the closure's execution.
/// - Returns: The return value of the `body` closure, if any.
public func withUTF8Buffer<R>(
_ body: (UnsafeBufferPointer<UInt8>) -> R) -> R {
if hasPointerRepresentation {
return body(UnsafeBufferPointer(
start: utf8Start, count: utf8CodeUnitCount))
} else {
var buffer: UInt64 = 0
var i = 0
let sink: (UInt8) -> Void = {
#if _endian(little)
buffer = buffer | (UInt64($0) << (UInt64(i) * 8))
#else
buffer = buffer | (UInt64($0) << (UInt64(7-i) * 8))
#endif
i += 1
}
UTF8.encode(unicodeScalar, into: sink)
return body(UnsafeBufferPointer(
start: UnsafePointer(Builtin.addressof(&buffer)),
count: i))
}
}
/// Creates an empty static string.
@_transparent
public init() {
self = ""
}
@_versioned
@_transparent
internal init(
_start: Builtin.RawPointer,
utf8CodeUnitCount: Builtin.Word,
isASCII: Builtin.Int1
) {
// We don't go through UnsafePointer here to make things simpler for alias
// analysis. A higher-level algorithm may be trying to make sure an
// unrelated buffer is not accessed or freed.
self._startPtrOrData = Builtin.ptrtoint_Word(_start)
self._utf8CodeUnitCount = utf8CodeUnitCount
self._flags = Bool(isASCII)
? (0x2 as UInt8)._value
: (0x0 as UInt8)._value
}
@_versioned
@_transparent
internal init(
unicodeScalar: Builtin.Int32
) {
self._startPtrOrData = UInt(UInt32(unicodeScalar))._builtinWordValue
self._utf8CodeUnitCount = 0._builtinWordValue
self._flags = Unicode.Scalar(_builtinUnicodeScalarLiteral: unicodeScalar).isASCII
? (0x3 as UInt8)._value
: (0x1 as UInt8)._value
}
@effects(readonly)
@_transparent
public init(_builtinUnicodeScalarLiteral value: Builtin.Int32) {
self = StaticString(unicodeScalar: value)
}
/// Creates an instance initialized to a single Unicode scalar.
///
/// Do not call this initializer directly. It may be used by the compiler
/// when you initialize a static string with a Unicode scalar.
@effects(readonly)
@_transparent
public init(unicodeScalarLiteral value: StaticString) {
self = value
}
@effects(readonly)
@_transparent
public init(
_builtinExtendedGraphemeClusterLiteral start: Builtin.RawPointer,
utf8CodeUnitCount: Builtin.Word,
isASCII: Builtin.Int1
) {
self = StaticString(
_builtinStringLiteral: start,
utf8CodeUnitCount: utf8CodeUnitCount,
isASCII: isASCII
)
}
/// Creates an instance initialized to a single character that is made up of
/// one or more Unicode code points.
///
/// Do not call this initializer directly. It may be used by the compiler
/// when you initialize a static string using an extended grapheme cluster.
@effects(readonly)
@_transparent
public init(extendedGraphemeClusterLiteral value: StaticString) {
self = value
}
@effects(readonly)
@_transparent
public init(
_builtinStringLiteral start: Builtin.RawPointer,
utf8CodeUnitCount: Builtin.Word,
isASCII: Builtin.Int1
) {
self = StaticString(
_start: start,
utf8CodeUnitCount: utf8CodeUnitCount,
isASCII: isASCII)
}
/// Creates an instance initialized to the value of a string literal.
///
/// Do not call this initializer directly. It may be used by the compiler
/// when you initialize a static string using a string literal.
@effects(readonly)
@_transparent
public init(stringLiteral value: StaticString) {
self = value
}
/// A string representation of the static string.
public var description: String {
return withUTF8Buffer {
(buffer) in
return String._fromWellFormedCodeUnitSequence(UTF8.self, input: buffer)
}
}
/// A textual representation of the static string, suitable for debugging.
public var debugDescription: String {
return self.description.debugDescription
}
}
extension StaticString {
public var customMirror: Mirror {
return Mirror(reflecting: description)
}
}
extension StaticString {
@available(*, unavailable, renamed: "utf8CodeUnitCount")
public var byteSize: Int {
Builtin.unreachable()
}
@available(*, unavailable, message: "use the 'String(_:)' initializer")
public var stringValue: String {
Builtin.unreachable()
}
}