Compare commits

...

18 Commits

Author SHA1 Message Date
Harald Wolff 5ba98b1611 V1.10.0 2024-04-23 10:36:22 +02:00
Harald Wolff b6a965eec8 Changed version 2024-04-23 10:32:42 +02:00
Harald Wolff d01126396c Added conversion to/from byte[] from/into string 2024-04-23 10:32:13 +02:00
Harald Wolff 00d2fdec51 version 0.1.8
ln.build - build0.l--n.de build job pending Details
2021-07-10 02:20:53 +02:00
Harald Wolff 2c25cc6385 update version string
ln.build - build0.l--n.de build job pending Details
2021-07-10 02:18:42 +02:00
Harald Wolff 5ed8193971 IPv4: Add implicit casting to System.Net.IPAddress
ln.build - build0.l--n.de build job pending Details
2021-07-10 01:38:42 +02:00
Harald Wolff db015ed6db Add Minimalistic IPv4 class
ln.build - build0.l--n.de build job pending Details
2021-07-10 01:31:18 +02:00
Harald Wolff 7510e7296a Release 0.1.7
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-28 08:41:10 +01:00
Harald Wolff 8bd39c3cc3 Added byte[].GetSingle() and byte[].GetDouble()
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-12 17:56:15 +01:00
Harald Wolff b7a64b4609 Updated .gitignore
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-09 14:28:03 +01:00
Harald Wolff bd3d57dd04 Release 0.1.6
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-09 13:21:57 +01:00
Harald Wolff 6e99dc4260 Updated build.ln for ln.build >= 0.4.3
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-09 12:50:51 +01:00
Harald Wolff 1d02544745 Reorganize for unit tests, add URI.WithXXX methods
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-08 15:59:31 +01:00
Harald Wolff 38bc2953d6 Added TypeDescriptor.GetConverter().ConvertFromInvariantString(..) to Cast.To(..)
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-07 09:04:42 +01:00
Harald Wolff 1b3255dbb0 New: bool Cast.To(.., out object targetValue)
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-07 01:07:53 +01:00
Harald Wolff 8b3aa7b2b6 Added build.ln
ln.build - build0.waldrennach.l--n.de build job pending Details
2020-12-05 11:59:45 +01:00
Harald Wolff d98571f8c1 V0.1.2 (test commit hook) 2020-11-19 00:16:51 +01:00
Harald Wolff-Thobaben 45fc0683c5 V0.1.1 2020-11-18 17:18:31 +01:00
25 changed files with 276 additions and 17 deletions

1
.gitignore vendored
View File

@ -39,3 +39,4 @@ Thumbs.db
# dotCover
*.dotCover
.build

17
build.ln 100644
View File

@ -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"
]
}
]
}

48
ln.type.sln 100644
View File

@ -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

View File

@ -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();
}
}
}

View File

@ -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>

View File

@ -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)
{

View File

@ -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);

View File

@ -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);
}

45
ln.type/IPv4.cs 100644
View File

@ -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());
}
}

View File

@ -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('@');
}

View File

@ -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>