2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-07-28 18:13:49 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:29 +00:00
|
|
|
// Copyright © 2011-2020 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
|
|
|
|
|
|
|
|
namespace DiscImageChef
|
|
|
|
|
{
|
2016-01-13 19:59:44 +00:00
|
|
|
public static partial class ArrayHelpers
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2019-11-25 00:54:39 +00:00
|
|
|
/// <summary>Fills an array with the specified value</summary>
|
2017-12-23 03:51:42 +00:00
|
|
|
/// <param name="destinationArray">Array</param>
|
|
|
|
|
/// <param name="value">Value</param>
|
|
|
|
|
/// <typeparam name="T">Array type</typeparam>
|
2019-11-25 00:54:39 +00:00
|
|
|
public static void ArrayFill<T>(T[] destinationArray, T value) => ArrayFill(destinationArray, new[]
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2019-11-25 00:54:39 +00:00
|
|
|
value
|
|
|
|
|
});
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2019-11-25 00:54:39 +00:00
|
|
|
/// <summary>Fills an array with the contents of the specified array</summary>
|
2017-12-23 03:51:42 +00:00
|
|
|
/// <param name="destinationArray">Array</param>
|
|
|
|
|
/// <param name="value">Value</param>
|
|
|
|
|
/// <typeparam name="T">Array type</typeparam>
|
2015-10-05 18:58:13 +01:00
|
|
|
public static void ArrayFill<T>(T[] destinationArray, T[] value)
|
|
|
|
|
{
|
2019-11-25 00:54:39 +00:00
|
|
|
if(destinationArray == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(destinationArray));
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2017-12-24 02:46:53 +00:00
|
|
|
if(value.Length > destinationArray.Length)
|
|
|
|
|
throw new ArgumentException("Length of value array must not be more than length of destination");
|
2015-10-05 18:58:13 +01:00
|
|
|
|
|
|
|
|
// set the initial array value
|
|
|
|
|
Array.Copy(value, destinationArray, value.Length);
|
|
|
|
|
|
|
|
|
|
int arrayToFillHalfLength = destinationArray.Length / 2;
|
|
|
|
|
int copyLength;
|
|
|
|
|
|
2017-12-24 02:46:53 +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
|
|
|
|
|
|
|
|
Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength);
|
|
|
|
|
}
|
2016-09-21 01:54:09 +01:00
|
|
|
|
2019-11-25 00:54:39 +00:00
|
|
|
/// <summary>Converts a byte array to its hexadecimal representation</summary>
|
2017-12-23 03:51:42 +00:00
|
|
|
/// <param name="array">Byte array</param>
|
|
|
|
|
/// <param name="upper"><c>true</c> to use uppercase</param>
|
|
|
|
|
/// <returns></returns>
|
2017-12-22 16:35:31 +00:00
|
|
|
public static string ByteArrayToHex(byte[] array, bool upper = false)
|
2016-09-21 01:54:09 +01:00
|
|
|
{
|
2019-11-25 00:54:39 +00:00
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for(long i = 0; i < array.LongLength; i++)
|
|
|
|
|
sb.AppendFormat("{0:x2}", array[i]);
|
2016-09-21 01:54:09 +01:00
|
|
|
|
|
|
|
|
return upper ? sb.ToString().ToUpper() : sb.ToString();
|
|
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|