Skip to content

Commit 46096ed

Browse files
authored
Silence warnings about String.init(validatingUTF8:) on Swift 6. (#415)
Swift 6 deprecates `String.init(validatingUTF8:)` and replaces it with the identical `String.init(validatingCString:)`. This change adopts the new API when compiling with Swift 6. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 9ff0148 commit 46096ed

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

Sources/Testing/Support/Additions/CommandLineAdditions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension CommandLine {
2121
static func arguments() -> [String] {
2222
UnsafeBufferPointer(start: unsafeArgv, count: Int(argc)).lazy
2323
.compactMap { $0 }
24-
.compactMap { String(validatingUTF8: $0) }
24+
.compactMap { String(validatingUTF8CString: $0) }
2525
}
2626

2727
/// The path to the current process' executable.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
private import _TestingInternals
12+
13+
extension String {
14+
init?(validatingUTF8CString cString: UnsafePointer<CChar>) {
15+
#if compiler(>=5.11)
16+
self.init(validatingCString: cString)
17+
#else
18+
self.init(validatingUTF8: cString)
19+
#endif
20+
}
21+
}

Sources/Testing/Support/Environment.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum Environment {
5757
break
5858
}
5959

60-
if let row = String(validatingUTF8: rowp),
60+
if let row = String(validatingUTF8CString: rowp),
6161
let (key, value) = _splitEnvironmentVariable(row) {
6262
result[key] = value
6363
}
@@ -164,14 +164,14 @@ enum Environment {
164164
if let equals = strchr(rowp, CInt(UInt8(ascii: "="))) {
165165
let keyLength = UnsafeRawPointer(equals) - UnsafeRawPointer(rowp)
166166
if 0 == strncmp(rowp, name, keyLength) {
167-
return String(validatingUTF8: equals + 1)
167+
return String(validatingUTF8CString: equals + 1)
168168
}
169169
}
170170
}
171171
return nil
172172
}
173173
#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
174-
getenv(name).flatMap { String(validatingUTF8: $0) }
174+
getenv(name).flatMap { String(validatingUTF8CString: $0) }
175175
#elseif os(Windows)
176176
name.withCString(encodedAs: UTF16.self) { name in
177177
func getVariable(maxCount: Int) -> String? {

Sources/Testing/Support/Versions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ let operatingSystemVersion: String = {
3636
if 0 == uname(&name) {
3737
let release = withUnsafeBytes(of: name.release) { release in
3838
release.withMemoryRebound(to: CChar.self) { release in
39-
String(validatingUTF8: release.baseAddress!) ?? ""
39+
String(validatingUTF8CString: release.baseAddress!) ?? ""
4040
}
4141
}
4242
let version = withUnsafeBytes(of: name.version) { version in
4343
version.withMemoryRebound(to: CChar.self) { version in
44-
String(validatingUTF8: version.baseAddress!) ?? ""
44+
String(validatingUTF8CString: version.baseAddress!) ?? ""
4545
}
4646
}
4747
switch (release, version) {
@@ -157,7 +157,7 @@ func sysctlbyname(_ name: String, as _: String.Type) -> String? {
157157
if 0 == sysctlbyname(name, nil, &szValue, nil, 0) {
158158
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: szValue) { buffer in
159159
if 0 == sysctlbyname(name, buffer.baseAddress!, &szValue, nil, 0) {
160-
return String(validatingUTF8: buffer.baseAddress!)
160+
return String(validatingUTF8CString: buffer.baseAddress!)
161161
}
162162
return nil
163163
}

Sources/Testing/Traits/Tags/Tag.Color+Loading.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private var _homeDirectoryPath: String? {
2222
return if let homeVariable = Environment.variable(named: "HOME") {
2323
homeVariable
2424
} else if let pwd = getpwuid(geteuid()) {
25-
String(validatingUTF8: pwd.pointee.pw_dir)
25+
String(validatingUTF8CString: pwd.pointee.pw_dir)
2626
} else {
2727
nil
2828
}

0 commit comments

Comments
 (0)