-
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.
Merge remote-tracking branch 'upstream/master' into add-ut-24q3
- Loading branch information
Showing
74 changed files
with
2,331 additions
and
632 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,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); | ||
} | ||
} | ||
} |
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,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)); | ||
} | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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) | ||
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); | ||
} | ||
} | ||
} |
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,53 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// SecureStringExtensions.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.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
namespace Neo.Extensions | ||
{ | ||
public static class SecureStringExtensions | ||
{ | ||
public static string? GetClearText(this SecureString secureString) | ||
{ | ||
if (secureString is null) | ||
throw new ArgumentNullException(nameof(secureString)); | ||
|
||
var unmanagedStringPtr = IntPtr.Zero; | ||
|
||
try | ||
{ | ||
unmanagedStringPtr = Marshal.SecureStringToGlobalAllocUnicode(secureString); | ||
return Marshal.PtrToStringUni(unmanagedStringPtr); | ||
} | ||
finally | ||
{ | ||
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedStringPtr); | ||
} | ||
} | ||
|
||
public static SecureString ToSecureString(this string value, bool asReadOnly = true) | ||
{ | ||
unsafe | ||
{ | ||
fixed (char* passwordChars = value) | ||
{ | ||
var securePasswordString = new SecureString(passwordChars, value.Length); | ||
|
||
if (asReadOnly) | ||
securePasswordString.MakeReadOnly(); | ||
return securePasswordString; | ||
} | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// StringExtensions.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 StringExtensions | ||
{ | ||
/// <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; | ||
} | ||
} | ||
} |
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
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)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.