Skip to content

Commit

Permalink
changed from __refvalue(__makeref(...) to Unsafe.As
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Dermody committed May 29, 2024
1 parent be30e72 commit 25b799f
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 90 deletions.
24 changes: 13 additions & 11 deletions BrightData/Converter/ConvertToDecimal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +34,17 @@ public ConvertToDecimal(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static decimal FromString(T str) => decimal.Parse(__refvalue(__makeref(str), string));
decimal FromSingle(T data) => System.Convert.ToDecimal(GetSingle(data));
decimal FromDouble(T data) => System.Convert.ToDecimal(GetDouble(data));
decimal FromSByte(T data) => GetSByte(data);
decimal FromByte(T data) => GetByte(data);
decimal FromInt16(T data) => GetInt16(data);
decimal FromUInt16(T data) => GetUInt16(data);
decimal FromInt32(T data) => GetInt32(data);
decimal FromUInt32(T data) => GetUInt32(data);
decimal FromInt64(T data) => GetInt64(data);
decimal FromUInt64(T data) => GetUInt64(data);
static decimal FromString(T str) => decimal.Parse(Unsafe.As<T, string>(ref str));
static decimal FromSingle(T data) => System.Convert.ToDecimal(GetSingle(data));
static decimal FromDouble(T data) => System.Convert.ToDecimal(GetDouble(data));
static decimal FromSByte(T data) => GetSByte(data);
static decimal FromByte(T data) => GetByte(data);
static decimal FromInt16(T data) => GetInt16(data);
static decimal FromUInt16(T data) => GetUInt16(data);
static decimal FromInt32(T data) => GetInt32(data);
static decimal FromUInt32(T data) => GetUInt32(data);
static decimal FromInt64(T data) => GetInt64(data);
static decimal FromUInt64(T data) => GetUInt64(data);
decimal ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToDouble.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +33,17 @@ public ConvertToDouble(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static double FromString(T str) => double.Parse(__refvalue(__makeref(str), string));
double FromSingle(T data) => System.Convert.ToDouble(GetSingle(data));
double FromDecimal(T data) => System.Convert.ToDouble(GetDecimal(data));
double FromSByte(T data) => GetSByte(data);
double FromByte(T data) => GetByte(data);
double FromInt16(T data) => GetInt16(data);
double FromUInt16(T data) => GetUInt16(data);
double FromInt32(T data) => GetInt32(data);
double FromUInt32(T data) => GetUInt32(data);
double FromInt64(T data) => GetInt64(data);
double FromUInt64(T data) => GetUInt64(data);
static double FromString(T str) => double.Parse(Unsafe.As<T, string>(ref str));
static double FromSingle(T data) => System.Convert.ToDouble(GetSingle(data));
static double FromDecimal(T data) => System.Convert.ToDouble(GetDecimal(data));
static double FromSByte(T data) => GetSByte(data);
static double FromByte(T data) => GetByte(data);
static double FromInt16(T data) => GetInt16(data);
static double FromUInt16(T data) => GetUInt16(data);
static double FromInt32(T data) => GetInt32(data);
static double FromUInt32(T data) => GetUInt32(data);
static double FromInt64(T data) => GetInt64(data);
static double FromUInt64(T data) => GetUInt64(data);
double ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToFloat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +33,17 @@ public ConvertToFloat(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static float FromString(T str) => float.Parse(__refvalue(__makeref(str), string));
float FromDouble(T data) => System.Convert.ToSingle(GetDouble(data));
float FromDecimal(T data) => System.Convert.ToSingle(GetDecimal(data));
float FromSByte(T data) => GetSByte(data);
float FromByte(T data) => GetByte(data);
float FromInt16(T data) => GetInt16(data);
float FromUInt16(T data) => GetUInt16(data);
float FromInt32(T data) => GetInt32(data);
float FromUInt32(T data) => GetUInt32(data);
float FromInt64(T data) => GetInt64(data);
float FromUInt64(T data) => GetUInt64(data);
static float FromString(T str) => float.Parse(Unsafe.As<T, string>(ref str));
static float FromDouble(T data) => System.Convert.ToSingle(GetDouble(data));
static float FromDecimal(T data) => System.Convert.ToSingle(GetDecimal(data));
static float FromSByte(T data) => GetSByte(data);
static float FromByte(T data) => GetByte(data);
static float FromInt16(T data) => GetInt16(data);
static float FromUInt16(T data) => GetUInt16(data);
static float FromInt32(T data) => GetInt32(data);
static float FromUInt32(T data) => GetUInt32(data);
static float FromInt64(T data) => GetInt64(data);
static float FromUInt64(T data) => GetUInt64(data);
float ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToInt.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -31,17 +32,17 @@ public ConvertToInt(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static int FromString(T str) => int.Parse(__refvalue(__makeref(str), string));
int FromSingle(T data) => System.Convert.ToInt32(GetSingle(data));
int FromDouble(T data) => System.Convert.ToInt32(GetDouble(data));
int FromDecimal(T data) => System.Convert.ToInt32(GetDecimal(data));
int FromSByte(T data) => GetSByte(data);
int FromByte(T data) => GetByte(data);
int FromInt16(T data) => GetInt16(data);
int FromUInt16(T data) => GetUInt16(data);
int FromUInt32(T data) => System.Convert.ToInt32(GetUInt32(data));
int FromInt64(T data) => System.Convert.ToInt32(GetInt64(data));
int FromUInt64(T data) => System.Convert.ToInt32(GetUInt64(data));
static int FromString(T str) => int.Parse(Unsafe.As<T, string>(ref str));
static int FromSingle(T data) => System.Convert.ToInt32(GetSingle(data));
static int FromDouble(T data) => System.Convert.ToInt32(GetDouble(data));
static int FromDecimal(T data) => System.Convert.ToInt32(GetDecimal(data));
static int FromSByte(T data) => GetSByte(data);
static int FromByte(T data) => GetByte(data);
static int FromInt16(T data) => GetInt16(data);
static int FromUInt16(T data) => GetUInt16(data);
static int FromUInt32(T data) => System.Convert.ToInt32(GetUInt32(data));
static int FromInt64(T data) => System.Convert.ToInt32(GetInt64(data));
static int FromUInt64(T data) => System.Convert.ToInt32(GetUInt64(data));
int ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToLong.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +33,17 @@ public ConvertToLong(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static long FromString(T str) => long.Parse(__refvalue(__makeref(str), string));
long FromSingle(T data) => System.Convert.ToInt64(GetSingle(data));
long FromDouble(T data) => System.Convert.ToInt64(GetDouble(data));
long FromDecimal(T data) => System.Convert.ToInt64(GetDecimal(data));
long FromSByte(T data) => GetSByte(data);
long FromByte(T data) => GetByte(data);
long FromInt16(T data) => GetInt16(data);
long FromUInt16(T data) => GetUInt16(data);
long FromInt32(T data) => GetInt32(data);
long FromUInt32(T data) => GetUInt32(data);
long FromUInt64(T data) => System.Convert.ToInt64(GetUInt64(data));
static long FromString(T str) => long.Parse(Unsafe.As<T, string>(ref str));
static long FromSingle(T data) => System.Convert.ToInt64(GetSingle(data));
static long FromDouble(T data) => System.Convert.ToInt64(GetDouble(data));
static long FromDecimal(T data) => System.Convert.ToInt64(GetDecimal(data));
static long FromSByte(T data) => GetSByte(data);
static long FromByte(T data) => GetByte(data);
static long FromInt16(T data) => GetInt16(data);
static long FromUInt16(T data) => GetUInt16(data);
static long FromInt32(T data) => GetInt32(data);
static long FromUInt32(T data) => GetUInt32(data);
static long FromUInt64(T data) => System.Convert.ToInt64(GetUInt64(data));
long ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToShort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +33,17 @@ public ConvertToShort(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static short FromString(T str) => short.Parse(__refvalue(__makeref(str), string));
short FromSingle(T data) => System.Convert.ToInt16(GetSingle(data));
short FromDouble(T data) => System.Convert.ToInt16(GetDouble(data));
short FromDecimal(T data) => System.Convert.ToInt16(GetDecimal(data));
short FromSByte(T data) => GetSByte(data);
short FromByte(T data) => GetByte(data);
short FromUInt16(T data) => System.Convert.ToInt16(GetUInt16(data));
short FromInt32(T data) => System.Convert.ToInt16(GetInt32(data));
short FromUInt32(T data) => System.Convert.ToInt16(GetUInt32(data));
short FromInt64(T data) => System.Convert.ToInt16(GetInt64(data));
short FromUInt64(T data) => System.Convert.ToInt16(GetUInt64(data));
static short FromString(T str) => short.Parse(Unsafe.As<T, string>(ref str));
static short FromSingle(T data) => System.Convert.ToInt16(GetSingle(data));
static short FromDouble(T data) => System.Convert.ToInt16(GetDouble(data));
static short FromDecimal(T data) => System.Convert.ToInt16(GetDecimal(data));
static short FromSByte(T data) => GetSByte(data);
static short FromByte(T data) => GetByte(data);
static short FromUInt16(T data) => System.Convert.ToInt16(GetUInt16(data));
static short FromInt32(T data) => System.Convert.ToInt16(GetInt32(data));
static short FromUInt32(T data) => System.Convert.ToInt16(GetUInt32(data));
static short FromInt64(T data) => System.Convert.ToInt16(GetInt64(data));
static short FromUInt64(T data) => System.Convert.ToInt16(GetUInt64(data));
short ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConvertToSignedByte.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand Down Expand Up @@ -32,17 +33,17 @@ public ConvertToSignedByte(bool throwOnFailure = false) : base(throwOnFailure)
};
}

static sbyte FromString(T str) => sbyte.Parse(__refvalue(__makeref(str), string));
sbyte FromSingle(T data) => System.Convert.ToSByte(GetSingle(data));
sbyte FromDouble(T data) => System.Convert.ToSByte(GetDouble(data));
sbyte FromDecimal(T data) => System.Convert.ToSByte(GetDecimal(data));
sbyte FromByte(T data) => System.Convert.ToSByte(GetByte(data));
sbyte FromInt16(T data) => System.Convert.ToSByte(GetInt16(data));
sbyte FromUInt16(T data) => System.Convert.ToSByte(GetUInt16(data));
sbyte FromInt32(T data) => System.Convert.ToSByte(GetInt32(data));
sbyte FromUInt32(T data) => System.Convert.ToSByte(GetUInt32(data));
sbyte FromInt64(T data) => System.Convert.ToSByte(GetInt64(data));
sbyte FromUInt64(T data) => System.Convert.ToSByte(GetUInt64(data));
static sbyte FromString(T str) => sbyte.Parse(Unsafe.As<T, string>(ref str));
static sbyte FromSingle(T data) => System.Convert.ToSByte(GetSingle(data));
static sbyte FromDouble(T data) => System.Convert.ToSByte(GetDouble(data));
static sbyte FromDecimal(T data) => System.Convert.ToSByte(GetDecimal(data));
static sbyte FromByte(T data) => System.Convert.ToSByte(GetByte(data));
static sbyte FromInt16(T data) => System.Convert.ToSByte(GetInt16(data));
static sbyte FromUInt16(T data) => System.Convert.ToSByte(GetUInt16(data));
static sbyte FromInt32(T data) => System.Convert.ToSByte(GetInt32(data));
static sbyte FromUInt32(T data) => System.Convert.ToSByte(GetUInt32(data));
static sbyte FromInt64(T data) => System.Convert.ToSByte(GetInt64(data));
static sbyte FromUInt64(T data) => System.Convert.ToSByte(GetUInt64(data));
sbyte ConvertGeneric(T data)
{
var (ret, wasConverted) = (_genericConverter ??= new()).ConvertValue(data);
Expand Down
23 changes: 12 additions & 11 deletions BrightData/Converter/ConverterBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.Converter
{
Expand All @@ -13,17 +14,17 @@ internal abstract class ConverterBase<T>(bool throwOnFailure)
protected GenericConverter<float>? _genericConverter = null;
protected readonly bool _throwOnFailure = throwOnFailure;

protected float GetSingle(T data) => __refvalue(__makeref(data), float);
protected double GetDouble(T data) => __refvalue(__makeref(data), double);
protected sbyte GetSByte(T data) => __refvalue(__makeref(data), sbyte);
protected byte GetByte(T data) => __refvalue(__makeref(data), byte);
protected short GetInt16(T data) => __refvalue(__makeref(data), short);
protected ushort GetUInt16(T data) => __refvalue(__makeref(data), ushort);
protected int GetInt32(T data) => __refvalue(__makeref(data), int);
protected uint GetUInt32(T data) => __refvalue(__makeref(data), uint);
protected long GetInt64(T data) => __refvalue(__makeref(data), long);
protected ulong GetUInt64(T data) => __refvalue(__makeref(data), ulong);
protected decimal GetDecimal(T data) => __refvalue(__makeref(data), decimal);
protected static float GetSingle(T data) => Unsafe.As<T, float>(ref data);
protected static double GetDouble(T data) => Unsafe.As<T, double>(ref data);
protected static sbyte GetSByte(T data) => Unsafe.As<T, sbyte>(ref data);
protected static byte GetByte(T data) => Unsafe.As<T, byte>(ref data);
protected static short GetInt16(T data) => Unsafe.As<T, short>(ref data);
protected static ushort GetUInt16(T data) => Unsafe.As<T, ushort>(ref data);
protected static int GetInt32(T data) => Unsafe.As<T, int>(ref data);
protected static uint GetUInt32(T data) => Unsafe.As<T, uint>(ref data);
protected static long GetInt64(T data) => Unsafe.As<T, long>(ref data);
protected static ulong GetUInt64(T data) => Unsafe.As<T, ulong>(ref data);
protected static decimal GetDecimal(T data) => Unsafe.As<T, decimal>(ref data);

public Type From => typeof(T);
}
Expand Down
5 changes: 3 additions & 2 deletions BrightData/DataTable/Rows/TableRowBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace BrightData.DataTable.Rows
{
Expand Down Expand Up @@ -28,8 +29,8 @@ public T Get<T>(uint columnIndex)
{
if (typeof(T) == typeof(string))
{
var str = ret.ToString();
return __refvalue(__makeref(str), T);
var str = ret.ToString() ?? string.Empty;
return Unsafe.As<string, T>(ref str);
}

// attempt to convert the type
Expand Down

0 comments on commit 25b799f

Please sign in to comment.