Skip to content

Commit

Permalink
Replace some LegacyRegex instantiations with regex literals
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Feb 13, 2024
1 parent abf6f26 commit d12bb86
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/D2Commands/CommandCategory.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Utils

fileprivate let emojiPattern = try! LegacyRegex(from: ":[^:]:")
fileprivate let emojiPattern = #/:[^:]:/#

public enum CommandCategory: String, CaseIterable, CustomStringConvertible, Equatable {
case administration
Expand Down Expand Up @@ -57,6 +57,6 @@ public enum CommandCategory: String, CaseIterable, CustomStringConvertible, Equa
}
}
public var plainDescription: String {
emojiPattern.replace(in: description, with: "").trimmingCharacters(in: .whitespacesAndNewlines)
description.replacing(emojiPattern, with: "").trimmingCharacters(in: .whitespacesAndNewlines)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import D2MessageIO
import D2Permissions
import Utils

fileprivate let inputPattern = try! LegacyRegex(from: "(?:(?:(?:<\\S+>)|(?:@\\S+))\\s+)+(.+)")
fileprivate let inputPattern = #/(?:(?:(?:<\S+>)|(?:@\S+))\s+)+(?<level>.+)/#

public class GrantPermissionCommand: StringCommand {
public let info = CommandInfo(
Expand All @@ -18,8 +18,8 @@ public class GrantPermissionCommand: StringCommand {
}

public func invoke(with input: String, output: any CommandOutput, context: CommandContext) {
if let parsedArgs = inputPattern.firstGroups(in: input) {
let rawLevel = parsedArgs[1]
if let parsedArgs = try? inputPattern.firstMatch(in: input) {
let rawLevel = String(parsedArgs.level)
if let level = PermissionLevel.of(rawLevel) {
var response = ""
var changedPermissions = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import D2MessageIO
import D2Permissions
import Utils

fileprivate let inputPattern = try! LegacyRegex(from: "(?:(?:<\\S+>)|(?:@\\S+))(?:\\s+(?:(?:<\\S+>)|(?:@\\S+)))*\\s*")
fileprivate let inputPattern = #/(?:(?:<\S+>)|(?:@\S+))(?:\s+(?:(?:<\S+>)|(?:@\S+)))*\s*/#

public class RevokePermissionCommand: StringCommand {
public let info = CommandInfo(
Expand All @@ -18,7 +18,7 @@ public class RevokePermissionCommand: StringCommand {
}

public func invoke(with input: String, output: any CommandOutput, context: CommandContext) {
if inputPattern.matchCount(in: input) > 0 {
if input.matches(of: inputPattern).count > 0 {
var response = ""
var changedPermissions = false

Expand Down

0 comments on commit d12bb86

Please sign in to comment.