mirror of
https://github.com/kwsch/NHSE.git
synced 2026-04-05 08:25:03 -05:00
The previous method was fine, not sure what the underlying issue is. The unit tests assert that the sequence is fully replaced.
31 lines
730 B
C#
31 lines
730 B
C#
using System;
|
|
|
|
namespace NHSE.Core;
|
|
|
|
/// <summary>
|
|
/// Array reusable logic
|
|
/// </summary>
|
|
public static class ArrayUtil
|
|
{
|
|
public static int ReplaceOccurrences(this Span<byte> array, ReadOnlySpan<byte> pattern, ReadOnlySpan<byte> swap)
|
|
{
|
|
if (pattern.Length != swap.Length)
|
|
return -1;
|
|
if (pattern.SequenceEqual(swap))
|
|
return 0;
|
|
|
|
int count = 0;
|
|
int ofs = 0;
|
|
while (true)
|
|
{
|
|
var index = array[ofs..].IndexOf(pattern);
|
|
if (index == -1)
|
|
return count;
|
|
ofs += index;
|
|
|
|
swap.CopyTo(array[ofs..]);
|
|
ofs += swap.Length; // skip past swapped data
|
|
++count;
|
|
}
|
|
}
|
|
} |