-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add Span-based {Try}Parse methods to primitive types #13389
Conversation
|
|
||
| public static bool TryParse(ReadOnlySpan<char> value, out bool result) | ||
| { | ||
| ReadOnlySpan<char> trueSpan = TrueLiteral.AsSpan(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the clarification.
src/mscorlib/shared/System/Byte.cs
Outdated
|
|
||
| private static byte Parse(String s, NumberStyles style, NumberFormatInfo info) | ||
| { | ||
| if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); |
There was a problem hiding this comment.
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));
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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.
1120203 to
3e1314f
Compare
…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]>
…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]>
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