| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| // Licensed to the .NET Foundation under one or more agreements. |
| // The .NET Foundation licenses this file to you under the MIT license. |
| // See the LICENSE file in the project root for more information. |
| #if NET461_OR_GREATER || NETSTANDARD2_0 |
| //https://github.com/bgrainger/IndexRange |
| //https://gist.github.com/bgrainger/fb2c18659c2cdfce494c82a8c4803360 |
| |
| namespace System.Runtime.CompilerServices |
| { |
| internal static class RuntimeHelpers |
| { |
| /// <summary> |
| /// Slices the specified array using the specified range. |
| /// </summary> |
| public static T[] GetSubArray<T>(T[] array, Range range) |
| { |
| if (array == null) |
| { |
| throw new ArgumentNullException(); |
| } |
| |
| (int offset, int length) = range.GetOffsetAndLength(array.Length); |
| |
| if (default(T)! != null || typeof(T[]) == array.GetType()) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757) |
| { |
| // We know the type of the array to be exactly T[]. |
| |
| if (length == 0) |
| { |
| return Array.Empty<T>(); |
| } |
| |
| var dest = new T[length]; |
| Array.Copy(array, offset, dest, 0, length); |
| return dest; |
| } |
| else |
| { |
| // The array is actually a U[] where U:T. |
| T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType()!, length); |
| Array.Copy(array, offset, dest, 0, length); |
| return dest; |
| } |
| } |
| } |
| } |
| #endif |