Skip to content

Commit

Permalink
独立实现IndexRange,减少外部包依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Aug 13, 2022
1 parent bbefbce commit 9e573bb
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 11 deletions.
111 changes: 111 additions & 0 deletions NewLife.Core/Common/Index.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#if NETFRAMEWORK || NETSTANDARD2_0
using System.Runtime.CompilerServices;

namespace System;

/// <summary></summary>
public readonly struct Index : IEquatable<Index>
{
/// <summary></summary>
private readonly Int32 _value;

/// <summary></summary>
public static Index Start => new(0);

/// <summary></summary>
public static Index End => new(-1);

/// <summary></summary>
public Int32 Value => _value < 0 ? ~_value : _value;

/// <summary></summary>
public Boolean IsFromEnd => _value < 0;

/// <summary></summary>
/// <param name="value"></param>
/// <param name="fromEnd"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Index(Int32 value, Boolean fromEnd = false)
{
if (value < 0)
{
throw new ArgumentOutOfRangeException("value", "value must be non-negative");
}
_value = fromEnd ? ~value : value;
}

private Index(Int32 value) => _value = value;

/// <summary></summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Index FromStart(Int32 value)
{
if (value < 0)
{
throw new ArgumentOutOfRangeException("value", "value must be non-negative");
}
return new Index(value);
}

/// <summary></summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Index FromEnd(Int32 value)
{
if (value < 0)
{
throw new ArgumentOutOfRangeException("value", "value must be non-negative");
}
return new Index(~value);
}

/// <summary></summary>
/// <param name="length"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Int32 GetOffset(Int32 length)
{
var offset = _value;
if (IsFromEnd)
{
offset += length + 1;
}
return offset;
}

/// <summary></summary>
/// <param name="value"></param>
/// <returns></returns>
public override Boolean Equals(Object value) => value is Index index && _value == index._value;

/// <summary></summary>
/// <param name="other"></param>
/// <returns></returns>
public Boolean Equals(Index other) => _value == other._value;

/// <summary></summary>
/// <returns></returns>
public override Int32 GetHashCode() => _value;

/// <summary></summary>
/// <param name="value"></param>
public static implicit operator Index(Int32 value) => FromStart(value);

/// <summary></summary>
/// <returns></returns>
public override String ToString()
{
if (IsFromEnd)
{
return "^" + (UInt32)Value;
}
return ((UInt32)Value).ToString();
}
}
#endif
2 changes: 2 additions & 0 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ private void LoadWindowsInfo()
str = Execute("reg", @"query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid");
if (!str.IsNullOrEmpty() && str.Contains("REG_SZ")) Guid = str.Substring("REG_SZ", null).Trim();

#if NETSTANDARD || NETCOREAPP
if (OSName.IsNullOrEmpty())
OSName = RuntimeInformation.OSDescription.TrimStart("Microsoft").Trim();
#endif
if (OSVersion.IsNullOrEmpty())
OSVersion = Environment.OSVersion.Version.ToString();
}
Expand Down
84 changes: 84 additions & 0 deletions NewLife.Core/Common/Range.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#if NETFRAMEWORK || NETSTANDARD2_0
using System.Runtime.CompilerServices;

namespace System;

/// <summary></summary>
public readonly struct Range : IEquatable<Range>
{
/// <summary></summary>
public Index Start { get; }

/// <summary></summary>
public Index End { get; }

/// <summary></summary>
public static Range All => new(Index.Start, Index.End);

/// <summary></summary>
/// <param name="start"></param>
/// <param name="end"></param>
public Range(Index start, Index end)
{
Start = start;
End = end;
}

/// <summary></summary>
/// <param name="value"></param>
/// <returns></returns>
public override Boolean Equals(Object value)
{
if (value is Range r)
{
if (r.Start.Equals(Start))
{
return r.End.Equals(End);
}
}
return false;
}

/// <summary></summary>
/// <param name="other"></param>
/// <returns></returns>
public Boolean Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End);

/// <summary></summary>
/// <returns></returns>
public override Int32 GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode();

/// <summary></summary>
/// <returns></returns>
public override String ToString() => Start.ToString() + ".." + End;

/// <summary></summary>
/// <param name="start"></param>
/// <returns></returns>
public static Range StartAt(Index start) => new(start, Index.End);

/// <summary></summary>
/// <param name="end"></param>
/// <returns></returns>
public static Range EndAt(Index end) => new(Index.Start, end);

/// <summary></summary>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//[CLSCompliant(false)]
public (Int32 Offset, Int32 Length) GetOffsetAndLength(Int32 length)
{
var startIndex = Start;
var start = ((!startIndex.IsFromEnd) ? startIndex.Value : (length - startIndex.Value));
var endIndex = End;
var end = ((!endIndex.IsFromEnd) ? endIndex.Value : (length - endIndex.Value));
if ((UInt32)end > (UInt32)length || (UInt32)start > (UInt32)end)
{
throw new ArgumentOutOfRangeException("length");
}
return (start, end - start);
}
}
#endif
14 changes: 14 additions & 0 deletions NewLife.Core/Common/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static Boolean IsConsole
/// <summary>是否Mono环境</summary>
public static Boolean Mono { get; } = Type.GetType("Mono.Runtime") != null;

#if NETSTANDARD || NETCOREAPP
private static Boolean? _IsWeb;
/// <summary>是否Web环境</summary>
public static Boolean IsWeb
Expand Down Expand Up @@ -78,6 +79,19 @@ public static Boolean IsWeb

/// <summary>是否OSX环境</summary>
public static Boolean OSX => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#else
/// <summary>是否Web环境</summary>
public static Boolean IsWeb => !String.IsNullOrEmpty(System.Web.HttpRuntime.AppDomainAppId);

/// <summary>是否Windows环境</summary>
public static Boolean Windows { get; } = Environment.OSVersion.Platform <= PlatformID.WinCE;

/// <summary>是否Linux环境</summary>
public static Boolean Linux { get; } = Environment.OSVersion.Platform == PlatformID.Unix;

/// <summary>是否OSX环境</summary>
public static Boolean OSX { get; } = Environment.OSVersion.Platform == PlatformID.MacOSX;
#endif
#endregion

#region 扩展
Expand Down
2 changes: 2 additions & 0 deletions NewLife.Core/Data/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public IList<ArraySegment<Byte>> ToSegments()
return list;
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
/// <summary>转为Span</summary>
/// <returns></returns>
public Span<Byte> AsSpan()
Expand All @@ -355,6 +356,7 @@ public Memory<Byte> AsMemory()

return new Memory<Byte>(ToArray());
}
#endif

/// <summary>获取封包的数据流形式</summary>
/// <returns></returns>
Expand Down
4 changes: 3 additions & 1 deletion NewLife.Core/Log/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ protected static String GetHead()
var tar = asm.GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>();
if (tar != null) target = !tar.FrameworkDisplayName.IsNullOrEmpty() ? tar.FrameworkDisplayName : tar.FrameworkName;
}
#if NETSTANDARD || NETCOREAPP
target = RuntimeInformation.FrameworkDescription;
#endif

if (String.IsNullOrEmpty(name))
{
Expand Down Expand Up @@ -263,6 +265,6 @@ protected static String GetHead()

return sb.ToString();
}
#endregion
#endregion
}
}
5 changes: 5 additions & 0 deletions NewLife.Core/Log/XTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,16 @@ public static void UseConsole(Boolean useColor = true, Boolean useFileLog = true
// 适当加大控制台窗口
try
{
#if NETSTANDARD || NETCOREAPP
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (Console.WindowWidth <= 80) Console.WindowWidth = Console.WindowWidth * 3 / 2;
if (Console.WindowHeight <= 25) Console.WindowHeight = Console.WindowHeight * 3 / 2;
}
#else
if (Console.WindowWidth <= 80) Console.WindowWidth = Console.WindowWidth * 3 / 2;
if (Console.WindowHeight <= 25) Console.WindowHeight = Console.WindowHeight * 3 / 2;
#endif
}
catch { }

Expand Down
2 changes: 2 additions & 0 deletions NewLife.Core/Net/NetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public static void SetTcpKeepAlive(this Socket socket, Boolean iskeepalive, Int3
BitConverter.GetBytes((UInt32)starttime).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
BitConverter.GetBytes((UInt32)interval).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);

#if NETSTANDARD || NETCOREAPP
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
#endif
}

private static readonly ICache _Cache = MemoryCache.Instance;
Expand Down
17 changes: 10 additions & 7 deletions NewLife.Core/NewLife.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</AssemblyVersion>
<OutputPath>..\..\Bin</OutputPath>
<OutputPath>..\Bin</OutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<!--<Nullable>enable</Nullable>-->
Expand All @@ -28,7 +28,7 @@
<RepositoryUrl>https://github.com/NewLifeX/X</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>新生命团队;X组件;NewLife;$(AssemblyName)</PackageTags>
<PackageReleaseNotes>合并WinCore项目;DbTable支持Xml序列化;DbTable支持DataTable互转;Http协议兼容性修正;HttpClient优化埋点数据标签</PackageReleaseNotes>
<PackageReleaseNotes>恢复支持net461和netstandard2.0;独立实现IndexRange</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -43,7 +43,7 @@
</PackageReference>
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net5.0-windows' or '$(TargetFramework)'=='net6.0-windows'">
<PropertyGroup Condition="'$(TargetFramework)'=='net461' or '$(TargetFramework)'=='net5.0-windows' or '$(TargetFramework)'=='net6.0-windows'">
<DefineConstants>$(DefineConstants);__WIN__</DefineConstants>
</PropertyGroup>

Expand All @@ -56,18 +56,20 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net461' or '$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="IndexRange" Version="1.0.2" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<!--<PackageReference Include="IndexRange" Version="1.0.2" />-->
<!--<PackageReference Include="System.Memory" Version="4.5.5" />-->
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<!--<PackageReference Include="System.Net.Http" Version="4.3.4" />-->
<!--<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />-->
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<Reference Include="System.Net.Http" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -116,6 +118,7 @@
<Compile Remove="Data\GeoAddress.cs" />
<Compile Remove="Data\GeoArea.cs" />
<Compile Remove="Data\GeoPoint.cs" />
<Compile Remove="Data\IMemoryEncoder.cs" />
<Compile Remove="Extension\EndPointExtensions.cs" />
<Compile Remove="Http\DefaultHttpClientFactory.cs" />
<Compile Remove="Http\HttpClient.cs" />
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Security/NewLife.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin</OutputPath>
<OutputPath>..\Bin</OutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<SignAssembly>True</SignAssembly>
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<OutputPath>..\..\Test\</OutputPath>
<OutputPath>..\Bin\Test\</OutputPath>
<LangVersion>latest</LangVersion>
<VersionPrefix>9.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest.Core/XUnitTest.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<OutputPath>..\BinCoreTest</OutputPath>
<OutputPath>..\Bin\UnitTest</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants>
Expand Down

0 comments on commit 9e573bb

Please sign in to comment.