-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add Encoding.GetBytes(string, offset, count) #19574
Comments
@bartonjs public virtual int GetBytes(String s, int charIndex, int charCount,byte[] bytes, int byteIndex) I think this address your request here. but it may make sense expose the |
No, because you don't know how big the buffer needs to be without GetByteCount. And the version that just returns a |
Should this wait for public virtual byte[] GetBytes(ReadOnlySpan<char> span); And it would cover the existing use cases of |
@svick that make sense to wait for Span. |
Chatted with @bartonjs - this one is higher priority as there is not good safe alternative. If there's plan to have |
I am not seeing this one is urgent to have so I believe we can just wait for Span. why we are seeing this high priority. for me it is still nice to have feature as there is different ways achieve same results. |
If there's a concrete plan that adds Span to Encoding, this is closable. If there's no concrete plan, this will allow already existing over-allocating code to be cleaned up. |
talked offline with @bartonjs and I am fine if we can take this and discuss it with the design reviewers. |
We shouldn't block API additions on We think API looks fine as proposed. |
@karelz I am working on this issue. |
thanks @AlexRadch please include me in your PR. |
@bartonjs methods should be virtual or not? |
The answer is driven in the PR: dotnet/coreclr#8651 (comment) |
For the record: The API proposal was updated by @tarekgh - see dotnet/coreclr#8651 (comment). |
Reopening the issue since it needs a |
@AlexRadch , did you have a chance to work on exposing the APIs in the corefx side? thanks. |
No activity for 1.5 months, unassigning - the issue is back "up for grabs", for anyone to pick it up. |
Looks like this is already implemented in coreclr/corert. I'll grab exposing and testing this |
@hughbe this is great. |
Assigning to @hughbe ... |
This is technically source breaking right? var encoding = new UTF8Encoding();
encoding.GetByteCount(null, 0, 0) String and char[] can both be assigned from null, so this is now ambiguous and fails to compile |
It's only ambiguous with a literal null, and it throws ANE when null is passed to it, so the only code that will be broken is code that would always throw. Source-breaking that code is doing the author a favour. |
Yup as I thought - it's unlikely |
we already has the same case before so this is not really something new. for example we have Encoding.GetBytes(char[]) and Encoding.GetBytes(string). I am not really worried about the null case (without casts). |
The Encoding class has methods for encoding the middle of a
char[]
, but not for the middle of astring
. A caller is forced to switch to unsafe (char*) or to re-allocate aschar[]
.The
GetBytes(string, int, int)
method would allow for the typical "just give me a big enough array" while theGetByteCount(string, int, int)
method would allow for ensuring that an existing buffer is sufficiently big to call the existingGetBytes(string, int, int, byte[], int)
method.The text was updated successfully, but these errors were encountered: