parent
89a21c96c0
commit
a245f5a0d4
@ -1,48 +0,0 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Common.Extensions
|
||||
{
|
||||
// The MS CollectionExtensions are only available in netcoreapp
|
||||
public static class CollectionExtensions
|
||||
{
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
|
||||
{
|
||||
dictionary.TryGetValue(key, out var ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all the elements of the current collection to the specified list
|
||||
/// starting at the specified destination array index. The index is specified as a 32-bit integer.
|
||||
/// </summary>
|
||||
/// <param name="source">The current collection that is the source of the elements.</param>
|
||||
/// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
|
||||
/// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
|
||||
{
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
destination[index + i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all the elements of the current collection to the specified list
|
||||
/// starting at the specified destination array index. The index is specified as a 32-bit integer.
|
||||
/// </summary>
|
||||
/// <param name="source">The current collection that is the source of the elements.</param>
|
||||
/// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
|
||||
/// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void CopyTo<T>(this IReadOnlyCollection<T> source, IList<T> destination, int index = 0)
|
||||
{
|
||||
foreach (T item in source)
|
||||
{
|
||||
destination[index++] = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides <c>CopyTo</c> extensions methods for <see cref="IReadOnlyList{T}" />.
|
||||
/// </summary>
|
||||
public static class CollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies all the elements of the current collection to the specified list
|
||||
/// starting at the specified destination array index. The index is specified as a 32-bit integer.
|
||||
/// </summary>
|
||||
/// <param name="source">The current collection that is the source of the elements.</param>
|
||||
/// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
|
||||
/// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
|
||||
{
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
destination[index + i] = source[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace MediaBrowser.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Encoding and decoding hex strings.
|
||||
/// </summary>
|
||||
public static class Hex
|
||||
{
|
||||
internal const string HexCharsLower = "0123456789abcdef";
|
||||
internal const string HexCharsUpper = "0123456789ABCDEF";
|
||||
|
||||
/// <summary>
|
||||
/// Encodes <c>bytes</c> as a hex string.
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
/// <param name="lowercase"></param>
|
||||
/// <returns><c>bytes</c> as a hex string.</returns>
|
||||
public static string Encode(ReadOnlySpan<byte> bytes, bool lowercase = true)
|
||||
{
|
||||
var hexChars = lowercase ? HexCharsLower : HexCharsUpper;
|
||||
|
||||
// TODO: use string.Create when it's supports spans
|
||||
// Ref: https://github.com/dotnet/corefx/issues/29120
|
||||
char[] s = new char[bytes.Length * 2];
|
||||
int j = 0;
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
s[j++] = hexChars[bytes[i] >> 4];
|
||||
s[j++] = hexChars[bytes[i] & 0x0f];
|
||||
}
|
||||
|
||||
return new string(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a hex string into bytes.
|
||||
/// </summary>
|
||||
/// <param name="str">The <see cref="string" />.</param>
|
||||
/// <returns>The decoded bytes.</returns>
|
||||
public static byte[] Decode(ReadOnlySpan<char> str)
|
||||
{
|
||||
byte[] bytes = new byte[str.Length / 2];
|
||||
int j = 0;
|
||||
for (int i = 0; i < str.Length; i += 2)
|
||||
{
|
||||
bytes[j++] = byte.Parse(
|
||||
str.Slice(i, 2),
|
||||
NumberStyles.HexNumber,
|
||||
CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace MediaBrowser.Common
|
||||
{
|
||||
public static class HexHelper
|
||||
{
|
||||
public static byte[] FromHexString(string str)
|
||||
{
|
||||
byte[] bytes = new byte[str.Length / 2];
|
||||
for (int i = 0; i < str.Length; i += 2)
|
||||
{
|
||||
bytes[i / 2] = byte.Parse(str.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static string ToHexString(byte[] bytes)
|
||||
=> BitConverter.ToString(bytes).Replace("-", "");
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,19 @@
|
||||
using MediaBrowser.Common;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Common.Tests
|
||||
{
|
||||
public class HexTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("00")]
|
||||
[InlineData("01")]
|
||||
[InlineData("000102030405060708090a0b0c0d0e0f")]
|
||||
[InlineData("0123456789abcdef")]
|
||||
public void RoundTripTest(string data)
|
||||
{
|
||||
Assert.Equal(data, Hex.Encode(Hex.Decode(data)));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue