Skip to content

Commit

Permalink
Remove unsafe code in GetSingle and GetDouble
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed Oct 14, 2020
1 parent af2355e commit 6ebfdef
Showing 1 changed file with 3 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -4449,35 +4450,9 @@ private ulong GetUInt64(int pos)
return (ulong)((ulong)hi) << 32 | lo;
}

private float GetSingle(int offset)
{
byte[] data = _data;
uint tmp = (uint)(data[offset]
| data[offset + 1] << 8
| data[offset + 2] << 16
| data[offset + 3] << 24);
unsafe
{
return *((float*)&tmp);
}
}
private float GetSingle(int offset) => BinaryPrimitives.ReadSingleLittleEndian(_data.AsSpan(offset));

private double GetDouble(int offset)
{
uint lo = (uint)(_data[offset + 0]
| _data[offset + 1] << 8
| _data[offset + 2] << 16
| _data[offset + 3] << 24);
uint hi = (uint)(_data[offset + 4]
| _data[offset + 5] << 8
| _data[offset + 6] << 16
| _data[offset + 7] << 24);
ulong tmp = ((ulong)hi) << 32 | lo;
unsafe
{
return *((double*)&tmp);
}
}
private double GetDouble(int offset) => BinaryPrimitives.ReadDoubleLittleEndian(_data.AsSpan(offset));

private Exception ThrowUnexpectedToken(BinXmlToken token)
{
Expand Down

0 comments on commit 6ebfdef

Please sign in to comment.