Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimein-prThere is an active PR which will close this issue when it is merged
Milestone
Description
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.)
Reacted by ちーず(・8・)けーき and Raul Santos
Reacted by Copilot
Metadata
Metadata
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimein-prThere is an active PR which will close this issue when it is merged
Activity
koszeggy commentedon Apr 12, 2026
I'm pinging this to prevent from cleaning up.
tannergooding commentedon Apr 20, 2026
@koszeggy, if you can update the proposal to only include
IParsableandISpanParsablethen I can mark this ready for review.Runecannot 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 theValueproperty and create a new rune when done with their arithmetic or bitwise operations.koszeggy commentedon Apr 21, 2026
I updated the OP.
Understood. I took
charas a base, without thinking twice why it could implement all those interfaces.6 remaining items
bartonjs commentedon May 5, 2026
Video
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); }koszeggy commentedon May 6, 2026
Just noting that though
charimplements the span-related interfaces explicitly, it still has public[Try]Parsemethods forstring. Could you please consider doing it similarly forRune? I understand that it comes from the old times, but a publicly visibleParseoverload (even if for string only) helps to highlight the existence of the API.Also, calling
Parsefrom a non-generic caller would be a bit cumbersome (at least until the bridge feature is implemented):tannergooding commentedon May 6, 2026
You shouldn't be calling Parse from a non-generic context, it's a case you should explicitly be using
DecodeinsteadWe really don't want users to be using
Parseotherwise, it's just here for if you're in a generic context and happen to encounter aRuneparameter.System.Text.Rune.ParseandSystem.Text.Rune.TryParsetoSystem.Text.Rune(withoutIFormatProvider? provider& not Explicit Interface Implementations) #129463 as a duplicate of this issueSystem.Text.Rune.ParseandSystem.Text.Rune.TryParsetoSystem.Text.Rune(withoutIFormatProvider? provider& not Explicit Interface Implementations) #129463 as a duplicate of this issuetats-u commentedon Jun 16, 2026
Both should be prepared. We don't need extra garbage argument unless forced by the interface.
tats-u commentedon Jun 16, 2026
Parseis very helpful in unit tests. (API is based on #129463)tannergooding commentedon Jun 24, 2026
As was already covered, if you're in a non-generic context than other APIs like
Decodealready exist and are preferred.Parse does not do what you want here.