Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Move] Part-5 Classes into Different Library - Neo.Extensions #3409

Merged
merged 42 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
bf93412
Part-1 `Neo.IO` - move
cschuchardt88 Jul 2, 2024
4d08154
Part-2
cschuchardt88 Jul 2, 2024
597c9ca
Merge branch 'master' into rebuild/the-split-2
cschuchardt88 Jul 5, 2024
9881579
Added `BigInteger` to `Neo.Extensions`
cschuchardt88 Jul 5, 2024
8779151
Found more `BigInteger`
cschuchardt88 Jul 5, 2024
ed34707
Added `ByteArray` to `Neo.Extensions`
cschuchardt88 Jul 5, 2024
5b12304
Added `DateTime` Extensions to `Neo.Extensions`
cschuchardt88 Jul 7, 2024
0f6627a
Added `HashSetExtensions`, `HashSetExtensions2`, `IpAddressExtensions…
cschuchardt88 Jul 7, 2024
95f8a2d
Merge branch 'master' into rebuild/the-split-4
cschuchardt88 Jul 7, 2024
721ce58
Added `ICollection`, `Memory`, `String`, `Unsafe` extensions
cschuchardt88 Jul 7, 2024
129a32f
Adding `using`
cschuchardt88 Jul 7, 2024
397cc1f
dotnet format
cschuchardt88 Jul 7, 2024
dbbf5b3
Merge branch 'master' into rebuild/the-split-2
shargon Jul 8, 2024
b64435b
Added Tests
cschuchardt88 Jul 9, 2024
583e610
Merge branch 'rebuild/the-split-2' of https://github.com/cschuchardt8…
cschuchardt88 Jul 9, 2024
6a50385
Merge branch 'rebuild/the-split-2' into rebuild/the-split-3
cschuchardt88 Jul 9, 2024
f381b89
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 Jul 9, 2024
6261f70
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
58f7fee
Added `tests` from `Part-2`
cschuchardt88 Jul 9, 2024
7f74300
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 Jul 9, 2024
c790166
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
e644bd3
Added `tests` for `PART-4`
cschuchardt88 Jul 9, 2024
79a3578
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
6fa0fda
Added `tests` for `PART-5`
cschuchardt88 Jul 9, 2024
1640c9b
merge master
cschuchardt88 Aug 3, 2024
da0f201
Made changes and fixes
cschuchardt88 Aug 3, 2024
fffe338
Merge branch 'master' into rebuild/the-split-5
cschuchardt88 Aug 24, 2024
58f46ea
Fixes
cschuchardt88 Aug 24, 2024
6676e1f
Merge branch 'master' into rebuild/the-split-5
Jim8y Aug 27, 2024
c30a0db
Apply suggestions from code review
shargon Aug 29, 2024
9b009dd
Update tests/Neo.Extensions.Tests/UT_StringExtensions.cs
shargon Aug 29, 2024
adefc36
Merge branch 'master' into rebuild/the-split-5
shargon Aug 29, 2024
4c8e13b
Merge branch 'master' into rebuild/the-split-5
Jim8y Aug 29, 2024
3e5a0dd
@shagron review changes
cschuchardt88 Sep 1, 2024
17e0c3b
Merge branch 'rebuild/the-split-5' of https://github.com/cschuchardt8…
cschuchardt88 Sep 1, 2024
c1dd9f3
formating
cschuchardt88 Sep 1, 2024
1e877bf
Moved `UnsafeData` tests to `UT_UnsafeData`
cschuchardt88 Sep 1, 2024
9fe3ba9
Merge branch 'master' into rebuild/the-split-5
cschuchardt88 Sep 1, 2024
9e3ce91
Formating
cschuchardt88 Sep 1, 2024
4d4dc97
Merge branch 'master' into rebuild/the-split-5
shargon Sep 11, 2024
f60851d
Merge branch 'master' into rebuild/the-split-5
cschuchardt88 Sep 14, 2024
db012ce
Merge branch 'master' into rebuild/the-split-5
Jim8y Sep 27, 2024
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
12 changes: 12 additions & 0 deletions src/Neo.Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ 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;
}

shargon marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
10 changes: 9 additions & 1 deletion 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 All @@ -30,7 +31,14 @@ public class OrCondition : WitnessCondition
/// </summary>
public WitnessCondition[] Expressions;

public override int Size => base.Size + Expressions.GetVarSize();
public override int Size
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
return base.Size + Expressions.GetVarSize();
}
}

public override WitnessConditionType Type => WitnessConditionType.Or;

protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
Expand Down
Loading
Loading