Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,9 @@
<Compile Include="$(CommonSourceRoot)System\IO\StreamExtensions.netfx.cs">
<Link>System\IO\StreamExtensions.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)System\Text\EncodingExtensions.netfx.cs">
<Link>System\Text\EncodingExtensions.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)System\Runtime\CompilerServices\IsExternalInit.netfx.cs">
<Link>System\Runtime\CompilerServices\IsExternalInit.netfx.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8651,36 +8651,18 @@ private Task WriteEncodingChar(string s, Encoding encoding, TdsParserStateObject

private byte[] SerializeEncodingChar(string s, int numChars, int offset, Encoding encoding)
{
#if NETFRAMEWORK
char[] charData;
byte[] byteData = null;

// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null)
{
encoding = _defaultEncoding;
}

charData = s.ToCharArray(offset, numChars);

byteData = new byte[encoding.GetByteCount(charData, 0, charData.Length)];
encoding.GetBytes(charData, 0, charData.Length, byteData, 0);
encoding ??= _defaultEncoding;

return byteData;
#else
return encoding.GetBytes(s, offset, numChars);
#endif
}

private Task WriteEncodingChar(string s, int numChars, int offset, Encoding encoding, TdsParserStateObject stateObj, bool canAccumulate = true)
{
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null)
{
encoding = _defaultEncoding;
}
encoding ??= _defaultEncoding;

// Optimization: if the entire string fits in the current buffer, then copy it directly
int bytesLeft = stateObj._outBuff.Length - stateObj._outBytesUsed;
Expand All @@ -8692,23 +8674,14 @@ private Task WriteEncodingChar(string s, int numChars, int offset, Encoding enco
}
else
{
#if NETFRAMEWORK
char[] charData = s.ToCharArray(offset, numChars);
byte[] byteData = encoding.GetBytes(charData, 0, numChars);
Debug.Assert(byteData != null, "no data from encoding");
return stateObj.WriteByteArray(byteData, byteData.Length, 0, canAccumulate);
#else
byte[] byteData = encoding.GetBytes(s, offset, numChars);
Debug.Assert(byteData != null, "no data from encoding");
return stateObj.WriteByteArray(byteData, byteData.Length, 0, canAccumulate);
#endif
}
}

internal int GetEncodingCharLength(string value, int numChars, int charOffset, Encoding encoding)
{
// UNDONE: (PERF) this is an expensive way to get the length. Also, aren't we
// UNDONE: (PERF) going through these steps twice when we write out a value?
if (string.IsNullOrEmpty(value))
{
return 0;
Expand All @@ -8726,9 +8699,7 @@ internal int GetEncodingCharLength(string value, int numChars, int charOffset, E
encoding = _defaultEncoding;
}

char[] charData = value.ToCharArray(charOffset, numChars);

return encoding.GetByteCount(charData, 0, numChars);
return encoding.GetByteCount(value, charOffset, numChars);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ internal void SetString(string value, int offset, int length)
}
else
{
char[] chars = value.ToCharArray(offset, length);
bytes = _stateObj.Parser._defaultEncoding.GetBytes(chars);
bytes = _stateObj.Parser._defaultEncoding.GetBytes(value, offset, length);
}
SetBytes(0, bytes, 0, bytes.Length);
SetBytesLength(bytes.Length);
Expand All @@ -376,7 +375,7 @@ internal void SetString(string value, int offset, int length)
}
else
{
bytes = _stateObj.Parser._defaultEncoding.GetBytes(value.ToCharArray(offset, length));
bytes = _stateObj.Parser._defaultEncoding.GetBytes(value, offset, length);
}
_stateObj.Parser.WriteSqlVariantHeader(9 + bytes.Length, TdsEnums.SQLBIGVARCHAR, 7, _stateObj);
_stateObj.Parser.WriteUnsignedInt(collation._info, _stateObj); // propbytes: collation.Info
Expand Down
Comment thread
paulmedynski marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Comment thread
paulmedynski marked this conversation as resolved.
#if NETFRAMEWORK

using System.Diagnostics;

namespace System.Text;

internal static class EncodingExtensions
{
public static int GetByteCount(this Encoding encoding, string s, int offset, int count)
{
ReadOnlySpan<char> slicedString = s.AsSpan(offset, count);

// This also implicitly checks for a null string. If the input string is null, slicedString
// will be default(ReadOnlySpan<char>), which also has a length of null.
Comment thread
paulmedynski marked this conversation as resolved.
Outdated
if (slicedString.Length == 0)
{
return 0;
}

unsafe
{
fixed (char* str = slicedString)
{
return encoding.GetByteCount(str, slicedString.Length);
}
}
}

public static byte[] GetBytes(this Encoding encoding, string s, int index, int count)
{
ReadOnlySpan<char> slicedString = s.AsSpan(index, count);
byte[] bytes;
int bytesWritten;
Comment thread
paulmedynski marked this conversation as resolved.
Outdated

// This also implicitly checks for a null string. If the input string is null, slicedString
// will be default(ReadOnlySpan<char>), which also has a length of null.
if (slicedString.Length == 0)
{
return Array.Empty<byte>();
}

unsafe
{
fixed (char* str = slicedString)
{
int byteCount = encoding.GetByteCount(str, slicedString.Length);

bytes = new byte[byteCount];

fixed (byte* destArray = &bytes[0])
{
bytesWritten = encoding.GetBytes(str, slicedString.Length, destArray, bytes.Length);

Debug.Assert(bytesWritten == byteCount);
}
}
}

return bytes;
}
}

#endif
Loading