2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
19
19
20
20
/// A string type designed to represent text that is known at compile time.
21
21
///
22
- /// Instances of the `StaticString` type are immutable. `StaticString` provides
23
- /// limited, pointer-based access to its contents, unlike Swift's more
24
- /// commonly used `String` type. A static string can store its value as a
25
- /// pointer to an ASCII code unit sequence, as a pointer to a UTF-8 code unit
26
- /// sequence, or as a single Unicode scalar value.
22
+ /// Instances of the `StaticString` type are immutable.
23
+ ///
24
+ /// `StaticString` provides only low-level access to its contents, unlike
25
+ /// Swift's more commonly used `String` type. A static string can use
26
+ /// either of the following as its storage:
27
+ ///
28
+ /// * a pointer to a null-terminated sequence of UTF-8 code units:
29
+ ///
30
+ /// let emoji: StaticString = "\u{1F600}"
31
+ /// emoji.hasPointerRepresentation //-> true
32
+ /// emoji.isASCII //-> false
33
+ /// emoji.unicodeScalar //-> Fatal error!
34
+ /// emoji.utf8CodeUnitCount //-> 4
35
+ /// emoji.utf8Start[0] //-> 0xF0
36
+ /// emoji.utf8Start[1] //-> 0x9F
37
+ /// emoji.utf8Start[2] //-> 0x98
38
+ /// emoji.utf8Start[3] //-> 0x80
39
+ /// emoji.utf8Start[4] //-> 0x00
40
+ ///
41
+ /// * a single Unicode scalar value, under very limited circumstances:
42
+ ///
43
+ /// struct MyStaticScalar: ExpressibleByUnicodeScalarLiteral {
44
+ /// typealias UnicodeScalarLiteralType = StaticString
45
+ /// let value: StaticString
46
+ /// init(unicodeScalarLiteral value: StaticString) {
47
+ /// self.value = value
48
+ /// }
49
+ /// }
50
+ ///
51
+ /// let emoji: StaticString = MyStaticScalar("\u{1F600}").value
52
+ /// emoji.hasPointerRepresentation //-> false
53
+ /// emoji.isASCII //-> false
54
+ /// emoji.unicodeScalar.value //-> 0x1F600
55
+ /// emoji.utf8CodeUnitCount //-> Fatal error!
56
+ /// emoji.utf8Start //-> Fatal error!
57
+ ///
58
+ /// You can use the `withUTF8Buffer(_:)` method to access a static string's
59
+ /// contents, regardless of which representation the static string uses.
60
+ ///
61
+ /// emoji.withUTF8Buffer { utf8 in
62
+ /// utf8.count //-> 4
63
+ /// utf8[0] //-> 0xF0
64
+ /// utf8[1] //-> 0x9F
65
+ /// utf8[2] //-> 0x98
66
+ /// utf8[3] //-> 0x80
67
+ /// utf8[4] //-> Fatal error!
68
+ /// }
27
69
@frozen
28
70
public struct StaticString
29
71
: _ExpressibleByBuiltinUnicodeScalarLiteral ,
@@ -42,7 +84,7 @@ public struct StaticString
42
84
internal var _startPtrOrData : Builtin . Word
43
85
44
86
/// If `_startPtrOrData` is a pointer, contains the length of the UTF-8 data
45
- /// in bytes.
87
+ /// in bytes (excluding the null terminator) .
46
88
@usableFromInline
47
89
internal var _utf8CodeUnitCount : Builtin . Word
48
90
@@ -51,16 +93,15 @@ public struct StaticString
51
93
/// - bit 0: set to 0 if `_startPtrOrData` is a pointer, or to 1 if it is a
52
94
/// Unicode scalar.
53
95
///
54
- /// - bit 1: set to 1 if `_startPtrOrData` is a pointer and string data is
55
- /// ASCII.
96
+ /// - bit 1: set to 1 if `_startPtrOrData` either points to an ASCII code unit
97
+ /// sequence, or stores an ASCII scalar value .
56
98
@usableFromInline
57
99
internal var _flags : Builtin . Int8
58
100
59
- /// A pointer to the beginning of the string's UTF-8 encoded representation .
101
+ /// A pointer to a null-terminated sequence of UTF-8 code units .
60
102
///
61
- /// The static string must store a pointer to either ASCII or UTF-8 code
62
- /// units. Accessing this property when `hasPointerRepresentation` is
63
- /// `false` triggers a runtime error.
103
+ /// - Important: Accessing this property when `hasPointerRepresentation` is
104
+ /// `false` triggers a runtime error.
64
105
@_transparent
65
106
public var utf8Start : UnsafePointer < UInt8 > {
66
107
_precondition (
@@ -69,11 +110,10 @@ public struct StaticString
69
110
return UnsafePointer ( bitPattern: UInt ( _startPtrOrData) ) !
70
111
}
71
112
72
- /// The stored Unicode scalar value.
113
+ /// A single Unicode scalar value.
73
114
///
74
- /// The static string must store a single Unicode scalar value. Accessing
75
- /// this property when `hasPointerRepresentation` is `true` triggers a
76
- /// runtime error.
115
+ /// - Important: Accessing this property when `hasPointerRepresentation` is
116
+ /// `true` triggers a runtime error.
77
117
@_transparent
78
118
public var unicodeScalar : Unicode . Scalar {
79
119
_precondition (
@@ -82,10 +122,10 @@ public struct StaticString
82
122
return Unicode . Scalar ( UInt32 ( UInt ( _startPtrOrData) ) ) !
83
123
}
84
124
85
- /// The length in bytes of the static string's ASCII or UTF-8 representation .
125
+ /// The number of UTF-8 code units (excluding the null terminator) .
86
126
///
87
- /// - Warning: If the static string stores a single Unicode scalar value, the
88
- /// value of `utf8CodeUnitCount` is unspecified .
127
+ /// - Important: Accessing this property when `hasPointerRepresentation` is
128
+ /// `false` triggers a runtime error .
89
129
@_transparent
90
130
public var utf8CodeUnitCount : Int {
91
131
_precondition (
@@ -99,29 +139,25 @@ public struct StaticString
99
139
return Builtin . inttoptr_Word ( _startPtrOrData)
100
140
}
101
141
102
- /// A Boolean value indicating whether the static string stores a pointer to
103
- /// ASCII or UTF-8 code units.
142
+ /// A Boolean value that indicates whether the static string stores a
143
+ /// pointer to a null-terminated sequence of UTF-8 code units.
144
+ ///
145
+ /// If `hasPointerRepresentation` is `false`, the static string stores a
146
+ /// single Unicode scalar value.
104
147
@_transparent
105
148
public var hasPointerRepresentation : Bool {
106
149
return ( UInt8 ( _flags) & 0x1 ) == 0
107
150
}
108
151
109
- /// A Boolean value that is `true` if the static string stores a pointer to
110
- /// ASCII code units.
111
- ///
112
- /// Use this property in conjunction with `hasPointerRepresentation` to
113
- /// determine whether a static string with pointer representation stores an
114
- /// ASCII or UTF-8 code unit sequence.
115
- ///
116
- /// - Warning: If the static string stores a single Unicode scalar value, the
117
- /// value of `isASCII` is unspecified.
152
+ /// A Boolean value that indicates whether the static string represents only
153
+ /// ASCII code units (or an ASCII scalar value).
118
154
@_transparent
119
155
public var isASCII : Bool {
120
156
return ( UInt8 ( _flags) & 0x2 ) != 0
121
157
}
122
158
123
159
/// Invokes the given closure with a buffer containing the static string's
124
- /// UTF-8 code unit sequence.
160
+ /// UTF-8 code unit sequence (excluding the null terminator) .
125
161
///
126
162
/// This method works regardless of whether the static string stores a
127
163
/// pointer or a single Unicode scalar value.
@@ -132,12 +168,13 @@ public struct StaticString
132
168
/// - Parameter body: A closure that takes a buffer pointer to the static
133
169
/// string's UTF-8 code unit sequence as its sole argument. If the closure
134
170
/// has a return value, that value is also used as the return value of the
135
- /// `withUTF8Buffer(invoke :)` method. The pointer argument is valid only
136
- /// for the duration of the method's execution.
171
+ /// `withUTF8Buffer(_ :)` method. The pointer argument is valid only for the
172
+ /// duration of the method's execution.
137
173
/// - Returns: The return value, if any, of the `body` closure.
138
174
@_transparent
139
175
public func withUTF8Buffer< R> (
140
- _ body: ( UnsafeBufferPointer < UInt8 > ) -> R ) -> R {
176
+ _ body: ( UnsafeBufferPointer < UInt8 > ) -> R
177
+ ) -> R {
141
178
if hasPointerRepresentation {
142
179
return body ( UnsafeBufferPointer (
143
180
start: utf8Start, count: utf8CodeUnitCount) )
@@ -256,7 +293,7 @@ public struct StaticString
256
293
self = value
257
294
}
258
295
259
- /// A string representation of the static string.
296
+ /// A textual representation of the static string.
260
297
public var description : String {
261
298
return withUTF8Buffer { String . _uncheckedFromUTF8 ( $0) }
262
299
}
0 commit comments