Skip to content

Commit f385144

Browse files
authored
Reduce use of raw \n to improve cross-platform compatibility (#7322)
This is really just a start, there are a few hundred more occurences of raw `\n` in the codebase that may or may not be problematic.
1 parent cc05a6b commit f385144

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public final class SwiftTargetBuildDescription {
196196
// but it is the closest to accurate we can do at this point
197197
func containsAtMain(fileSystem: FileSystem, path: AbsolutePath) throws -> Bool {
198198
let content: String = try self.fileSystem.readFileContents(path)
199-
let lines = content.split(separator: "\n").compactMap { String($0).spm_chuzzle() }
199+
let lines = content.split(whereSeparator: { $0.isNewline }).map { $0.trimmingCharacters(in: .whitespaces) }
200200

201201
var multilineComment = false
202202
for line in lines {

Sources/Build/BuildOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
503503
metadata.pluginName = result.plugin.name
504504
return metadata
505505
}
506-
for line in result.textOutput.split(separator: "\n") {
506+
for line in result.textOutput.split(whereSeparator: { $0.isNewline }) {
507507
diagnosticsEmitter.emit(info: line)
508508
}
509509
for diag in result.diagnostics {

Sources/SPMTestSupport/MockRegistry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private struct MockRegistryArchiver: Archiver {
450450

451451
private func readFileContents(_ path: AbsolutePath) throws -> [String] {
452452
let content: String = try self.fileSystem.readFileContents(path)
453-
return content.split(separator: "\n").map(String.init)
453+
return content.split(whereSeparator: { $0.isNewline }).map(String.init)
454454
}
455455
}
456456

Sources/SourceControl/GitRepository.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public final class GitRepository: Repository, WorkingCheckout {
505505
"remote",
506506
failureMessage: "Couldn’t get the list of remotes"
507507
)
508-
let remoteNames = remoteNamesOutput.split(separator: "\n").map(String.init)
508+
let remoteNames = remoteNamesOutput.split(whereSeparator: { $0.isNewline }).map(String.init)
509509
return try remoteNames.map { name in
510510
// For each remote get the url.
511511
let url = try callGit(
@@ -529,7 +529,7 @@ public final class GitRepository: Repository, WorkingCheckout {
529529
try self.cachedBranches.memoize {
530530
try self.lock.withLock {
531531
let branches = try callGit("branch", "-l", failureMessage: "Couldn’t get the list of branches")
532-
return branches.split(separator: "\n").map { $0.dropFirst(2) }.map(String.init)
532+
return branches.split(whereSeparator: { $0.isNewline }).map { $0.dropFirst(2) }.map(String.init)
533533
}
534534
}
535535
}
@@ -546,7 +546,7 @@ public final class GitRepository: Repository, WorkingCheckout {
546546
"-l",
547547
failureMessage: "Couldn’t get the list of tags"
548548
)
549-
return tagList.split(separator: "\n").map(String.init)
549+
return tagList.split(whereSeparator: { $0.isNewline }).map(String.init)
550550
}
551551
}
552552
}
@@ -780,7 +780,7 @@ public final class GitRepository: Repository, WorkingCheckout {
780780
output = try error.result.utf8Output().spm_chomp()
781781
}
782782

783-
return stringPaths.map(output.split(separator: "\n").map {
783+
return stringPaths.map(output.split(whereSeparator: { $0.isNewline }).map {
784784
let string = String($0).replacingOccurrences(of: "\\\\", with: "\\")
785785
if string.utf8.first == UInt8(ascii: "\"") {
786786
return String(string.dropFirst(1).dropLast(1))

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ final class BuildToolTests: CommandsTestCase {
369369
do {
370370
let result = try execute(packagePath: fixturePath)
371371
XCTAssertMatch(result.stdout, .regex("\\[[1-9][0-9]*\\/[1-9][0-9]*\\] Compiling"))
372-
let lines = result.stdout.split(separator: "\n")
372+
let lines = result.stdout.split(whereSeparator: { $0.isNewline })
373373
XCTAssertMatch(String(lines.last!), .regex("Build complete! \\([0-9]*\\.[0-9]*s\\)"))
374374
}
375375

@@ -382,7 +382,7 @@ final class BuildToolTests: CommandsTestCase {
382382
// test third time, to make sure message is presented even when nothing to build (cached)
383383
let result = try execute(packagePath: fixturePath)
384384
XCTAssertNoMatch(result.stdout, .regex("\\[[1-9][0-9]*\\/[1-9][0-9]*\\] Compiling"))
385-
let lines = result.stdout.split(separator: "\n")
385+
let lines = result.stdout.split(whereSeparator: { $0.isNewline })
386386
XCTAssertMatch(String(lines.last!), .regex("Build complete! \\([0-9]*\\.[0-9]*s\\)"))
387387
}
388388
}

Tests/CommandsTests/RunToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class RunToolTests: CommandsTestCase {
7979
func testUnreachableExecutable() throws {
8080
try fixture(name: "Miscellaneous/UnreachableTargets") { fixturePath in
8181
let (output, _) = try execute(["bexec"], packagePath: fixturePath.appending("A"))
82-
let outputLines = output.split(separator: "\n")
82+
let outputLines = output.split(whereSeparator: { $0.isNewline })
8383
XCTAssertMatch(String(outputLines[0]), .contains("BTarget2"))
8484
}
8585
}

Tests/FunctionalTests/PluginTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class PluginTests: XCTestCase {
474474
func pluginEmittedOutput(_ data: Data) {
475475
// Add each line of emitted output as a `.info` diagnostic.
476476
dispatchPrecondition(condition: .onQueue(delegateQueue))
477-
let textlines = String(decoding: data, as: UTF8.self).split(separator: "\n")
477+
let textlines = String(decoding: data, as: UTF8.self).split(whereSeparator: { $0.isNewline })
478478
print(textlines.map{ "[TEXT] \($0)" }.joined(separator: "\n"))
479479
diagnostics.append(contentsOf: textlines.map{
480480
Basics.Diagnostic(severity: .info, message: String($0), metadata: .none)
@@ -756,7 +756,7 @@ class PluginTests: XCTestCase {
756756
func pluginEmittedOutput(_ data: Data) {
757757
// Add each line of emitted output as a `.info` diagnostic.
758758
dispatchPrecondition(condition: .onQueue(delegateQueue))
759-
let textlines = String(decoding: data, as: UTF8.self).split(separator: "\n")
759+
let textlines = String(decoding: data, as: UTF8.self).split(whereSeparator: { $0.isNewline })
760760
diagnostics.append(contentsOf: textlines.map{
761761
Basics.Diagnostic(severity: .info, message: String($0), metadata: .none)
762762
})

0 commit comments

Comments
 (0)