.NET Regular expressions on bytes instead of chars

前端 未结 4 1848
栀梦
栀梦 2020-12-29 07:46

I\'m trying to do some parsing that will be easier using regular expressions.

The input is an array (or enumeration) of bytes.

I don\'t want to convert the b

4条回答
  •  隐瞒了意图╮
    2020-12-29 08:01

    As an alternative to using unsafe, just consider writing a simple, recursive comparer like:

    static bool Evaluate(byte[] data, byte[] sequence, int dataIndex=0, int sequenceIndex=0)
    {
           if (sequence[sequenceIndex] == data[dataIndex])
           {
               if (sequenceIndex == sequence.Length - 1)
                   return true;
               else if (dataIndex == data.Length - 1)
                   return false;
               else
                   return Evaluate(data, sequence, dataIndex + 1, sequenceIndex + 1);
           }
           else
           {
               if (dataIndex < data.Length - 1)
                   return Evaluate(data, sequence, dataIndex+1, 0);
               else
                   return false;
           }
    }
    

    You could improve efficiency in a number of ways (i.e. seeking the first byte match instead of iterating, etc.) but this could get you started... hope it helps.

提交回复
热议问题