Skip to content

Commit 8b8920c

Browse files
committed
Post-merge fixup
Move GetByteCount and GetBytes into EncodingExtensions.cs
1 parent 2ddfe3b commit 8b8920c

3 files changed

Lines changed: 60 additions & 72 deletions

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
112112
_pendingCancel = false;
113113

114114
using var diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction);
115-
using var eventScope = TryEventScope.Create($"SqlCommand.ExecuteReader | API | Object Id {ObjectID}");
115+
using var eventScope = SqlClientEventScope.Create($"SqlCommand.ExecuteReader | API | Object Id {ObjectID}");
116116
// @TODO: Do we want to have a correlation trace event here like nonquery and xml?
117117
// @TODO: Basically, this doesn't follow the same pattern as nonquery, scalar, or xml. Doesn't seem right.
118118

src/Microsoft.Data.SqlClient/src/System/Text/EncodingExtensions.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using System.Diagnostics;
66

7+
#nullable enable
8+
79
namespace System.Text;
810

911
internal static class EncodingExtensions
@@ -69,4 +71,61 @@ public static unsafe string CreateStringFromNullTerminated(this Encoding encodin
6971
return encoding.GetString(pBytes, preNullBytes);
7072
}
7173
}
74+
75+
#if NETFRAMEWORK
76+
public static int GetByteCount(this Encoding encoding, string? s, int offset, int count)
77+
{
78+
if (s is null)
79+
{
80+
throw new ArgumentNullException(nameof(s));
81+
}
82+
83+
ReadOnlySpan<char> slicedString = s.AsSpan(offset, count);
84+
85+
if (slicedString.Length == 0)
86+
{
87+
return 0;
88+
}
89+
90+
unsafe
91+
{
92+
fixed (char* str = slicedString)
93+
{
94+
return encoding.GetByteCount(str, slicedString.Length);
95+
}
96+
}
97+
}
98+
99+
public static byte[] GetBytes(this Encoding encoding, string? s, int index, int count)
100+
{
101+
if (s is null)
102+
{
103+
throw new ArgumentNullException(nameof(s));
104+
}
105+
106+
ReadOnlySpan<char> slicedString = s.AsSpan(index, count);
107+
108+
if (slicedString.Length == 0)
109+
{
110+
return Array.Empty<byte>();
111+
}
112+
113+
unsafe
114+
{
115+
fixed (char* str = slicedString)
116+
{
117+
int byteCount = encoding.GetByteCount(str, slicedString.Length);
118+
byte[] bytes = new byte[byteCount];
119+
120+
fixed (byte* destArray = &bytes[0])
121+
{
122+
int bytesWritten = encoding.GetBytes(str, slicedString.Length, destArray, bytes.Length);
123+
124+
Debug.Assert(bytesWritten == byteCount);
125+
return bytes;
126+
}
127+
}
128+
}
129+
}
130+
#endif
72131
}

src/Microsoft.Data.SqlClient/src/System/Text/EncodingExtensions.netfx.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)