Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Mar 21, 2024
2 parents 6e7e429 + 833d0fa commit 737dc70
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
19 changes: 19 additions & 0 deletions CSUtilities/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ public static T GetValueByName<T>(string name)
}

/// <summary>
/// Adds a flag value to enum
/// </summary>
public static T AddFlag<T>(this T value, T flag)
where T : Enum
{
return (T)(object)((int)(object)value | (int)(object)flag);
}

/// <summary>
/// Removes the flag value from enum
/// </summary>
public static T RemoveFlag<T>(this T value, T flag)
where T : Enum
{
return (T)(object)((int)(object)value & ~(int)(object)flag);
}

/// <summary>
/// Gets a string value for a particular enum value.
/// Converts the string representation of the name or numeric value of one or more
/// enumerated constants to an equivalent enumerated object
/// </summary>
Expand Down
24 changes: 21 additions & 3 deletions CSUtilities/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSUtilities.Extensions
{
Expand Down Expand Up @@ -31,6 +29,26 @@ public static Queue<T> ToQueue<T>(this IEnumerable<T> enumerable)
return new Queue<T>(enumerable);
}

/// <summary>
/// Gets the element in an specific index or it's default value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable"></param>
/// <param name="index"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryGet<T>(this IEnumerable<T> enumerable, int index, out T result)
{
if (enumerable.Count() < index)
{
result = default(T);
return false;
}

result = enumerable.ElementAt(index);
return true;
}

public static IEnumerable<T> RemoveLastEquals<T>(this IEnumerable<T> enumerable, T element)
{
List<T> lst = new List<T>(enumerable);
Expand Down
56 changes: 52 additions & 4 deletions CSUtilities/IO/StreamIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CSUtilities.IO
{
Expand Down Expand Up @@ -67,13 +69,17 @@ public StreamIO(Stream stream, bool createCopy, bool resetPosition)
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
_stream = (Stream)new MemoryStream(buffer);

if (resetPosition)
//Reset the position to the begining
_stream.Position = 0L;
}
else
{
this._stream = stream;
}

if (resetPosition)
{
//Reset the position to the begining
_stream.Position = 0L;
}

stream.Position = position;
}
Expand Down Expand Up @@ -121,6 +127,26 @@ public byte[] GetBytes(int offset, int length)
return buffer;
}

public async Task<byte[]> GetBytesAsync(int offset, int length)
{
if (length < 0)
throw new ArgumentOutOfRangeException("Length cannot be negative.");

//Save the current position
long save = this.Position;
//Set the position to the begining
this.Position = offset;

byte[] buffer = await this.ReadBytesAsync(length);
//if (this.m_stream.Read(buffer, offset, length) < length)
// throw new EndOfStreamException();

//Reset the position
this.Position = save;

return buffer;
}

/// <summary>
/// Look into a byte without moving the position of the stream.
/// </summary>
Expand Down Expand Up @@ -156,6 +182,15 @@ public virtual byte ReadByte()
return b;
}

public virtual async Task<byte> ReadByteAsync(CancellationToken cancellationToken = default)
{
byte[] arr = new byte[1];
byte b = await _stream.ReadAsync(arr, 0, 1, cancellationToken) == 1 ?
arr[0] : throw new EndOfStreamException();

return b;
}

/// <summary>
/// Read a character from the stream
/// </summary>
Expand Down Expand Up @@ -186,6 +221,19 @@ public virtual byte[] ReadBytes(int length)
return buffer;
}

public virtual async Task<byte[]> ReadBytesAsync(int length, CancellationToken cancellationToken = default)
{
if (length < 0)
throw new ArgumentOutOfRangeException("Length cannot be negative.");

byte[] buffer = new byte[length];

if (await this._stream.ReadAsync(buffer, 0, length, cancellationToken) < length)
throw new EndOfStreamException();

return buffer;
}

/// <summary>
/// Read the stream as a string until it finds the match character
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion CSUtilities/Mutation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if !NET7_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -179,3 +180,4 @@ private static object processObject(object obj)
}
}
}
#endif

0 comments on commit 737dc70

Please sign in to comment.