From 6290651668e37efbfe21d7067b3141f95fdc0a49 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Tue, 21 May 2024 22:29:23 +0200 Subject: [PATCH] =?UTF-8?q?clean=20project=20code:=20by=20JetBrains=20Ride?= =?UTF-8?q?r=20(Code=20cleanup=EF=BB=BF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 283 ++++++++++++++++++++----------------- src/Byter.csproj | 66 ++++----- src/extension/Primitive.cs | 16 +-- 3 files changed, 188 insertions(+), 177 deletions(-) diff --git a/README.md b/README.md index 8023009..08e14f5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # Byter -Byter is a bytes serializer. It can serialize and deserialize from primitive type. + +Byter is a bytes serializer. It can serialize and deserialize from primitive type. > ###### Byter is very stable, super easy to learn, extremely fast and inexpensive (2 bytes or ``sizeof(char)`` of overhead per data written) and ``100%`` written in ``C#`` and it's FREE!


## Install + - #### Nuget [SEE HERE](https://www.nuget.org/packages/Byter) ###### .NET CLI ```rb @@ -19,10 +21,13 @@ Byter is a bytes serializer. It can serialize and deserialize from primitive typ ## Usage #### Namespace + ```csharp using Byter ``` + #### Types + ```ts [ `byte`, @@ -46,123 +51,129 @@ using Byter
- ## Writer + > Constructor + - - ```cs - _ = new Writer(); // Create default instance - _ = new Writer(new Writer()); // Create instance and copy from existing Writer - _ = new Writer(ref new Writer()); // Create instance and copy from existing Writer (using ref) - ``` +```cs +_ = new Writer(); // Create default instance +_ = new Writer(new Writer()); // Create instance and copy from existing Writer +_ = new Writer(ref new Writer()); // Create instance and copy from existing Writer (using ref) +```
> Proprietary - - #### ``Length`` - ```cs - using Byter; - var w = new Writer(); +- #### ``Length`` + ```cs + using Byter; - // Get lenght of buffer - int lenght = w.Length; - ``` + var w = new Writer(); + + // Get lenght of buffer + int lenght = w.Length; + ```
> Methods - - #### ``Write(dynamic value)`` - ```cs - using Byter; - - // Create writer instance; - using var w = new Writer(); - - // Write string - w.Write("KEZERO"); - - // Write char - w.Write('K'); - - // Write Float3 (Vector3) - w.Write(new Float3(10F, 10F, 10F)); - - // Get bytes (buffer) - byte[] buffer = w.GetBytes(); - - // Get byte list (buffer) - List bytes = w.GetList(); - ``` - - - #### ``GetBytes()`` - ```cs - using Byter; - - var w = new Writer(); - - // Return buffer on instance - byte[] buffer = w.GetBytes(); - ``` - - - #### ``GetList()`` - ```cs - using Byter; - - var w = new Writer(); + +- #### ``Write(dynamic value)`` + ```cs + using Byter; + + // Create writer instance; + using var w = new Writer(); - // Return buffer on instance as byte list - List bytes = w.GetList(); - ``` + // Write string + w.Write("KEZERO"); - - #### ``Clear()`` - ```cs - using Byter; + // Write char + w.Write('K'); - var w = new Writer(); - w.Write((int)1000); - w.Write((float)100f); + // Write Float3 (Vector3) + w.Write(new Float3(10F, 10F, 10F)); - // Clear internal buffer and reset internal index - w.Clear(); - ``` + // Get bytes (buffer) + byte[] buffer = w.GetBytes(); + + // Get byte list (buffer) + List bytes = w.GetList(); + ``` + +- #### ``GetBytes()`` + ```cs + using Byter; + + var w = new Writer(); + + // Return buffer on instance + byte[] buffer = w.GetBytes(); + ``` + +- #### ``GetList()`` + ```cs + using Byter; + + var w = new Writer(); + + // Return buffer on instance as byte list + List bytes = w.GetList(); + ``` + +- #### ``Clear()`` + ```cs + using Byter; + + var w = new Writer(); + w.Write((int)1000); + w.Write((float)100f); + + // Clear internal buffer and reset internal index + w.Clear(); + ```
## Reader + > Constructor + - - ```cs - _ = new Reader(new Writer()); // Create instance and copy buffer from existing Writer - _ = new Reader(ref new Writer()); // Create instance and copy buffer from existing Writer (ref Writer) - _ = new Reader(new byte[] { 1, 1, 1, 1 }); // Create instance from buffer (bytes (byte[])) - ``` +```cs +_ = new Reader(new Writer()); // Create instance and copy buffer from existing Writer +_ = new Reader(ref new Writer()); // Create instance and copy buffer from existing Writer (ref Writer) +_ = new Reader(new byte[] { 1, 1, 1, 1 }); // Create instance from buffer (bytes (byte[])) +```
> Proprietary - - #### ``Length`` - ```cs - using Byter; - byte[] buffer = ...; - var r = new Reader(buffer); +- #### ``Length`` + ```cs + using Byter; - // Get lenght of buffer - int lenght = r.Length; - ``` + byte[] buffer = ...; + var r = new Reader(buffer); - - #### ``Position`` - ```cs - using Byter; + // Get lenght of buffer + int lenght = r.Length; + ``` - byte[] buffer = ...; - var r = new Reader(buffer); +- #### ``Position`` + ```cs + using Byter; + + byte[] buffer = ...; + var r = new Reader(buffer); + + // return current index of readed buffer + int position = r.Position; + ``` - // return current index of readed buffer - int position = r.Position; - ``` - - #### ``Success`` ```cs using Byter; @@ -177,67 +188,74 @@ using Byter bool success = r.Success; ``` - ###### WARNING - Internally, before data is written a prefix is added in front of it, so when reading it always compares the prefix of the (data type) you want to read with the strings in the read buffer. if the prefixes do not match then o (Reader. Success = False), eg. If you write an (int) and try to read a float (Reader.Success = False) because the prefix of an (int) is different from that of a (float), it is recommended to read all the data and at the end check the success, if it is (Reader.Success = False) then one or more data is corrupt. This means that Writer and Reader add dipping to your write and read data. + Internally, before data is written a prefix is added in front of it, so when reading it always compares the prefix + of the (data type) you want to read with the strings in the read buffer. if the prefixes do not match then o ( + Reader. Success = False), eg. If you write an (int) and try to read a float (Reader.Success = False) because the + prefix of an (int) is different from that of a (float), it is recommended to read all the data and at the end + check the success, if it is (Reader.Success = False) then one or more data is corrupt. This means that Writer and + Reader add dipping to your write and read data.
> Methods - - #### ``Read()`` ``Read(Encoding encoding)``; - ```cs - using Byter; - - byte[] buffer = ...; - - // Create reader instance - using r = new Reader(buffer); - - string name = r.Read(); - char firstLatter = r.Read(); - Float3 position = r.Read(); - - // Check if is success - if (r.Success) - { - Console.WriteLine($"Name: {name}"); - Console.WriteLine($"First Latter: {firstLatter}"); - Console.WriteLine($"Position: {position}"); - } - else - { - Console.WriteLine("Error on get data"); - } - ``` - - #### ``Seek(int position)``; - ```cs - using Byter; +- #### ``Read()`` ``Read(Encoding encoding)``; + ```cs + using Byter; + + byte[] buffer = ...; + + // Create reader instance + using r = new Reader(buffer); + + string name = r.Read(); + char firstLatter = r.Read(); + Float3 position = r.Read(); + + // Check if is success + if (r.Success) + { + Console.WriteLine($"Name: {name}"); + Console.WriteLine($"First Latter: {firstLatter}"); + Console.WriteLine($"Position: {position}"); + } + else + { + Console.WriteLine("Error on get data"); + } + ``` - using var w = new Writer(); - w.Write("KEZERO"); - w.Write((int) 1024); +- #### ``Seek(int position)``; + ```cs + using Byter; - using var r = new Reader(ref w); + using var w = new Writer(); + w.Write("KEZERO"); + w.Write((int) 1024); - string name = r.Read(); // name: KEZERO - int age = r.Read(); // age: 1024 - - // Move index (Reader.Position) for target value "MIN VALUE IS 0"; - r.Seek(10); // move current index for read for 10 (when start read using .Read will start read on 10 index from buffer); + using var r = new Reader(ref w); - // Reset internal position - r.Seek(0); + string name = r.Read(); // name: KEZERO + int age = r.Read(); // age: 1024 + + // Move index (Reader.Position) for target value "MIN VALUE IS 0"; + r.Seek(10); // move current index for read for 10 (when start read using .Read will start read on 10 index from buffer); - string name2 = r.Read(); // name: KEZERO (because the start index of this string on buffer is 0) - int age2 = r.Read(); age: 1024; + // Reset internal position + r.Seek(0); - // NEED READ LAST INT - r.Seek(r.Position - sizeof(int) + sizeof(char) /* int size is 4 + char size is 2. The 2 bytes is overhead of protocol */); - int age3 = r.Read(); age: 1024 (because i return 4bytes before old current value) - ``` + string name2 = r.Read(); // name: KEZERO (because the start index of this string on buffer is 0) + int age2 = r.Read(); age: 1024; + + // NEED READ LAST INT + r.Seek(r.Position - sizeof(int) + sizeof(char) /* int size is 4 + char size is 2. The 2 bytes is overhead of protocol */); + int age3 = r.Read(); age: 1024 (because i return 4bytes before old current value) + ```


#### Sample + ```csharp using Byter; @@ -297,6 +315,7 @@ reader.Dispose(); // Destroy Reader


#### Install using git submodule + ```rb # Install - recommend a stable branch e.g. "1.x" or use a fork repository, --depth clone last sources git submodule add --name byter --depth 1 --branch main "https://github.com/alec1o/byter" vendor/byter diff --git a/src/Byter.csproj b/src/Byter.csproj index b7f18a9..2989633 100644 --- a/src/Byter.csproj +++ b/src/Byter.csproj @@ -1,38 +1,38 @@  - - - - + + + + - - netstandard2.0 - Byter - 2.0.0 - alec1o - alec1o - alec1o - GitHub - Byte, Bytes, Array, Encode, Decode - true - https://github.com/alec1o/Byter - https://github.com/alec1o/Byter - README.md - LICENSE.md - - + Add Float2 struct (Vector2) - + Add Float3 struct (Vector3) - + Add Float4 struct (Vector4 / Quaternion) - + Downgrade .NET standard 2.1 to 2.0 - + New docs - - - Byte parse. Convert byte to object and object to byte + + netstandard2.0 + Byter + 2.0.0 + alec1o + alec1o + alec1o + GitHub + Byte, Bytes, Array, Encode, Decode + true + https://github.com/alec1o/Byter + https://github.com/alec1o/Byter + README.md + LICENSE.md + + + Add Float2 struct (Vector2) + + Add Float3 struct (Vector3) + + Add Float4 struct (Vector4 / Quaternion) + + Downgrade .NET standard 2.1 to 2.0 + + New docs + + + Byte parse. Convert byte to object and object to byte - Docs: https://github.com/alec1o/Byter/blob/main/README.md - Sample: https://github.com/alec1o/Byter/blob/main/README.md - Fork me: https://github.com/alec1o/Byter - - Byter - + Docs: https://github.com/alec1o/Byter/blob/main/README.md + Sample: https://github.com/alec1o/Byter/blob/main/README.md + Fork me: https://github.com/alec1o/Byter + + Byter + diff --git a/src/extension/Primitive.cs b/src/extension/Primitive.cs index e685210..e4d6c3e 100644 --- a/src/extension/Primitive.cs +++ b/src/extension/Primitive.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Numerics; @@ -9,8 +8,9 @@ public static class PrimitiveExtension { internal static byte[] ToPrimitive(this T value) { - return ToPrimitive(value, typeof(T)); + return ToPrimitive(value, typeof(T)); } + internal static byte[] ToPrimitive(this T value, Type type) { var primitive = new Primitive(); @@ -123,10 +123,7 @@ internal static (T Value, bool IsError) FromPrimitive(Primitive primitive) internal static (object Value, bool IsError) FromPrimitive(Type type, Primitive primitive) { - if (type == null || primitive == null) - { - return (default, true); - } + if (type == null || primitive == null) return (default, true); object value = null; @@ -226,13 +223,8 @@ internal static (object Value, bool IsError) FromPrimitive(Type type, Primitive } if (primitive.IsValid) - { return (value, value == null); - } - else - { - return (default, true); - } + return (default, true); } } } \ No newline at end of file