Skip to content

Commit f89859f

Browse files
committed
Inline some of the parameter string parsing.
1 parent 8c7da21 commit f89859f

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Sources/SwiftProtobufPluginLibrary/CodeGenerator.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,16 @@ struct InternalCodeGeneratorParameter: CodeGeneratorParameter {
107107
return []
108108
}
109109
let parts = parameter.components(separatedBy: ",")
110-
let asPairs = parts.map { partition(string: $0, atFirstOccurrenceOf: "=") }
111-
let result = asPairs.map { (key: trimWhitespace($0), value: trimWhitespace($1)) }
112-
return result
110+
return parts.map { s -> (key: String, value: String) in
111+
guard let index = s.range(of: "=")?.lowerBound else {
112+
// Key only, no value ("baz" in example).
113+
return (trimWhitespace(s), "")
114+
}
115+
return (
116+
key: trimWhitespace(s[..<index]),
117+
value: trimWhitespace(s[s.index(after: index)...])
118+
)
119+
}
113120
}
114121
}
115122

Sources/SwiftProtobufPluginLibrary/StringUtils.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
import Foundation
1212

13-
func partition(string: String, atFirstOccurrenceOf substring: String) -> (String, String) {
14-
guard let index = string.range(of: substring)?.lowerBound else {
15-
return (string, "")
16-
}
17-
return (String(string[..<index]),
18-
String(string[string.index(after: index)...]))
13+
@inlinable
14+
func trimWhitespace(_ s: String) -> String {
15+
return s.trimmingCharacters(in: .whitespacesAndNewlines)
1916
}
2017

21-
func trimWhitespace(_ s: String) -> String {
18+
@inlinable
19+
func trimWhitespace(_ s: String.SubSequence) -> String {
2220
return s.trimmingCharacters(in: .whitespacesAndNewlines)
2321
}

0 commit comments

Comments
 (0)