Skip to content

Commit 7513547

Browse files
Update 0163-string-revision-1.md (#682)
* Update 0163-string-revision-1.md * Update 0163-string-revision-1.md
1 parent 101d0f6 commit 7513547

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

proposals/0163-string-revision-1.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ a `const char *`).
8686

8787
The C string interop methods will be updated to those described
8888
[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
9191
"non-repairing" variants of construction from nul-terminated C strings. In both
9292
of the construction APIs, any invalid encoding sequence detected will have its
9393
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
9797
down by possible error repair separately. Construction that is aborted when
9898
encoding errors are detected can be accomplished using APIs on the encoding.
9999

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+
100104
The current transcoding support will be updated to improve usability and
101105
performance. The primary changes will be:
102106

@@ -145,6 +149,15 @@ C string interop will be consolidated on the following methods:
145149

146150
```swift
147151
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+
148161
/// Constructs a `String` having the same contents as `nulTerminatedUTF8`.
149162
///
150163
/// - Parameter nulTerminatedUTF8: a sequence of contiguous UTF-8 encoded
@@ -165,6 +178,11 @@ extension String {
165178
/// pointer to a null-terminated sequence of UTF-8 code units.
166179
func withCString<Result>(
167180
_ 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
168186
}
169187
```
170188

0 commit comments

Comments
 (0)