public int CopyTo(Span <byte> buffer) { var first = First; if (first.Length > buffer.Length) { first.Slice(buffer.Length).CopyTo(buffer); return(buffer.Length); } first.CopyTo(buffer); // TODO (pri 2): do we need to compute the length here? return(first.Length + _rest.CopyTo(buffer.Slice(first.Length, ComputeLength() - first.Length))); }
// TODO (pri 3): I am pretty sure this whole routine can be written much better // searches values that potentially straddle between first and rest internal static long IndexOfStraddling(this ReadOnlySpan <byte> first, IMemoryList <byte> rest, ReadOnlySpan <byte> value) { Debug.Assert(first.IndexOf(value) == -1); if (rest == null) { return(-1); } // we only need to search the end of the first buffer. More precisely, only up to value.Length - 1 bytes in the first buffer // The other bytes in first, were already search and presumably did not match int bytesToSkipFromFirst = 0; if (first.Length > value.Length - 1) { bytesToSkipFromFirst = first.Length - value.Length - 1; } // now that we know how many bytes we need to skip, create slice of first buffer with bytes that need to be searched. ReadOnlySpan <byte> bytesToSearchAgain; if (bytesToSkipFromFirst > 0) { bytesToSearchAgain = first.Slice(bytesToSkipFromFirst); } else { bytesToSearchAgain = first; } long index; // now combine the bytes from the end of the first buffer with bytes in the rest, and serarch the combined buffer // this check is a small optimization: if the first byte from the value does not exist in the bytesToSearchAgain, there is no reason to combine if (bytesToSearchAgain.IndexOf(value[0]) != -1) { var combinedBufferLength = value.Length << 1; var combined = combinedBufferLength < 128 ? stackalloc byte[combinedBufferLength] : // TODO (pri 3): I think this could be eliminated by chunking values new byte[combinedBufferLength]; bytesToSearchAgain.CopyTo(combined); int combinedLength = bytesToSearchAgain.Length + rest.CopyTo(combined.Slice(bytesToSearchAgain.Length)); combined = combined.Slice(0, combinedLength); if (combined.Length < value.Length) { return(-1); } index = combined.IndexOf(value); if (index != -1) { return(index + bytesToSkipFromFirst); } } // try to find the bytes in _rest index = rest.IndexOf(value); if (index != -1) { return(first.Length + index); } return(-1); }