@@ -86,8 +86,8 @@ a `const char *`).
86
86
87
87
The C string interop methods will be updated to those described
88
88
[ here] ( https://github.com/apple/swift/blob/master/docs/StringManifesto.md#c-string-interop ) :
89
- a single ` withCString ` operation and two ` init(cString:) ` constructors, one for
90
- UTF8 and one for arbitrary encodings. The primary change is to remove
89
+ two ` withCString ` operations and two ` init(cString:) ` constructors, one each for
90
+ UTF8 and for arbitrary encodings. The primary change is to remove
91
91
"non-repairing" variants of construction from nul-terminated C strings. In both
92
92
of the construction APIs, any invalid encoding sequence detected will have its
93
93
longest valid prefix replaced by ` U+FFFD ` , the Unicode replacement character,
@@ -97,6 +97,10 @@ recorded in the String's encoding such that future accesses need not be slowed
97
97
down by possible error repair separately. Construction that is aborted when
98
98
encoding errors are detected can be accomplished using APIs on the encoding.
99
99
100
+ Additionally, an ` init ` that takes a collection of code units and an encoding
101
+ will allow for construction of a ` String ` from arbitrary collections – for example,
102
+ an ` UnsafeBuffer ` containing a non-nul-terminated C string.
103
+
100
104
The current transcoding support will be updated to improve usability and
101
105
performance. The primary changes will be:
102
106
@@ -145,6 +149,15 @@ C string interop will be consolidated on the following methods:
145
149
146
150
``` swift
147
151
extension String {
152
+ /// Constructs a `String` having the same contents as `codeUnits`.
153
+ ///
154
+ /// - Parameter codeUnits: a collection of code units in
155
+ /// the given `encoding`.
156
+ /// - Parameter encoding: describes the encoding in which the code units
157
+ /// should be interpreted.
158
+ init <C : Collection , Encoding : UnicodeEncoding >(codeUnits : C, encoding : Encoding)
159
+ where C.Iterator .Element == Encodeding.CodeUnit
160
+
148
161
/// Constructs a `String` having the same contents as `nulTerminatedUTF8`.
149
162
///
150
163
/// - Parameter nulTerminatedUTF8: a sequence of contiguous UTF-8 encoded
@@ -165,6 +178,11 @@ extension String {
165
178
/// pointer to a null-terminated sequence of UTF-8 code units.
166
179
func withCString <Result >(
167
180
_ body : (UnsafePointer <CChar >) throws -> Result) rethrows -> Result
181
+
182
+ /// Invokes the given closure on the contents of the string, represented as a
183
+ /// pointer to a null-terminated sequence of code units in the given encoding.
184
+ func withCString <Result , Encoding : UnicodeEncoding >(encoding : Encoding,
185
+ _ body : (UnsafePointer <Encoding.CodeUnit >) throws -> Result) rethrows -> Result
168
186
}
169
187
```
170
188
0 commit comments