Skip to content

Commit 39eef35

Browse files
committed
小的第三方库移动到库内。 便于维护。
1 parent e685882 commit 39eef35

File tree

3 files changed

+128
-13
lines changed

3 files changed

+128
-13
lines changed
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
3+
<PropertyGroup>
44
<TargetFrameworks>net5.0;net6.0;netstandard2.1;netstandard2.0;net461</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
6-
7-
</PropertyGroup>
8-
9-
<ItemGroup>
10-
<PackageReference Include="ApacheThrift" Version="0.14.1" />
11-
12-
136

7+
</PropertyGroup>
148

9+
<ItemGroup>
10+
<PackageReference Include="ApacheThrift" Version="0.14.1" />
1511
</ItemGroup>
1612
<ItemGroup Condition="'$(TargetFramework)' == 'net461' or '$(TargetFramework)' == 'netstandard2.0'">
17-
18-
<PackageReference Include="Grax.ArrayExtensions" Version="1.0.2" />
19-
<PackageReference Include="TA.System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray" Version="1.0.1" />
20-
2113
<PackageReference Include="IndexRange" Version="1.0.2" />
2214
</ItemGroup>
23-
15+
2416
</Project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#if NET461_OR_GREATER || NETSTANDARD2_0
2+
//https://github.com/Grax32/ArrayExtensions/blob/master/docs/index.md
3+
4+
using System;
5+
namespace Grax32.Extensions
6+
{
7+
public static class ArrayExtensions
8+
{
9+
public static void Fill<T>(this T[] destinationArray, T value)
10+
{
11+
if (destinationArray == null)
12+
{
13+
throw new ArgumentNullException(nameof(destinationArray));
14+
}
15+
16+
destinationArray[0] = value;
17+
FillInternal(destinationArray, 1);
18+
}
19+
20+
public static void Fill<T>(this T[] destinationArray, T[] values)
21+
{
22+
if (destinationArray == null)
23+
{
24+
throw new ArgumentNullException(nameof(destinationArray));
25+
}
26+
27+
var copyLength = values.Length;
28+
var destinationArrayLength = destinationArray.Length;
29+
30+
if (copyLength == 0)
31+
{
32+
throw new ArgumentException("Parameter must contain at least one value.", nameof(values));
33+
}
34+
35+
if (copyLength > destinationArrayLength)
36+
{
37+
// value to copy is longer than destination,
38+
// so fill destination with first part of value
39+
Array.Copy(values, destinationArray, destinationArrayLength);
40+
return;
41+
}
42+
43+
Array.Copy(values, destinationArray, copyLength);
44+
45+
FillInternal(destinationArray, copyLength);
46+
}
47+
48+
private static void FillInternal<T>(this T[] destinationArray, int copyLength)
49+
{
50+
var destinationArrayLength = destinationArray.Length;
51+
var destinationArrayHalfLength = destinationArrayLength / 2;
52+
53+
// looping copy from beginning of array to current position
54+
// doubling copy length with each pass
55+
for (; copyLength < destinationArrayHalfLength; copyLength *= 2)
56+
{
57+
Array.Copy(
58+
sourceArray: destinationArray,
59+
sourceIndex: 0,
60+
destinationArray: destinationArray,
61+
destinationIndex: copyLength,
62+
length: copyLength);
63+
}
64+
65+
// we're past halfway, meaning only a single copy remains
66+
// exactly fill remainder of array
67+
Array.Copy(
68+
sourceArray: destinationArray,
69+
sourceIndex: 0,
70+
destinationArray: destinationArray,
71+
destinationIndex: copyLength,
72+
length: destinationArrayLength - copyLength);
73+
}
74+
}
75+
}
76+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
#if NET461_OR_GREATER || NETSTANDARD2_0
5+
//https://github.com/bgrainger/IndexRange
6+
//https://gist.github.com/bgrainger/fb2c18659c2cdfce494c82a8c4803360
7+
8+
namespace System.Runtime.CompilerServices
9+
{
10+
internal static class RuntimeHelpers
11+
{
12+
/// <summary>
13+
/// Slices the specified array using the specified range.
14+
/// </summary>
15+
public static T[] GetSubArray<T>(T[] array, Range range)
16+
{
17+
if (array == null)
18+
{
19+
throw new ArgumentNullException();
20+
}
21+
22+
(int offset, int length) = range.GetOffsetAndLength(array.Length);
23+
24+
if (default(T)! != null || typeof(T[]) == array.GetType()) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
25+
{
26+
// We know the type of the array to be exactly T[].
27+
28+
if (length == 0)
29+
{
30+
return Array.Empty<T>();
31+
}
32+
33+
var dest = new T[length];
34+
Array.Copy(array, offset, dest, 0, length);
35+
return dest;
36+
}
37+
else
38+
{
39+
// The array is actually a U[] where U:T.
40+
T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType()!, length);
41+
Array.Copy(array, offset, dest, 0, length);
42+
return dest;
43+
}
44+
}
45+
}
46+
}
47+
#endif

0 commit comments

Comments
 (0)