Skip to content

Commit bcb2f00

Browse files
committed
Migrate PianoScaleCommand to regex literals
1 parent 82bb174 commit bcb2f00

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: Sources/D2Commands/music/piano/PianoScaleCommand.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Utils
22
import MusicTheory
33

4-
fileprivate let argsPattern = try! LegacyRegex(from: "(?:(\\w+)\\s+)?(\\w+[b#]?)")
4+
fileprivate let argsPattern = #/(?:(?<scale>\w+)\s+)?(?<key>\w+[b#]?)/#
55
fileprivate let scales: [String: (Note) -> Scale] = [
66
"major": MajorScale.init,
77
"minor": MinorScale.init,
@@ -29,13 +29,13 @@ public class PianoScaleCommand: StringCommand {
2929

3030
public func invoke(with input: String, output: any CommandOutput, context: CommandContext) {
3131
do {
32-
guard let parsedArgs = argsPattern.firstGroups(in: input) else {
32+
guard let parsedArgs = try? argsPattern.firstMatch(in: input) else {
3333
output.append(errorText: info.helpText!)
3434
return
3535
}
3636

37-
let rawScale = parsedArgs[1].nilIfEmpty ?? defaultScale
38-
let rawKey = parsedArgs[2].nilIfEmpty ?? "C3"
37+
let rawScale = parsedArgs.scale.map { String($0) } ?? defaultScale
38+
let rawKey = String(parsedArgs.key)
3939

4040
guard let scale = scales[rawScale] else {
4141
output.append(errorText: "Unknown scale `\(rawScale)`. Try one of these: \(scales.keys.map { "`\($0)`" }.joined(separator: ", "))")

0 commit comments

Comments
 (0)