2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-28 18:13:49 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : ArrayFill.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Helpers.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Fills an array with a specified value.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// No license specified by creator.
|
|
|
|
|
//
|
|
|
|
|
// Published on https://github.com/mykohsu/Extensions/blob/master/ArrayExtensions.cs
|
|
|
|
|
//
|
|
|
|
|
// Assuming open source.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:08 +00:00
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// Copyright(C) 2014 mykohsu
|
|
|
|
|
// ****************************************************************************/
|
2015-10-05 18:58:13 +01:00
|
|
|
|
|
|
|
|
using System;
|
2016-09-21 01:54:09 +01:00
|
|
|
using System.Text;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Helpers;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static partial class ArrayHelpers
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Fills an array with the specified value</summary>
|
|
|
|
|
/// <param name="destinationArray">Array</param>
|
|
|
|
|
/// <param name="value">Value</param>
|
|
|
|
|
/// <typeparam name="T">Array type</typeparam>
|
2023-10-04 17:34:37 +01:00
|
|
|
public static void ArrayFill<T>(T[] destinationArray, T value) => ArrayFill(destinationArray, new[]
|
|
|
|
|
{
|
|
|
|
|
value
|
|
|
|
|
});
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Fills an array with the contents of the specified array</summary>
|
|
|
|
|
/// <param name="destinationArray">Array</param>
|
|
|
|
|
/// <param name="value">Value</param>
|
|
|
|
|
/// <typeparam name="T">Array type</typeparam>
|
|
|
|
|
public static void ArrayFill<T>(T[] destinationArray, T[] value)
|
|
|
|
|
{
|
2022-11-15 01:35:06 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(destinationArray);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(value.Length > destinationArray.Length)
|
2022-11-28 10:22:27 +00:00
|
|
|
throw new ArgumentException(Localization.Length_of_value_array_must_not_be_more_than_length_of_destination);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// set the initial array value
|
|
|
|
|
Array.Copy(value, destinationArray, value.Length);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
int arrayToFillHalfLength = destinationArray.Length / 2;
|
|
|
|
|
int copyLength;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(copyLength = value.Length; copyLength < arrayToFillHalfLength; copyLength <<= 1)
|
|
|
|
|
Array.Copy(destinationArray, 0, destinationArray, copyLength, copyLength);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength);
|
|
|
|
|
}
|
2016-09-21 01:54:09 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Converts a byte array to its hexadecimal representation</summary>
|
|
|
|
|
/// <param name="array">Byte array</param>
|
|
|
|
|
/// <param name="upper"><c>true</c> to use uppercase</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string ByteArrayToHex(byte[] array, bool upper = false)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(long i = 0; i < array.LongLength; i++)
|
2023-10-04 08:16:20 +01:00
|
|
|
sb.Append($"{array[i]:x2}");
|
2016-09-21 01:54:09 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return upper ? sb.ToString().ToUpper() : sb.ToString();
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|