[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)); } } } }
public T this[int index] { get { if ((uint)index >= (uint)_count) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } return(_array[_offset + index]); } set { if ((uint)index >= (uint)_count) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } _array[_offset + index] = value; } }
// Copies length elements from sourceArray, starting at index 0, to // destinationArray, starting at index 0. // public static unsafe void Copy(Array sourceArray, Array destinationArray, int length) { if (sourceArray == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.sourceArray); } if (destinationArray == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.destinationArray); } MethodTable *pMT = RuntimeHelpers.GetMethodTable(sourceArray); if (pMT == RuntimeHelpers.GetMethodTable(destinationArray) && !pMT->IsMultiDimensionalArray && (uint)length <= sourceArray.NativeLength && (uint)length <= destinationArray.NativeLength) { nuint byteCount = (uint)length * (nuint)pMT->ComponentSize; ref byte src = ref Unsafe.As <RawArrayData>(sourceArray).Data; ref byte dst = ref Unsafe.As <RawArrayData>(destinationArray).Data;
// Constructs a DateTimeOffset from a string. The string must specify a // date and optionally a time in a culture-specific or universal format. // Leading and trailing whitespace characters are allowed. // public static DateTimeOffset ParseExact(string input, string format, IFormatProvider?formatProvider, DateTimeStyles styles) { styles = ValidateStyles(styles, nameof(styles)); if (input == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input); } if (format == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format); } TimeSpan offset; DateTime dateResult = DateTimeParse.ParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles, out offset); return(new DateTimeOffset(dateResult.Ticks, offset)); }
public int IndexOfAny(char[] anyOf, int startIndex, int count) { if (anyOf is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.anyOf); } if ((uint)startIndex > (uint)Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if ((uint)count > (uint)(Length - startIndex)) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } int result = new ReadOnlySpan <char>(ref Unsafe.Add(ref _firstChar, startIndex), count).IndexOfAny(anyOf); return(result < 0 ? result : result + startIndex); }
public void SetValue(object value, int index) { if (Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); } var lb = GetLowerBound(0); if (index < lb || index > GetUpperBound(0)) { throw new IndexOutOfRangeException("Index has to be >= lower bound and <= upper bound of the array."); } if (GetType().GetElementType().IsPointer) { throw new NotSupportedException(SR.NotSupported_Type); } SetValueImpl(value, index - lb); }
public ReadOnlySpan(T[]? array, int start, int length) { if (array == null) { if (start != 0 || length != 0) ThrowHelper.ThrowArgumentOutOfRangeException(); this = default; return; // returns default } #if BIT64 // See comment in Span<T>.Slice for how this works. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length) ThrowHelper.ThrowArgumentOutOfRangeException(); #else if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) ThrowHelper.ThrowArgumentOutOfRangeException(); #endif _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start)); _length = length; }
public object GetValue(int index) { if (Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); } var lb = GetLowerBound(0); if (index < lb || index > GetUpperBound(0)) { throw new IndexOutOfRangeException("Index has to be between upper and lower bound of the array."); } if (GetType().GetElementType() !.IsPointer) { throw new NotSupportedException(SR.NotSupported_Type); } return(GetValueImpl(index - lb)); }
public ReadOnlyMemory <T> Slice(int start, int length) { // Used to maintain the high-bit which indicates whether the Memory has been pre-pinned or not. int capturedLength = _length; int actualLength = _length & RemoveFlagsBitMask; #if BIT64 // See comment in Span<T>.Slice for how this works. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)actualLength) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); } #else if ((uint)start > (uint)actualLength || (uint)length > (uint)(actualLength - start)) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); } #endif // Set the high-bit to match the this._length high bit (1 for pre-pinned, 0 for unpinned). return(new ReadOnlyMemory <T>(_object, _index + start, length | (capturedLength & ~RemoveFlagsBitMask))); }
public Span(T[] array, int start) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (default(T) == null && array.GetType() != typeof(T[])) { ThrowHelper.ThrowArrayTypeMismatchException_ArrayTypeMustBeExactMatch(typeof(T)); } int arrayLength = array.Length; if ((uint)start > (uint)arrayLength) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); } _length = arrayLength - start; _pinnable = Unsafe.As <Pinnable <T> >(array); _byteOffset = SpanHelpers.PerTypeValues <T> .ArrayAdjustment.Add <T>(start); }
public Utf8String Substring(int startIndex) { if ((uint)startIndex > (uint)this.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex); } // Optimizations: since instances are immutable, we can return 'this' or the known // Empty instance if the caller passed us a startIndex at the string boundary. if (startIndex == 0) { return(this); } if (startIndex == Length) { return(Empty); } return(InternalSubstring(startIndex, Length - startIndex)); }
// Skips zero-initialization of the array if possible. If T contains object references, // the array is always zero-initialized. internal static T[] AllocateUninitializedArray <T>(int length) { if (length < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.lengths, 0, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } #if DEBUG // in DEBUG arrays of any length can be created uninitialized #else // otherwise small arrays are allocated using `new[]` as that is generally faster. // // The threshold was derived from various simulations. // As it turned out the threshold depends on overal pattern of all allocations and is typically in 200-300 byte range. // The gradient around the number is shallow (there is no perf cliff) and the exact value of the threshold does not matter a lot. // So it is 256 bytes including array header. if (Unsafe.SizeOf <T>() * length < 256 - 3 * IntPtr.Size) { return(new T[length]); } #endif return((T[])AllocateNewArray(typeof(T[]).TypeHandle.Value, length, zeroingOptional: true)); }
public unsafe int LastIndexOf(char value, int startIndex, int count) { if (Length == 0) { return(-1); } if ((uint)startIndex >= (uint)Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if ((uint)count > (uint)startIndex + 1) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } int startSearchAt = startIndex + 1 - count; int result = SpanHelpers.LastIndexOf(ref Unsafe.Add(ref _firstChar, startSearchAt), value, count); return(result < 0 ? result : result + startSearchAt); }
public Span(T[] array, int start, int length) { if (array == null) { if (start != 0 || length != 0) { ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default; return; // returns default } if (default(T) == null && array.GetType() != typeof(T[])) { ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) { ThrowHelper.ThrowArgumentOutOfRangeException(); } _pointer = new ByReference <T>(ref Unsafe.Add(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()), start)); _length = length; }
/// <summary> /// Copies the characters from the source span into the destination, converting each character to uppercase. /// </summary> public static int ToUpper(this ReadOnlySpan <char> source, Span <char> destination, CultureInfo culture) { if (culture == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.culture); } // Assuming that changing case does not affect length if (destination.Length < source.Length) { return(-1); } if (GlobalizationMode.Invariant) { culture.TextInfo.ToUpperAsciiInvariant(source, destination); } else { culture.TextInfo.ChangeCase(source, destination, toUpper: true); } return(source.Length); }
[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.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.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))); } } } }
internal Memory(T[]?array, int start) { if (array == null) { if (start != 0) { ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default; return; // returns default } if (default(T) ! == null && array.GetType() != typeof(T[])) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757) { ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = array; _index = start; _length = array.Length - start; }
T IList <T> .this[int index] { get { ThrowInvalidOperationIfDefault(); if (index < 0 || index >= _count) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } return(_array[_offset + index]); } set { ThrowInvalidOperationIfDefault(); if (index < 0 || index >= _count) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } _array[_offset + index] = value; } }
// A span-based equivalent of String.GetHashCode(StringComparison). Uses the specified comparison type. public static int GetHashCode(ReadOnlySpan <char> value, StringComparison comparisonType) { switch (comparisonType) { case StringComparison.CurrentCulture: case StringComparison.CurrentCultureIgnoreCase: return(CultureInfo.CurrentCulture.CompareInfo.GetHashCode(value, GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.InvariantCulture: case StringComparison.InvariantCultureIgnoreCase: return(CultureInfo.InvariantCulture.CompareInfo.GetHashCode(value, GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.Ordinal: return(GetHashCode(value)); case StringComparison.OrdinalIgnoreCase: return(GetHashCodeOrdinalIgnoreCase(value)); default: ThrowHelper.ThrowArgumentException(ExceptionResource.NotSupported_StringComparison, ExceptionArgument.comparisonType); Debug.Fail("Should not reach this point."); return(default); } }
internal Memory(T[] array, int start) { if (array == null) { if (start != 0) { ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default; return; // returns default } if (default(T) == null && array.GetType() != typeof(T[])) { ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = array; _index = start; _length = array.Length - start; }
public override bool Equals(object obj) { ThrowHelper.ThrowInvalidOperationException_ForBoxingSpans(); return(false); }
void IList <T> .Insert(int index, T item) { ThrowHelper.ThrowNotSupportedException(); }
void IList <T> .RemoveAt(int index) { ThrowHelper.ThrowNotSupportedException(); }
void ICollection <T> .Clear() { ThrowHelper.ThrowNotSupportedException(); }
void ICollection <T> .Add(T item) { ThrowHelper.ThrowNotSupportedException(); }
bool ICollection <T> .Remove(T item) { ThrowHelper.ThrowNotSupportedException(); return(default);
internal void InternalArray__ICollection_Clear() { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); }
static ushort INumber <ushort> .Create <TOther>(TOther value) { if (typeof(TOther) == typeof(byte)) { return((byte)(object)value); } else if (typeof(TOther) == typeof(char)) { return((char)(object)value); } else if (typeof(TOther) == typeof(decimal)) { return(checked ((ushort)(decimal)(object)value)); } else if (typeof(TOther) == typeof(double)) { return(checked ((ushort)(double)(object)value)); } else if (typeof(TOther) == typeof(short)) { return(checked ((ushort)(short)(object)value)); } else if (typeof(TOther) == typeof(int)) { return(checked ((ushort)(int)(object)value)); } else if (typeof(TOther) == typeof(long)) { return(checked ((ushort)(long)(object)value)); } else if (typeof(TOther) == typeof(nint)) { return(checked ((ushort)(nint)(object)value)); } else if (typeof(TOther) == typeof(sbyte)) { return(checked ((ushort)(sbyte)(object)value)); } else if (typeof(TOther) == typeof(float)) { return(checked ((ushort)(float)(object)value)); } else if (typeof(TOther) == typeof(ushort)) { return((ushort)(object)value); } else if (typeof(TOther) == typeof(uint)) { return(checked ((ushort)(uint)(object)value)); } else if (typeof(TOther) == typeof(ulong)) { return(checked ((ushort)(ulong)(object)value)); } else if (typeof(TOther) == typeof(nuint)) { return(checked ((ushort)(nuint)(object)value)); } else { ThrowHelper.ThrowNotSupportedException(); return(default);
internal void InternalArray__ICollection_Add <T> (T item) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); }
internal bool InternalArray__ICollection_Remove <T> (T item) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); return(default);