我有一个类型的缓冲区ReadOnlySequence<byte>。我想提取的亚序列(其中将包含0 - n条消息)通过知道从它与每一个消息结束0x1c, 0x0d(如所描述的在这里)。
我知道缓冲区有一个扩展方法PositionOf但它
返回第一次出现的位置
item的ReadOnlySequence<T>。
我正在寻找一种方法来返回最后一次出现的位置。我试图自己实现它,这是我迄今为止所拥有的
private SequencePosition? GetLastPosition(ReadOnlySequence<byte> buffer)
{
// Do not modify the real buffer
ReadOnlySequence<byte> temporaryBuffer = buffer;
SequencePosition? lastPosition = null;
do
{
/*
Find the first occurence of the delimiters in the buffer
This only takes a byte, what to do with the delimiters? { 0x1c, 0x0d }
*/
SequencePosition? foundPosition = temporaryBuffer.PositionOf(???);
// Is there still an occurence?
if (foundPosition != null)
{
lastPosition = foundPosition;
// cut off the sequence for the next run
temporaryBuffer = temporaryBuffer.Slice(0, lastPosition.Value);
}
else
{
// this is required because otherwise this loop is infinite if lastPosition was set once
break;
}
} while (lastPosition != null);
return lastPosition;
}
Run Code Online (Sandbox Code Playgroud)
我正在挣扎。首先,该PositionOf方法只需要 abyte但有两个分隔符,所以我必须传入 a byte[]。接下来我想我可以“以某种方式”优化循环。
你有什么想法如何找到这些分隔符的最后一次出现吗?
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |