This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add Encoding.GetBytes(string, offset, count) #8651
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f51fcef
Add Encoding.GetBytes(string, offset, count)
AlexRadch ee140f0
Optimize GetBytes
AlexRadch 8365eca
// Deligate parameters validation to string.ToCharArray method
AlexRadch c307bea
Fix model.xml
AlexRadch 2fe7879
Fix
AlexRadch 01a1a49
Made unsafe
AlexRadch 29aaf03
Environment.GetResourceString("ArgumentNull_String")
AlexRadch 4dce9b8
Debug.Assert(byteCount >= bytesReceived);
AlexRadch 1796c4e
// encoding == Encoding.UTF8
AlexRadch b2aeeac
fixed (char *pChar = string)
AlexRadch fab56e7
fixed (char* pChar = str)
AlexRadch 3ff89ba
merge
AlexRadch 6adee8a
if (byteCount == 0) { return Array.Empty<byte>(); }
AlexRadch f0c8bf8
fixed (byte* pBytes = bytes)
AlexRadch af21b10
non virtual
AlexRadch 523554c
fixed (byte* pBytes = &bytes[0])
AlexRadch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -869,7 +869,7 @@ public virtual int GetByteCount(char[] chars) | |
[Pure] | ||
public virtual int GetByteCount(String s) | ||
{ | ||
if (s==null) | ||
if (s == null) | ||
throw new ArgumentNullException(nameof(s)); | ||
Contract.EndContractBlock(); | ||
|
||
|
@@ -884,6 +884,34 @@ public virtual int GetByteCount(String s) | |
[Pure] | ||
public abstract int GetByteCount(char[] chars, int index, int count); | ||
|
||
// Returns the number of bytes required to encode a string range. | ||
// | ||
[Pure] | ||
public int GetByteCount(string s, int index, int count) | ||
{ | ||
if (s == null) | ||
throw new ArgumentNullException(nameof(s), | ||
Environment.GetResourceString("ArgumentNull_String")); | ||
if (index < 0) | ||
throw new ArgumentOutOfRangeException(nameof(index), | ||
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); | ||
if (count < 0) | ||
throw new ArgumentOutOfRangeException(nameof(count), | ||
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); | ||
if (index > s.Length - count) | ||
throw new ArgumentOutOfRangeException(nameof(index), | ||
Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); | ||
Contract.EndContractBlock(); | ||
|
||
unsafe | ||
{ | ||
fixed (char* pChar = s) | ||
{ | ||
return GetByteCount(pChar + index, count); | ||
} | ||
} | ||
} | ||
|
||
// We expect this to be the workhorse for NLS encodings | ||
// unfortunately for existing overrides, it has to call the [] version, | ||
// which is really slow, so this method should be avoided if you're calling | ||
|
@@ -978,10 +1006,49 @@ public virtual byte[] GetBytes(String s) | |
return bytes; | ||
} | ||
|
||
// Returns a byte array containing the encoded representation of the given | ||
// string range. | ||
// | ||
[Pure] | ||
public byte[] GetBytes(string s, int index, int count) | ||
{ | ||
if (s == null) | ||
throw new ArgumentNullException(nameof(s), | ||
Environment.GetResourceString("ArgumentNull_String")); | ||
if (index < 0) | ||
throw new ArgumentOutOfRangeException(nameof(index), | ||
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); | ||
if (count < 0) | ||
throw new ArgumentOutOfRangeException(nameof(count), | ||
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); | ||
if (index > s.Length - count) | ||
throw new ArgumentOutOfRangeException(nameof(index), | ||
Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); | ||
Contract.EndContractBlock(); | ||
|
||
unsafe | ||
{ | ||
fixed (char* pChar = s) | ||
{ | ||
int byteCount = GetByteCount(pChar + index, count); | ||
if (byteCount == 0) | ||
return Array.Empty<byte>(); | ||
|
||
byte[] bytes = new byte[byteCount]; | ||
fixed (byte* pBytes = &bytes[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried fixed (byte *pBytes = bytes)
{
} and it is working. could you fix this one? I think this is the last change requested in this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works but it generates worse code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I missed the previous comments regarding this part. I am ok with the change then. @jkotas are you ok now with the changes so I can merge it? |
||
{ | ||
int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount); | ||
Debug.Assert(byteCount == bytesReceived); | ||
} | ||
return bytes; | ||
} | ||
} | ||
} | ||
|
||
public virtual int GetBytes(String s, int charIndex, int charCount, | ||
byte[] bytes, int byteIndex) | ||
{ | ||
if (s==null) | ||
if (s == null) | ||
throw new ArgumentNullException(nameof(s)); | ||
Contract.EndContractBlock(); | ||
return GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should validate all passed parameters to this method too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are validated in next line
char[] chars = s.ToCharArray(index, count);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although I prefer validating the parameters in this method so the thrown exception can be more clear about the problem but I see the perf benefit too. so I am ok to keep what you have but please add assert in the methods you are introducing
In reply to: 92667264 [](ancestors = 92667264)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument validation will be needed here once the implementation changed to be the efficient one without the allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added comment
// Deligate parameters validation to string.ToCharArray method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlexRadch I assume you are going to change the implementation to use the unsafe code instead of the allocation as @jkotas mentioned. right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will