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
;
6 using System
.Runtime
.CompilerServices
;
11 internal static partial class PasteArguments
14 /// Repastes a set of arguments into a linear string that parses back into the originals under pre- or post-2008 VC parsing rules.
15 /// The rules for parsing the executable name (argv[0]) are special, so you must indicate whether the first argument actually is argv[0].
17 internal static string Paste(IEnumerable
<string> arguments
, bool pasteFirstArgumentUsingArgV0Rules
)
19 var stringBuilder
= new StringBuilder();
21 foreach (string argument
in arguments
)
23 if (pasteFirstArgumentUsingArgV0Rules
)
25 pasteFirstArgumentUsingArgV0Rules
= false;
27 // Special rules for argv[0]
28 // - Backslash is a normal character.
29 // - Quotes used to include whitespace characters.
30 // - Parsing ends at first whitespace outside quoted region.
31 // - No way to get a literal quote past the parser.
33 bool hasWhitespace
= false;
34 foreach (char c
in argument
)
38 throw new ApplicationException("The argv[0] argument cannot include a double quote.");
40 if (char.IsWhiteSpace(c
))
45 if (argument
.Length
== 0 || hasWhitespace
)
47 stringBuilder
.Append(Quote
);
48 stringBuilder
.Append(argument
);
49 stringBuilder
.Append(Quote
);
53 stringBuilder
.Append(argument
);
58 AppendArgument(stringBuilder
, argument
);
62 return stringBuilder
.ToString();