Allow shorter signed hex literals with appropriate type suffixes (#11844)

This commit is contained in:
Joel Sallow (/u/ta11ow)
2020-04-20 16:51:40 -07:00
committed by GitHub
parent 4989167fbf
commit ba53621894
2 changed files with 51 additions and 17 deletions
@@ -1460,17 +1460,28 @@ namespace System.Management.Automation.Language
switch (c)
{
case '0': return '\0';
case 'a': return '\a';
case 'b': return '\b';
case 'e': return '\u001b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'u': return ScanUnicodeEscape(out surrogateCharacter);
case 'v': return '\v';
default: return c;
case '0':
return '\0';
case 'a':
return '\a';
case 'b':
return '\b';
case 'e':
return '\u001b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'u':
return ScanUnicodeEscape(out surrogateCharacter);
case 'v':
return '\v';
default:
return c;
}
}
@@ -3557,8 +3568,9 @@ namespace System.Management.Automation.Language
{
try
{
NumberStyles style = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint |
NumberStyles.AllowExponent;
NumberStyles style = NumberStyles.AllowLeadingSign
| NumberStyles.AllowDecimalPoint
| NumberStyles.AllowExponent;
if (real)
{
@@ -3685,9 +3697,21 @@ namespace System.Management.Automation.Language
}
// If we're expecting a sign bit, remove the leading 0 added in ScanNumberHelper
if (!suffix.HasFlag(NumberSuffixFlags.Unsigned) && ((strNum.Length - 1) & 7) == 0)
if (!suffix.HasFlag(NumberSuffixFlags.Unsigned))
{
strNum = strNum.Slice(1);
var expectedLength = suffix switch
{
NumberSuffixFlags.SignedByte => 2,
NumberSuffixFlags.Short => 4,
NumberSuffixFlags.Long => 16,
// No suffix flag can mean int or long depending on input string length
_ => strNum.Length < 16 ? 8 : 16
};
if (strNum.Length == expectedLength + 1)
{
strNum = strNum.Slice(1);
}
}
style = NumberStyles.AllowHexSpecifier;
@@ -4988,7 +5012,10 @@ namespace System.Management.Automation.Language
_currentIndex = _tokenStart;
c = GetChar();
if (strNum == null) { return ScanGenericToken(c); }
if (strNum == null)
{
return ScanGenericToken(c);
}
}
return NewToken(TokenKind.Exclaim);
@@ -703,7 +703,7 @@ foo``u{2195}abc
if ( $IsLinux -or $IsMacOS ) {
# because we execute on *nix based on executable bit, and the file name doesn't matter
# so we can use the same filename as for windows, just make sure it's executable with chmod
"#!/bin/sh`necho ""Hello World""" | out-file -encoding ASCII $shellfile
"#!/bin/sh`necho ""Hello World""" | Out-File -encoding ASCII $shellfile
/bin/chmod +x $shellfile
}
else {
@@ -931,6 +931,7 @@ foo``u{2195}abc
@{ Script = "0x0y"; ExpectedValue = "0"; ExpectedType = [sbyte] }
@{ Script = "0x41y"; ExpectedValue = "65"; ExpectedType = [sbyte] }
@{ Script = "-0x41y"; ExpectedValue = "-65"; ExpectedType = [sbyte] }
@{ Script = "0xFFy"; ExpectedValue = "-1"; ExpectedType = [sbyte] }
#Binary
@{ Script = "0b0y"; ExpectedValue = "0"; ExpectedType = [sbyte] }
@{ Script = "0b10y"; ExpectedValue = "2"; ExpectedType = [sbyte] }
@@ -957,6 +958,7 @@ foo``u{2195}abc
@{ Script = "0x0s"; ExpectedValue = "0"; ExpectedType = [short] }
@{ Script = "0x41s"; ExpectedValue = "65"; ExpectedType = [short] }
@{ Script = "-0x41s"; ExpectedValue = "-65"; ExpectedType = [short] }
@{ Script = "0xFFFFs"; ExpectedValue = "-1"; ExpectedType = [short] }
#Binary
@{ Script = "0b0s"; ExpectedValue = "0"; ExpectedType = [short] }
@{ Script = "0b10s"; ExpectedValue = "2"; ExpectedType = [short] }
@@ -985,6 +987,7 @@ foo``u{2195}abc
@{ Script = "0x0l"; ExpectedValue = "0"; ExpectedType = [long] }
@{ Script = "0x41l"; ExpectedValue = "65"; ExpectedType = [long] }
@{ Script = "-0x41l"; ExpectedValue = "-65"; ExpectedType = [long] }
@{ Script = "0xFFFFFFFFFFFFFFFFl"; ExpectedValue = "-1"; ExpectedType = [long] }
#Binary
@{ Script = "0b0l"; ExpectedValue = "0"; ExpectedType = [long] }
@{ Script = "0b10l"; ExpectedValue = "2"; ExpectedType = [long] }
@@ -1078,6 +1081,7 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0uy"; ExpectedValue = "0"; ExpectedType = [byte] }
@{ Script = "0x41uy"; ExpectedValue = "65"; ExpectedType = [byte] }
@{ Script = "0xFFuy"; ExpectedValue = [byte]::MaxValue; ExpectedType = [byte] }
#Binary
@{ Script = "0b0uy"; ExpectedValue = "0"; ExpectedType = [byte] }
@{ Script = "0b10uy"; ExpectedValue = "2"; ExpectedType = [byte] }
@@ -1098,6 +1102,8 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0us"; ExpectedValue = "0"; ExpectedType = [ushort] }
@{ Script = "0x41us"; ExpectedValue = "65"; ExpectedType = [ushort] }
@{ Script = "0x41us"; ExpectedValue = "65"; ExpectedType = [ushort] }
@{ Script = "0xFFFFus"; ExpectedValue = [ushort]::MaxValue; ExpectedType = [ushort] }
#Binary
@{ Script = "0b0us"; ExpectedValue = "0"; ExpectedType = [ushort] }
@{ Script = "0b10us"; ExpectedValue = "2"; ExpectedType = [ushort] }
@@ -1119,6 +1125,7 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0ul"; ExpectedValue = "0"; ExpectedType = [ulong] }
@{ Script = "0x41ul"; ExpectedValue = "65"; ExpectedType = [ulong] }
@{ Script = "0xFFFFFFFFFFFFFFFFul"; ExpectedValue = [ulong]::MaxValue; ExpectedType = [ulong] }
#Binary
@{ Script = "0b0ul"; ExpectedValue = "0"; ExpectedType = [ulong] }
@{ Script = "0b10ul"; ExpectedValue = "2"; ExpectedType = [ulong] }