1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Collections
.Generic
;
10 internal static partial class PasteArguments
13 /// Repastes a set of arguments into a linear string that parses back into the originals under pre- or post-2008 VC parsing rules.
14 /// The rules for parsing the executable name (argv[0]) are special, so you must indicate whether the first argument actually is argv[0].
16 internal static string Paste(IEnumerable
<string> arguments
, bool pasteFirstArgumentUsingArgV0Rules
)
18 var stringBuilder
= new StringBuilder();
20 foreach (string argument
in arguments
)
22 if (pasteFirstArgumentUsingArgV0Rules
)
24 pasteFirstArgumentUsingArgV0Rules
= false;
26 // Special rules for argv[0]
27 // - Backslash is a normal character.
28 // - Quotes used to include whitespace characters.
29 // - Parsing ends at first whitespace outside quoted region.
30 // - No way to get a literal quote past the parser.
32 bool hasWhitespace
= false;
33 foreach (char c
in argument
)
37 throw new ApplicationException("The argv[0] argument cannot include a double quote.");
39 if (char.IsWhiteSpace(c
))
44 if (argument
.Length
== 0 || hasWhitespace
)
46 stringBuilder
.Append(Quote
);
47 stringBuilder
.Append(argument
);
48 stringBuilder
.Append(Quote
);
52 stringBuilder
.Append(argument
);
57 AppendArgument(stringBuilder
, argument
);
61 return stringBuilder
.ToString();