mirror of
https://github.com/swiftbar/SwiftBar.git
synced 2024-07-14 19:20:52 +03:00
Improve shell escaping heuristics #402
Signed-off-by: Alex Mazanov <alexandr.mazanov@gmail.com>
This commit is contained in:
parent
28ef89ff82
commit
d81efbc3dc
@ -34,17 +34,23 @@ extension String {
|
||||
|
||||
extension String {
|
||||
var isEnclosedInQuotes: Bool {
|
||||
let regex = #"^(?:'.*'|".*")$"#
|
||||
return range(of: regex, options: .regularExpression) != nil
|
||||
hasPrefix("'") && hasSuffix("'")
|
||||
}
|
||||
|
||||
var needsShellQuoting: Bool {
|
||||
let specialCharacters = " \t\n\"'`$\\|&;()<>[]*?{}!^~#%"
|
||||
let shellOperators = ["&&", "||", ";", "|", "<", ">"]
|
||||
|
||||
// Check if the string is exactly a logical operator
|
||||
if shellOperators.contains(self) {
|
||||
return false
|
||||
}
|
||||
|
||||
return rangeOfCharacter(from: CharacterSet(charactersIn: specialCharacters)) != nil
|
||||
}
|
||||
|
||||
func quoteIfNeeded() -> String {
|
||||
guard needsShellQuoting else { return self }
|
||||
return isEnclosedInQuotes ? self : "\'\(self)\'"
|
||||
return isEnclosedInQuotes ? self : "\'\(replacingOccurrences(of: "'", with: "'\\''"))\'"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user