Skip to content

Commit 81d1e04

Browse files
committed
Fixing issue rapierorg#38 (Interception of incomplete commands)
1 parent 8a43f95 commit 81d1e04

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Sources/TelegramBotSDK/Router/Command.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public class Command {
2727
/// By default prefixing is optional.
2828
/// Ignored if `exactMatch` flag is set.
2929
public static let slashRequired = Options(rawValue: 1 << 1)
30-
30+
3131
/// Case sensitive comparision of commands.
3232
public static let caseSensitive = Options(rawValue: 1 << 2)
3333
}
34-
34+
3535
static let whitespaceAndNewline = CharacterSet.whitespacesAndNewlines
3636

3737
let name: String
3838
let nameWords: [String]
3939
let options: Options
40-
40+
4141
public init(_ name: String, options: Options = []) {
4242
self.options = options
4343
let finalName: String
@@ -50,7 +50,7 @@ public class Command {
5050
self.name = finalName
5151
nameWords = finalName.components(separatedBy: T.whitespaceAndNewline)
5252
}
53-
53+
5454
public func fetchFrom(_ scanner: Scanner, caseSensitive: Bool = false) -> (command: String, startsWithSlash: Bool)? {
5555
if nameWords.isEmpty {
5656
// This is "match all" rule
@@ -69,45 +69,45 @@ public class Command {
6969
var userCommand = ""
7070
var isFirstWord = true
7171
var firstWordStartsWithSlash = false
72-
72+
7373
// Each word in nameWords should match a word (possibly abbreviated) from scanner
7474
for nameWord in nameWords {
7575
guard let word = scanner.scanUpToCharacters(from: T.whitespaceAndNewline) else {
7676
return nil
7777
}
78-
78+
7979
if isFirstWord {
8080
firstWordStartsWithSlash = word.hasPrefix("/")
8181
}
82-
82+
8383
if options.contains(.exactMatch) {
84-
84+
8585
guard nameWord.hasPrefix(word, caseInsensitive: !caseSensitive) else {
8686
return nil
8787
}
88-
88+
8989
userCommand += word
90-
90+
9191
} else {
92-
92+
9393
if isFirstWord && options.contains(.slashRequired) {
9494
guard firstWordStartsWithSlash else { return nil }
9595
}
96-
96+
9797
let processedWord: String
9898
if isFirstWord && firstWordStartsWithSlash {
9999
processedWord = word.substring(from: word.index(after: word.startIndex))
100100
} else {
101101
processedWord = word
102102
}
103-
104-
guard nameWord.hasPrefix(processedWord, caseInsensitive: !caseSensitive) else {
103+
104+
guard nameWord.compare(processedWord, options: !caseSensitive ? [.caseInsensitive] : []) == .orderedSame else {
105105
return nil
106106
}
107-
107+
108108
userCommand += processedWord
109109
}
110-
110+
111111
isFirstWord = false
112112
}
113113
return (userCommand, firstWordStartsWithSlash)

0 commit comments

Comments
 (0)