Skip to content
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

Add primitive type Span-based Parse/TryParse/TryFormat methods #22837

Closed
8 tasks done
stephentoub opened this issue Jul 18, 2017 · 13 comments
Closed
8 tasks done

Add primitive type Span-based Parse/TryParse/TryFormat methods #22837

stephentoub opened this issue Jul 18, 2017 · 13 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime tenet-performance Performance related issue
Milestone

Comments

@stephentoub
Copy link
Member

Separated out of https://github.com/dotnet/corefx/issues/21281 for tracking purposes.

  • Implement parsing in System.Private.CoreLib in coreclr
  • Implement parsing in System.Private.CoreLib in corert (unless primitive types are "shared" by then)
  • Expose parsing from System.Runtime contract in corefx
  • Add parsing tests to System.Runtime tests in corefx
  • Implement formatting in System.Private.CoreLib in coreclr
  • Implement formatting in System.Private.CoreLib in corert (unless primitive types are "shared" by then)
  • Expose formatting from System.Runtime contract in corefx
  • Add formatting tests to System.Runtime tests in corefx
namespace System
{
    public struct Int16
    {
        public short Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null);
        public bool TryParse(ReadOnlySpan<char> s, out short result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null);

        public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null);}// similar Parse/TryParse/TryFormat methods for each of:
    // Int32
    // Int64
    // UInt16
    // UInt32
    // UInt64
    // Decimal
    // Double
    // Single
    // Boolean (but no NumberStyle/NumberFormatInfo args)
}
@WinCPP
Copy link
Contributor

WinCPP commented Aug 7, 2017

@stephentoub may I work on this?

@stephentoub
Copy link
Member Author

stephentoub commented Aug 7, 2017

@WinCPP, sure, thanks. Note that this one will likely require lots of perf testing to verify existing parse/formatting functionality isn't regressed, as the right way to implement it is likely to make lots of small changes around the existing implementations to switch from using arrays to using spans so that both the array-based and span-based entrypoints can share the same mostly-existing code paths.

@WinCPP
Copy link
Contributor

WinCPP commented Aug 8, 2017

@stephentoub yup I understand.

@WinCPP
Copy link
Contributor

WinCPP commented Aug 14, 2017

@stephentoub I think I need to let go off this issue that I grabbed. Being remote, I think I am handicapped in knowing what is being done / what is to be done. And this one appears to be quite a big one to do by myself. Sorry for the haste... Although I can work collaboratively...

@stephentoub
Copy link
Member Author

Ok, thanks.

@stephentoub stephentoub self-assigned this Aug 15, 2017
@stephentoub
Copy link
Member Author

stephentoub commented Aug 21, 2017

The implementation of the parsing side of this is complete. The formatting side still needs to be done, and would benefit from corert's managed formatting implementation being ported to coreclr, where the formatting is currently done in native code in the runtime.

@stephentoub stephentoub removed their assignment Aug 21, 2017
@stephentoub
Copy link
Member Author

Formatting work depends on https://github.com/dotnet/coreclr/issues/13544

@mazong1123
Copy link
Contributor

Since we have new implementation of formatting in native coreclr (dotnet/coreclr#12894 and an in-progress one dotnet/coreclr#14646), is it better to port these native implementations to managed code other than port from corert?

@stephentoub stephentoub self-assigned this Nov 15, 2017
@stephentoub
Copy link
Member Author

Separated decimal, double, and single TryFormat methods into their own issues. Everything else is complete as of dotnet/corefx#25307.

@YairHalberstadt
Copy link
Contributor

Is there any reason char.TryParse wasn't updated? Or is it simply considered to niche to be worthwhile?

@stephentoub
Copy link
Member Author

Is there any reason char.TryParse wasn't updated? Or is it simply considered to niche to be worthwhile?

Just because it doesn't really add value in that there's no actual parsing, just accessing a single character in the string. Instead of:

if (char.TryParse(str, out char c))
{
    Use(c);
}

you just do:

if (!string.IsNullOrEmpty(str))
{
    Use(str[0]);
}

@YairHalberstadt
Copy link
Contributor

YairHalberstadt commented Nov 16, 2019

Thanks @stephentoub

I only just realised that char.Parse doesn't deal with strings of escape characters such as @"\\" (which makes both my question and it kind of pointless :)). Is there an alternative method which does?

@stephentoub
Copy link
Member Author

Is there an alternative method which does?

You mean that uses C# language rules to parse/interpret the string? I'm not aware of one in the .NET core libraries. You'd likely need to use Roslyn's APIs.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

5 participants