Skip to content

Commit 2941fb2

Browse files
[NFC] Use extended delimiters for regex strings (#3074)
* [NFC] use extended delimiters for regex strings This removes a lot of backslashes and make the strings more readable. * [NFC] use a multi-line raw string literal for `ToolsVersion.toolsVersionRegex` The spaces used for "scoping" are ignored by setting the `.allowCommentsAndWhitespace` option.
1 parent ed1a7bb commit 2941fb2

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

Diff for: Sources/Commands/SwiftTool.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -767,13 +767,13 @@ private func sandboxProfile(allowedDirectories: [AbsolutePath]) -> String {
767767
stream <<< "(allow file-write*" <<< "\n"
768768
for directory in Platform.darwinCacheDirectories() {
769769
// For compiler module cache.
770-
stream <<< " (regex #\"^\(directory.pathString)/org\\.llvm\\.clang.*\")" <<< "\n"
770+
stream <<< ##" (regex #"^\##(directory.pathString)/org\.llvm\.clang.*")"## <<< "\n"
771771
// For archive tool.
772-
stream <<< " (regex #\"^\(directory.pathString)/ar.*\")" <<< "\n"
772+
stream <<< ##" (regex #"^\##(directory.pathString)/ar.*")"## <<< "\n"
773773
// For xcrun cache.
774-
stream <<< " (regex #\"^\(directory.pathString)/xcrun.*\")" <<< "\n"
774+
stream <<< ##" (regex #"^\##(directory.pathString)/xcrun.*")"## <<< "\n"
775775
// For autolink files.
776-
stream <<< " (regex #\"^\(directory.pathString)/.*\\.(swift|c)-[0-9a-f]+\\.autolink\")" <<< "\n"
776+
stream <<< ##" (regex #"^\##(directory.pathString)/.*\.(swift|c)-[0-9a-f]+\.autolink")"## <<< "\n"
777777
}
778778
for directory in allowedDirectories {
779779
stream <<< " (subpath \"\(directory.pathString)\")" <<< "\n"

Diff for: Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
147147

148148
internal func apiURL(_ url: String) -> Foundation.URL? {
149149
do {
150-
let regex = try NSRegularExpression(pattern: "([^/@]+)[:/]([^:/]+)/([^/]+)\\.git$", options: .caseInsensitive)
150+
let regex = try NSRegularExpression(pattern: #"([^/@]+)[:/]([^:/]+)/([^/]+)\.git$"#, options: .caseInsensitive)
151151
if let match = regex.firstMatch(in: url, options: [], range: NSRange(location: 0, length: url.count)) {
152152
if let hostRange = Range(match.range(at: 1), in: url),
153153
let ownerRange = Range(match.range(at: 2), in: url),

Diff for: Sources/PackageLoading/ManifestLoader.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -900,7 +900,7 @@ private func sandboxProfile(toolsVersion: ToolsVersion, cacheDirectories: [Absol
900900
// Allow writing in temporary locations.
901901
stream <<< "(allow file-write*" <<< "\n"
902902
for directory in Platform.darwinCacheDirectories() {
903-
stream <<< " (regex #\"^\(directory.pathString)/org\\.llvm\\.clang.*\")" <<< "\n"
903+
stream <<< ##" (regex #"^\##(directory.pathString)/org\.llvm\.clang.*")"## <<< "\n"
904904
}
905905
for directory in cacheDirectories {
906906
stream <<< " (subpath \"\(directory.pathString)\")" <<< "\n"

Diff for: Sources/PackageLoading/ToolsVersionLoader.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension Manifest {
6767
do { contents = try fileSystem.getDirectoryContents(packagePath) } catch {
6868
throw ToolsVersionLoader.Error.inaccessiblePackage(path: packagePath, reason: String(describing: error))
6969
}
70-
let regex = try! RegEx(pattern: "^Package@swift-(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?.swift$")
70+
let regex = try! RegEx(pattern: #"^Package@swift-(\d+)(?:\.(\d+))?(?:\.(\d+))?.swift$"#)
7171

7272
// Collect all version-specific manifests at the given package path.
7373
let versionSpecificManifests = Dictionary(contents.compactMap{ file -> (ToolsVersion, String)? in

Diff for: Sources/PackageModel/SwiftLanguageVersion.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct SwiftLanguageVersion {
5757
}
5858

5959
/// Regex for parsing the Swift language version.
60-
private static let regex = try! RegEx(pattern: "^(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?$")
60+
private static let regex = try! RegEx(pattern: #"^(\d+)(?:\.(\d+))?(?:\.(\d+))?$"#)
6161

6262
/// Create an instance of Swift language version from the given string.
6363
///

Diff for: Sources/PackageModel/ToolsVersion.swift

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -35,14 +35,20 @@ public struct ToolsVersion: Equatable, Hashable, Codable {
3535

3636
/// Regex pattern to parse tools version. The format is SemVer 2.0 with an
3737
/// addition that specifying the patch version is optional.
38-
static let toolsVersionRegex = try! NSRegularExpression(pattern: "^" +
39-
"(\\d+)\\.(\\d+)(?:\\.(\\d+))?" +
40-
"(" +
41-
"\\-[A-Za-z\\d]+(?:\\.[A-Za-z\\d]+)*" +
42-
")?" +
43-
"(" +
44-
"\\+[A-Za-z\\d]+(?:\\.[A-Za-z\\d]+)*" +
45-
")?$", options: [])
38+
static let toolsVersionRegex = try! NSRegularExpression(
39+
pattern: #"""
40+
^
41+
(\d+)\.(\d+)(?:\.(\d+))?
42+
(
43+
\-[A-Za-z\d]+(?:\.[A-Za-z\d]+)*
44+
)?
45+
(
46+
\+[A-Za-z\d]+(?:\.[A-Za-z\d]+)*
47+
)?
48+
$
49+
"""#,
50+
options: [.allowCommentsAndWhitespace]
51+
)
4652

4753
/// The major version number.
4854
public var major: Int {

0 commit comments

Comments
 (0)