static int IndexOf(IByteBuffer haystack, IByteBuffer needle) { var haystackSpan = haystack.GetReadableSpan(); var needleSpan = needle.GetReadableSpan(0, needle.Capacity); return(SpanHelpers.IndexOf( ref MemoryMarshal.GetReference(haystackSpan), haystackSpan.Length, ref MemoryMarshal.GetReference(needleSpan), needleSpan.Length)); }
public static int Compare(IByteBuffer bufferA, IByteBuffer bufferB) { if (bufferA.IsSingleIoBuffer && bufferB.IsSingleIoBuffer) { var spanA = bufferA.GetReadableSpan(); var spanB = bufferB.GetReadableSpan(); return(SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(spanA), spanA.Length, ref MemoryMarshal.GetReference(spanB), spanB.Length)); } return(CompareSlow(bufferA, bufferB)); }
public static string DecodeString(IByteBuffer src, int readerIndex, int len, Encoding encoding) { if (0u >= (uint)len) { return(string.Empty); } #if NET451 if (src.IsSingleIoBuffer) { ArraySegment <byte> ioBuf = src.GetIoBuffer(readerIndex, len); return(encoding.GetString(ioBuf.Array, ioBuf.Offset, ioBuf.Count)); } else { int maxLength = encoding.GetMaxCharCount(len); IByteBuffer buffer = src.Allocator.HeapBuffer(maxLength); try { buffer.WriteBytes(src, readerIndex, len); ArraySegment <byte> ioBuf = buffer.GetIoBuffer(); return(encoding.GetString(ioBuf.Array, ioBuf.Offset, ioBuf.Count)); } finally { // Release the temporary buffer again. buffer.Release(); } } #else var source = src.GetReadableSpan(readerIndex, len); #if NETCOREAPP || NETSTANDARD_2_0_GREATER return(encoding.GetString(source)); #else unsafe { fixed(byte *bytes = &MemoryMarshal.GetReference(source)) { return(encoding.GetString(bytes, source.Length)); } } #endif #endif }
/// <summary> /// Returns <c>true</c> if and only if the two specified buffers are /// identical to each other for {@code length} bytes starting at {@code aStartIndex} /// index for the {@code a} buffer and {@code bStartIndex} index for the {@code b} buffer. /// A more compact way to express this is: /// <p /> /// {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]} /// </summary> public static bool Equals(IByteBuffer a, int aStartIndex, IByteBuffer b, int bStartIndex, int length) { if (aStartIndex < 0 || bStartIndex < 0 || length < 0) { ThrowHelper.ThrowArgumentException_NonNegative(); } if (a.WriterIndex - length < aStartIndex || b.WriterIndex - length < bStartIndex) { return(false); } if (a.IsSingleIoBuffer && b.IsSingleIoBuffer) { var spanA = a.GetReadableSpan(aStartIndex, length); var spanB = b.GetReadableSpan(bStartIndex, length); return(SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(spanA), ref MemoryMarshal.GetReference(spanB), length)); } return(EqualsSlow(a, aStartIndex, b, bStartIndex, length)); }
protected internal override ReadOnlySpan <byte> _GetReadableSpan(int index, int count) { if (0u >= (uint)count) { return(ReadOnlySpan <byte> .Empty); } switch (_componentCount) { case 0: return(ReadOnlySpan <byte> .Empty); case 1: //ComponentEntry c = _components[0]; //return c.Buffer.GetReadableSpan(index, count); ComponentEntry c = _components[0]; IByteBuffer buf = c.Buffer; if (buf.IsSingleIoBuffer) { return(buf.GetReadableSpan(c.Idx(index), count)); } break; } var merged = new Memory <byte>(new byte[count]); var buffers = GetSequence(index, count); int offset = 0; foreach (var buf in buffers) { Debug.Assert(merged.Length - offset >= buf.Length); buf.CopyTo(merged.Slice(offset)); offset += buf.Length; } return(merged.Span); }
public ReadOnlySpan <byte> GetReadableSpan(int index, int count) { return(_buffer.GetReadableSpan(index, count)); }