@@ -27,17 +27,17 @@ public class Command {
27
27
/// By default prefixing is optional.
28
28
/// Ignored if `exactMatch` flag is set.
29
29
public static let slashRequired = Options ( rawValue: 1 << 1 )
30
-
30
+
31
31
/// Case sensitive comparision of commands.
32
32
public static let caseSensitive = Options ( rawValue: 1 << 2 )
33
33
}
34
-
34
+
35
35
static let whitespaceAndNewline = CharacterSet . whitespacesAndNewlines
36
36
37
37
let name : String
38
38
let nameWords : [ String ]
39
39
let options : Options
40
-
40
+
41
41
public init ( _ name: String , options: Options = [ ] ) {
42
42
self . options = options
43
43
let finalName : String
@@ -50,7 +50,7 @@ public class Command {
50
50
self . name = finalName
51
51
nameWords = finalName. components ( separatedBy: T . whitespaceAndNewline)
52
52
}
53
-
53
+
54
54
public func fetchFrom( _ scanner: Scanner , caseSensitive: Bool = false ) -> ( command: String , startsWithSlash: Bool ) ? {
55
55
if nameWords. isEmpty {
56
56
// This is "match all" rule
@@ -69,45 +69,45 @@ public class Command {
69
69
var userCommand = " "
70
70
var isFirstWord = true
71
71
var firstWordStartsWithSlash = false
72
-
72
+
73
73
// Each word in nameWords should match a word (possibly abbreviated) from scanner
74
74
for nameWord in nameWords {
75
75
guard let word = scanner. scanUpToCharacters ( from: T . whitespaceAndNewline) else {
76
76
return nil
77
77
}
78
-
78
+
79
79
if isFirstWord {
80
80
firstWordStartsWithSlash = word. hasPrefix ( " / " )
81
81
}
82
-
82
+
83
83
if options. contains ( . exactMatch) {
84
-
84
+
85
85
guard nameWord. hasPrefix ( word, caseInsensitive: !caseSensitive) else {
86
86
return nil
87
87
}
88
-
88
+
89
89
userCommand += word
90
-
90
+
91
91
} else {
92
-
92
+
93
93
if isFirstWord && options. contains ( . slashRequired) {
94
94
guard firstWordStartsWithSlash else { return nil }
95
95
}
96
-
96
+
97
97
let processedWord : String
98
98
if isFirstWord && firstWordStartsWithSlash {
99
99
processedWord = word. substring ( from: word. index ( after: word. startIndex) )
100
100
} else {
101
101
processedWord = word
102
102
}
103
-
104
- guard nameWord. hasPrefix ( processedWord, caseInsensitive : !caseSensitive) else {
103
+
104
+ guard nameWord. compare ( processedWord, options : !caseSensitive ? [ . caseInsensitive ] : [ ] ) == . orderedSame else {
105
105
return nil
106
106
}
107
-
107
+
108
108
userCommand += processedWord
109
109
}
110
-
110
+
111
111
isFirstWord = false
112
112
}
113
113
return ( userCommand, firstWordStartsWithSlash)
0 commit comments