Skip to content

[API Proposal]: Add [Try]Parse to Rune #60231

Closed

Description

@koszeggy

Background and motivation

In many aspects Rune is similar to char, it has also the same formatting features, still, it does not support parsing.

As a workaround, one can use the following solution:

private static bool TryParseRune(string s, out Rune value)
{
    if (s == null)
        throw new ArgumentNullException(nameof(s));
    if (Rune.TryGetRuneAt(s, 0, out Rune rune) && rune.Utf16SequenceLength == s.Length)
    {
        value = rune;
        return true;
    }

    value = default;
    return false;
}

But unfortunately TryGetRune has no overloads for ReadOnlySpan<char> so the span version of the workaround should either to use SpanRuneEnumerator or some switch over the length of the span.

Interestingly enough, Rune implements IUtf8SpanParsable<Rune> lately (only explicitly though), but not IParsable<Rune> and ISpanParsable<Rune>.

API Proposal

public struct Rune : IComparable, IComparable<Rune>, IEquatable<Rune>
     , IUtf8SpanFormattable, IUtf8SpanParsable<Rune> // added in .NET 8
+    , IParsable<Rune>, ISpanParsable<Rune> 
{
+    public static Rune Parse(string s, IFormatProvider? provider);
+    public static bool TryParse(string s, IFormatProvider? provider, out Rune result);

+    public static Rune Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
+    public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Rune result);
}

API Usage

// Explicit usage example
var r1 = Rune.Parse("🙂");
bool success = Rune.TryParse("👀", out Rune r2);

// Interface usage example
static T Parse<T>(string s) where T : IParseable<T> => T.Parse(s);
var r3 = Parse<Rune>("😎");

Risks

I don't see any. But to be really similar to char maybe a RuneConverter could also be necessary to be able convert to and from string in already existing libraries (eg. XML/JSON serializers, etc.)

Activity

  1. added
    api-suggestion
    on Oct 10, 2021
  2. ghost added
    untriaged
    on Oct 10, 2021
  3. added
    needs-further-triage
    and removed
    untriaged
    on Oct 10, 2021
  4. added this to the Future milestone on Oct 10, 2021
  5. added
    backlog-cleanup-candidate
    on Apr 11, 2026
  6. koszeggy commented on Apr 12, 2026

    @koszeggy
    ContributorAuthor

    I'm pinging this to prevent from cleaning up.

  7. removed
    backlog-cleanup-candidate
    on Apr 12, 2026
  8. tannergooding commented on Apr 20, 2026

    @tannergooding
    Member

    @koszeggy, if you can update the proposal to only include IParsable and ISpanParsable then I can mark this ready for review.

    Rune cannot implement the full numeric interfaces as it is not a numeric type and explicitly doesn't allow any bit-pattern. Bad things can happen if you have an ill-formed Rune and so users should just work with the Value property and create a new rune when done with their arithmetic or bitwise operations.

  9. koszeggy commented on Apr 21, 2026

    @koszeggy
    ContributorAuthor

    I updated the OP.

    Rune cannot implement the full numeric interfaces as it is not a numeric type and explicitly doesn't allow any bit-pattern.

    Understood. I took char as a base, without thinking twice why it could implement all those interfaces.

  10. 6 remaining items

  11. bartonjs commented on May 5, 2026

    @bartonjs
    Member

    Video

    • Since IUtf8SpanFormattable/Parsable was explicitly implemented, let's do the same here.
    public struct Rune : IComparable, IComparable<Rune>, IEquatable<Rune>
         , IUtf8SpanFormattable, IUtf8SpanParsable<Rune> // added in .NET 8
    +    , IParsable<Rune>, ISpanParsable<Rune> 
    {
    +    static Rune IParsable<Rune>.Parse(string s, IFormatProvider? provider);
    +    static bool IParsable<Rune>.TryParse(string s, IFormatProvider? provider, out Rune result);
    
    +    static Rune ISpanParsable<Rune>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
    +    static bool ISpanParsable<Rune>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Rune result);
    }
  12. added
    api-approved
    and removed
    api-ready-for-review
    on May 5, 2026
  13. koszeggy commented on May 6, 2026

    @koszeggy
    ContributorAuthor

    Since IUtf8SpanFormattable/Parsable was explicitly implemented, let's do the same here.

    Just noting that though char implements the span-related interfaces explicitly, it still has public [Try]Parse methods for string. Could you please consider doing it similarly for Rune? I understand that it comes from the old times, but a publicly visible Parse overload (even if for string only) helps to highlight the existence of the API.

    Also, calling Parse from a non-generic caller would be a bit cumbersome (at least until the bridge feature is implemented):

    private static object? MyParse(string s, Type t, IFormatProvider? provider)
    {
        // [...] other simple types
        if (t == typeof(char))
            return Char.Parse(s);
        // [...] other simple types
    
        if (t == typeof(Rune))
            // return Rune.Parse(s); // wouldn't it be simpler/nicer?
            return return ExplicitParseHelper<Rune>.Parse(s, provider); // though provider makes no sense here
    
        // [...] other approaches, e.g. TypeConverter, etc.
    }
    
    // Helper class for types that implement IParsable<T> explicitly only
    private static class ExplicitParseHelper<TParsable>
        where TParsable : IParsable<TParsable>
    {
        internal static TParsable Parse(string s, IFormatProvider? provider) => TParsable.Parse(s, provider);
        internal static bool TryParse(string s, IFormatProvider? provider, out TParsable? result) => TParsable.TryParse(s, provider, out result);
    }
  14. tannergooding commented on May 6, 2026

    @tannergooding
    Member

    You shouldn't be calling Parse from a non-generic context, it's a case you should explicitly be using Decode instead

    We really don't want users to be using Parse otherwise, it's just here for if you're in a generic context and happen to encounter a Rune parameter.

  15. added
    in-pr
    on Jun 16, 2026
  16. tats-u commented on Jun 16, 2026

    @tats-u

    Since IUtf8SpanFormattable/Parsable was explicitly implemented, let's do the same here.

    Both should be prepared. We don't need extra garbage argument unless forced by the interface.

  17. tats-u commented on Jun 16, 2026

    @tats-u

    Parse is very helpful in unit tests. (API is based on #129463)

    [Theory]
    [InlineData("😀", 0x1F600, false)]
    [InlineData("A", 0x41, true)]
    [InlineData("あ", 0x3042, true)]
    [InlineData("𠮷", 0x20BB7, true)]
    [InlineData("👨‍👩‍👧‍👦", null, false)]
    public void Test_SingleCodePoint(string input, uint? codePoint, bool isLetter)
    {
        if (codePoint != null)
        {
            Rune rune = Rune.Parse(input);
            Assert.Equal(codePoint.GetValueOrDefault(), rune.Value);
            Assert.Equal(Rune.IsLetter(rune), isLetter);
        }
        else
        {
            Assert.Throws<FormatException>(() => Rune.Parse(input));
        }
    }
  18. tannergooding commented on Jun 24, 2026

    @tannergooding
    Member

    As was already covered, if you're in a non-generic context than other APIs like Decode already exist and are preferred.

    Parse does not do what you want here.

  19. locked and limited conversation to collaborators on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Runtimein-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions