Skip to content

Commit 0285602

Browse files
committed
[stdlib] overload String-from-C-string initializers in the case of inout conversion
1 parent 1e5d690 commit 0285602

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

stdlib/public/core/CString.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ extension String {
7272
).0
7373
}
7474

75+
@inlinable
76+
@_alwaysEmitIntoClient
77+
@available(*, deprecated)
78+
public init(cString nullTerminatedUTF8: inout CChar) {
79+
guard nullTerminatedUTF8 == 0 else {
80+
_preconditionFailure(
81+
"input of String.init(cString:) must be null-terminated"
82+
)
83+
}
84+
self = ""
85+
}
86+
7587
/// Creates a new string by copying the null-terminated UTF-8 data referenced
7688
/// by the given pointer.
7789
///
@@ -98,6 +110,18 @@ extension String {
98110
self = nullTerminatedUTF8.withCString(String.init(cString:))
99111
}
100112

113+
@inlinable
114+
@_alwaysEmitIntoClient
115+
@available(*, deprecated)
116+
public init(cString nullTerminatedUTF8: inout UInt8) {
117+
guard nullTerminatedUTF8 == 0 else {
118+
_preconditionFailure(
119+
"input of String.init(cString:) must be null-terminated"
120+
)
121+
}
122+
self = ""
123+
}
124+
101125
/// Creates a new string by copying and validating the null-terminated UTF-8
102126
/// data referenced by the given pointer.
103127
///
@@ -155,6 +179,18 @@ extension String {
155179
self = cString.withCString(String.init(cString:))
156180
}
157181

182+
@inlinable
183+
@_alwaysEmitIntoClient
184+
@available(*, deprecated)
185+
public init?(validatingUTF8 cString: inout CChar) {
186+
guard cString == 0 else {
187+
_preconditionFailure(
188+
"input of String.init(validatingUTF8:) must be null-terminated"
189+
)
190+
}
191+
self = ""
192+
}
193+
158194
/// Creates a new string by copying the null-terminated data referenced by
159195
/// the given pointer using the specified encoding.
160196
///
@@ -279,6 +315,24 @@ extension String {
279315
}
280316
}
281317

318+
@_specialize(where Encoding == Unicode.UTF8)
319+
@_specialize(where Encoding == Unicode.UTF16)
320+
@inlinable
321+
@_alwaysEmitIntoClient
322+
@available(*, deprecated)
323+
public static func decodeCString<Encoding: _UnicodeEncoding>(
324+
_ cString: inout Encoding.CodeUnit,
325+
as encoding: Encoding.Type,
326+
repairingInvalidCodeUnits isRepairing: Bool = true
327+
) -> (result: String, repairsMade: Bool)? {
328+
guard cString == 0 else {
329+
_preconditionFailure(
330+
"input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated"
331+
)
332+
}
333+
return ("", false)
334+
}
335+
282336
/// Creates a string from the null-terminated sequence of bytes at the given
283337
/// pointer.
284338
///
@@ -322,6 +376,22 @@ extension String {
322376
String(decodingCString: $0, as: sourceEncoding.self)
323377
}
324378
}
379+
380+
@_specialize(where Encoding == Unicode.UTF8)
381+
@_specialize(where Encoding == Unicode.UTF16)
382+
@inlinable // Fold away specializations
383+
@_alwaysEmitIntoClient
384+
public init<Encoding: Unicode.Encoding>(
385+
decodingCString nullTerminatedCodeUnits: inout Encoding.CodeUnit,
386+
as sourceEncoding: Encoding.Type
387+
) {
388+
guard nullTerminatedCodeUnits == 0 else {
389+
_preconditionFailure(
390+
"input of String.init(decodingCString:as:) must be null-terminated"
391+
)
392+
}
393+
self = ""
394+
}
325395
}
326396

327397
extension UnsafePointer where Pointee == UInt8 {

0 commit comments

Comments
 (0)