Files
Stephen Toub c256e409c9 More Corelib cleanup (dotnet/coreclr#26872)
* Remove unnecessary asserts

* Make several classes static

* Use is instead of as+null check

* Use T? instead of Nullable<T>

* more static classes

* Mark locals as const

* Merge declaration and initialization of some variables

* Remove unnecessary casts

* Remove unnecessary "unsafe"s

* Simplify several lambda expressions

* Remove redundant parentheses

* Remove redundant '== true'

* Remove redundant empty lines

* Simplify boolean comparison with '== false'

* Replace if-statement with return statement

* Use while for infinite loop

* Add static to all partial static class declarations

* Use ++/-- operator instead of assignment

* Use string.IsNullOrEmpty

* Use coalesce expression

* Simplify lazy initialization

* Use coalese expression

* Join string expressions

* Use regular string literal instead of verbatim string literal

* Optimize StringBuilder.Append calls

* Remove redundant assignment

* Remove unnecessary unsafe context

* Merge processor directives

* Use String.Equals instead of String.Compare

* Use Debug.Fail instead of Debug.Assert(false

* Remove Attribute suffix

* Use predefined type

* Use compound assignment

* Use while statement to create an infinite loop

* Remove redundant base ctor call

* Avoid using catch (Exception)

* Remove empty regions

* Span comparison to null

* Avoid unnecessary boxing of value type

* Expression is always equal to 'true'

* Remove unused method

* update coalesce assignment

* fix unsafe

* fix redundant parens

* Fix whitespace errors introduced

Signed-off-by: dotnet-bot <[email protected]>
2019-09-25 20:13:47 +02:00

65 lines
2.4 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Text;
namespace System
{
internal static partial class PasteArguments
{
/// <summary>
/// Repastes a set of arguments into a linear string that parses back into the originals under pre- or post-2008 VC parsing rules.
/// The rules for parsing the executable name (argv[0]) are special, so you must indicate whether the first argument actually is argv[0].
/// </summary>
internal static string Paste(IEnumerable<string> arguments, bool pasteFirstArgumentUsingArgV0Rules)
{
var stringBuilder = new StringBuilder();
foreach (string argument in arguments)
{
if (pasteFirstArgumentUsingArgV0Rules)
{
pasteFirstArgumentUsingArgV0Rules = false;
// Special rules for argv[0]
// - Backslash is a normal character.
// - Quotes used to include whitespace characters.
// - Parsing ends at first whitespace outside quoted region.
// - No way to get a literal quote past the parser.
bool hasWhitespace = false;
foreach (char c in argument)
{
if (c == Quote)
{
throw new ApplicationException("The argv[0] argument cannot include a double quote.");
}
if (char.IsWhiteSpace(c))
{
hasWhitespace = true;
}
}
if (argument.Length == 0 || hasWhitespace)
{
stringBuilder.Append(Quote);
stringBuilder.Append(argument);
stringBuilder.Append(Quote);
}
else
{
stringBuilder.Append(argument);
}
}
else
{
AppendArgument(stringBuilder, argument);
}
}
return stringBuilder.ToString();
}
}
}