Skip to content

Commit ca536f8

Browse files
committed
Remove use of NSString.replacingOccurrences(of:with:)
Prefer to use `String.replacing(_:with:)` instead of the `NSString` operations for the strings. This should avoid an unnecessary use of Foundation API and a dispatch to the `NSString` API. Bump the minimum platform version to 10.13 to deal with the API availability in the standard library.
1 parent 71ab073 commit ca536f8

File tree

13 files changed

+21
-20
lines changed

13 files changed

+21
-20
lines changed

Sources/Basics/Concurrency/AsyncProcess.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,8 @@ extension AsyncProcessResult.Error: CustomStringConvertible {
13161316
let indentation = " "
13171317
str.append(contentsOf: " output:\n")
13181318
str.append(contentsOf: indentation)
1319-
str.append(contentsOf: output.replacingOccurrences(of: "\n", with: "\n" + indentation))
1319+
str.append(contentsOf: output.split(whereSeparator: { $0.isNewline })
1320+
.joined(separator: "\n\(indentation)"))
13201321
if !output.hasSuffix("\n") {
13211322
str.append(contentsOf: "\n")
13221323
}

Sources/Basics/FileSystem/AbsolutePath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ extension AbsolutePath {
321321

322322
extension AbsolutePath {
323323
public var escapedPathString: String {
324-
self.pathString.replacingOccurrences(of: "\\", with: "\\\\")
324+
self.pathString.replacing("\\", with: "\\\\")
325325
}
326326
}
327327

Sources/Basics/FileSystem/NativePathExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension AbsolutePath {
2020
return URL(fileURLWithPath: self.pathString).withUnsafeFileSystemRepresentation {
2121
let repr = String(cString: $0!)
2222
if escaped {
23-
return repr.replacingOccurrences(of: "\\", with: "\\\\")
23+
return repr.replacing("\\", with: "\\\\")
2424
}
2525
return repr
2626
}

Sources/Basics/Netrc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public struct NetrcParser {
135135
let matchedString = nsString.substring(with: $0.range)
136136
if !matchedString.starts(with: "\"") {
137137
trimmedCommentsText = trimmedCommentsText
138-
.replacingOccurrences(of: matchedString, with: "")
138+
.replacing(matchedString, with: "")
139139
}
140140
}
141141
return trimmedCommentsText

Sources/Basics/Sandbox.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ extension AbsolutePath {
241241
/// Private computed property that returns a version of the path as a string quoted for use as a subpath in a .sb sandbox profile.
242242
fileprivate var quotedAsSubpathForSandboxProfile: String {
243243
"\"" + self.pathString
244-
.replacingOccurrences(of: "\\", with: "\\\\")
245-
.replacingOccurrences(of: "\"", with: "\\\"")
244+
.replacing("\\", with: "\\\\")
245+
.replacing("\"", with: "\\\"")
246246
+ "\""
247247
}
248248
}

Sources/Basics/Serialization/SerializedJSON.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension SerializedJSON: ExpressibleByStringInterpolation {
3131
fileprivate var value: String = ""
3232

3333
private func escape(_ string: String) -> String {
34-
string.replacingOccurrences(of: #"\"#, with: #"\\"#)
34+
string.replacing(#"\"#, with: #"\\"#)
3535
}
3636

3737
public init(literalCapacity: Int, interpolationCount: Int) {

Sources/Commands/Utilities/DOTManifestSerializer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ struct DOTManifestSerializer {
3838

3939
/// Quote the name and escape the quotes and backslashes
4040
func quoteName(_ name: String) -> String {
41-
"\"" + name.replacingOccurrences(of: "\"", with: "\\\"")
42-
.replacingOccurrences(of: "\\", with: "\\\\") + "\""
41+
"\"" + name.replacing("\"", with: "\\\"")
42+
.replacing("\\", with: "\\\\") + "\""
4343
}
4444

4545
mutating func writeDOT(to stream: OutputByteStream) {

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
785785
let vfsOverlayTempFilePath = tempDir.appending("vfs.yaml")
786786
try VFSOverlay(roots: [
787787
VFSOverlay.File(
788-
name: manifestPath._normalized.replacingOccurrences(of: #"\"#, with: #"\\"#),
788+
name: manifestPath._normalized.replacing(#"\"#, with: #"\\"#),
789789
externalContents: manifestTempFilePath._nativePathString(escaped: true)
790790
)
791791
]).write(to: vfsOverlayTempFilePath, fileSystem: localFileSystem)
@@ -1061,7 +1061,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
10611061

10621062
var environment = Environment.current
10631063
#if os(Windows)
1064-
let windowsPathComponent = runtimePath.pathString.replacingOccurrences(of: "/", with: "\\")
1064+
let windowsPathComponent = runtimePath.pathString.replacing("/", with: "\\")
10651065
environment.prependPath(key: .path, value: windowsPathComponent)
10661066
#endif
10671067

Sources/PackageLoading/ModuleMapGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public let moduleMapFilename = "module.modulemap"
1919

2020
extension AbsolutePath {
2121
fileprivate var moduleEscapedPathString: String {
22-
return self.pathString.replacingOccurrences(of: "\\", with: "\\\\")
22+
return self.pathString.replacing("\\", with: "\\\\")
2323
}
2424
}
2525

Sources/PackageModel/ManifestSourceGeneration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ extension TargetBuildSettingDescription.Kind {
695695
extension String {
696696
fileprivate var quotedForPackageManifest: String {
697697
return "\"" + self
698-
.replacingOccurrences(of: "\\", with: "\\\\")
699-
.replacingOccurrences(of: "\"", with: "\\\"")
698+
.replacing("\\", with: "\\\\")
699+
.replacing("\"", with: "\\\"")
700700
+ "\""
701701
}
702702
}

Sources/PackageRegistry/RegistryClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ extension HTTPClientHeaders {
24932493
return nil
24942494
}
24952495

2496-
return parts[1].replacingOccurrences(of: "\"", with: "")
2496+
return parts[1].replacing("\"", with: "")
24972497
}
24982498
}
24992499

Sources/SourceControl/GitRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public final class GitRepository: Repository, WorkingCheckout {
778778
}
779779

780780
return stringPaths.map(output.split(whereSeparator: { $0.isNewline }).map {
781-
let string = String($0).replacingOccurrences(of: "\\\\", with: "\\")
781+
let string = String($0).replacing("\\\\", with: "\\")
782782
if string.utf8.first == UInt8(ascii: "\"") {
783783
return String(string.dropFirst(1).dropLast(1))
784784
}

Sources/_InternalTestSupport/misc.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public func testWithTemporaryDirectory<Result>(
5858
body: (AbsolutePath) async throws -> Result
5959
) async throws -> Result {
6060
let cleanedFunction = function.description
61-
.replacingOccurrences(of: "(", with: "")
62-
.replacingOccurrences(of: ")", with: "")
63-
.replacingOccurrences(of: ".", with: "")
64-
.replacingOccurrences(of: ":", with: "_")
61+
.replacing("(", with: "")
62+
.replacing(")", with: "")
63+
.replacing(".", with: "")
64+
.replacing(":", with: "_")
6565
return try await withTemporaryDirectory(prefix: "spm-tests-\(cleanedFunction)") { tmpDirPath in
6666
defer {
6767
// Unblock and remove the tmp dir on deinit.

0 commit comments

Comments
 (0)