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-4 Classes into Different Library - Neo.Extensions #3408

Merged
merged 30 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 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
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
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
e644bd3
Added `tests` for `PART-4`
cschuchardt88 Jul 9, 2024
79888f0
merge from `master`
cschuchardt88 Jul 19, 2024
2f9cca4
Add `using Neo.Extensions` for unit tests
cschuchardt88 Jul 19, 2024
095445b
merge master
cschuchardt88 Jul 30, 2024
94eedbd
Merge branch 'master' into rebuild/the-split-4
Jim8y Aug 1, 2024
b05665b
Change `HashSetExtensions2` to `HashSetExtensions`
cschuchardt88 Aug 5, 2024
e1b628d
Merge branch 'master' into rebuild/the-split-4
shargon Aug 6, 2024
13831c2
Update tests/Neo.Extensions.Tests/UT_BigIntegerExtensions.cs
shargon Aug 6, 2024
0b80cec
Update and rename StringExtensdions.cs to StringExtensdios.cs
shargon Aug 6, 2024
9a97497
Rename StringExtensdios.cs to StringExtensions.cs
shargon Aug 6, 2024
42ca4cc
Merge branch 'master' into rebuild/the-split-4
cschuchardt88 Aug 10, 2024
c8a06c8
`dotnet format`
cschuchardt88 Aug 10, 2024
6018f62
Merge branch 'master' into rebuild/the-split-4
Jim8y Aug 19, 2024
ab65bff
Merge branch 'master' into rebuild/the-split-4
NGDAdmin Aug 23, 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
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Akka.Actor;
using Neo.ConsoleService;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P;
Expand Down
23 changes: 23 additions & 0 deletions src/Neo.Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// AssemblyExtensions.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 System.Reflection;

namespace Neo.Extensions
{
public static class AssemblyExtensions
{
public static string GetVersion(this Assembly assembly)
{
return assembly.GetName().Version!.ToString(3);
}
}
}
42 changes: 42 additions & 0 deletions src/Neo.Extensions/Collections/HashSetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// HashSetExtensions.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 System.Collections.Generic;

namespace Neo.Extensions
{
public static class HashSetExtensions
{
public static void Remove<T>(this HashSet<T> set, ISet<T> other)
{
if (set.Count > other.Count)
{
set.ExceptWith(other);
}
else
{
set.RemoveWhere(u => other.Contains(u));
}
}

public static void Remove<T, V>(this HashSet<T> set, IReadOnlyDictionary<T, V> other)
{
if (set.Count > other.Count)
{
set.ExceptWith(other.Keys);
}
else
{
set.RemoveWhere(u => other.ContainsKey(u));
}
}
}
}
38 changes: 38 additions & 0 deletions src/Neo.Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// DateTimeExtensions.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 System;

namespace Neo.Extensions
{
public static class DateTimeExtensions
{
/// <summary>
/// Converts a <see cref="DateTime"/> to timestamp.
/// </summary>
/// <param name="time">The <see cref="DateTime"/> to convert.</param>
/// <returns>The converted timestamp.</returns>
public static uint ToTimestamp(this DateTime time)
{
return (uint)new DateTimeOffset(time.ToUniversalTime()).ToUnixTimeSeconds();
}

/// <summary>
/// Converts a <see cref="DateTime"/> to timestamp in milliseconds.
/// </summary>
/// <param name="time">The <see cref="DateTime"/> to convert.</param>
/// <returns>The converted timestamp.</returns>
public static ulong ToTimestampMS(this DateTime time)
{
return (ulong)new DateTimeOffset(time.ToUniversalTime()).ToUnixTimeMilliseconds();
}
}
}
40 changes: 40 additions & 0 deletions src/Neo.Extensions/Net/IpAddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// IpAddressExtensions.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 System.Net;

namespace Neo.Extensions
{
public static class IpAddressExtensions
{
/// <summary>
/// Checks if address is IPv4 Mapped to IPv6 format, if so, Map to IPv4.
/// Otherwise, return current address.
/// </summary>
public static IPAddress UnMap(this IPAddress address)
{
if (address.IsIPv4MappedToIPv6)
address = address.MapToIPv4();
return address;
}

/// <summary>
/// Checks if IPEndPoint is IPv4 Mapped to IPv6 format, if so, unmap to IPv4.
/// Otherwise, return current endpoint.
/// </summary>
public static IPEndPoint UnMap(this IPEndPoint endPoint)
{
if (!endPoint.Address.IsIPv4MappedToIPv6)
return endPoint;
return new IPEndPoint(endPoint.Address.UnMap(), endPoint.Port);
}
}
}
34 changes: 34 additions & 0 deletions src/Neo.Extensions/RandomExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// RandomExtensions.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 System;
using System.Numerics;

namespace Neo.Extensions
{
public static class RandomExtensions
{
public static BigInteger NextBigInteger(this Random rand, int sizeInBits)
{
if (sizeInBits < 0)
shargon marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException("sizeInBits must be non-negative");
if (sizeInBits == 0)
return 0;
Span<byte> b = stackalloc byte[sizeInBits / 8 + 1];
rand.NextBytes(b);
if (sizeInBits % 8 == 0)
b[^1] = 0;
else
b[^1] &= (byte)((1 << sizeInBits % 8) - 1);
return new BigInteger(b);
}
}
}
36 changes: 36 additions & 0 deletions src/Neo.Extensions/StringExtensdions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// StringExtensdions.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 System;
using System.Globalization;

namespace Neo.Extensions
{
public static class StringExtensdions
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Converts a hex <see cref="string"/> to byte array.
/// </summary>
/// <param name="value">The hex <see cref="string"/> to convert.</param>
/// <returns>The converted byte array.</returns>
public static byte[] HexToBytes(this string value)
{
if (value == null || value.Length == 0)
return [];
if (value.Length % 2 == 1)
throw new FormatException();
var result = new byte[value.Length / 2];
for (var i = 0; i < result.Length; i++)
result[i] = byte.Parse(value.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier);
return result;
}
}
}
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/CreateMultiSigContractDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.SmartContract;
using Neo.Wallets;
using System;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/ImportCustomContractDialog.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.SmartContract;
using Neo.Wallets;
using System;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/ParametersEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.IO/Caching/HashSetCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Neo.IO.Caching
{
class HashSetCache<T> : IReadOnlyCollection<T> where T : IEquatable<T>
internal class HashSetCache<T> : IReadOnlyCollection<T> where T : IEquatable<T>
{
/// <summary>
/// Sets where the Hashes are stored
Expand Down
3 changes: 2 additions & 1 deletion src/Neo.IO/Neo.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<ItemGroup>
<InternalsVisibleTo Include="Neo" />
<InternalsVisibleTo Include="Neo.UnitTests" />
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
<InternalsVisibleTo Include="Neo.Extensions" />
<InternalsVisibleTo Include="Neo.Extensions.Tests" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Cryptography/ECC/ECCurve.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.Globalization;
using System.Numerics;

Expand Down
36 changes: 36 additions & 0 deletions src/Neo/Extensions/HashSetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// HashSetExtensions.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.Caching;
using System;
using System.Collections.Generic;

namespace Neo.Extensions
{
/// <summary>
/// A helper class that provides common functions.
/// </summary>
public static class HashSetExtensions
{
internal static void Remove<T>(this HashSet<T> set, HashSetCache<T> other)
where T : IEquatable<T>
{
if (set.Count > other.Count)
{
set.ExceptWith(other);
}
else
{
set.RemoveWhere(u => other.Contains(u));
}
}
}
}
Loading
Loading