下面的代碼應該工作,使用一個基本的兩遍算法來找到匹配位置,然後做替換。
請注意,對於10MB文件,我的半體面筆記本電腦需要大約10秒。如果你希望它變得更快,你可以使用字節數組和掩碼來實現它,而不是笨重而不那麼強大的BitArray抽象。
更好的是,你可以使用不安全的代碼,在那裏你可以使用指針和更快的複製...但是,因爲它是一個C#問題,你已經使用了BitArray抽象,我想我會告訴你怎樣才能實現。
private static BitArray Replace(BitArray input, BitArray pattern, BitArray replacement)
{
var replacementPositions = GetReplacementPositions(input, pattern);
return PerformReplacements(input, pattern.Length, replacement, replacementPositions);
}
private static List<int> GetReplacementPositions(BitArray input, BitArray pattern)
{
if (pattern.Length == 0) throw new Exception("Pattern cannot have 0 length");
var matchIndicies = new List<int>();
var maxCheckIndex = input.Length - pattern.Length;
var i = 0;
while (i <= maxCheckIndex)
{
if (MatchesAt(input, pattern, i))
{
matchIndicies.Add(i);
i += pattern.Length;
continue;
}
i++;
}
return matchIndicies;
}
private static bool MatchesAt(BitArray input, BitArray pattern, int index)
{
for (var j = 0; j < pattern.Length; j++)
{
if (input[index + j] != pattern[j]) return false;
}
return true;
}
private static BitArray PerformReplacements(BitArray input, int patternLength, BitArray replacement, List<int> replacementPositions)
{
var outLength = input.Length + replacementPositions.Count * (replacement.Length - patternLength);
var output = new BitArray(outLength);
var currentReadIndex = 0;
var currentWriteIndex = 0;
foreach (var matchPosition in replacementPositions)
{
var inputSubstringLength = matchPosition - currentReadIndex;
CopyFromTo(input, output, currentReadIndex, inputSubstringLength, currentWriteIndex);
currentReadIndex = matchPosition + patternLength;
currentWriteIndex += inputSubstringLength;
CopyFromTo(replacement, output, 0, replacement.Length, currentWriteIndex);
currentWriteIndex += replacement.Length;
}
CopyFromTo(input, output, currentReadIndex, input.Length - currentReadIndex, currentWriteIndex);
return output;
}
private static void CopyFromTo(BitArray from, BitArray to, int fromIndex, int fromLength, int toIndex)
{
for (var i = 0; i < fromLength; i++)
{
to.Set(toIndex + i, from.Get(fromIndex + i));
}
}
你能舉個例子嗎? –
假設我有這樣的模式:'010101',我想用'X'來改變它 – Ben
你的意思是把這個模式替換成它出現在數組中的任何地方? –