Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Conversation

@stephentoub
Copy link
Member

Adds Parse and TryParse methods to Boolean, Byte, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64, and Decimal.

Contributes to https://github.com/dotnet/corefx/issues/22403
cc: @jkotas, @KrzysztofCwalina, @tarekgh


public static bool TryParse(ReadOnlySpan<char> value, out bool result)
{
ReadOnlySpan<char> trueSpan = TrueLiteral.AsSpan();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrueLiteral.AsSpan() [](start = 42, length = 20)

is this fast operation or should we cache this value in some static?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fast operation, and you cannot cache Span in a static even if you wanted to - Span cannot be assigned to static field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the clarification.


private static byte Parse(String s, NumberStyles style, NumberFormatInfo info)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
Copy link
Member

@jkotas jkotas Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why there is extra private wrapper method in some cases (like here) vs. the code is inlined in other cases, for example the equivalent change for short is:

public static short Parse(String s, NumberStyles style, IFormatProvider provider) 
{ 
    NumberFormatInfo.ValidateParseStyleInteger(style); 
    if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);  
    return Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider));
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, other than when editing 12 types with a bazillion overloads, one can lose track of the pattern (especially when they're already inconsistent) :) I'll review and tweak where necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas, I pushed a commit to make these methods consistent across the primitive types.

@tarekgh
Copy link
Member

tarekgh commented Aug 16, 2017

LGTM.

Adds Parse and TryParse methods to Boolean, Byte, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64, and Decimal.
- Make delegation between overloads as consistent as possible across the primitive types: Boolean, SByte/Byte, Int16/UInt16, and Single/Double were doing it one way, whereas Decimal, Int32/UInt32, and Int64/UInt64 were doing it another way (most of this inconsistency was preexisting this PR, but my previous commit doubled-down on the inconsistency).  Changed the former to be like the latter.
@stephentoub stephentoub merged commit 12d5014 into dotnet:master Aug 16, 2017
@stephentoub stephentoub deleted the primitiveparsing branch August 16, 2017 21:09
dotnet-bot pushed a commit to dotnet/corert that referenced this pull request Aug 16, 2017
…13389)

* Add Span-based {Try}Parse methods to primitive types

Adds Parse and TryParse methods to Boolean, Byte, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64, and Decimal.

* Address PR feedback

- Make delegation between overloads as consistent as possible across the primitive types: Boolean, SByte/Byte, Int16/UInt16, and Single/Double were doing it one way, whereas Decimal, Int32/UInt32, and Int64/UInt64 were doing it another way (most of this inconsistency was preexisting this PR, but my previous commit doubled-down on the inconsistency).  Changed the former to be like the latter.

Signed-off-by: dotnet-bot <[email protected]>
stephentoub added a commit to dotnet/corert that referenced this pull request Aug 16, 2017
…13389)

* Add Span-based {Try}Parse methods to primitive types

Adds Parse and TryParse methods to Boolean, Byte, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64, and Decimal.

* Address PR feedback

- Make delegation between overloads as consistent as possible across the primitive types: Boolean, SByte/Byte, Int16/UInt16, and Single/Double were doing it one way, whereas Decimal, Int32/UInt32, and Int64/UInt64 were doing it another way (most of this inconsistency was preexisting this PR, but my previous commit doubled-down on the inconsistency).  Changed the former to be like the latter.

Signed-off-by: dotnet-bot <[email protected]>
@karelz karelz modified the milestone: 2.1.0 Aug 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants