From 919a4205f4205f49d9cd9bf3a280da269613faad Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 17 Oct 2017 21:46:16 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 40 ++++++++++++++++ Array.cs | 86 +++++++++++++++++++++++++++++++++ DataReader.cs | 15 ++++++ Endianess.cs | 8 ++++ HexString.cs | 56 ++++++++++++++++++++++ Properties/AssemblyInfo.cs | 26 ++++++++++ StringExtensions.cs | 30 ++++++++++++ Tripple.cs | 39 +++++++++++++++ Tuple.cs | 28 +++++++++++ UInt64Extension.cs | 98 ++++++++++++++++++++++++++++++++++++++ sharp.extensions.csproj | 44 +++++++++++++++++ 11 files changed, 470 insertions(+) create mode 100644 .gitignore create mode 100644 Array.cs create mode 100644 DataReader.cs create mode 100644 Endianess.cs create mode 100644 HexString.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 StringExtensions.cs create mode 100644 Tripple.cs create mode 100644 Tuple.cs create mode 100644 UInt64Extension.cs create mode 100644 sharp.extensions.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover diff --git a/Array.cs b/Array.cs new file mode 100644 index 0000000..6a1b047 --- /dev/null +++ b/Array.cs @@ -0,0 +1,86 @@ +using System; +namespace sharp.extensions +{ + public static class ArrayExtension + { + public static bool ArrayEquals(this T[] a,T[] b){ + if (a.Length != b.Length){ + return false; + } + + for (int n = 0; n < a.Length;n++){ + if (!a[n].Equals(b[n])){ + return false; + } + } + + return true; + } + + public static T[] Extend(this T[] source, int len) + { + if (source.Length < len) + { + T[] c = new T[len]; + Array.Copy(source, 0, c, 0, source.Length); + Fill(c, default(T),source.Length); + return c; + } + return source; + } + public static T[] Resize(this T[] source, int len) + { + if (source.Length < len) + { + T[] c = new T[len]; + Array.Copy(source, 0, c, 0, source.Length); + return c; + } else if (source.Length > len){ + return Segment(source, 0, len); + } + return Segment(source, 0); + } + + public static T[] Segment(this T[] source, int start) { + return Segment(source, start, source.Length - start); + } + public static T[] Segment(this T[] source,int start,int len){ + T[] temp = new T[len]; + Array.Copy(source,start,temp,0,len); + return temp; + } + + public static T[] Reverse(this T[] source){ + T[] t = new T[source.Length]; + Array.Copy(source,t,source.Length); + Array.Reverse(t); + return t; + } + + public static void Fill(this T[] a, T value) + { + Fill(a, value, 0, a.Length); + } + + public static void Fill(this T[] a, T value,int start) + { + Fill(a, value, start, a.Length - start); + } + + public static void Fill(this T[] a, T value,int start,int len) + { + for (int n = start; n < (start+len); n++) + { + a[n] = value; + } + } + public static T[] Insert(this T[] a, T[] insert, int start,int len) { + Array.Copy(insert,0,a,start,len); + return a; + } + public static T[] Insert(this T[] a,T[] insert,int start){ + return Insert(a, insert, start, insert.Length); + } + + } +} diff --git a/DataReader.cs b/DataReader.cs new file mode 100644 index 0000000..d370d14 --- /dev/null +++ b/DataReader.cs @@ -0,0 +1,15 @@ +using System; +using System.Security.Cryptography.X509Certificates; +using System.IO; + +namespace sharp.extensions +{ + public static class DataReader + { + public static Int64 readVarInt(this TextReader reader){ + + + return 0; + } + } +} diff --git a/Endianess.cs b/Endianess.cs new file mode 100644 index 0000000..c242d96 --- /dev/null +++ b/Endianess.cs @@ -0,0 +1,8 @@ +using System; +namespace sharp.extensions +{ + public enum Endianess + { + LittleEndian, BigEndian + } +} diff --git a/HexString.cs b/HexString.cs new file mode 100644 index 0000000..7261003 --- /dev/null +++ b/HexString.cs @@ -0,0 +1,56 @@ +using System; +using System.Text; + +namespace sharp.extensions +{ + public class HexString + { + public static byte[] toBytes(string hexstring) + { + byte[] bytes = new byte[hexstring.Length >> 1]; + for (int n = 0; n < hexstring.Length >> 1;n++){ + bytes[n] = Convert.ToByte(hexstring.Substring(n << 1, 2), 16); + } + return bytes; + } + + public static String toString(byte[] bytes) + { + return BitConverter.ToString(bytes).Replace("-", String.Empty); + } + + public static String toString(byte[] bytes,int groupsize) + { + StringBuilder sb = new StringBuilder(); + String hs = BitConverter.ToString(bytes).Replace("-", String.Empty); + int n = hs.Length / (groupsize * 2); + + for (int i = 0; i < n;i++){ + sb.Append(hs.Substring(n * (groupsize * 2), (groupsize * 2))); + sb.Append(" "); + } + + return sb.ToString(); + } + + public static String toString(byte[] bytes, int groupsize,int linewidth) + { + StringBuilder sb = new StringBuilder(); + String hs = BitConverter.ToString(bytes).Replace("-", String.Empty); + int n = hs.Length / (groupsize * 2); + + for (int i = 0; i < n; i++) + { + sb.Append(hs.Substring(i * (groupsize * 2), (groupsize * 2))); + if ((i!=0)&&((i % linewidth)==(linewidth-1))){ + sb.AppendLine(); + } else { + sb.Append(" "); + } + } + + return sb.ToString(); + } + + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..8125167 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("sharp.extensions")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/StringExtensions.cs b/StringExtensions.cs new file mode 100644 index 0000000..4dcdb66 --- /dev/null +++ b/StringExtensions.cs @@ -0,0 +1,30 @@ +using System; +namespace sharp.extensions +{ + public static class StringExtensions + { + public static string[] splitConstantWidth(this string s,int len){ + string[] result = new string[(s.Length+len-1) / len]; + for (int n = 0; n < result.Length;n++){ + result[n] = s.Substring(n * len, (n < result.Length - 1) ? len : s.Length % len); + } + return result; + } + + public static byte[] toBytes(this string hexstring) + { + byte[] bytes = new byte[hexstring.Length >> 1]; + for (int n = 0; n < hexstring.Length >> 1; n++) + { + bytes[n] = Convert.ToByte(hexstring.Substring(n << 1, 2), 16); + } + return bytes; + } + + public static String toHexString(this byte[] bytes) + { + return BitConverter.ToString(bytes).Replace("-", String.Empty); + } + + } +} diff --git a/Tripple.cs b/Tripple.cs new file mode 100644 index 0000000..1e95f33 --- /dev/null +++ b/Tripple.cs @@ -0,0 +1,39 @@ +using System; +namespace sharp.extensions +{ + public struct Tripple<_X>{ + public _X X { get; set; } + public _X Y { get; set; } + public _X Z { get; set; } + + public Tripple(_X x, _X y, _X z) + { + this.X = x; + this.Y = y; + this.Z = z; + } + + public Tuple<_X> XY { get { return new Tuple<_X>(X, Y); } } + public Tuple<_X> YZ { get { return new Tuple<_X>(Y, Z); } } + public Tuple<_X> XZ { get { return new Tuple<_X>(X, Z); } } + } + + public struct Tripple<_X, _Y, _Z> + { + public _X X { get; set; } + public _Y Y { get; set; } + public _Z Z { get; set; } + + public Tripple(_X x,_Y y,_Z z) + { + this.X = x; + this.Y = y; + this.Z = z; + } + + public Tuple<_X,_Y> XY { get { return new Tuple<_X,_Y>(X, Y); } } + public Tuple<_Y,_Z> YZ { get { return new Tuple<_Y,_Z>(Y, Z); } } + public Tuple<_X,_Z> XZ { get { return new Tuple<_X,_Z>(X, Z); } } + + } +} diff --git a/Tuple.cs b/Tuple.cs new file mode 100644 index 0000000..678b584 --- /dev/null +++ b/Tuple.cs @@ -0,0 +1,28 @@ +using System; +namespace sharp.extensions +{ + public struct Tuple<_X> + { + public _X X { get; set; } + public _X Y { get; set; } + + public Tuple(_X x, _X y) + { + this.X = x; + this.Y = y; + } + } + + public struct Tuple<_X,_Y> + { + public _X X { get; set; } + public _Y Y { get; set; } + + public Tuple(_X x,_Y y) + { + this.X = x; + this.Y = y; + } + } + +} diff --git a/UInt64Extension.cs b/UInt64Extension.cs new file mode 100644 index 0000000..2f9086a --- /dev/null +++ b/UInt64Extension.cs @@ -0,0 +1,98 @@ +using System; +using System.Runtime.CompilerServices; + +namespace sharp.extensions +{ + public static class UInt64Extender + { + public static UInt64 RotateLeft(this UInt64 value, int count) + { + return (value << count) | (value >> (64 - count)); + } + public static UInt64 RotateRight(this UInt64 value, int count) + { + return (value >> count) | (value << (64 - count)); + } + + public static byte[] GetBytes(this UInt64 value){ + return BitConverter.GetBytes(value); + } + public static byte[] GetBytes(this UInt64 value,Endianess endianess) + { + byte[] r = BitConverter.GetBytes(value); + if (endianess != value.CurrentEndianess()){ + Array.Reverse(r); + } + return r; + } + + public static byte[] GetBytes(this Int64 value) + { + return BitConverter.GetBytes(value); + } + public static byte[] GetBytes(this Int64 value, Endianess endianess) + { + byte[] r = BitConverter.GetBytes(value); + if (endianess != value.CurrentEndianess()) + { + Array.Reverse(r); + } + return r; + } + + public static byte[] GetBytes(this Int32 value) + { + return BitConverter.GetBytes(value); + } + public static byte[] GetBytes(this Int32 value, Endianess endianess) + { + byte[] r = BitConverter.GetBytes(value); + if (endianess != value.CurrentEndianess()) + { + Array.Reverse(r); + } + return r; + } + + public static byte[] GetBytes(this UInt32 value) + { + return BitConverter.GetBytes(value); + } + public static byte[] GetBytes(this UInt32 value, Endianess endianess) + { + byte[] r = BitConverter.GetBytes(value); + if (endianess != value.CurrentEndianess()) + { + Array.Reverse(r); + } + return r; + } + + + public static Endianess CurrentEndianess(this UInt64 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; } + public static Endianess CurrentEndianess(this UInt32 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; } + public static Endianess CurrentEndianess(this Int64 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; } + public static Endianess CurrentEndianess(this Int32 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; } + + public static byte[] getBytes(this UInt32[] values) + { + byte[] b = new byte[values.Length << 2]; + for (int n = 0; n < values.Length; n++) + { + b.Insert(values[n].GetBytes(), n << 2); + } + return b; + } + public static UInt32[] toUInt32(this byte[] values) + { + UInt32[] r = new UInt32[ (values.Length + 0x03) >> 2 ]; + values = values.Extend(r.Length << 2); + for (int n = 0; n < r.Length; n++) + { + r[n] = BitConverter.ToUInt32(values, (n << 2)); + } + return r; + } + + } +} diff --git a/sharp.extensions.csproj b/sharp.extensions.csproj new file mode 100644 index 0000000..ba2f16a --- /dev/null +++ b/sharp.extensions.csproj @@ -0,0 +1,44 @@ + + + + Debug + AnyCPU + {97CA3CA9-98B3-4492-B072-D7A5995B68E9} + Library + sharp.extensions + sharp.extensions + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + + + + + + + + + + + + + \ No newline at end of file