We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2ede9a7 commit d809640Copy full SHA for d809640
stdlib/public/core/CString.swift
@@ -126,6 +126,21 @@ extension String {
126
self = str
127
}
128
129
+ @inlinable
130
+ @_alwaysEmitIntoClient
131
+ public init?(validatingUTF8 cString: [CChar]) {
132
+ guard let length = cString.firstIndex(of: 0) else {
133
+ _preconditionFailure(
134
+ "input of String.init(validatingUTF8:) must be null-terminated"
135
+ )
136
+ }
137
+ guard let string = cString.prefix(length).withUnsafeBytes({
138
+ String._tryFromUTF8($0.assumingMemoryBound(to: UInt8.self))
139
+ }) else { return nil }
140
+
141
+ self = string
142
143
144
/// Creates a new string by copying the null-terminated data referenced by
145
/// the given pointer using the specified encoding.
146
///
0 commit comments