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

[API Proposal]: Add Support for ToBase In BigInteger Parsing #84684

Closed
j0nimost opened this issue Apr 12, 2023 · 3 comments
Closed

[API Proposal]: Add Support for ToBase In BigInteger Parsing #84684

j0nimost opened this issue Apr 12, 2023 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics

Comments

@j0nimost
Copy link

Background and motivation

Given a string, the current implementation of BigInteger.Parse and BigInteger.TryParse lacks support for ToBase i.e.; 2, 8, 10, 16

API Proposal

// Add Support For String and/or ReadOnlySpan<char>

public static BigInteger Parse(string value, int tobase);
public static BigInteger Parse(ReadOnlySpan<char> value, int tobase);


public static bool TryParse(string value, int tobase, out BigInteger result);
public static bool TryParse(ReadOnlySpan<char> value, int tobase, out BigInteger result);

API Usage

// Given a couple of strings; assumption: large strings
string binary = "101010101100";
string hex = "0x34";

ReadOnlySpan<char> binarySpan = binary.AsSpan();
ReadOnlySpan<char> hexSpan = hex.AsSpan();

BigInteger bigIntA = BigInteger.Parse(binary, 2);
BigInteger bigIntA2 = BigInteger.Parse(binarySpan, 2);

_ = BigInteger.TryParse(hex, 16, out BigInteger bigIntOut)
_ = BigInteger.TryParse(hexSpan, 16, out BigInteger bigIntOut2)

// etc.

Alternative Designs

No response

Risks

No response

@j0nimost j0nimost added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 12, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 12, 2023
@ghost
Copy link

ghost commented Apr 12, 2023

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Given a string, the current implementation of BigInteger.Parse and BigInteger.TryParse lacks support for ToBase i.e.; 2, 8, 10, 16

API Proposal

// Add Support For String and/or ReadOnlySpan<char>

public static BigInteger Parse(string value, int tobase);
public static BigInteger Parse(ReadOnlySpan<char> value, int tobase);


public static bool TryParse(string value, int tobase, out BigInteger result);
public static bool TryParse(ReadOnlySpan<char> value, int tobase, out BigInteger result);

API Usage

// Given a couple of strings; assumption: large strings
string binary = "101010101100";
string hex = "0x34";

ReadOnlySpan<char> binarySpan = binary.AsSpan();
ReadOnlySpan<char> hexSpan = hex.AsSpan();

BigInteger bigIntA = BigInteger.Parse(binary, 2);
BigInteger bigIntA2 = BigInteger.Parse(binarySpan, 2);

_ = BigInteger.TryParse(hex, 16, out BigInteger bigIntOut)
_ = BigInteger.TryParse(hexSpan, 16, out BigInteger bigIntOut2)

// etc.

Alternative Designs

No response

Risks

No response

Author: j0nimost
Assignees: -
Labels:

api-suggestion, area-System.Numerics, untriaged

Milestone: -

@j0nimost
Copy link
Author

An additional consideration is to add isUnsigned and isBigEndian flags to these methods.

@tannergooding
Copy link
Member

tannergooding commented Apr 12, 2023

Parse APIs explicitly do not take a base parameter. You are expected to call the overloads that take a System.Globalization.NumberStyles instead (such as Parse(string, NumberStyles)) and pass in the relevant styles.

For hexadecimal, this would be NumberStyles.HexNumber.
For binary, this will be NumberStyles.BinaryNumber once we finish implementing #83619

decimal is already supported as the default. There are no current plans for octal, but if there is enough interest in it in the future, it would likely be NumberStyles.OctalNumber

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 12, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Projects
None yet
Development

No branches or pull requests

2 participants