1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 18:58:47 +00:00

(#7) (#15) IEnumerable<byte> -> IROList<byte>

- GetString renamed to ToString
- No longer has optional Encoding parameter
This commit is contained in:
Oliver Booth 2021-03-07 11:56:52 +00:00
parent 4d31eee600
commit f073c13f03
2 changed files with 252 additions and 116 deletions

View File

@ -1,116 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace X10D
{
/// <summary>
/// Extension methods for <see cref="byte" />.
/// </summary>
public static class ByteExtensions
{
/// <summary>
/// Gets a <see cref="string" /> literally representing the raw values in the <see cref="byte" />[].
/// </summary>
/// <param name="bytes">The bytes to get.</param>
/// <returns>Returns a <see cref="string" />.</returns>
public static string AsString(this IEnumerable<byte> bytes)
{
return BitConverter.ToString(bytes.ToArray());
}
/// <summary>
/// Converts the <see cref="byte" />[] to an <see cref="short" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="short" />.</returns>
public static short GetInt16(this IEnumerable<byte> bytes)
{
return BitConverter.ToInt16(bytes.ToArray(), 0);
}
/// <summary>
/// Converts the <see cref="byte" />[] to an <see cref="int" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="int" />.</returns>
public static int GetInt32(this IEnumerable<byte> bytes)
{
return BitConverter.ToInt32(bytes.ToArray(), 0);
}
/// <summary>
/// Converts the <see cref="byte" />[] to an <see cref="long" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="long" />.</returns>
public static long GetInt64(this IEnumerable<byte> bytes)
{
return BitConverter.ToInt64(bytes.ToArray(), 0);
}
/// <summary>
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with
/// <see cref="Encoding.UTF8" /> encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns a <see cref="string" />.</returns>
public static string GetString(this IEnumerable<byte> bytes)
{
return bytes.GetString(Encoding.UTF8);
}
/// <summary>
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with the provided encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>Returns a <see cref="string" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="encoding" /> is <see langword="null" />.</exception>
public static string GetString(this IEnumerable<byte> bytes, Encoding encoding)
{
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
// ReSharper disable once SuggestVarOrType_Elsewhere
var array = bytes.ToArray();
return encoding.GetString(array, 0, array.Length);
}
/// <summary>
/// Converts the <see cref="byte" />[] to a <see cref="ushort" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="ushort" />.</returns>
[CLSCompliant(false)]
public static ushort GetUInt16(this IEnumerable<byte> bytes)
{
return BitConverter.ToUInt16(bytes.ToArray(), 0);
}
/// <summary>
/// Converts the <see cref="byte" />[] to an <see cref="uint" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="uint" />.</returns>
[CLSCompliant(false)]
public static uint GetUInt32(this IEnumerable<byte> bytes)
{
return BitConverter.ToUInt32(bytes.ToArray(), 0);
}
/// <summary>
/// Converts the <see cref="byte" />[] to an <see cref="ulong" />.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="ulong" />.</returns>
[CLSCompliant(false)]
public static ulong GetUInt64(this IEnumerable<byte> bytes)
{
return BitConverter.ToUInt64(bytes.ToArray(), 0);
}
}
}

View File

@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace X10D.ListExtensions
{
/// <summary>
/// Extension methods for <see cref="byte" /> array.
/// </summary>
public static class ListOfByteExtensions
{
/// <summary>
/// Converts the numeric value of each element of a specified list of bytes to its equivalent hexadecimal string
/// representation.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>
/// A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in
/// <paramref name="source" />; for example, "7F-2C-4A-00".
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static string AsString(this IReadOnlyList<byte> source)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToString(source.ToArray());
}
/// <summary>
/// Returns a double-precision floating point number converted from eight bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A double-precision floating point number formed by eight bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double ToDouble(this IReadOnlyList<byte> source)
{
return ToDouble(source, 0);
}
/// <summary>
/// Returns a double-precision floating point number converted from eight bytes at a specified position in a list of
/// bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>
/// A double-precision floating point number formed by eight bytes beginning at <paramref name="startIndex" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double ToDouble(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToDouble(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a 16-bit signed integer converted from two bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 16-bit signed integer formed by two bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static short ToInt16(this IReadOnlyList<byte> source)
{
return ToInt16(source, 0);
}
/// <summary>
/// Returns a 16-bit signed integer converted from two bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 16-bit signed integer formed by two bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static short ToInt16(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToInt16(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a 32-bit signed integer converted from four bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 32-bit signed integer formed by four bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int ToInt32(this IReadOnlyList<byte> source)
{
return ToInt32(source, 0);
}
/// <summary>
/// Returns a 32-bit signed integer converted from four bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 32-bit signed integer formed by four bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int ToInt32(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToInt32(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a 64-bit signed integer converted from eight bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 64-bit signed integer formed by eight bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long ToInt64(this IReadOnlyList<byte> source)
{
return ToInt64(source, 0);
}
/// <summary>
/// Returns a 64-bit signed integer converted from eight bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 64-bit signed integer formed by eight bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long ToInt64(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToInt64(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a single-precision floating point number converted from four bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A single-precision floating point number formed by four bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float ToSingle(this IReadOnlyList<byte> source)
{
return ToSingle(source, 0);
}
/// <summary>
/// Returns a single-precision floating point number converted from four bytes at a specified position in a list of
/// bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>
/// A single-precision floating point number formed by four bytes beginning at <paramref name="startIndex" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float ToSingle(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToSingle(source.ToArray(), startIndex);
}
/// <summary>
/// Decodes all the bytes within the current list of bytes to a string, using a specified encoding.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="encoding">The encoding which should be used to decode <paramref name="source" />.</param>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="source" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="encoding" /> is <see langword="null" />.</para>
/// </exception>
public static string ToString(this IReadOnlyList<byte> source, Encoding encoding)
{
Validate.IsNotNull(source, nameof(source));
Validate.IsNotNull(encoding, nameof(encoding));
return encoding.GetString(source.ToArray());
}
/// <summary>
/// Returns a 16-bit unsigned integer converted from two bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 16-bit unsigned integer formed by two bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ushort ToUInt16(this IReadOnlyList<byte> source)
{
return ToUInt16(source, 0);
}
/// <summary>
/// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 16-bit unsigned integer formed by two bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ushort ToUInt16(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToUInt16(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a 32-bit unsigned integer converted from four bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 32-bit unsigned integer formed by four bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static uint ToUInt32(this IReadOnlyList<byte> source)
{
return ToUInt32(source, 0);
}
/// <summary>
/// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 32-bit unsigned integer formed by four bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static uint ToUInt32(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToUInt32(source.ToArray(), startIndex);
}
/// <summary>
/// Returns a 64-bit unsigned integer converted from eight bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <returns>A 64-bit unsigned integer formed by eight bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ulong ToUInt64(this IReadOnlyList<byte> source)
{
return ToUInt64(source, 0);
}
/// <summary>
/// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a list of bytes.
/// </summary>
/// <param name="source">The source list of bytes.</param>
/// <param name="startIndex">The starting position within <paramref name="source" />.</param>
/// <returns>A 64-bit unsigned integer formed by eight bytes beginning at <paramref name="startIndex" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ulong ToUInt64(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
return BitConverter.ToUInt64(source.ToArray(), startIndex);
}
}
}