Skip to content

Commit 022e1cd

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.
1 parent cefff2b commit 022e1cd

16 files changed

+37
-13
lines changed

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 func escapedPathString() -> String {
324-
self.pathString.replacingOccurrences(of: "\\", with: "\\\\")
324+
self.pathString.replacing("\\", with: "\\\\")
325325
}
326326
}
327327

Sources/Basics/NativePathExtensions.swift

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

Sources/Basics/Netrc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public struct NetrcParser {
133133
var trimmedCommentsText = text
134134
matches.forEach {
135135
trimmedCommentsText = trimmedCommentsText
136-
.replacingOccurrences(of: nsString.substring(with: $0.range), with: "")
136+
.replacing(nsString.substring(with: $0.range), with: "")
137137
}
138138
return trimmedCommentsText
139139
}

Sources/Basics/Sandbox.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ extension AbsolutePath {
223223
/// Private computed property that returns a version of the path as a string quoted for use as a subpath in a .sb sandbox profile.
224224
fileprivate var quotedAsSubpathForSandboxProfile: String {
225225
"\"" + self.pathString
226-
.replacingOccurrences(of: "\\", with: "\\\\")
227-
.replacingOccurrences(of: "\"", with: "\\\"")
226+
.replacing("\\", with: "\\\\")
227+
.replacing("\"", with: "\\\"")
228228
+ "\""
229229
}
230230
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if os(macOS)
14+
extension String {
15+
internal func replacing(_ target: String, with replacement: String) -> String {
16+
target.replacingOccurrences(of: target, with: replacement)
17+
}
18+
}
19+
#endif

Sources/Commands/Utilities/DOTManifestSerializer.swift

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

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

4545
public mutating func writeDOT(to stream: OutputByteStream) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Basics/macOS+CompatibilityShims.swift

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
766766
// Run the compiled manifest.
767767
var environment = ProcessEnv.vars
768768
#if os(Windows)
769-
let windowsPathComponent = runtimePath.pathString.replacingOccurrences(of: "/", with: "\\")
769+
let windowsPathComponent = runtimePath.pathString.replacing("/", with: "\\")
770770
environment["Path"] = "\(windowsPathComponent);\(environment["Path"] ?? "")"
771771
#endif
772772

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Basics/macOS+CompatibilityShims.swift

Sources/PackageModel/ManifestSourceGeneration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ extension TargetBuildSettingDescription.Kind {
681681
extension String {
682682
fileprivate var quotedForPackageManifest: String {
683683
return "\"" + self
684-
.replacingOccurrences(of: "\\", with: "\\\\")
685-
.replacingOccurrences(of: "\"", with: "\\\"")
684+
.replacing("\\", with: "\\\\")
685+
.replacing("\"", with: "\\\"")
686686
+ "\""
687687
}
688688
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Basics/macOS+CompatibilityShims.swift

Sources/PackageRegistry/RegistryClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ extension HTTPClientHeaders {
21652165
return nil
21662166
}
21672167

2168-
return parts[1].replacingOccurrences(of: "\"", with: "")
2168+
return parts[1].replacing("\"", with: "")
21692169
}
21702170
}
21712171

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Basics/macOS+CompatibilityShims.swift

Sources/SourceControl/GitRepository.swift

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

777777
return stringPaths.map(output.split(separator: "\n").map {
778-
let string = String($0).replacingOccurrences(of: "\\\\", with: "\\")
778+
let string = String($0).replacing("\\\\", with: "\\")
779779
if string.utf8.first == UInt8(ascii: "\"") {
780780
return String(string.dropFirst(1).dropLast(1))
781781
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Basics/macOS+CompatibilityShims.swift

0 commit comments

Comments
 (0)