Skip to content

Commit

Permalink
[Move] Part-5 Classes into Different Library - Neo.Extensions (#3409
Browse files Browse the repository at this point in the history
)

* Part-1 `Neo.IO` - move

* Part-2

* Added `BigInteger` to `Neo.Extensions`

* Found more `BigInteger`

* Added `ByteArray` to `Neo.Extensions`

* Added `DateTime` Extensions to `Neo.Extensions`

* Added `HashSetExtensions`, `HashSetExtensions2`, `IpAddressExtensions`, `AssemblyExtensions`, `StringExtensdions`
Deleted `Helper.cs` file

* Added `ICollection`, `Memory`,  `String`, `Unsafe` extensions

* Adding `using`

* dotnet format

* Added Tests

* Added `tests` from `Part-2`

* Added `tests` for `PART-4`

* Added `tests` for `PART-5`

* Made changes and fixes

* Fixes

* Apply suggestions from code review

* Update tests/Neo.Extensions.Tests/UT_StringExtensions.cs

* @shagron review changes

* formating

* Moved `UnsafeData` tests to `UT_UnsafeData`

* Formating

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: Jimmy <[email protected]>
  • Loading branch information
3 people authored Sep 28, 2024
1 parent 6a4ea1f commit 9287c66
Show file tree
Hide file tree
Showing 57 changed files with 410 additions and 307 deletions.
11 changes: 11 additions & 0 deletions src/Neo.Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,16 @@ public static byte[] HexToBytes(this string value)
result[i] = byte.Parse(value.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier);
return result;
}

/// <summary>
/// Gets the size of the specified <see cref="string"/> encoded in variable-length encoding.
/// </summary>
/// <param name="value">The specified <see cref="string"/>.</param>
/// <returns>The size of the <see cref="string"/>.</returns>
public static int GetVarSize(this string value)
{
var size = Utility.StrictUTF8.GetByteCount(value);
return UnsafeData.GetVarSize(size) + size;
}
}
}
31 changes: 31 additions & 0 deletions src/Neo.Extensions/UnsafeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UnsafeData.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

namespace Neo.Extensions
{
public static class UnsafeData
{
/// <summary>
/// Gets the size of variable-length of the data.
/// </summary>
/// <param name="value">The length of the data.</param>
/// <returns>The size of variable-length of the data.</returns>
public static int GetVarSize(int value)
{
if (value < 0xFD)
return sizeof(byte);
else if (value <= 0xFFFF)
return sizeof(byte) + sizeof(ushort);
else
return sizeof(byte) + sizeof(uint);
}
}
}
74 changes: 74 additions & 0 deletions src/Neo/Extensions/Collections/ICollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ICollectionExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Neo.Extensions
{
public static class ICollectionExtensions
{
/// <summary>
/// Gets the size of the specified array encoded in variable-length encoding.
/// </summary>
/// <typeparam name="T">The type of the array element.</typeparam>
/// <param name="value">The specified array.</param>
/// <returns>The size of the array.</returns>
public static int GetVarSize<T>(this IReadOnlyCollection<T> value)
{
int value_size;
var t = typeof(T);
if (typeof(ISerializable).IsAssignableFrom(t))
{
value_size = value.OfType<ISerializable>().Sum(p => p.Size);
}
else if (t.GetTypeInfo().IsEnum)
{
int element_size;
var u = t.GetTypeInfo().GetEnumUnderlyingType();
if (u == typeof(sbyte) || u == typeof(byte))
element_size = 1;
else if (u == typeof(short) || u == typeof(ushort))
element_size = 2;
else if (u == typeof(int) || u == typeof(uint))
element_size = 4;
else //if (u == typeof(long) || u == typeof(ulong))
element_size = 8;
value_size = value.Count * element_size;
}
else
{
value_size = value.Count * Marshal.SizeOf<T>();
}
return UnsafeData.GetVarSize(value.Count) + value_size;
}

/// <summary>
/// Converts an <see cref="ISerializable"/> array to a byte array.
/// </summary>
/// <typeparam name="T">The type of the array element.</typeparam>
/// <param name="value">The <see cref="ISerializable"/> array to be converted.</param>
/// <returns>The converted byte array.</returns>
public static byte[] ToByteArray<T>(this IReadOnlyCollection<T> value)
where T : ISerializable
{
using MemoryStream ms = new();
using BinaryWriter writer = new(ms, Utility.StrictUTF8, true);
writer.Write(value);
writer.Flush();
return ms.ToArray();
}
}
}
60 changes: 60 additions & 0 deletions src/Neo/Extensions/MemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// MemoryExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;
using System;
using System.Reflection;

namespace Neo.Extensions
{
public static class MemoryExtensions
{
/// <summary>
/// Converts a byte array to an <see cref="ISerializable"/> object.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The byte array to be converted.</param>
/// <returns>The converted <see cref="ISerializable"/> object.</returns>
public static T AsSerializable<T>(this ReadOnlyMemory<byte> value)
where T : ISerializable, new()
{
if (value.IsEmpty) throw new FormatException();
MemoryReader reader = new(value);
return reader.ReadSerializable<T>();
}

/// <summary>
/// Converts a byte array to an <see cref="ISerializable"/> object.
/// </summary>
/// <param name="value">The byte array to be converted.</param>
/// <param name="type">The type to convert to.</param>
/// <returns>The converted <see cref="ISerializable"/> object.</returns>
public static ISerializable AsSerializable(this ReadOnlyMemory<byte> value, Type type)
{
if (!typeof(ISerializable).GetTypeInfo().IsAssignableFrom(type))
throw new InvalidCastException();
var serializable = (ISerializable)Activator.CreateInstance(type);
MemoryReader reader = new(value);
serializable.Deserialize(ref reader);
return serializable;
}

/// <summary>
/// Gets the size of the specified array encoded in variable-length encoding.
/// </summary>
/// <param name="value">The specified array.</param>
/// <returns>The size of the array.</returns>
public static int GetVarSize(this ReadOnlyMemory<byte> value)
{
return UnsafeData.GetVarSize(value.Length) + value.Length;
}
}
}
1 change: 1 addition & 0 deletions src/Neo/IO/Caching/ReflectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using System;
using System.Collections.Generic;
using System.Reflection;
Expand Down
118 changes: 0 additions & 118 deletions src/Neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Neo.IO
{
Expand All @@ -38,35 +35,6 @@ public static class Helper
return reader.ReadSerializable<T>();
}

/// <summary>
/// Converts a byte array to an <see cref="ISerializable"/> object.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The byte array to be converted.</param>
/// <returns>The converted <see cref="ISerializable"/> object.</returns>
public static T AsSerializable<T>(this ReadOnlyMemory<byte> value) where T : ISerializable, new()
{
if (value.IsEmpty) throw new FormatException();
MemoryReader reader = new(value);
return reader.ReadSerializable<T>();
}

/// <summary>
/// Converts a byte array to an <see cref="ISerializable"/> object.
/// </summary>
/// <param name="value">The byte array to be converted.</param>
/// <param name="type">The type to convert to.</param>
/// <returns>The converted <see cref="ISerializable"/> object.</returns>
public static ISerializable AsSerializable(this ReadOnlyMemory<byte> value, Type type)
{
if (!typeof(ISerializable).GetTypeInfo().IsAssignableFrom(type))
throw new InvalidCastException();
ISerializable serializable = (ISerializable)Activator.CreateInstance(type);
MemoryReader reader = new(value);
serializable.Deserialize(ref reader);
return serializable;
}

/// <summary>
/// Converts a byte array to an <see cref="ISerializable"/> array.
/// </summary>
Expand Down Expand Up @@ -124,77 +92,6 @@ public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
return result;
}

/// <summary>
/// Gets the size of variable-length of the data.
/// </summary>
/// <param name="value">The length of the data.</param>
/// <returns>The size of variable-length of the data.</returns>
public static int GetVarSize(int value)
{
if (value < 0xFD)
return sizeof(byte);
else if (value <= 0xFFFF)
return sizeof(byte) + sizeof(ushort);
else
return sizeof(byte) + sizeof(uint);
}

/// <summary>
/// Gets the size of the specified array encoded in variable-length encoding.
/// </summary>
/// <typeparam name="T">The type of the array element.</typeparam>
/// <param name="value">The specified array.</param>
/// <returns>The size of the array.</returns>
public static int GetVarSize<T>(this IReadOnlyCollection<T> value)
{
int value_size;
Type t = typeof(T);
if (typeof(ISerializable).IsAssignableFrom(t))
{
value_size = value.OfType<ISerializable>().Sum(p => p.Size);
}
else if (t.GetTypeInfo().IsEnum)
{
int element_size;
Type u = t.GetTypeInfo().GetEnumUnderlyingType();
if (u == typeof(sbyte) || u == typeof(byte))
element_size = 1;
else if (u == typeof(short) || u == typeof(ushort))
element_size = 2;
else if (u == typeof(int) || u == typeof(uint))
element_size = 4;
else //if (u == typeof(long) || u == typeof(ulong))
element_size = 8;
value_size = value.Count * element_size;
}
else
{
value_size = value.Count * Marshal.SizeOf<T>();
}
return GetVarSize(value.Count) + value_size;
}

/// <summary>
/// Gets the size of the specified array encoded in variable-length encoding.
/// </summary>
/// <param name="value">The specified array.</param>
/// <returns>The size of the array.</returns>
public static int GetVarSize(this ReadOnlyMemory<byte> value)
{
return GetVarSize(value.Length) + value.Length;
}

/// <summary>
/// Gets the size of the specified <see cref="string"/> encoded in variable-length encoding.
/// </summary>
/// <param name="value">The specified <see cref="string"/>.</param>
/// <returns>The size of the <see cref="string"/>.</returns>
public static int GetVarSize(this string value)
{
int size = Utility.StrictUTF8.GetByteCount(value);
return GetVarSize(size) + size;
}

/// <summary>
/// Reads a byte array of the specified size from a <see cref="BinaryReader"/>.
/// </summary>
Expand Down Expand Up @@ -315,21 +212,6 @@ public static byte[] ToArray(this ISerializable value)
return ms.ToArray();
}

/// <summary>
/// Converts an <see cref="ISerializable"/> array to a byte array.
/// </summary>
/// <typeparam name="T">The type of the array element.</typeparam>
/// <param name="value">The <see cref="ISerializable"/> array to be converted.</param>
/// <returns>The converted byte array.</returns>
public static byte[] ToByteArray<T>(this IReadOnlyCollection<T> value) where T : ISerializable
{
using MemoryStream ms = new();
using BinaryWriter writer = new(ms, Utility.StrictUTF8, true);
writer.Write(value);
writer.Flush();
return ms.ToArray();
}

/// <summary>
/// Writes an <see cref="ISerializable"/> object into a <see cref="BinaryWriter"/>.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Akka.IO;
using Neo.Extensions;
using Neo.IO;
using Neo.IO.Caching;
using System;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/AddrPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using System;
using System.IO;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.SmartContract;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/Conditions/OrCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.SmartContract;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/ExtensiblePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/FilterAddPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography;
using Neo.Extensions;
using Neo.IO;
using System;
using System.IO;
Expand Down
Loading

0 comments on commit 9287c66

Please sign in to comment.