forked from ln-dotnet/ln.type
Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
Harald Wolff | 5ba98b1611 | |
Harald Wolff | b6a965eec8 | |
Harald Wolff | d01126396c | |
Harald Wolff | 00d2fdec51 | |
Harald Wolff | 2c25cc6385 | |
Harald Wolff | 5ed8193971 | |
Harald Wolff | db015ed6db | |
Harald Wolff | 7510e7296a | |
Harald Wolff | 8bd39c3cc3 | |
Harald Wolff | b7a64b4609 | |
Harald Wolff | bd3d57dd04 | |
Harald Wolff | 6e99dc4260 | |
Harald Wolff | 1d02544745 | |
Harald Wolff | 38bc2953d6 | |
Harald Wolff | 1b3255dbb0 | |
Harald Wolff | 8b3aa7b2b6 | |
Harald Wolff | d98571f8c1 | |
Harald Wolff-Thobaben | 45fc0683c5 |
|
@ -39,3 +39,4 @@ Thumbs.db
|
|||
|
||||
# dotCover
|
||||
*.dotCover
|
||||
.build
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"templates": [
|
||||
"dotnet"
|
||||
],
|
||||
"env": {
|
||||
"NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/",
|
||||
"CONFIGURATION": "Release"
|
||||
},
|
||||
"stages": [
|
||||
{
|
||||
"name": "prepare",
|
||||
"commands": [
|
||||
"dotnet prepare */*.csproj"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26124.0
|
||||
MinimumVisualStudioVersion = 15.0.26124.0
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.type", "ln.type\ln.type.csproj", "{98686C62-A822-4045-8403-A6CBD40E7EB3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.type.tests", "ln.type.tests\ln.type.tests.csproj", "{EDFCB172-4EFD-4731-98CB-CB0E082F3204}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|x64.Build.0 = Release|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{98686C62-A822-4045-8403-A6CBD40E7EB3}.Release|x86.Build.0 = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|x64.Build.0 = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{EDFCB172-4EFD-4731-98CB-CB0E082F3204}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,41 @@
|
|||
using NUnit.Framework;
|
||||
|
||||
namespace ln.type.tests
|
||||
{
|
||||
public class URITests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test1()
|
||||
{
|
||||
string testBaseURI = "http://alpha:beta:gamma@myHost:3456/path/to/resource?query=all#fragmentA";
|
||||
|
||||
URI testURI = new URI(testBaseURI);
|
||||
Assert.AreEqual(testBaseURI, testURI.ToString(true));
|
||||
|
||||
Assert.AreEqual(
|
||||
"http://omega@myHost:3456/path/to/resource?query=all#fragmentA",
|
||||
testURI.WithUserInfo("omega").ToString(true)
|
||||
);
|
||||
Assert.AreEqual(
|
||||
"http://alpha:beta:gamma@myHost:3456/my/path?query=all#fragmentA",
|
||||
testURI.WithPath("/my/path").ToString(true)
|
||||
);
|
||||
Assert.AreEqual(
|
||||
"http://alpha:beta:gamma@myHost:3456/path/to/resource?such=a&shame#fragmentA",
|
||||
testURI.WithQuery("such=a&shame").ToString(true)
|
||||
);
|
||||
Assert.AreEqual(
|
||||
"https://alpha:beta:gamma@myHost:3456/path/to/resource?query=all#fragmentA",
|
||||
testURI.WithScheme("https").ToString(true)
|
||||
);
|
||||
|
||||
|
||||
Assert.Pass();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
|
||||
<ProjectReference Include="../ln.type/ln.type.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -62,6 +62,22 @@ namespace ln.type
|
|||
}
|
||||
public static uint GetUInt(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToUInt32(bytes.Slice(ref offset, 4).To(endianess), 0);
|
||||
|
||||
public static byte[] GetSingle(this byte[] bytes, ref int offset, out float value) => GetSingle(bytes, ref offset, out value, Endianess.LITTLE);
|
||||
public static byte[] GetSingle(this byte[] bytes, ref int offset, out float value, Endianess endianess)
|
||||
{
|
||||
value = GetSingle(bytes, ref offset, endianess);
|
||||
return bytes;
|
||||
}
|
||||
public static float GetSingle(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToSingle(bytes.Slice(ref offset, 4).To(endianess), 0);
|
||||
|
||||
public static byte[] GetDouble(this byte[] bytes, ref int offset, out double value) => GetDouble(bytes, ref offset, out value, Endianess.LITTLE);
|
||||
public static byte[] GetDouble(this byte[] bytes, ref int offset, out double value, Endianess endianess)
|
||||
{
|
||||
value = GetDouble(bytes, ref offset, endianess);
|
||||
return bytes;
|
||||
}
|
||||
public static double GetDouble(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToDouble(bytes.Slice(ref offset, 8).To(endianess), 0);
|
||||
|
||||
|
||||
public static byte[] GetBytes(this byte[] bytes,ref int offset,int length,out byte[] value)
|
||||
{
|
|
@ -11,6 +11,9 @@ using System;
|
|||
using System.Reflection;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Net.Http.Headers;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace ln.type
|
||||
{
|
||||
public static class Cast
|
||||
|
@ -18,26 +21,40 @@ namespace ln.type
|
|||
public static T To<T>(object value) => (T)To(value, typeof(T));
|
||||
public static object To(object value, Type targetType)
|
||||
{
|
||||
if (!To(value, targetType, out object targetValue))
|
||||
throw new NotSupportedException();
|
||||
return targetValue;
|
||||
}
|
||||
public static bool To(object value, Type targetType, out object targetValue)
|
||||
{
|
||||
targetValue = null;
|
||||
if (Object.ReferenceEquals(null, value))
|
||||
return null;
|
||||
return true;
|
||||
|
||||
if (targetType.IsInstanceOfType(value))
|
||||
return value;
|
||||
{
|
||||
targetValue = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
object casted;
|
||||
|
||||
if (
|
||||
!Implicit(value, targetType, out casted) &&
|
||||
!Implicit(value, value.GetType(), targetType, out casted) &&
|
||||
!Explicit(value, targetType, out casted) &&
|
||||
!Explicit(value, value.GetType(), targetType, out casted)
|
||||
if (targetType.Equals(typeof(byte[])) && value is string svalue)
|
||||
{
|
||||
targetValue = Convert.FromBase64String(svalue);
|
||||
} else if (targetType.Equals(typeof(string)) && (value is byte[] bytes))
|
||||
{
|
||||
targetValue = Convert.ToBase64String(bytes);
|
||||
} else if (
|
||||
!Implicit(value, targetType, out targetValue) &&
|
||||
!Implicit(value, value.GetType(), targetType, out targetValue) &&
|
||||
!Explicit(value, targetType, out targetValue) &&
|
||||
!Explicit(value, value.GetType(), targetType, out targetValue)
|
||||
)
|
||||
{
|
||||
if (targetType.IsEnum)
|
||||
{
|
||||
if (value is string sValue)
|
||||
{
|
||||
casted = Enum.Parse(targetType, sValue);
|
||||
targetValue = Enum.Parse(targetType, sValue);
|
||||
} else
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
|
@ -49,16 +66,25 @@ namespace ln.type
|
|||
Array castedArray = Array.CreateInstance(targetType.GetElementType(), array.Length);
|
||||
|
||||
for (int n = 0; n < array.Length; n++)
|
||||
castedArray.SetValue(To(array.GetValue(n), targetType.GetElementType()), n);
|
||||
{
|
||||
if (!To(array.GetValue(n), targetType.GetElementType(), out object el))
|
||||
return false;
|
||||
|
||||
casted = castedArray;
|
||||
castedArray.SetValue(el, n);
|
||||
}
|
||||
|
||||
targetValue = castedArray;
|
||||
}
|
||||
else if ((value is string text) && (TypeDescriptor.GetConverter(targetType).CanConvertFrom(typeof(string))))
|
||||
{
|
||||
targetValue = TypeDescriptor.GetConverter(targetType).ConvertFromInvariantString(value as string);
|
||||
}
|
||||
else
|
||||
{
|
||||
casted = Convert.ChangeType(value, targetType);
|
||||
targetValue = Convert.ChangeType(value, targetType);
|
||||
}
|
||||
}
|
||||
return casted;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Implicit(object value, Type type, out object casted) => Implicit(value, type, type, out casted);
|
|
@ -209,6 +209,28 @@ namespace ln.type
|
|||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
public static string ReadCString(this Stream stream)
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
int p = 0, ch;
|
||||
while ((ch = stream.ReadByte()) != -1)
|
||||
{
|
||||
if (ch == 0)
|
||||
break;
|
||||
|
||||
if (p == buffer.Length)
|
||||
{
|
||||
byte[] nb = new byte[buffer.Length << 1];
|
||||
Array.Copy(buffer, nb, buffer.Length);
|
||||
buffer = nb;
|
||||
}
|
||||
|
||||
buffer[p++] = (byte)ch;
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(buffer, 0, p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace ln.type
|
||||
{
|
||||
public class IPv4
|
||||
{
|
||||
public static readonly IPv4 ANY = new IPv4();
|
||||
|
||||
private UInt32 _ip;
|
||||
private IPv4()
|
||||
{
|
||||
}
|
||||
public IPv4(UInt32 ip)
|
||||
{
|
||||
_ip = ip;
|
||||
}
|
||||
|
||||
public IPv4(byte[] ipbytes)
|
||||
{
|
||||
_ip = BitConverter.ToUInt32(ipbytes, 0);
|
||||
}
|
||||
|
||||
public byte[] ToBytes => BitConverter.GetBytes(_ip);
|
||||
|
||||
public override bool Equals(Object obj) => (obj is IPv4 other) && _ip == other._ip;
|
||||
public override int GetHashCode() => (int)_ip;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
byte[] ipbytes = ToBytes;
|
||||
return string.Format("{0}.{1}.{2}.{3}", ipbytes[3], ipbytes[2], ipbytes[1], ipbytes[0]);
|
||||
}
|
||||
|
||||
public static IPv4 operator ++(IPv4 ip) => new IPv4(ip._ip + 1);
|
||||
public static IPv4 operator --(IPv4 ip) => new IPv4(ip._ip - 1);
|
||||
|
||||
public static IPv4 operator +(IPv4 ip, int n) => new IPv4((uint)(ip._ip + n));
|
||||
public static IPv4 operator -(IPv4 ip, int n) => new IPv4((uint)(ip._ip - n));
|
||||
|
||||
public static implicit operator IPAddress(IPv4 ipv4) => new IPAddress(ipv4.ToBytes);
|
||||
public static implicit operator IPv4(IPAddress ipAddress) => new IPv4(ipAddress.GetAddressBytes());
|
||||
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
// *
|
||||
// **/
|
||||
using System;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
namespace ln.type
|
||||
{
|
||||
|
@ -43,6 +44,20 @@ namespace ln.type
|
|||
Parse(String.Format("{0}://{1}{2}", scheme, authority, path));
|
||||
ParseAuthority();
|
||||
}
|
||||
public URI(String scheme,string[] userinfo,string host,string port,string path,string query,string fragment)
|
||||
{
|
||||
Scheme = scheme;
|
||||
UserInfo = userinfo ?? new string[0];
|
||||
Host = host;
|
||||
Port = port;
|
||||
if (userinfo.Length == 0)
|
||||
Authority = String.Format("{0}:{1}", Host, Port);
|
||||
else
|
||||
Authority = String.Format("{2}@{0}:{1}", Host, Port, string.Join(':', UserInfo));
|
||||
Path = path;
|
||||
Query = query;
|
||||
Fragment = fragment;
|
||||
}
|
||||
public URI(String scheme,String authority,string path,string query,string fragment)
|
||||
{
|
||||
Scheme = scheme;
|
||||
|
@ -75,6 +90,13 @@ namespace ln.type
|
|||
}
|
||||
}
|
||||
|
||||
public URI WithUserInfo(params string[] userInfo) => new URI(Scheme,userInfo, Host, Port, Path, Query, Fragment);
|
||||
public URI WithHost(string host) => new URI(Scheme,UserInfo, host, Port, Path, Query, Fragment);
|
||||
public URI WithPort(string port) => new URI(Scheme,UserInfo, Host, port, Path, Query, Fragment);
|
||||
public URI WithScheme(string scheme) => new URI(scheme, UserInfo, Host, Port, Path, Query, Fragment);
|
||||
public URI WithPath(string path) => new URI(Scheme, UserInfo, Host, Port, path, Query, Fragment);
|
||||
public URI WithQuery(string query) => new URI(Scheme, UserInfo, Host, Port, Path, query, Fragment);
|
||||
public URI WithFragment(string fragment) => new URI(Scheme, UserInfo, Host, Port, Path, Query, fragment);
|
||||
|
||||
private void Parse(String uri)
|
||||
{
|
||||
|
@ -181,7 +203,8 @@ namespace ln.type
|
|||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
public override string ToString() => ToString(false);
|
||||
public string ToString(bool withFullUserInfo)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
|
@ -193,7 +216,10 @@ namespace ln.type
|
|||
stringBuilder.Append("//");
|
||||
if (UserInfo.Length > 0)
|
||||
{
|
||||
stringBuilder.Append(UserInfo[0]);
|
||||
if (withFullUserInfo)
|
||||
stringBuilder.Append(String.Join(':',UserInfo));
|
||||
else
|
||||
stringBuilder.Append(UserInfo[0]);
|
||||
stringBuilder.Append('@');
|
||||
}
|
||||
|
|
@ -3,11 +3,12 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.1.0</Version>
|
||||
<Version>0.1.9</Version>
|
||||
<Authors>Harald Wolff-Thobaben</Authors>
|
||||
<Company>l--n.de</Company>
|
||||
<AssemblyVersion>0.0.1.0</AssemblyVersion>
|
||||
<FileVersion>0.0.1.0</FileVersion>
|
||||
<PackageVersion>0.1.10-preview0</PackageVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
Loading…
Reference in New Issue