|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +namespace Dotnet.Script.DependencyModel.Internal |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// <para> |
| 9 | + /// Performs operations on <see cref="System.String"/> instances that contain command line information. |
| 10 | + /// </para> |
| 11 | + /// <para> |
| 12 | + /// Tip: a ready-to-use package with this functionality is available at https://www.nuget.org/packages/Gapotchenko.FX.Diagnostics.CommandLine. |
| 13 | + /// </para> |
| 14 | + /// </summary> |
| 15 | + /// <summary> |
| 16 | + /// Available |
| 17 | + /// </summary> |
| 18 | + static class CommandLine |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Escapes and optionally quotes a command line argument. |
| 22 | + /// </summary> |
| 23 | + /// <param name="value">The command line argument.</param> |
| 24 | + /// <returns>The escaped and optionally quoted command line argument.</returns> |
| 25 | + public static string EscapeArgument(string value) |
| 26 | + { |
| 27 | + if (value == null) |
| 28 | + return null; |
| 29 | + |
| 30 | + int length = value.Length; |
| 31 | + if (length == 0) |
| 32 | + return string.Empty; |
| 33 | + |
| 34 | + var sb = new StringBuilder(); |
| 35 | + Escape.AppendQuotedText(sb, value); |
| 36 | + |
| 37 | + if (sb.Length == length) |
| 38 | + return value; |
| 39 | + |
| 40 | + return sb.ToString(); |
| 41 | + } |
| 42 | + |
| 43 | + static class Escape |
| 44 | + { |
| 45 | + public static void AppendQuotedText(StringBuilder sb, string text) |
| 46 | + { |
| 47 | + bool quotingRequired = IsQuotingRequired(text); |
| 48 | + if (quotingRequired) |
| 49 | + sb.Append('"'); |
| 50 | + |
| 51 | + int numberOfQuotes = 0; |
| 52 | + for (int i = 0; i < text.Length; i++) |
| 53 | + { |
| 54 | + if (text[i] == '"') |
| 55 | + numberOfQuotes++; |
| 56 | + } |
| 57 | + |
| 58 | + if (numberOfQuotes > 0) |
| 59 | + { |
| 60 | + if ((numberOfQuotes % 2) != 0) |
| 61 | + throw new Exception("Command line parameter cannot contain an odd number of double quotes."); |
| 62 | + text = text.Replace("\\\"", "\\\\\"").Replace("\"", "\\\""); |
| 63 | + } |
| 64 | + |
| 65 | + sb.Append(text); |
| 66 | + |
| 67 | + if (quotingRequired && text.EndsWith("\\")) |
| 68 | + sb.Append('\\'); |
| 69 | + |
| 70 | + if (quotingRequired) |
| 71 | + sb.Append('"'); |
| 72 | + } |
| 73 | + |
| 74 | + static bool IsQuotingRequired(string parameter) => |
| 75 | + !AllowedUnquotedRegex.IsMatch(parameter) || |
| 76 | + DefinitelyNeedQuotesRegex.IsMatch(parameter); |
| 77 | + |
| 78 | + static Regex m_CachedAllowedUnquotedRegex; |
| 79 | + |
| 80 | + static Regex AllowedUnquotedRegex => |
| 81 | + m_CachedAllowedUnquotedRegex ??= new Regex( |
| 82 | + @"^[a-z\\/:0-9\._\-+=]*$", |
| 83 | + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); |
| 84 | + |
| 85 | + static Regex m_CachedDefinitelyNeedQuotesRegex; |
| 86 | + |
| 87 | + static Regex DefinitelyNeedQuotesRegex => |
| 88 | + m_CachedDefinitelyNeedQuotesRegex ??= new Regex( |
| 89 | + "[|><\\s,;\"]+", |
| 90 | + RegexOptions.CultureInvariant); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments