字节[]数组模式搜索

字节[]数组模式搜索

任何人都知道如何搜索/匹配字节[]数组中的字节模式,然后返回位置。

例如

byte[] pattern = new byte[] {12,3,5,76,8,0,6,125};byte[] toBeSearched = new byte[] {23,36,43,76,125,56,34,234,12,3,5,76,8,0,6,125,234,56,211,122,22,4,7,89,76,64,12,3,5,76,8,0,6,125}


catspeake
浏览 342回答 3
3回答

慕容森

我可以建议一些不涉及创建字符串、复制数组或不安全代码的内容:using System;using System.Collections.Generic;static class ByteArrayRocks {&nbsp; &nbsp; static readonly int [] Empty = new int [0];&nbsp; &nbsp; public static int [] Locate (this byte [] self, byte [] candidate)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (IsEmptyLocate (self, candidate))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Empty;&nbsp; &nbsp; &nbsp; &nbsp; var list = new List<int> ();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < self.Length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!IsMatch (self, i, candidate))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.Add (i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return list.Count == 0 ? Empty : list.ToArray ();&nbsp; &nbsp; }&nbsp; &nbsp; static bool IsMatch (byte [] array, int position, byte [] candidate)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (candidate.Length > (array.Length - position))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < candidate.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array [position + i] != candidate [i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; static bool IsEmptyLocate (byte [] array, byte [] candidate)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return array == null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || candidate == null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || array.Length == 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || candidate.Length == 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || candidate.Length > array.Length;&nbsp; &nbsp; }&nbsp; &nbsp; static void Main ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var data = new byte [] { 23, 36, 43, 76, 125, 56, 34, 234, 12, 3, 5, 76, 8, 0, 6, 125, 234, 56, 211, 122, 22, 4, 7, 89, 76, 64, 12, 3, 5, 76, 8, 0, 6, 125 };&nbsp; &nbsp; &nbsp; &nbsp; var pattern = new byte [] { 12, 3, 5, 76, 8, 0, 6, 125 };&nbsp; &nbsp; &nbsp; &nbsp; foreach (var position in data.Locate (pattern))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine (position);&nbsp; &nbsp; }}编辑(通过抽象)&nbsp;-&nbsp;移动内容岗在这里,因为这不是一个答案出于好奇,我创建了一个包含不同答案的小基准。下面是一百万次迭代的结果:solution [Locate]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 00:00:00.7714027solution [FindAll]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;00:00:03.5404399solution [SearchBytePattern]: 00:00:01.1105190solution [MatchBytePattern]:&nbsp; 00:00:03.0658212

一只甜甜圈

使用Linq方法public&nbsp;static&nbsp;IEnumerable<int>&nbsp;PatternAt(byte[]&nbsp;source,&nbsp;byte[]&nbsp;pattern){ &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;source.Length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(source.Skip(i).Take(pattern.Length).SequenceEqual(pattern)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}很简单!
打开App,查看更多内容
随时随地看视频慕课网APP