Skip to content

Commit

Permalink
improve reset impl
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 6, 2024
1 parent 31413d1 commit 4a1a5dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/by/By.Reset.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Byter
{
public partial class By : IBy
{
public void Reset()
{
InternalReset(Array.Empty<byte>());
}

public void Reset(byte[] buffer)
{
InternalReset((buffer == null || buffer.Length <= 0) ? Array.Empty<byte>() : buffer.ToArray());
}

public void Reset(List<byte> buffer)
{
InternalReset((buffer == null || buffer.Count <= 0) ? Array.Empty<byte>() : buffer.ToArray());
}

private void InternalReset(byte[] buffer)
{
Index = 0;
IsValid = true;
_vault.Clear();

if (buffer != null && buffer.Length >= 1)
{
_vault.AddRange(buffer);
}
}
}
}
4 changes: 4 additions & 0 deletions src/by/IBy.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace Byter
{
internal interface IBy
Expand All @@ -7,6 +9,8 @@ internal interface IBy
byte[] Buffer { get; }

void Reset();
void Reset(byte[] buffer);
void Reset(List<byte> buffer);
void Add<T>(T value);
T Get<T>();
}
Expand Down

0 comments on commit 4a1a5dc

Please sign in to comment.