-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) * 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
1 parent
6a4ea1f
commit 9287c66
Showing
57 changed files
with
410 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.