Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2125,15 +2125,25 @@ internal static void SetToRandomBytes(Span<byte> outBytes)
}

/// <summary>
/// Converts 'idData' which is assumed to be HEX Unicode characters to binary
/// puts it in 'outBytes'
/// Converts 'charData' which is assumed to be HEX Unicode characters to binary
/// and puts it in 'destination'. destination.Length * 2 must equal charData.Length.
/// </summary>
internal static void SetSpanFromHexChars(ReadOnlySpan<char> charData, Span<byte> outBytes)
internal static void SetSpanFromHexChars(ReadOnlySpan<char> charData, Span<byte> destination)
{
Debug.Assert(outBytes.Length * 2 == charData.Length);
for (int i = 0; i < outBytes.Length; i++)
outBytes[i] = HexByteFromChars(charData[i * 2], charData[i * 2 + 1]);
if (destination.Length * 2 != charData.Length)
{
throw new ArgumentOutOfRangeException(nameof(destination));
}

#if NET
OperationStatus status = Convert.FromHexString(charData, destination, out _, out _);
Debug.Assert(status == OperationStatus.Done);
#else
for (int i = 0; i < destination.Length; i++)
destination[i] = HexByteFromChars(charData[i * 2], charData[i * 2 + 1]);
#endif
Comment thread
martincostello marked this conversation as resolved.
Comment thread
MihaZupan marked this conversation as resolved.
}

internal static byte HexByteFromChars(char char1, char char2)
{
int hi = HexConverter.FromLowerChar(char1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,28 @@ public void ActivitySpanIdTests()
Assert.Equal(idStr, id.ToString());
}

[Theory]
[InlineData(0)]
[InlineData(15)]
[InlineData(17)]
public void ActivityTraceId_CopyTo_ThrowsForInvalidDestinationLength(int length)
{
ActivityTraceId id = ActivityTraceId.CreateRandom();
byte[] destination = new byte[length];
Assert.Throws<ArgumentOutOfRangeException>(() => id.CopyTo(destination));
}

[Theory]
[InlineData(0)]
[InlineData(7)]
[InlineData(9)]
public void ActivitySpanId_CopyTo_ThrowsForInvalidDestinationLength(int length)
{
ActivitySpanId id = ActivitySpanId.CreateRandom();
byte[] destination = new byte[length];
Assert.Throws<ArgumentOutOfRangeException>(() => id.CopyTo(destination));
}

/****** WC3 Format tests *****/

[Fact]
Expand Down
Loading