Skip to content

Remove use of NSString.replacingOccurrences(of:with:) #6650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/Basics/Concurrency/AsyncProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,8 @@ extension AsyncProcessResult.Error: CustomStringConvertible {
let indentation = " "
str.append(contentsOf: " output:\n")
str.append(contentsOf: indentation)
str.append(contentsOf: output.replacingOccurrences(of: "\n", with: "\n" + indentation))
str.append(contentsOf: output.split(whereSeparator: { $0.isNewline })
.joined(separator: "\n\(indentation)"))
if !output.hasSuffix("\n") {
str.append(contentsOf: "\n")
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/FileSystem/AbsolutePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ extension AbsolutePath {

extension AbsolutePath {
public var escapedPathString: String {
self.pathString.replacingOccurrences(of: "\\", with: "\\\\")
self.pathString.replacing("\\", with: "\\\\")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/FileSystem/NativePathExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension AbsolutePath {
return URL(fileURLWithPath: self.pathString).withUnsafeFileSystemRepresentation {
let repr = String(cString: $0!)
if escaped {
return repr.replacingOccurrences(of: "\\", with: "\\\\")
return repr.replacing("\\", with: "\\\\")
}
return repr
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/Netrc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public struct NetrcParser {
let matchedString = nsString.substring(with: $0.range)
if !matchedString.starts(with: "\"") {
trimmedCommentsText = trimmedCommentsText
.replacingOccurrences(of: matchedString, with: "")
.replacing(matchedString, with: "")
}
}
return trimmedCommentsText
Expand Down
4 changes: 2 additions & 2 deletions Sources/Basics/Sandbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ extension AbsolutePath {
/// Private computed property that returns a version of the path as a string quoted for use as a subpath in a .sb sandbox profile.
fileprivate var quotedAsSubpathForSandboxProfile: String {
"\"" + self.pathString
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacing("\\", with: "\\\\")
.replacing("\"", with: "\\\"")
+ "\""
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/Serialization/SerializedJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension SerializedJSON: ExpressibleByStringInterpolation {
fileprivate var value: String = ""

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

public init(literalCapacity: Int, interpolationCount: Int) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commands/Utilities/DOTManifestSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct DOTManifestSerializer {

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

mutating func writeDOT(to stream: OutputByteStream) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageLoading/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
let vfsOverlayTempFilePath = tempDir.appending("vfs.yaml")
try VFSOverlay(roots: [
VFSOverlay.File(
name: manifestPath._normalized.replacingOccurrences(of: #"\"#, with: #"\\"#),
name: manifestPath._normalized.replacing(#"\"#, with: #"\\"#),
externalContents: manifestTempFilePath._nativePathString(escaped: true)
)
]).write(to: vfsOverlayTempFilePath, fileSystem: localFileSystem)
Expand Down Expand Up @@ -1061,7 +1061,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {

var environment = Environment.current
#if os(Windows)
let windowsPathComponent = runtimePath.pathString.replacingOccurrences(of: "/", with: "\\")
let windowsPathComponent = runtimePath.pathString.replacing("/", with: "\\")
environment.prependPath(key: .path, value: windowsPathComponent)
#endif

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageLoading/ModuleMapGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public let moduleMapFilename = "module.modulemap"

extension AbsolutePath {
fileprivate var moduleEscapedPathString: String {
return self.pathString.replacingOccurrences(of: "\\", with: "\\\\")
return self.pathString.replacing("\\", with: "\\\\")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageModel/ManifestSourceGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ extension TargetBuildSettingDescription.Kind {
extension String {
fileprivate var quotedForPackageManifest: String {
return "\"" + self
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacing("\\", with: "\\\\")
.replacing("\"", with: "\\\"")
+ "\""
}
}
2 changes: 1 addition & 1 deletion Sources/PackageRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,7 @@ extension HTTPClientHeaders {
return nil
}

return parts[1].replacingOccurrences(of: "\"", with: "")
return parts[1].replacing("\"", with: "")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceControl/GitRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public final class GitRepository: Repository, WorkingCheckout {
}

return stringPaths.map(output.split(whereSeparator: { $0.isNewline }).map {
let string = String($0).replacingOccurrences(of: "\\\\", with: "\\")
let string = String($0).replacing("\\\\", with: "\\")
if string.utf8.first == UInt8(ascii: "\"") {
return String(string.dropFirst(1).dropLast(1))
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/_InternalTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public func testWithTemporaryDirectory<Result>(
body: (AbsolutePath) async throws -> Result
) async throws -> Result {
let cleanedFunction = function.description
.replacingOccurrences(of: "(", with: "")
.replacingOccurrences(of: ")", with: "")
.replacingOccurrences(of: ".", with: "")
.replacingOccurrences(of: ":", with: "_")
.replacing("(", with: "")
.replacing(")", with: "")
.replacing(".", with: "")
.replacing(":", with: "_")
return try await withTemporaryDirectory(prefix: "spm-tests-\(cleanedFunction)") { tmpDirPath in
defer {
// Unblock and remove the tmp dir on deinit.
Expand Down