Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
f3b0413
Merge pull request #1 from PowerShell/master
vexx32 Aug 12, 2018
c0c1248
Add tokenizer logic for recognising numeric tokens with 'u' suffix
vexx32 Aug 13, 2018
37802f0
inline very temporary variables
vexx32 Aug 13, 2018
dc9dc4c
Add typesuffix trait to uppercase and lowercase 'u'
vexx32 Aug 13, 2018
88377e4
Add case to get ulong vs uint depending on total value of number
vexx32 Aug 13, 2018
567809f
Fix some small things codefactor complained about
vexx32 Aug 13, 2018
34812ba
Add type mappings for `uint` and `ulong` accelerators
vexx32 Aug 13, 2018
47287fb
Add cases for new type accelerators
vexx32 Aug 13, 2018
da25c79
Add parser tests for uint literals
vexx32 Aug 13, 2018
8491b10
Fix typos in tests
vexx32 Aug 13, 2018
cc90f72
fix ulong parsing
vexx32 Aug 13, 2018
ee9c641
Fix silly error
vexx32 Aug 13, 2018
0d66604
Remove unneeded cast
vexx32 Aug 14, 2018
3c56a94
Adding ushort accelerator as well (maybe unnecessary?)
vexx32 Aug 14, 2018
5146db0
Update accelerator tests
vexx32 Aug 14, 2018
27ed4f0
Fix type conversion
vexx32 Aug 14, 2018
ace51dc
Remove stray characters breaking hash literal
vexx32 Aug 14, 2018
cdae19e
Parse as UInt64 directly
vexx32 Aug 14, 2018
a4c18aa
Small whitespace rule resolution
vexx32 Aug 14, 2018
dae82c1
Fix weird whitespace alignment discrepancy
vexx32 Aug 14, 2018
bad1533
Attempt #2
vexx32 Aug 14, 2018
d9bfbc8
Get alignment back to more or less how it was initially
vexx32 Aug 14, 2018
ebb795e
This is where they should be. I think
vexx32 Aug 14, 2018
3738191
Nope it was a full tab, I lied.
vexx32 Aug 14, 2018
b67b98e
Screw it, keep it Normal:tm:
vexx32 Aug 14, 2018
7a6b44e
Add code path to mirror `L` long token codepaths
vexx32 Aug 15, 2018
625a223
Move token detection after real check
vexx32 Aug 15, 2018
27f1457
Add tests for rounding on token parse
vexx32 Aug 15, 2018
8595619
Convert `suffix` across the board to string
vexx32 Aug 15, 2018
415a129
Add behaviour to account for new suffix behaviour
vexx32 Aug 15, 2018
beba14f
Add case for nonsensical type suffixes and don't accept parse
vexx32 Aug 15, 2018
05bd457
Changes for CodeFactor
vexx32 Aug 15, 2018
29da202
Add parser tests for 'ul' suffix
vexx32 Aug 15, 2018
4a28fa9
Oh look, I totally forgot the type syffix on this test.
vexx32 Aug 15, 2018
5024265
Remember: double->(u)int type casts in this manner
vexx32 Aug 15, 2018
f2b365c
Add NumberSuffixFlags
vexx32 Aug 15, 2018
6b0ff63
replace suffix references' types
vexx32 Aug 15, 2018
ba850c8
Change string/char checking for suffixes into flag checking
vexx32 Aug 15, 2018
26c7565
Fix switch syntax
vexx32 Aug 15, 2018
1b0439b
Fix enum name reference
vexx32 Aug 15, 2018
7985774
Add enum summary comments
vexx32 Aug 15, 2018
8da4969
Fix switch casing to not permit unknown two-char suffixes
vexx32 Aug 15, 2018
fa4e985
Do not allow `dl` or `ll` suffixes
vexx32 Aug 15, 2018
4633871
Fixes for CodeFactor ♥
vexx32 Aug 15, 2018
e646ea8
Remove public designation from NumberSuffixFlags
vexx32 Aug 15, 2018
28666a6
Fix some typos in my code comments
vexx32 Aug 16, 2018
09b85f1
Add `s` as a permissibly type suffix
vexx32 Aug 16, 2018
e55282f
add parse cases for `s` type tokens
vexx32 Aug 16, 2018
e42556b
Add missing ushort case to tokenizer
vexx32 Aug 16, 2018
9e84f1c
TryGetNumberValue -> change param to ReadOnlySpan<char>
vexx32 Aug 16, 2018
feb15bc
Shift if series -> if/else series
vexx32 Aug 17, 2018
a1f43d9
Swap Convert.ChangeType() for Convert.To[Type]
vexx32 Aug 17, 2018
bf8b731
Add missing tests for invalid tokens
vexx32 Aug 17, 2018
8bd503c
Remove superfluous comment block
vexx32 Aug 17, 2018
eb47fd3
Fix tests to operate properly
vexx32 Aug 17, 2018
523385e
Slight tidy
vexx32 Aug 17, 2018
6c729c6
Begin adding helper utils for conversions
vexx32 Aug 17, 2018
22498e0
Replace throwable methods with non-throwing
vexx32 Aug 17, 2018
7c04d68
Eliminate throwing conversions from real number parsing
vexx32 Aug 17, 2018
4fc3990
Shorten temp variable names
vexx32 Aug 17, 2018
ba93604
More helper functions!
vexx32 Aug 17, 2018
9da5a0e
Finish rebuilding the tokenizer with helper functions.
vexx32 Aug 17, 2018
cdb8bce
Fix the parsing. I think.
vexx32 Aug 18, 2018
b9a74f6
Refactor integer parsing to cast instead of continually calling TryParse
vexx32 Aug 18, 2018
4bf3435
Refactor multiplier -> switch statement
vexx32 Aug 18, 2018
5782443
Small fixes for CodeFactor
vexx32 Aug 18, 2018
781fb7f
Fix sign bits in hex readings
vexx32 Aug 18, 2018
2159c9a
Add some sign bit checks to the hex literal parsing
vexx32 Aug 18, 2018
d9d265b
Add logic to enable use of sign bits properly... I think.
vexx32 Aug 18, 2018
d3618c7
Remove busted logic for int hex sign bits that wasn't doing anything
vexx32 Aug 18, 2018
f1411d5
Let huge hex values parse as bigint
vexx32 Aug 18, 2018
4a21a1e
Commented out the test failing because the huge number can now be parsed
vexx32 Aug 19, 2018
5d2a39b
Allow hex numbers to specify long values without suffix
vexx32 Aug 19, 2018
25913b4
Add case for dealing with unsigned ints
vexx32 Aug 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Management.Automation.Configuration;
using System.Management.Automation.Internal;
using System.Management.Automation.Security;
using System.Numerics;
using System.Reflection;
using Microsoft.PowerShell.Commands;
using Microsoft.Win32;
Expand All @@ -33,6 +34,161 @@ namespace System.Management.Automation
/// </summary>
internal static class Utils
{
internal static bool TryConvertInt16(double value, out short result)
{
if (value > Int16.MaxValue || value < Int16.MinValue) {
result = 0;
return false;
}

result = (short)Math.Round(value);
return true;
}

internal static bool TryConvertInt16(BigInteger value, out short result)
{
if (value > Int16.MaxValue || value < Int16.MinValue)
{
result = 0;
return false;
}

result = (short)value;
return true;
}

internal static bool TryConvertInt32(BigInteger value, out int result)
{
if (value > Int32.MaxValue || value < Int32.MinValue)
{
result = 0;
return false;
}

result = (int)value;
return true;
}

internal static bool TryConvertInt64(double value, out long result)
{
if (value > Int64.MaxValue || value < Int64.MinValue)
{
result = 0;
return false;
}

result = (long)Math.Round(value);
return true;
}

internal static bool TryConvertInt64(BigInteger value, out long result)
{
if (value > Int64.MaxValue || value < Int64.MinValue)
{
result = 0;
return false;
}

result = (long)value;
return true;
}

internal static bool TryConvertUInt16(ulong value, out ushort result)
{
if (value > UInt16.MaxValue || value < UInt16.MinValue)
{
result = 0;
return false;
}

result = (ushort)value;
return true;
}

internal static bool TryConvertUInt16(BigInteger value, out ushort result)
{
if (value > UInt16.MaxValue || value < UInt16.MinValue)
{
result = 0;
return false;
}

result = (ushort)value;
return true;
}

internal static bool TryConvertUInt32(ulong value, out uint result)
{
if (value > UInt32.MaxValue || value < UInt32.MinValue)
{
result = 0;
return false;
}

result = (uint)value;
return true;
}

internal static bool TryConvertUInt32(BigInteger value, out uint result)
{
if (value > UInt32.MaxValue || value < UInt32.MinValue)
{
result = 0;
return false;
}

result = (uint)value;
return true;
}

internal static bool TryConvertUInt64(double value, out ulong result)
{
if (value > UInt64.MaxValue || value < UInt64.MinValue)
{
result = 0;
return false;
}

result = (ulong)Math.Round(value);
return true;
}

internal static bool TryConvertUInt64(BigInteger value, out ulong result)
{
if (value > UInt64.MaxValue || value < UInt64.MinValue)
{
result = 0;
return false;
}

result = (ulong)value;
return true;
}

internal static bool TryConvertDecimal(BigInteger value, out decimal result)
{
if (value > (BigInteger)Decimal.MaxValue || value < (BigInteger)Decimal.MinValue)
{
result = 0;
return false;
}

result = (decimal)value;
return true;
}

internal static bool TryConvertDouble(BigInteger value, out double result)
{
if (value > (BigInteger)Double.MaxValue || value < (BigInteger)Double.MinValue)
{
result = 0;
return false;
}

result = (double)value;
return true;
}

// From System.Web.Util.HashCodeCombiner
internal static int CombineHashCodes(int h1, int h2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ static CharExtensions()
/* P */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.MultiplierStart,
/* Q */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* R */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* S */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* S */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.TypeSuffix,
/* T */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.MultiplierStart,
/* U */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* U */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.TypeSuffix,
/* V */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* W */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* X */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
Expand Down Expand Up @@ -187,9 +187,9 @@ static CharExtensions()
/* p */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.MultiplierStart,
/* q */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* r */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* s */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* s */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.TypeSuffix,
/* t */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.MultiplierStart,
/* u */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* u */ CharTraits.IdentifierStart | CharTraits.VarNameFirst | CharTraits.TypeSuffix,
/* v */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* w */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
/* x */ CharTraits.IdentifierStart | CharTraits.VarNameFirst,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ internal static class CoreTypes
{ typeof(Guid), new[] { "guid" } },
{ typeof(Hashtable), new[] { "hashtable" } },
{ typeof(int), new[] { "int", "int32" } },
{ typeof(Int16), new[] { "int16" } },
{ typeof(Int16), new[] { "short", "int16" } },
{ typeof(long), new[] { "long", "int64" } },
{ typeof(CimInstance), new[] { "ciminstance" } },
{ typeof(CimClass), new[] { "cimclass" } },
Expand Down Expand Up @@ -770,9 +770,9 @@ internal static class CoreTypes
{ typeof(BigInteger), new[] { "bigint" } },
{ typeof(SecureString), new[] { "securestring" } },
{ typeof(TimeSpan), new[] { "timespan" } },
{ typeof(UInt16), new[] { "uint16" } },
{ typeof(UInt32), new[] { "uint32" } },
{ typeof(UInt64), new[] { "uint64" } },
{ typeof(UInt16), new[] { "ushort", "uint16" } },
{ typeof(UInt32), new[] { "uint", "uint32" } },
{ typeof(UInt64), new[] { "ulong", "uint64" } },
{ typeof(Uri), new[] { "uri" } },
{ typeof(ValidateCountAttribute), new[] { "ValidateCount" } },
{ typeof(ValidateDriveAttribute), new[] { "ValidateDrive" } },
Expand Down
Loading