Ejemplo n.º 1
0
        public static int IndexOf <T>(T[] array, T value, int startIndex, int count)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }

            if ((uint)startIndex > (uint)array.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }

            if ((uint)count > (uint)(array.Length - startIndex))
            {
                ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
            }

            if (RuntimeHelpers.IsBitwiseEquatable <T>())
            {
                if (Unsafe.SizeOf <T>() == sizeof(byte))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref array.GetRawSzArrayData(), startIndex),
                        Unsafe.As <T, byte>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(char))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, char>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, char>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(int))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, int>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, int>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(long))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, long>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, long>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
            }

            return(EqualityComparer <T> .Default.IndexOf(array, value, startIndex, count));
        }
Ejemplo n.º 2
0
        public static ulong ToUInt64(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((uint)startIndex >= value.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }
            if (startIndex > value.Length - 8)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            Contract.EndContractBlock();

            return((ulong)ToInt64(value, startIndex));
        }
Ejemplo n.º 3
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        unsafe public static float ToSingle(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((uint)startIndex >= value.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }
            if (startIndex > value.Length - 4)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            Contract.EndContractBlock();

            int val = ToInt32(value, startIndex);

            return(*(float *)&val);
        }
Ejemplo n.º 4
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static unsafe long ToInt64(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }

            if ((uint)startIndex >= value.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }

            if (startIndex > value.Length - 8)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            Contract.EndContractBlock();

            fixed(byte *pbyte = &value[startIndex])
            {
                if (startIndex % 8 == 0)   // data is aligned
                {
                    return(*((long *)pbyte));
                }
                else
                {
                    if (IsLittleEndian)
                    {
                        int i1 = (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24);
                        int i2 = (*(pbyte + 4)) | (*(pbyte + 5) << 8) | (*(pbyte + 6) << 16) | (*(pbyte + 7) << 24);
                        return((uint)i1 | ((long)i2 << 32));
                    }
                    else
                    {
                        int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
                        int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7));
                        return((uint)i2 | ((long)i1 << 32));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static unsafe int ToInt32(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }

            if ((uint)startIndex >= value.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }

            if (startIndex > value.Length - 4)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            Contract.EndContractBlock();

            fixed(byte *pbyte = &value[startIndex])
            {
                if (startIndex % 4 == 0)   // data is aligned
                {
                    return(*((int *)pbyte));
                }
                else
                {
                    if (IsLittleEndian)
                    {
                        return((*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24));
                    }
                    else
                    {
                        return((*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3)));
                    }
                }
            }
        }