dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.99k stars 4.66k forks source link

[API Proposal]: Expose `Rune.MaxUtf16CharsPerRune` and `Rune.MaxUtf8BytesPerRune` #91510

Open Neme12 opened 1 year ago

Neme12 commented 1 year ago

Background and motivation

Currently, Rune has internal constants of MaxUtf16CharsPerRune and MaxUtf8BytesPerRune. I think it would make sense to make them public as they are really useful when calling the EncodeToUtf16 and EncodeToUtf8 methods so that you can create a buffer of the appropriate size. Currently, you have to hard-code the values 2 and 4 for that as magic constants, which feels meh.

API Proposal

 namespace System.Text;

 public readonly partial struct Rune
 {
-    internal const int MaxUtf16CharsPerRune = 2;
+    public const int MaxUtf16SequenceLength = 2;

-    internal const int MaxUtf8BytesPerRune = 4;
+    public const int MaxUtf8SequenceLength = 4;
 }

API Usage

Span<char> buffer = stackalloc char[Rune.MaxUtf16SequenceLength];
rune.EncodeToUtf16(buffer);
// and
Span<byte> buffer = stackalloc char[Rune.MaxUtf8SequenceLength];
rune.EncodeToUtf8(buffer);

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

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

Issue Details
### Background and motivation Currently, `Rune` has internal constants of `MaxUtf16CharsPerRune` and `MaxUtf8BytesPerRune`. I think it would make sense to make them public as they are really useful when calling the `EncodeToUtf16` and `EncodeToUtf8` methods so that you can create a buffer of the appropriate size. Currently, you have to hard-code the values 2 and 4 for that as magic constants, which feels meh. ### API Proposal ```diff namespace System.Text; public readonly partial struct Rune { - internal const int MaxUtf16CharsPerRune = 2; + public const int MaxUtf16CharsPerRune = 2; - internal const int MaxUtf8BytesPerRune = 4; + public const int MaxUtf8BytesPerRune = 4; } ``` ### API Usage ```csharp Span buffer = stackalloc char[Rune.MaxUtf16CharsPerRune]; rune.EncodeToUtf16(buffer); // and Span buffer = stackalloc char[Rune.MaxUtf8BytesPerRune]; rune.EncodeToUtf8(buffer); ``` ### Alternative Designs Alternative names might be `MaxUtf16SequenceLength` and `MaxUtf8SequenceLength` to match the existing instance `Utf16SequenceLength` and `Utf8SequenceLength` properties. ### Risks _No response_
Author: Neme12
Assignees: -
Labels: `api-suggestion`, `area-System.Runtime`, `untriaged`
Milestone: -
tannergooding commented 1 year ago

CC. @GrabYourPitchforks for input

Neme12 commented 1 year ago

I renamed the properties to MaxUtf16SequenceLength and MaxUtf8SequenceLength to match the existing properties of Utf16SequenceLength and Utf8SequenceLength.

GrabYourPitchforks commented 1 year ago

CC. @GrabYourPitchforks Levi Broderick FTE for input

I have no arguments against it. The values are stable. If there's a use case for it, expose it.

Neme12 commented 1 year ago

The use case is whenever I'm calling EncodeToUtf16 (or EncodeToUtf8) on a single Rune into a temporary buffer that I have to allocate with the necessary size.