From 699036dc43c66c90a9f8e6dc3699cc0be549614a Mon Sep 17 00:00:00 2001 From: "Alecio Furanze (Ale)" Date: Mon, 8 Jul 2024 22:41:54 +0200 Subject: [PATCH] primitive write and read null and empty string #29 #30 primitive write and read null and empty string #29 #30 --- src/src/primitive/partials/Primitive.cs | 1 + src/src/primitive/partials/PrimitiveAdd.cs | 9 ++++----- src/src/primitive/partials/PrimitiveGet.cs | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/src/primitive/partials/Primitive.cs b/src/src/primitive/partials/Primitive.cs index 041e49b..4840b34 100644 --- a/src/src/primitive/partials/Primitive.cs +++ b/src/src/primitive/partials/Primitive.cs @@ -6,6 +6,7 @@ public partial class Primitive : IPrimitive { private static readonly IPrimitivePrefix Prefix; private readonly List _bytes = new List(); + private readonly byte[] _nullPrefix = { 0, 9 }; // 0 mean begin - 9 mean end. (from begin to end) static Primitive() { diff --git a/src/src/primitive/partials/PrimitiveAdd.cs b/src/src/primitive/partials/PrimitiveAdd.cs index 5d606bc..3032a7f 100644 --- a/src/src/primitive/partials/PrimitiveAdd.cs +++ b/src/src/primitive/partials/PrimitiveAdd.cs @@ -125,12 +125,11 @@ public void String(string value) Vault.Add(Prefix.String); var bytes = Encoding.UTF8.GetBytes(value ?? string.Empty); + var length = (uint)bytes.LongLength; - uint size = (uint)bytes.LongLength; - - Vault.AddRange(BitConverter.GetBytes(size)); + Vault.AddRange(BitConverter.GetBytes(length)); - if (bytes.Length > 0) Vault.AddRange(bytes); + if (length > 0) Vault.AddRange(bytes); } public void Class(T value) @@ -189,7 +188,7 @@ public void Struct(T value) } uint size = 0; - + foreach (var prop in props) if (prop.CanRead && prop.CanWrite) { diff --git a/src/src/primitive/partials/PrimitiveGet.cs b/src/src/primitive/partials/PrimitiveGet.cs index f88f4b9..d65cab1 100644 --- a/src/src/primitive/partials/PrimitiveGet.cs +++ b/src/src/primitive/partials/PrimitiveGet.cs @@ -330,7 +330,9 @@ public string String() Position += sizeof(uint); - if (valueSize <= 0 || valueSize > Vault.Count - Position) throw new InvalidDataException(); + if (valueSize == 0) return default; + + if (valueSize > Vault.Count - Position) throw new InvalidDataException(); var value = Vault.GetRange((int)Position, (int)valueSize).ToArray();