Skip to content

Commit

Permalink
Move consts and methods to single CharExtensions class (#9992)
Browse files Browse the repository at this point in the history
  • Loading branch information
iSazonov authored and TravisEz13 committed Jun 25, 2019
1 parent 86a1697 commit d639046
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 77 deletions.
Expand Up @@ -11,10 +11,10 @@
using System.Management.Automation.Configuration;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;

using Dbg = System.Management.Automation.Diagnostics;

Expand Down Expand Up @@ -573,7 +573,7 @@ private static (string SwitchKey, bool ShouldBreak) GetSwitchKey(string[] args,
return (SwitchKey: null, ShouldBreak: false);
}

if (!SpecialCharacters.IsDash(switchKey[0]) && switchKey[0] != '/')
if (!CharExtensions.IsDash(switchKey[0]) && switchKey[0] != '/')
{
// then its a file
if (parser != null)
Expand All @@ -590,7 +590,7 @@ private static (string SwitchKey, bool ShouldBreak) GetSwitchKey(string[] args,
switchKey = switchKey.Substring(1);

// chop off the second dash so we're agnostic wrt specifying - or --
if (!string.IsNullOrEmpty(switchKey) && SpecialCharacters.IsDash(switchKey[0]))
if (!string.IsNullOrEmpty(switchKey) && CharExtensions.IsDash(switchKey[0]))
{
switchKey = switchKey.Substring(1);
}
Expand Down Expand Up @@ -889,7 +889,7 @@ private void ParseHelper(string[] args)
{
string arg = args[i];

if (!string.IsNullOrEmpty(arg) && SpecialCharacters.IsDash(arg[0]))
if (!string.IsNullOrEmpty(arg) && CharExtensions.IsDash(arg[0]))
{
break;
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@ bool TryGetBoolValue(string arg, out bool boolValue)
_collectedArgs.Add(new CommandParameter(pendingParameter, arg));
pendingParameter = null;
}
else if (!string.IsNullOrEmpty(arg) && SpecialCharacters.IsDash(arg[0]) && arg.Length > 1)
else if (!string.IsNullOrEmpty(arg) && CharExtensions.IsDash(arg[0]) && arg.Length > 1)
{
int offset = arg.IndexOf(':');
if (offset >= 0)
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/engine/CmdletInfo.cs
Expand Up @@ -3,7 +3,7 @@

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Management.Automation.Language;
using System.Text;

namespace System.Management.Automation
Expand Down Expand Up @@ -173,7 +173,7 @@ internal static bool SplitCmdletName(string name, out string verb, out string no
int index = 0;
for (int i = 0; i < name.Length; i++)
{
if (SpecialCharacters.IsDash(name[i]))
if (CharExtensions.IsDash(name[i]))
{
index = i;
break;
Expand Down
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation.Language;
using System.Text;

namespace System.Management.Automation
Expand Down Expand Up @@ -449,7 +450,7 @@ internal string GetParameterSetName(uint parameterSet)
new Collection<MergedCompiledCommandParameter>();

// Skip the leading '-' if present
if (name.Length > 0 && SpecialCharacters.IsDash(name[0]))
if (name.Length > 0 && CharExtensions.IsDash(name[0]))
{
name = name.Substring(1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/engine/lang/codegen.cs
Expand Up @@ -29,7 +29,7 @@ public static string EscapeSingleQuotedStringContent(string value)
foreach (char c in value)
{
sb.Append(c);
if (SpecialCharacters.IsSingleQuote(c))
if (CharExtensions.IsSingleQuote(c))
{
// double-up quotes to escape them
sb.Append(c);
Expand Down Expand Up @@ -76,7 +76,7 @@ public static string EscapeFormatStringContent(string value)
foreach (char c in value)
{
sb.Append(c);
if (SpecialCharacters.IsCurlyBracket(c))
if (CharExtensions.IsCurlyBracket(c))
{
// double-up curly brackets to escape them
sb.Append(c);
Expand Down
67 changes: 0 additions & 67 deletions src/System.Management.Automation/engine/lang/parserutils.cs
Expand Up @@ -21,73 +21,6 @@

namespace System.Management.Automation
{
#region SpecialCharacters
/// <summary>
/// Define the various unicode special characters that
/// the parser has to deal with.
/// </summary>
internal static class SpecialCharacters
{
public const char enDash = (char)0x2013;
public const char emDash = (char)0x2014;
public const char horizontalBar = (char)0x2015;

public const char quoteSingleLeft = (char)0x2018; // left single quotation mark
public const char quoteSingleRight = (char)0x2019; // right single quotation mark
public const char quoteSingleBase = (char)0x201a; // single low-9 quotation mark
public const char quoteReversed = (char)0x201b; // single high-reversed-9 quotation mark
public const char quoteDoubleLeft = (char)0x201c; // left double quotation mark
public const char quoteDoubleRight = (char)0x201d; // right double quotation mark
public const char quoteLowDoubleLeft = (char)0x201E;// low double left quote used in german.

public static bool IsDash(char c)
{
return (c == enDash || c == emDash || c == horizontalBar || c == '-');
}

public static bool IsSingleQuote(char c)
{
return (c == quoteSingleLeft || c == quoteSingleRight || c == quoteSingleBase ||
c == quoteReversed || c == '\'');
}

public static bool IsDoubleQuote(char c)
{
return (c == '"' || c == quoteDoubleLeft || c == quoteDoubleRight || c == quoteLowDoubleLeft);
}

public static bool IsQuote(char c)
{
return (IsSingleQuote(c) || IsDoubleQuote(c));
}

public static bool IsDelimiter(char c, char delimiter)
{
if (delimiter == '"') return IsDoubleQuote(c);
if (delimiter == '\'') return IsSingleQuote(c);
return (c == delimiter);
}

public static bool IsCurlyBracket(char c)
{
return (c == '{' || c == '}');
}
/// <summary>
/// Canonicalize the quote character - map all of the aliases for " or '
/// into their ascii equivalent.
/// </summary>
/// <param name="c">The character to map.</param>
/// <returns>The mapped character.</returns>
public static char AsQuote(char c)
{
if (IsSingleQuote(c)) return '\'';
if (IsDoubleQuote(c)) return '"';
return (c);
}
};

#endregion SpecialChars

#region Flow Control Exceptions

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/System.Management.Automation/engine/parser/CharTraits.cs
Expand Up @@ -227,6 +227,11 @@ static CharExtensions()
/* 0x7F */ CharTraits.None,
};

public static bool IsCurlyBracket(char c)
{
return (c == '{' || c == '}');
}

// Return true if the character is a whitespace character.
// Newlines are not whitespace.
internal static bool IsWhitespace(this char c)
Expand Down

0 comments on commit d639046

Please sign in to comment.