-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Parse numeric strings as numbers again during conversions #8681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
667b40b
:sparkles: Consolidate cast methods from string to numerics!
vexx32 c6b21d6
:wrench: Fix recursion!
vexx32 cf60710
:wrench: fix some fallbacks from the parsed conversions
vexx32 8e5cd97
:wrench: Update binders for ScanNumber()
vexx32 ad6a82f
:white_check_mark: Empty commit to retrigger CIs
vexx32 245b3c1
Merge remote-tracking branch 'upstream/master' into ConsolidateConver…
vexx32 f1b4e29
:recycle: Tidy try/catch pattern into helper method
vexx32 b7caec5
:white_check_mark: add Conversions tests
vexx32 be5adde
:white_check_mark: Fix failing tests
vexx32 f85d0d2
Update src/System.Management.Automation/engine/LanguagePrimitives.cs
iSazonov df8e5cd
Update src/System.Management.Automation/engine/LanguagePrimitives.cs
iSazonov 7c6b44c
:recycle: Catch general exception in TryScanNumber & cleanup
vexx32 749892c
:pencil: Fix mismatching parens
vexx32 d687455
:pencil: Fix xml doc tag for code factor
vexx32 9143574
:recycle: Remove warning comment
vexx32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2885,6 +2885,34 @@ private static Uri ConvertStringToUri(object valueToConvert, | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to use Parser.ScanNumber to get the value of a numeric string. | ||
| /// </summary> | ||
| /// <param name="strToConvert">The string to convert to a number.</param> | ||
| /// <param name="resultType">The resulting value type to convert to.</param> | ||
| /// <param name="result">The resulting numeric value.</param> | ||
| /// <returns> | ||
| /// True if the parse succeeds, false if a parse exception arises. | ||
| /// In all other cases, an exception will be thrown. | ||
| /// </returns> | ||
| private static bool TryScanNumber(string strToConvert, Type resultType, out object result) | ||
| { | ||
| try | ||
| { | ||
| result = Convert.ChangeType( | ||
| Parser.ScanNumber(strToConvert, resultType, shouldTryCoercion: false), | ||
| resultType, | ||
| System.Globalization.CultureInfo.InvariantCulture.NumberFormat); | ||
| return true; | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Parse or convert failed | ||
| result = null; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static object ConvertStringToInteger(object valueToConvert, | ||
| Type resultType, | ||
| bool recursion, | ||
|
|
@@ -2907,7 +2935,14 @@ private static object ConvertStringToInteger(object valueToConvert, | |
| TypeConverter integerConverter = LanguagePrimitives.GetIntegerSystemConverter(resultType); | ||
| try | ||
| { | ||
| return integerConverter.ConvertFrom(strToConvert); | ||
| if (TryScanNumber(strToConvert, resultType, out object result)) | ||
| { | ||
| return result; | ||
| } | ||
| else | ||
| { | ||
| return integerConverter.ConvertFrom(strToConvert); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
|
|
@@ -2946,8 +2981,9 @@ private static object ConvertStringToDecimal(object valueToConvert, | |
| TypeTable backupTable) | ||
| { | ||
| Diagnostics.Assert(valueToConvert is string, "Value to convert must be a string"); | ||
| var strToConvert = valueToConvert as string; | ||
|
|
||
| if (((string)valueToConvert).Length == 0) | ||
| if (strToConvert.Length == 0) | ||
| { | ||
| typeConversion.WriteLine("Returning numeric zero."); | ||
| // This is not wrapped in a try/catch because it can't fail. | ||
|
|
@@ -2957,8 +2993,15 @@ private static object ConvertStringToDecimal(object valueToConvert, | |
| typeConversion.WriteLine("Converting to decimal."); | ||
| try | ||
| { | ||
| return Convert.ChangeType(valueToConvert, resultType, | ||
| System.Globalization.CultureInfo.InvariantCulture.NumberFormat); | ||
| typeConversion.WriteLine("Parsing string value to account for multipliers and type suffixes"); | ||
| if (TryScanNumber(strToConvert, resultType, out object result)) | ||
| { | ||
| return result; | ||
| } | ||
| else | ||
| { | ||
| return Convert.ChangeType(strToConvert, resultType, CultureInfo.InvariantCulture.NumberFormat); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
|
|
@@ -2967,7 +3010,7 @@ private static object ConvertStringToDecimal(object valueToConvert, | |
| { | ||
| try | ||
| { | ||
| return ConvertNumericThroughDouble(valueToConvert, resultType); | ||
| return ConvertNumericThroughDouble(strToConvert, resultType); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -2977,7 +3020,7 @@ private static object ConvertStringToDecimal(object valueToConvert, | |
|
|
||
| throw new PSInvalidCastException("InvalidCastFromStringToDecimal", e, | ||
| ExtendedTypeSystem.InvalidCastExceptionWithInnerException, | ||
| valueToConvert.ToString(), resultType.ToString(), e.Message); | ||
| strToConvert, resultType.ToString(), e.Message); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please format code. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -2989,8 +3032,9 @@ private static object ConvertStringToReal(object valueToConvert, | |
| TypeTable backupTable) | ||
| { | ||
| Diagnostics.Assert(valueToConvert is string, "Value to convert must be a string"); | ||
| var strToConvert = valueToConvert as string; | ||
|
|
||
| if (((string)valueToConvert).Length == 0) | ||
| if (strToConvert.Length == 0) | ||
| { | ||
| typeConversion.WriteLine("Returning numeric zero."); | ||
| // This is not wrapped in a try/catch because it can't fail. | ||
|
|
@@ -3000,15 +3044,23 @@ private static object ConvertStringToReal(object valueToConvert, | |
| typeConversion.WriteLine("Converting to double or single."); | ||
| try | ||
| { | ||
| return Convert.ChangeType(valueToConvert, resultType, | ||
| System.Globalization.CultureInfo.InvariantCulture.NumberFormat); | ||
| typeConversion.WriteLine("Parsing string value to account for multipliers and type suffixes"); | ||
|
|
||
| if (TryScanNumber(strToConvert, resultType, out object result)) | ||
| { | ||
| return result; | ||
| } | ||
| else | ||
| { | ||
| return Convert.ChangeType(strToConvert, resultType, CultureInfo.InvariantCulture.NumberFormat); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| typeConversion.WriteLine("Exception converting to double or single: \"{0}\".", e.Message); | ||
| throw new PSInvalidCastException("InvalidCastFromStringToDoubleOrSingle", e, | ||
| ExtendedTypeSystem.InvalidCastExceptionWithInnerException, | ||
| valueToConvert.ToString(), resultType.ToString(), e.Message); | ||
| strToConvert, resultType.ToString(), e.Message); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please format code. |
||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.