diff --git a/Sources/MpcNET/Types/IFilter.cs b/Sources/MpcNET/Types/IFilter.cs index 78dfdb3..864184d 100644 --- a/Sources/MpcNET/Types/IFilter.cs +++ b/Sources/MpcNET/Types/IFilter.cs @@ -100,8 +100,6 @@ namespace MpcNET.Types } /// - /// Gets the formatted command - /// /// String values are quoted with single or double quotes, /// and special characters within those values must be escaped with the backslash (\). /// Keep in mind that the backslash is also the escape character on the protocol level, @@ -115,21 +113,28 @@ namespace MpcNET.Types /// /// (https://mpd.readthedocs.io/en/stable/protocol.html#filters) /// + /// Value to escape + /// + private string escape(string value) + { + var escapedValue = value.Replace(@"\", @"\\\\") + .Replace("'", @"\\'") + .Replace(@"""", @"\\\"""); + + return $@"\""{escapedValue}\"""; + } + + /// + /// Gets the formatted command + /// public string GetFormattedCommand() { - string make = ""; + string command = $"({Name} {Operator.GetDescription()} {escape(Value)})"; if (Negation) - make += "(!"; - - make += "(" + Name + " "; - make += Operator.GetDescription(); - make += " " + "\\\"" + Value + "\\\"" + ")"; - - if (Negation) - make += ")"; - - return make; + return $"(!{command})"; + else + return command; } } }