Skip to content

Commit

Permalink
primitive write and read null and empty string #29 #30
Browse files Browse the repository at this point in the history
primitive write and read null and empty string #29 #30
  • Loading branch information
alec1o committed Jul 8, 2024
1 parent 0799cb4 commit 699036d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/src/primitive/partials/Primitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public partial class Primitive : IPrimitive
{
private static readonly IPrimitivePrefix Prefix;
private readonly List<byte> _bytes = new List<byte>();
private readonly byte[] _nullPrefix = { 0, 9 }; // 0 mean begin - 9 mean end. (from begin to end)

static Primitive()
{
Expand Down
9 changes: 4 additions & 5 deletions src/src/primitive/partials/PrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T value)
Expand Down Expand Up @@ -189,7 +188,7 @@ public void Struct<T>(T value)
}

uint size = 0;

foreach (var prop in props)
if (prop.CanRead && prop.CanWrite)
{
Expand Down
4 changes: 3 additions & 1 deletion src/src/primitive/partials/PrimitiveGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 699036d

Please sign in to comment.