Skip to content

Commit faf0d48

Browse files
committed
Merge pull request #273 from ddunbar/optimize-YAML-quote
[Build] Optimize YAML quoting for strings.
2 parents cc42506 + cd293e6 commit faf0d48

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Sources/Build/YAML.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ protocol YAMLRepresentable {
1515
extension String: YAMLRepresentable {
1616
var YAML: String {
1717
if self == "" { return "\"\"" }
18+
for c in utf8 {
19+
switch c {
20+
case UInt8(ascii: "@"), UInt8(ascii: " "), UInt8(ascii: "-"), UInt8(ascii: "&"):
21+
return "\"\(self)\""
22+
default:
23+
continue
24+
}
25+
}
1826
return self
1927
}
2028
}
@@ -28,15 +36,6 @@ extension Bool: YAMLRepresentable {
2836

2937
extension Array where Element: YAMLRepresentable {
3038
var YAML: String {
31-
func quote(_ input: String) -> String {
32-
for c in input.characters {
33-
if c == "@" || c == " " || c == "-" || c == "&" {
34-
return "\"\(input)\""
35-
}
36-
}
37-
return input
38-
}
39-
let stringArray = self.flatMap { String($0) }
40-
return "[" + stringArray.map(quote).joined(separator: ", ") + "]"
39+
return "[" + map{$0.YAML}.joined(separator: ", ") + "]"
4140
}
4241
}

0 commit comments

Comments
 (0)