Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / PasteArguments.Windows.cs
blob7cdcbc4533a0538d7512573fa7c41910ee682a5d
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;
7 using System.Text;
9 namespace System
11 internal static partial class PasteArguments
13 /// <summary>
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].
16 /// </summary>
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)
36 if (c == Quote)
38 throw new ApplicationException("The argv[0] argument cannot include a double quote.");
40 if (char.IsWhiteSpace(c))
42 hasWhitespace = true;
45 if (argument.Length == 0 || hasWhitespace)
47 stringBuilder.Append(Quote);
48 stringBuilder.Append(argument);
49 stringBuilder.Append(Quote);
51 else
53 stringBuilder.Append(argument);
56 else
58 AppendArgument(stringBuilder, argument);
62 return stringBuilder.ToString();