Skip to content

[API Proposal]: Utf8Formatter/Utf8Parser should have UInt128 and Int128 overloads #73842

Description

@krwq

Background and motivation

As I started looking into #73500 I realized Utf8Formatter/Utf8Parser are missing UInt128/Int128 overloads and therefore we need to unnecessarily do UTF16 <--> UTF8 conversion.

cc: @tannergooding

API Proposal

namespace System.Buffers.Text;

public static partial class Utf8Formatter
{
    public static bool TryFormat(UInt128 value, System.Span<byte> destination, out int bytesWritten, System.Buffers.StandardFormat format = default(System.Buffers.StandardFormat)) { throw null; }
    public static bool TryFormat(Int128 value, System.Span<byte> destination, out int bytesWritten, System.Buffers.StandardFormat format = default(System.Buffers.StandardFormat)) { throw null; }
}

public static partial class Utf8Parser
{
    public static bool TryParse(System.ReadOnlySpan<byte> source, out UInt128 value, out int bytesConsumed, char standardFormat = '\0') { throw null; }
    public static bool TryParse(System.ReadOnlySpan<byte> source, out Int128 value, out int bytesConsumed, char standardFormat = '\0') { throw null; }
}

API Usage

if (Utf8Parser.TryParse(someUtf8Span, out UInt128 value, out int bytesConsumed) && someUtf8Span.Length == bytesConsumed)
{
    Console.WriteLine($"Successfully parsed UInt128 value: {value}");
}

UInt128 value = ...;
bool result = Utf8Formatter.TryFormat(value, utf8Output, out int bytesWritten);

// and similar for Int128

Alternative Designs

No response

Risks

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Aug 12, 2022
  2. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    untriagedNew issue has not been triaged by the area owner
    on Aug 12, 2022
  3. added this to the 8.0.0 milestone on Aug 12, 2022
  4. bartonjs commented on Sep 6, 2022

    @bartonjs
    Member

    Video

    Looks good as proposed

    namespace System.Buffers.Text;
    
    public static partial class Utf8Formatter
    {
        public static bool TryFormat(UInt128 value, System.Span<byte> destination, out int bytesWritten, System.Buffers.StandardFormat format = default(System.Buffers.StandardFormat)) { throw null; }
        public static bool TryFormat(Int128 value, System.Span<byte> destination, out int bytesWritten, System.Buffers.StandardFormat format = default(System.Buffers.StandardFormat)) { throw null; }
    }
    
    public static partial class Utf8Parser
    {
        public static bool TryParse(System.ReadOnlySpan<byte> source, out UInt128 value, out int bytesConsumed, char standardFormat = '\0') { throw null; }
        public static bool TryParse(System.ReadOnlySpan<byte> source, out Int128 value, out int bytesConsumed, char standardFormat = '\0') { throw null; }
    }
  5. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Sep 6, 2022
  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on May 16, 2023
  7. hrrrrustic commented on May 17, 2023

    @hrrrrustic
    Contributor

    @krwq @tannergooding
    I'm trying to implement this API, but there are a lot of usages Convert.ChangeType and Convert.ToIntXX in the tests. Unfortunately, these methods are not support {U}Int128, is it by design or just nobody asked for this? Does this methods kind of obsolete and should be workarounded or I should create an issue with API proposal?

    For Convert.ChangeType U{Int128} should implement IConvertible
    For Convert.ToIntXX just add new methods

    Btw, why U{Int128} returns a new instance every time in properties like {Max/Min}Value or NegativeOne? Other number types returns private consts or static readonly fields

    public static Int128 MinValue => new Int128(0x8000_0000_0000_0000, 0);

  8. hrrrrustic commented on May 21, 2023

    @hrrrrustic
    Contributor

    @dotnet/area-system-buffers (does this tags work? 🤔)
    This is kind of blocking linked PR, need some help for comment above

  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 6, 2023
  10. tannergooding commented on Jul 24, 2023

    @tannergooding
    Member

    Much like with #53768 (comment), the plan is for APIs to simply implement IUtf8SpanFormattable and IUtf8SpanParsable moving forward.

    For number types in particular, parsing where the first invalid character is treated as "end of string" rather than "invalid input" will be supported via #87171

  11. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    and removed
    api-approvedAPI was approved in API review, it can be implemented
    on Jul 24, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions