private bool IndexOf(ref IndexOfResult result, IBuffer buffer) { int start = buffer.Postion; int length = buffer.Length; Span <byte> data = buffer.Memory.Span; Span <byte> eof = result.EofData; int eoflen = eof.Length; result.End = buffer; int eofindex = result.EofIndex; int endpoint; for (int i = start; i < length; i++) { result.Length++; if (data[i] == eof[eofindex]) { eofindex++; endpoint = i; if (eofindex == eoflen) { result.EndPostion = endpoint; return(true); } } else { eofindex = 0; } } result.EofIndex = eofindex; result.End = null; return(false); }
private IndexOfResult IndexOf(byte[] eof) { IndexOfResult result = new IndexOfResult { EofData = eof }; if (eof == null || _length < eof.Length) { return(result); } IBuffer rbuffer = GetReadBuffer(); if (rbuffer == null) { return(result); } result.Start = rbuffer; result.StartPostion = rbuffer.Postion; if (IndexOf(ref result, rbuffer)) { return(result); } rbuffer = rbuffer.Next; while (rbuffer != null) { if (IndexOf(ref result, rbuffer)) { return(result); } rbuffer = rbuffer.Next; } return(result); }
public bool TryReadWith(byte[] eof, out string value, bool returnEof = false) { value = null; IndexOfResult result = IndexOf(eof); int length = result.Length; if (result.End != null) { if (result.Start.Id == result.End.Id) { char[] charSpan = _charCacheBlock; if (result.Length < _cacheBlockLength) { var len = Encoding.GetChars(result.Start.Data, result.StartPostion, length, charSpan, 0); if (returnEof) { value = new string(charSpan, 0, len); } else { value = new string(charSpan, 0, len - eof.Length); } ReadFree(length); } else { if (returnEof) { value = ReadString(result.Length); } else { value = ReadString(result.Length - eof.Length); Read(eof, 0, eof.Length); } } } else { if (returnEof) { value = ReadString(result.Length); } else { value = ReadString(result.Length - eof.Length); Read(eof, 0, eof.Length); } } return(true); } return(false); }
public bool TryReadWith(byte[] eof, out byte[] value, bool returnEof = false) { value = null; IndexOfResult result = IndexOf(eof); if (result.End != null) { int readLength; if (returnEof) { readLength = result.Length; value = Read(readLength); } else { readLength = result.Length - eof.Length; value = Read(readLength); Read(eof, 0, eof.Length); } return(true); } return(false); }