public void TestGet() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment<int>( array, 1, 2 ); Assert.That( target.Get( 0 ), Is.EqualTo( 1 ) ); Assert.That( target.Get( 1 ), Is.EqualTo( 2 ) ); }
public void TestGet() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment <int>(array, 1, 2); Assert.That(target.Get(0), Is.EqualTo(1)); Assert.That(target.Get(1), Is.EqualTo(2)); }
public static Single NetworkByteDWordToFloatNativeByteOrder(ArraySegment <Byte> bytes) { if (BitConverter.IsLittleEndian) { // Network to Little var value = new ByteUnion.DWordValue { Byte0 = bytes.Get(3), Byte1 = bytes.Get(2), Byte2 = bytes.Get(1), Byte3 = bytes.Get(0), }; return(value.Float); } else { // Network to Big var value = new ByteUnion.DWordValue { Byte0 = bytes.Get(0), Byte1 = bytes.Get(1), Byte2 = bytes.Get(2), Byte3 = bytes.Get(3), }; return(value.Float); } }
private void Merge(ArraySegment <T> array1, ArraySegment <T> array2, ArraySegment <T> output) { for (int i = 0, j = 0, k = 0; k < output.Count; ++k) { if (j >= array2.Count || (i < array1.Count && array1.Get(i).CompareTo(array2.Get(j)) < 0)) { output.Set(k, array1.Get(i++)); } else { output.Set(k, array2.Get(j++)); } } }
public unsafe void Decompress(ArraySegment<byte> input, ArraySegment<byte> output) { var bufsiz = input.Get(0) << 24 | input.Get(1) << 16 | input.Get(2) << 8 | input.Get(3); if (bufsiz + 4 > input.Count || bufsiz < 0) throw new ArgumentOutOfRangeException("Not enough bytes in input."); fixed (byte* inPtr = input.Array) { fixed (byte* outPtr = output.Array) { int ret = Lz4Net.Lz4.LZ4_uncompress(inPtr + input.Offset + 4, outPtr + output.Offset, output.Count); if (ret != bufsiz) throw new Exception("Did not decompress the right number of bytes."); } } }
public void TestGetNegative() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment <int>(array, 1, 2); target.Get(-1); }
public void TestGetTooBig() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment <int>(array, 1, 2); target.Get(3); }
public static List <ArraySegment <byte> > Split(this ArraySegment <byte> msg, char separator, int count) { var ret = new List <ArraySegment <byte> >(); int i = 0; for (int substringNdx = 1; substringNdx < count; substringNdx++) { if (i == msg.Count) { break; } int splitCount = 0; while ((i + splitCount) < msg.Count && msg.Get(i + splitCount) != (byte)separator) { splitCount++; } ret.Add(Substring(msg, i, splitCount)); i += splitCount + 1; } if (i != msg.Count) { ret.Add(Substring(msg, i, msg.Count - i)); } return(ret); }
static void DecodePayload(ArraySegment <Byte> bytes, ArraySegment <Byte> key) { for (int i = 0; i < bytes.Count; ++i) { var unmasked = (Byte)(bytes.Get(i) ^ key.Get(i % 4)); bytes.Set(i, unmasked); } }
public static T[] TakeReversedArray <T>(this ArraySegment <T> self, Int32 n) { var array = new T[n]; var x = n - 1; for (int i = 0; i < n; ++i, --x) { array[i] = self.Get(x); } return(array); }
public static void Swap <T>(this ArraySegment <T> array, int i, int j) { if (array == null) { throw new ArgumentNullException("array"); } if (i < 0 || i >= array.Count) { throw new ArgumentOutOfRangeException("i"); } if (j < 0 || j >= array.Count) { throw new ArgumentOutOfRangeException("j"); } var temp = array.Get(i); array.Set(i, array.Get(j)); array.Set(j, temp); }
public static UInt16 NetworkByteWordToUnsignedNativeByteOrder(ArraySegment <Byte> bytes) { if (BitConverter.IsLittleEndian) { // Network to Little var value = new ByteUnion.WordValue { Byte0 = bytes.Get(1), Byte1 = bytes.Get(0), }; return(value.Unsigned); } else { // Network to Big var value = new ByteUnion.WordValue { Byte0 = bytes.Get(0), Byte1 = bytes.Get(1), }; return(value.Unsigned); } }
public int Push(ArraySegment <Byte> bytes, int i) { if (m_isFrameHeader) { for (; i < bytes.Count; ++i) { if (bytes.Get(i) == 0x0a) { m_isFrameHeader = false; ++i; break; } m_header.Add(bytes.Get(i)); } } for (; i < bytes.Count && m_fill < m_body.Length; ++i, ++m_fill) { m_body[m_fill] = bytes.Get(i); } return(i); }
private void Sort(ArraySegment <TValue> array) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Count <= 1) { return; } int pivot = array.Count - 1; int i = 0; int j = pivot - 1; while (i <= j) { if (array.Get(i).CompareTo(array.Get(pivot)) > 0) { array.Swap(i, j--); } else { ++i; } } array.Swap(pivot, i); Sort(array.Segment(0, i)); if (i < array.Count - 1) { Sort(array.Segment(i + 1, array.Count - i - 1)); } }
private void Sort(ArraySegment <T> array, ArraySegment <T> output, bool inPlace) { if (array == null) { throw new ArgumentNullException("array"); } if (output == null) { throw new ArgumentNullException("output"); } if (output.Count != array.Count || output.Offset != array.Offset) { throw new ArgumentException("output"); } if (array.Count <= 1) { if (!inPlace && array.Count == 1) { output.Set(0, array.Get(0)); } return; } int i = array.Count / 2; var segment1 = array.Segment(0, i); var output1 = output.Segment(0, i); var segment2 = array.Segment(i, array.Count - i); var output2 = output.Segment(i, array.Count - i); this.Sort(segment1, output1, !inPlace); this.Sort(segment2, output2, !inPlace); if (inPlace) { this.Merge(output1, output2, array); } else { this.Merge(segment1, segment2, output); } }
public static UInt64 NetworkByteQWordToUnsignedNativeByteOrder(ArraySegment <Byte> bytes) { if (BitConverter.IsLittleEndian) { // Network to Little var value = new ByteUnion.QWordValue { Byte0 = bytes.Get(7), Byte1 = bytes.Get(6), Byte2 = bytes.Get(5), Byte3 = bytes.Get(4), Byte4 = bytes.Get(3), Byte5 = bytes.Get(2), Byte6 = bytes.Get(1), Byte7 = bytes.Get(0), }; return(value.Unsigned); } else { // Network to Big var value = new ByteUnion.QWordValue { Byte0 = bytes.Get(0), Byte1 = bytes.Get(1), Byte2 = bytes.Get(2), Byte3 = bytes.Get(3), Byte4 = bytes.Get(4), Byte5 = bytes.Get(5), Byte6 = bytes.Get(6), Byte7 = bytes.Get(7), }; return(value.Unsigned); } }
static MsgPackType GetFormat(ArraySegment <Byte> bytes) { return((MsgPackType)bytes.Get(0)); }
/// <summary> /// ArrayとMap以外のタイプのペイロードを得る /// </summary> /// <returns></returns> static ArraySegment <Byte> GetBody(ArraySegment <Byte> bytes, MsgPackType formatType) { switch (formatType) { case MsgPackType.FIX_STR: return(bytes.Advance(1).Take(0)); case MsgPackType.FIX_STR_0x01: return(bytes.Advance(1).Take(1)); case MsgPackType.FIX_STR_0x02: return(bytes.Advance(1).Take(2)); case MsgPackType.FIX_STR_0x03: return(bytes.Advance(1).Take(3)); case MsgPackType.FIX_STR_0x04: return(bytes.Advance(1).Take(4)); case MsgPackType.FIX_STR_0x05: return(bytes.Advance(1).Take(5)); case MsgPackType.FIX_STR_0x06: return(bytes.Advance(1).Take(6)); case MsgPackType.FIX_STR_0x07: return(bytes.Advance(1).Take(7)); case MsgPackType.FIX_STR_0x08: return(bytes.Advance(1).Take(8)); case MsgPackType.FIX_STR_0x09: return(bytes.Advance(1).Take(9)); case MsgPackType.FIX_STR_0x0A: return(bytes.Advance(1).Take(10)); case MsgPackType.FIX_STR_0x0B: return(bytes.Advance(1).Take(11)); case MsgPackType.FIX_STR_0x0C: return(bytes.Advance(1).Take(12)); case MsgPackType.FIX_STR_0x0D: return(bytes.Advance(1).Take(13)); case MsgPackType.FIX_STR_0x0E: return(bytes.Advance(1).Take(14)); case MsgPackType.FIX_STR_0x0F: return(bytes.Advance(1).Take(15)); case MsgPackType.FIX_STR_0x10: return(bytes.Advance(1).Take(16)); case MsgPackType.FIX_STR_0x11: return(bytes.Advance(1).Take(17)); case MsgPackType.FIX_STR_0x12: return(bytes.Advance(1).Take(18)); case MsgPackType.FIX_STR_0x13: return(bytes.Advance(1).Take(19)); case MsgPackType.FIX_STR_0x14: return(bytes.Advance(1).Take(20)); case MsgPackType.FIX_STR_0x15: return(bytes.Advance(1).Take(21)); case MsgPackType.FIX_STR_0x16: return(bytes.Advance(1).Take(22)); case MsgPackType.FIX_STR_0x17: return(bytes.Advance(1).Take(23)); case MsgPackType.FIX_STR_0x18: return(bytes.Advance(1).Take(24)); case MsgPackType.FIX_STR_0x19: return(bytes.Advance(1).Take(25)); case MsgPackType.FIX_STR_0x1A: return(bytes.Advance(1).Take(26)); case MsgPackType.FIX_STR_0x1B: return(bytes.Advance(1).Take(27)); case MsgPackType.FIX_STR_0x1C: return(bytes.Advance(1).Take(28)); case MsgPackType.FIX_STR_0x1D: return(bytes.Advance(1).Take(29)); case MsgPackType.FIX_STR_0x1E: return(bytes.Advance(1).Take(30)); case MsgPackType.FIX_STR_0x1F: return(bytes.Advance(1).Take(31)); case MsgPackType.STR8: case MsgPackType.BIN8: { var count = bytes.Get(1); return(bytes.Advance(1 + 1).Take(count)); } case MsgPackType.STR16: case MsgPackType.BIN16: { var count = EndianConverter.NetworkByteWordToUnsignedNativeByteOrder(bytes.Advance(1)); return(bytes.Advance(1 + 2).Take(count)); } case MsgPackType.STR32: case MsgPackType.BIN32: { var count = EndianConverter.NetworkByteDWordToUnsignedNativeByteOrder(bytes.Advance(1)); return(bytes.Advance(1 + 4).Take((int)count)); } case MsgPackType.NIL: case MsgPackType.TRUE: case MsgPackType.FALSE: case MsgPackType.POSITIVE_FIXNUM: case MsgPackType.POSITIVE_FIXNUM_0x01: case MsgPackType.POSITIVE_FIXNUM_0x02: case MsgPackType.POSITIVE_FIXNUM_0x03: case MsgPackType.POSITIVE_FIXNUM_0x04: case MsgPackType.POSITIVE_FIXNUM_0x05: case MsgPackType.POSITIVE_FIXNUM_0x06: case MsgPackType.POSITIVE_FIXNUM_0x07: case MsgPackType.POSITIVE_FIXNUM_0x08: case MsgPackType.POSITIVE_FIXNUM_0x09: case MsgPackType.POSITIVE_FIXNUM_0x0A: case MsgPackType.POSITIVE_FIXNUM_0x0B: case MsgPackType.POSITIVE_FIXNUM_0x0C: case MsgPackType.POSITIVE_FIXNUM_0x0D: case MsgPackType.POSITIVE_FIXNUM_0x0E: case MsgPackType.POSITIVE_FIXNUM_0x0F: case MsgPackType.POSITIVE_FIXNUM_0x10: case MsgPackType.POSITIVE_FIXNUM_0x11: case MsgPackType.POSITIVE_FIXNUM_0x12: case MsgPackType.POSITIVE_FIXNUM_0x13: case MsgPackType.POSITIVE_FIXNUM_0x14: case MsgPackType.POSITIVE_FIXNUM_0x15: case MsgPackType.POSITIVE_FIXNUM_0x16: case MsgPackType.POSITIVE_FIXNUM_0x17: case MsgPackType.POSITIVE_FIXNUM_0x18: case MsgPackType.POSITIVE_FIXNUM_0x19: case MsgPackType.POSITIVE_FIXNUM_0x1A: case MsgPackType.POSITIVE_FIXNUM_0x1B: case MsgPackType.POSITIVE_FIXNUM_0x1C: case MsgPackType.POSITIVE_FIXNUM_0x1D: case MsgPackType.POSITIVE_FIXNUM_0x1E: case MsgPackType.POSITIVE_FIXNUM_0x1F: case MsgPackType.POSITIVE_FIXNUM_0x20: case MsgPackType.POSITIVE_FIXNUM_0x21: case MsgPackType.POSITIVE_FIXNUM_0x22: case MsgPackType.POSITIVE_FIXNUM_0x23: case MsgPackType.POSITIVE_FIXNUM_0x24: case MsgPackType.POSITIVE_FIXNUM_0x25: case MsgPackType.POSITIVE_FIXNUM_0x26: case MsgPackType.POSITIVE_FIXNUM_0x27: case MsgPackType.POSITIVE_FIXNUM_0x28: case MsgPackType.POSITIVE_FIXNUM_0x29: case MsgPackType.POSITIVE_FIXNUM_0x2A: case MsgPackType.POSITIVE_FIXNUM_0x2B: case MsgPackType.POSITIVE_FIXNUM_0x2C: case MsgPackType.POSITIVE_FIXNUM_0x2D: case MsgPackType.POSITIVE_FIXNUM_0x2E: case MsgPackType.POSITIVE_FIXNUM_0x2F: case MsgPackType.POSITIVE_FIXNUM_0x30: case MsgPackType.POSITIVE_FIXNUM_0x31: case MsgPackType.POSITIVE_FIXNUM_0x32: case MsgPackType.POSITIVE_FIXNUM_0x33: case MsgPackType.POSITIVE_FIXNUM_0x34: case MsgPackType.POSITIVE_FIXNUM_0x35: case MsgPackType.POSITIVE_FIXNUM_0x36: case MsgPackType.POSITIVE_FIXNUM_0x37: case MsgPackType.POSITIVE_FIXNUM_0x38: case MsgPackType.POSITIVE_FIXNUM_0x39: case MsgPackType.POSITIVE_FIXNUM_0x3A: case MsgPackType.POSITIVE_FIXNUM_0x3B: case MsgPackType.POSITIVE_FIXNUM_0x3C: case MsgPackType.POSITIVE_FIXNUM_0x3D: case MsgPackType.POSITIVE_FIXNUM_0x3E: case MsgPackType.POSITIVE_FIXNUM_0x3F: case MsgPackType.POSITIVE_FIXNUM_0x40: case MsgPackType.POSITIVE_FIXNUM_0x41: case MsgPackType.POSITIVE_FIXNUM_0x42: case MsgPackType.POSITIVE_FIXNUM_0x43: case MsgPackType.POSITIVE_FIXNUM_0x44: case MsgPackType.POSITIVE_FIXNUM_0x45: case MsgPackType.POSITIVE_FIXNUM_0x46: case MsgPackType.POSITIVE_FIXNUM_0x47: case MsgPackType.POSITIVE_FIXNUM_0x48: case MsgPackType.POSITIVE_FIXNUM_0x49: case MsgPackType.POSITIVE_FIXNUM_0x4A: case MsgPackType.POSITIVE_FIXNUM_0x4B: case MsgPackType.POSITIVE_FIXNUM_0x4C: case MsgPackType.POSITIVE_FIXNUM_0x4D: case MsgPackType.POSITIVE_FIXNUM_0x4E: case MsgPackType.POSITIVE_FIXNUM_0x4F: case MsgPackType.POSITIVE_FIXNUM_0x50: case MsgPackType.POSITIVE_FIXNUM_0x51: case MsgPackType.POSITIVE_FIXNUM_0x52: case MsgPackType.POSITIVE_FIXNUM_0x53: case MsgPackType.POSITIVE_FIXNUM_0x54: case MsgPackType.POSITIVE_FIXNUM_0x55: case MsgPackType.POSITIVE_FIXNUM_0x56: case MsgPackType.POSITIVE_FIXNUM_0x57: case MsgPackType.POSITIVE_FIXNUM_0x58: case MsgPackType.POSITIVE_FIXNUM_0x59: case MsgPackType.POSITIVE_FIXNUM_0x5A: case MsgPackType.POSITIVE_FIXNUM_0x5B: case MsgPackType.POSITIVE_FIXNUM_0x5C: case MsgPackType.POSITIVE_FIXNUM_0x5D: case MsgPackType.POSITIVE_FIXNUM_0x5E: case MsgPackType.POSITIVE_FIXNUM_0x5F: case MsgPackType.POSITIVE_FIXNUM_0x60: case MsgPackType.POSITIVE_FIXNUM_0x61: case MsgPackType.POSITIVE_FIXNUM_0x62: case MsgPackType.POSITIVE_FIXNUM_0x63: case MsgPackType.POSITIVE_FIXNUM_0x64: case MsgPackType.POSITIVE_FIXNUM_0x65: case MsgPackType.POSITIVE_FIXNUM_0x66: case MsgPackType.POSITIVE_FIXNUM_0x67: case MsgPackType.POSITIVE_FIXNUM_0x68: case MsgPackType.POSITIVE_FIXNUM_0x69: case MsgPackType.POSITIVE_FIXNUM_0x6A: case MsgPackType.POSITIVE_FIXNUM_0x6B: case MsgPackType.POSITIVE_FIXNUM_0x6C: case MsgPackType.POSITIVE_FIXNUM_0x6D: case MsgPackType.POSITIVE_FIXNUM_0x6E: case MsgPackType.POSITIVE_FIXNUM_0x6F: case MsgPackType.POSITIVE_FIXNUM_0x70: case MsgPackType.POSITIVE_FIXNUM_0x71: case MsgPackType.POSITIVE_FIXNUM_0x72: case MsgPackType.POSITIVE_FIXNUM_0x73: case MsgPackType.POSITIVE_FIXNUM_0x74: case MsgPackType.POSITIVE_FIXNUM_0x75: case MsgPackType.POSITIVE_FIXNUM_0x76: case MsgPackType.POSITIVE_FIXNUM_0x77: case MsgPackType.POSITIVE_FIXNUM_0x78: case MsgPackType.POSITIVE_FIXNUM_0x79: case MsgPackType.POSITIVE_FIXNUM_0x7A: case MsgPackType.POSITIVE_FIXNUM_0x7B: case MsgPackType.POSITIVE_FIXNUM_0x7C: case MsgPackType.POSITIVE_FIXNUM_0x7D: case MsgPackType.POSITIVE_FIXNUM_0x7E: case MsgPackType.POSITIVE_FIXNUM_0x7F: case MsgPackType.NEGATIVE_FIXNUM: case MsgPackType.NEGATIVE_FIXNUM_0x01: case MsgPackType.NEGATIVE_FIXNUM_0x02: case MsgPackType.NEGATIVE_FIXNUM_0x03: case MsgPackType.NEGATIVE_FIXNUM_0x04: case MsgPackType.NEGATIVE_FIXNUM_0x05: case MsgPackType.NEGATIVE_FIXNUM_0x06: case MsgPackType.NEGATIVE_FIXNUM_0x07: case MsgPackType.NEGATIVE_FIXNUM_0x08: case MsgPackType.NEGATIVE_FIXNUM_0x09: case MsgPackType.NEGATIVE_FIXNUM_0x0A: case MsgPackType.NEGATIVE_FIXNUM_0x0B: case MsgPackType.NEGATIVE_FIXNUM_0x0C: case MsgPackType.NEGATIVE_FIXNUM_0x0D: case MsgPackType.NEGATIVE_FIXNUM_0x0E: case MsgPackType.NEGATIVE_FIXNUM_0x0F: case MsgPackType.NEGATIVE_FIXNUM_0x10: case MsgPackType.NEGATIVE_FIXNUM_0x11: case MsgPackType.NEGATIVE_FIXNUM_0x12: case MsgPackType.NEGATIVE_FIXNUM_0x13: case MsgPackType.NEGATIVE_FIXNUM_0x14: case MsgPackType.NEGATIVE_FIXNUM_0x15: case MsgPackType.NEGATIVE_FIXNUM_0x16: case MsgPackType.NEGATIVE_FIXNUM_0x17: case MsgPackType.NEGATIVE_FIXNUM_0x18: case MsgPackType.NEGATIVE_FIXNUM_0x19: case MsgPackType.NEGATIVE_FIXNUM_0x1A: case MsgPackType.NEGATIVE_FIXNUM_0x1B: case MsgPackType.NEGATIVE_FIXNUM_0x1C: case MsgPackType.NEGATIVE_FIXNUM_0x1D: case MsgPackType.NEGATIVE_FIXNUM_0x1E: case MsgPackType.NEGATIVE_FIXNUM_0x1F: return(bytes.Advance(1).Take(0)); case MsgPackType.UINT8: case MsgPackType.INT8: return(bytes.Advance(1).Take(1)); case MsgPackType.UINT16: case MsgPackType.INT16: return(bytes.Advance(1).Take(2)); case MsgPackType.UINT32: case MsgPackType.INT32: case MsgPackType.FLOAT: return(bytes.Advance(1).Take(4)); case MsgPackType.UINT64: case MsgPackType.INT64: case MsgPackType.DOUBLE: return(bytes.Advance(1).Take(8)); case MsgPackType.FIX_EXT_1: return(bytes.Advance(2).Take(1)); case MsgPackType.FIX_EXT_2: return(bytes.Advance(2).Take(2)); case MsgPackType.FIX_EXT_4: return(bytes.Advance(2).Take(4)); case MsgPackType.FIX_EXT_8: return(bytes.Advance(2).Take(8)); case MsgPackType.FIX_EXT_16: return(bytes.Advance(2).Take(16)); case MsgPackType.EXT8: { var count = bytes.Get(1); return(bytes.Advance(1 + 1 + 1).Take(count)); } case MsgPackType.EXT16: { var count = EndianConverter.NetworkByteWordToUnsignedNativeByteOrder(bytes.Advance(1)); return(bytes.Advance(1 + 2 + 1).Take(count)); } case MsgPackType.EXT32: { var count = EndianConverter.NetworkByteDWordToUnsignedNativeByteOrder(bytes.Advance(1)); return(bytes.Advance(1 + 4 + 1).Take((int)count)); } default: throw new ArgumentException("unknown type: " + formatType); } }
public bool Parse(ArraySegment <Byte> bytes) { if (bytes.Count < 2) { return(false); } var b0 = bytes.Get(0); IsFin = (b0 & 0x80) != 0; if (!IsFin) { throw new NotImplementedException(); } OpCode = (WebSocketFrameOpCode)(b0 & 0x0F); var b1 = bytes.Get(1); HasMask = (b1 & 0x80) != 0; if (!HasMask) { throw new ArgumentException(); } var payload0 = (Byte)(b1 & 0x7F); switch (payload0) { case 127: if (bytes.Count < 14) { return(false); } PayloadSize = BitConverter.ToInt64(bytes.Array, bytes.Offset + 2); if (bytes.Count < 14 + PayloadSize) { return(false); } MaskKeyPosition = 10; break; case 126: if (bytes.Count < 8) { return(false); } PayloadSize = BitConverter.ToUInt16(bytes.Array, bytes.Offset + 2); if (bytes.Count < 8 + PayloadSize) { return(false); } MaskKeyPosition = 4; break; default: if (bytes.Count < 6) { return(false); } PayloadSize = payload0; if (bytes.Count < 6 + PayloadSize) { return(false); } MaskKeyPosition = 2; break; } MaskKey = new ArraySegment <byte>(bytes.Array, bytes.Offset + MaskKeyPosition, 4); if (PayloadSize > int.MaxValue) { throw new OverflowException(); } Payload = new ArraySegment <byte>(bytes.Array, bytes.Offset + MaskKeyPosition + 4, (int)PayloadSize); DecodePayload(Payload, MaskKey); return(true); }
public void TestGetTooBig() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment<int>( array, 1, 2 ); target.Get( 3 ); }
public void TestGet_Normal() { var backingStore = new int[] { 0, 1, 1, 1, 0 }; var random = new Random(); int value1 = random.Next( 1, Byte.MaxValue ); int value2 = random.Next( 1, Byte.MaxValue ); int value3 = random.Next( 1, Byte.MaxValue ); backingStore[ 1 ] = value1; backingStore[ 2 ] = value2; backingStore[ 3 ] = value3; var target = new ArraySegment<int>( backingStore, 1, 3 ); Assert.AreEqual( value1, target.Get( 0 ) ); Assert.AreEqual( value2, target.Get( 1 ) ); Assert.AreEqual( value3, target.Get( 2 ) ); }
private DataParseStatus ParseStatusLine(ArraySegment<byte> data, ref int bytesParsed) { //HTTP/1.1 301 Moved Permanently\r\n //HTTP/1.1 200 OK\r\n var parseStatus = DataParseStatus.DataTooBig; var statusLineLength = data.Count; var initialBytesParsed = bytesParsed; var effectiveMax = _maximumResponseHeadersLength <= 0 ? int.MaxValue : (_maximumResponseHeadersLength - _totalResponseHeadersLength + bytesParsed); if (statusLineLength < effectiveMax) { parseStatus = DataParseStatus.NeedMoreData; effectiveMax = statusLineLength; } if (bytesParsed >= effectiveMax) { goto quit; } switch (_statusState) { case BeforeVersionNumbers: { while (_totalResponseHeadersLength - initialBytesParsed + bytesParsed < BeforeVersionNumberBytes.Length) { if (BeforeVersionNumberBytes[_totalResponseHeadersLength - initialBytesParsed + bytesParsed] != data.Get(bytesParsed)) { parseStatus = DataParseStatus.Invalid; goto quit; } if (++bytesParsed == effectiveMax) { goto quit; } } if (data.Get(bytesParsed) == '.') { parseStatus = DataParseStatus.Invalid; goto quit; } _statusState = MajorVersionNumber; goto case MajorVersionNumber; } case MajorVersionNumber: { while (data.Get(bytesParsed) != '.') { if (data.Get(bytesParsed) < '0' || data.Get(bytesParsed) > '9') { parseStatus = DataParseStatus.Invalid; goto quit; } _statusLineValues.MajorVersion = _statusLineValues.MajorVersion * 10 + data.Get(bytesParsed) - '0';//1,10 if (++bytesParsed == effectiveMax) { goto quit; } } // Need visibility past the dot. if (bytesParsed + 1 == effectiveMax) { goto quit; } bytesParsed++; if (data.Get(bytesParsed) == ' ') { parseStatus = DataParseStatus.Invalid; goto quit; } _statusState = MinorVersionNumber; goto case MinorVersionNumber; } case MinorVersionNumber: { while (data.Get(bytesParsed) != ' ') { if (data.Get(bytesParsed) < '0' || data.Get(bytesParsed) > '9') { parseStatus = DataParseStatus.Invalid; goto quit; } _statusLineValues.MinorVersion = _statusLineValues.MinorVersion * 10 + data.Get(bytesParsed) - '0'; if (++bytesParsed == effectiveMax) { goto quit; } } _statusState = StatusCodeNumber; _statusLineValues.StatusCode = 1; // Move past the space. if (++bytesParsed == effectiveMax) { goto quit; } goto case StatusCodeNumber; } case StatusCodeNumber: { while (data.Get(bytesParsed) >= '0' && data.Get(bytesParsed) <= '9') { if (_statusLineValues.StatusCode >= 1000) { parseStatus = DataParseStatus.Invalid; goto quit; } _statusLineValues.StatusCode = _statusLineValues.StatusCode * 10 + data.Get(bytesParsed) - '0'; if (++bytesParsed == effectiveMax) { goto quit; } } if (data.Get(bytesParsed) != ' ' || _statusLineValues.StatusCode < 1000) { if (data.Get(bytesParsed) == '\r' && _statusLineValues.StatusCode >= 1000) { _statusLineValues.StatusCode -= 1000; _statusState = AfterCarriageReturn; if (++bytesParsed == effectiveMax) { goto quit; } goto case AfterCarriageReturn; } parseStatus = DataParseStatus.Invalid; goto quit; } _statusLineValues.StatusCode -= 1000; _statusState = AfterStatusCode; // Move past the space. if (++bytesParsed == effectiveMax) { goto quit; } goto case AfterStatusCode; } case AfterStatusCode: { var beginning = bytesParsed; while (data.Get(bytesParsed) != '\r') { if (data.Get(bytesParsed) < ' ' || data.Get(bytesParsed) == 127) { parseStatus = DataParseStatus.Invalid; goto quit; } if (++bytesParsed == effectiveMax) { var s = Encoding.ASCII.GetString(data.Array, beginning, bytesParsed - beginning); if (_statusLineValues.StatusDescription == null) { _statusLineValues.StatusDescription = s; } else { _statusLineValues.StatusDescription += s; } goto quit; } } if (bytesParsed > beginning) { var s = Encoding.ASCII.GetString(data.Array, data.Offset + beginning, bytesParsed - beginning); if (_statusLineValues.StatusDescription == null) { _statusLineValues.StatusDescription = s; } else { _statusLineValues.StatusDescription += s; } } else if (_statusLineValues.StatusDescription == null) { _statusLineValues.StatusDescription = ""; } _statusState = AfterCarriageReturn; // Move past the CR. if (++bytesParsed == effectiveMax) { goto quit; } goto case AfterCarriageReturn; } case AfterCarriageReturn: { if (data.Get(bytesParsed) != '\n') { parseStatus = DataParseStatus.Invalid; goto quit; } parseStatus = DataParseStatus.Done; bytesParsed++; break; } } quit: _totalResponseHeadersLength += bytesParsed - initialBytesParsed; return parseStatus; }
public void TestGetNegative() { var array = new int[] { 0, 1, 2, 3 }; var target = new ArraySegment<int>( array, 1, 2 ); target.Get( -1 ); }