Skip to content

Commit 2ad4391

Browse files
committed
move TryReadChars to shared and cleanup
1 parent 63668a3 commit 2ad4391

File tree

4 files changed

+53
-60
lines changed

4 files changed

+53
-60
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
using System.Diagnostics;
1111
using System.Globalization;
1212
using System.IO;
13-
using System.Runtime.CompilerServices;
14-
using System.Runtime.InteropServices;
1513
using System.Security.Authentication;
1614
using System.Text;
1715
using System.Threading;
@@ -12511,7 +12509,6 @@ private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsPar
1251112509
charsToRead = (int)(stateObj._longlenleft >> 1);
1251212510
}
1251312511

12514-
1251512512
if (!stateObj.TryReadChars(buff, offst, charsToRead, out charsRead))
1251612513
{
1251712514
charsRead = 0;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

-48
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Diagnostics;
8-
using System.Linq;
97
using System.Runtime.InteropServices;
108
using System.Security;
11-
using System.Text;
129
using System.Threading;
1310
using System.Threading.Tasks;
1411
using Microsoft.Data.Common;
@@ -491,51 +488,6 @@ internal bool TryReadChar(out char value)
491488
return true;
492489
}
493490

494-
internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out int charsCopied)
495-
{
496-
charsCopied = 0;
497-
while (charsCopied < charsCount)
498-
{
499-
// check if the current buffer contains some bytes we need to copy and copy them
500-
// in a block
501-
int bytesToRead = Math.Min(
502-
(charsCount - charsCopied) * 2,
503-
unchecked((_inBytesRead - _inBytesUsed) & (int)0xFFFFFFFE) // it the result is odd take off the 0 to make it even
504-
);
505-
if (bytesToRead > 0)
506-
{
507-
Buffer.BlockCopy(
508-
_inBuff,
509-
_inBytesUsed,
510-
chars,
511-
(charsOffset + charsCopied) * 2, // offset in bytes,
512-
bytesToRead
513-
);
514-
charsCopied = bytesToRead / 2;
515-
_inBytesUsed += bytesToRead;
516-
_inBytesPacket -= bytesToRead;
517-
}
518-
519-
// if the number of chars requested is lower than the number copied then we need
520-
// to request a new packet, use TryReadChar() to do this then loop back to see
521-
// if we can copy another bulk of chars from the new buffer
522-
523-
if (charsCopied < charsCount)
524-
{
525-
bool result = TryReadChar(out chars[charsOffset + charsCopied]);
526-
if (result)
527-
{
528-
charsCopied += 1;
529-
}
530-
else
531-
{
532-
return false;
533-
}
534-
}
535-
}
536-
return true;
537-
}
538-
539491
internal bool TryReadInt16(out short value)
540492
{
541493
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -13528,7 +13528,6 @@ internal void WriteParameterVarLen(MetaType type, int size, bool isNull, TdsPars
1352813528
// Returns the actual chars read
1352913529
private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsParserStateObject stateObj, out int charsRead)
1353013530
{
13531-
1353213531
Debug.Assert((buff == null && len == 0) || (buff.Length >= offst + len), "Invalid length sent to ReadPlpUnicodeChars()!");
1353313532
Debug.Assert((stateObj._longlen != 0) && (stateObj._longlen != TdsEnums.SQL_PLP_NULL),
1353413533
"Out of sync plp read request");
@@ -13539,18 +13538,18 @@ private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsPar
1353913538
return true;
1354013539
}
1354113540

13542-
charsRead = len;
13541+
int charsToRead = len;
1354313542

1354413543
// stateObj._longlenleft is in bytes
13545-
if ((stateObj._longlenleft >> 1) < (ulong)len)
13546-
charsRead = (int)(stateObj._longlenleft >> 1);
13544+
if ((stateObj._longlenleft / 2) < (ulong)len)
13545+
{
13546+
charsToRead = (int)(stateObj._longlenleft >> 1);
13547+
}
1354713548

13548-
for (int ii = 0; ii < charsRead; ii++)
13549+
if (!stateObj.TryReadChars(buff, offst, charsToRead, out charsRead))
1354913550
{
13550-
if (!stateObj.TryReadChar(out buff[offst + ii]))
13551-
{
13552-
return false;
13553-
}
13551+
charsRead = 0;
13552+
return false;
1355413553
}
1355513554

1355613555
stateObj._longlenleft -= ((ulong)charsRead << 1);

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

+45
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,51 @@ internal bool TryStartNewRow(bool isNullCompressed, int nullBitmapColumnsCount =
436436
return true;
437437
}
438438

439+
internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out int charsCopied)
440+
{
441+
charsCopied = 0;
442+
while (charsCopied < charsCount)
443+
{
444+
// check if the current buffer contains some bytes we need to copy and copy them
445+
// in a block
446+
int bytesToRead = Math.Min(
447+
(charsCount - charsCopied) * 2,
448+
unchecked((_inBytesRead - _inBytesUsed) & (int)0xFFFFFFFE) // it the result is odd take off the 0 to make it even
449+
);
450+
if (bytesToRead > 0)
451+
{
452+
Buffer.BlockCopy(
453+
_inBuff,
454+
_inBytesUsed,
455+
chars,
456+
(charsOffset + charsCopied) * 2, // offset in bytes,
457+
bytesToRead
458+
);
459+
charsCopied += (bytesToRead / 2);
460+
_inBytesUsed += bytesToRead;
461+
_inBytesPacket -= bytesToRead;
462+
}
463+
464+
// if the number of chars requested is lower than the number copied then we need
465+
// to request a new packet, use TryReadChar() to do this then loop back to see
466+
// if we can copy another bulk of chars from the new buffer
467+
468+
if (charsCopied < charsCount)
469+
{
470+
bool result = TryReadChar(out chars[charsOffset + charsCopied]);
471+
if (result)
472+
{
473+
charsCopied += 1;
474+
}
475+
else
476+
{
477+
return false;
478+
}
479+
}
480+
}
481+
return true;
482+
}
483+
439484
internal bool IsRowTokenReady()
440485
{
441486
// Removing one byte since TryReadByteArray\TryReadByte will aggressively read the next packet if there is no data left - so we need to ensure there is a spare byte

0 commit comments

Comments
 (0)