Skip to content

Commit 454c204

Browse files
authored
Merge pull request #93 from Carthage/modify-escaping-behavior
Actually escape whitespace characters instead of quoting the entire string
2 parents ad81078 + 380442d commit 454c204

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

ReactiveTask.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
CD52F6441DD0411100B2F764 /* Availability.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD52F6431DD0411100B2F764 /* Availability.swift */; };
11+
CD72E5711E7A4A4800E2DE29 /* StringExtensionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD72E5701E7A4A4800E2DE29 /* StringExtensionSpec.swift */; };
1112
D02130921AF87B6500B9EC20 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D02130911AF87B6500B9EC20 /* Result.framework */; };
1213
D0BFEA5E1A2D1E5E00E23194 /* ReactiveTask.h in Headers */ = {isa = PBXBuildFile; fileRef = D0BFEA5D1A2D1E5E00E23194 /* ReactiveTask.h */; settings = {ATTRIBUTES = (Public, ); }; };
1314
D0BFEA641A2D1E5E00E23194 /* ReactiveTask.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0BFEA581A2D1E5E00E23194 /* ReactiveTask.framework */; };
@@ -31,6 +32,7 @@
3132

3233
/* Begin PBXFileReference section */
3334
CD52F6431DD0411100B2F764 /* Availability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Availability.swift; sourceTree = "<group>"; };
35+
CD72E5701E7A4A4800E2DE29 /* StringExtensionSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtensionSpec.swift; sourceTree = "<group>"; };
3436
D02130911AF87B6500B9EC20 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3537
D0BFEA581A2D1E5E00E23194 /* ReactiveTask.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ReactiveTask.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3638
D0BFEA5C1A2D1E5E00E23194 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -132,6 +134,7 @@
132134
D0BFEA671A2D1E5E00E23194 /* ReactiveTaskTests */ = {
133135
isa = PBXGroup;
134136
children = (
137+
CD72E5701E7A4A4800E2DE29 /* StringExtensionSpec.swift */,
135138
D0BFEAA11A2D212800E23194 /* TaskSpec.swift */,
136139
D0BFEA681A2D1E5E00E23194 /* Supporting Files */,
137140
);
@@ -336,6 +339,7 @@
336339
buildActionMask = 2147483647;
337340
files = (
338341
D0BFEAA21A2D212800E23194 /* TaskSpec.swift in Sources */,
342+
CD72E5711E7A4A4800E2DE29 /* StringExtensionSpec.swift in Sources */,
339343
);
340344
runOnlyForDeploymentPostprocessing = 0;
341345
};

Sources/Task.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,21 @@ public struct Task {
4646
}
4747
}
4848

49-
private extension String {
50-
var escaped: String {
51-
if rangeOfCharacter(from: .whitespaces) != nil {
52-
return "\"\(self)\""
53-
} else {
54-
return self
55-
}
49+
extension String {
50+
private static let whitespaceRegularExpression = try! NSRegularExpression(pattern: "\\s")
51+
52+
var escapingWhitespaces: String {
53+
return String.whitespaceRegularExpression.stringByReplacingMatches(
54+
in: self,
55+
range: NSRange(location: 0, length: self.utf16.count),
56+
withTemplate: "\\\\$0"
57+
)
5658
}
5759
}
5860

5961
extension Task: CustomStringConvertible {
6062
public var description: String {
61-
return "\(launchPath) \(arguments.map { $0.escaped }.joined(separator: " "))"
63+
return "\(launchPath) \(arguments.map { $0.escapingWhitespaces }.joined(separator: " "))"
6264
}
6365
}
6466

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Quick
2+
import Nimble
3+
@testable import ReactiveTask
4+
5+
class StringExtensionSpec: QuickSpec {
6+
override func spec() {
7+
describe("escapingWhitespaces") {
8+
it("should escape whitespace characters") {
9+
expect("a b c".escapingWhitespaces).to(equal("a\\ b\\ c"))
10+
expect("d\te\tf".escapingWhitespaces).to(equal("d\\\te\\\tf"))
11+
}
12+
13+
it("should not change the original string if it does not contain whitespaces") {
14+
expect("ReactiveTask").to(equal("ReactiveTask"))
15+
}
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)