Skip to content

Commit

Permalink
Replace comparisons with enums that are contextually more correct
Browse files Browse the repository at this point in the history
* Increases readability
  • Loading branch information
thorium-cfx committed May 8, 2024
1 parent 037152c commit ca2a478
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions MsgPack/MsgPackDeserializerConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public object DeserializeAsObject()
{
MsgPackCode type = ReadType();

if (type < MsgPackCode.Nil)
if (type <= MsgPackCode.FixStrMax)
{
if (type <= MsgPackCode.FixIntPositiveMax)
return type;
Expand All @@ -83,9 +83,9 @@ public object DeserializeAsObject()

return ReadString((uint)type % 32u);
}
else if (type > MsgPackCode.Map32)
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
{
return unchecked((sbyte)type); // fix negative number
return unchecked((sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -138,7 +138,7 @@ public object DeserializeAsObject()

internal unsafe void SkipObject(MsgPackCode type)
{
if (type < MsgPackCode.Nil)
if (type <= MsgPackCode.FixStrMax)
{
if (type <= MsgPackCode.FixMapMax)
SkipMap((uint)type % 16u);
Expand All @@ -149,7 +149,7 @@ internal unsafe void SkipObject(MsgPackCode type)

return;
}
else if (type > MsgPackCode.Map32)
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
{
return;
}
Expand Down Expand Up @@ -210,9 +210,9 @@ public unsafe bool DeserializeAsBool()
MsgPackCode type = ReadType();
if (type <= MsgPackCode.FixIntPositiveMax) // positive fixint
return type != 0;
else if (type < MsgPackCode.Nil) // fixstr
else if (type <= MsgPackCode.FixStrMax) // fixstr
return ReadStringAsTrueish((uint)type % 32u);
else if (type > MsgPackCode.Map32) // negative fixint
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return true; // is always != 0

switch (type)
Expand Down Expand Up @@ -251,10 +251,10 @@ public uint DeserializeAsUInt32()
return (uint)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return uint.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((uint)(sbyte)type); // fix negative number
if (type <= MsgPackCode.FixStrMax)
return uint.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((uint)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -295,10 +295,10 @@ public ulong DeserializeAsUInt64()
return (ulong)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return ulong.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((ulong)(sbyte)type); // fix negative number
if (type <= MsgPackCode.FixStrMax)
return ulong.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((ulong)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -339,10 +339,10 @@ public int DeserializeAsInt32()
return (int)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return int.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((int)(sbyte)type); // fix negative number
if (type <= MsgPackCode.FixStrMax)
return int.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((int)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -383,10 +383,10 @@ public long DeserializeAsInt64()
return (long)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return long.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((long)(sbyte)type); // fix negative number
if (type <= MsgPackCode.FixStrMax)
return long.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((long)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -427,10 +427,10 @@ public float DeserializeAsFloat32()
return (float)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return float.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((float)(sbyte)type); // negative fixint
if (type <= MsgPackCode.FixStrMax)
return float.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((float)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -471,10 +471,10 @@ public double DeserializeAsFloat64()
return (double)type;
else if (type >= MsgPackCode.FixStrMin) // fixstr
{
if (type < MsgPackCode.Nil)
return double.Parse(ReadString((uint)type - 0xA0));
else if (type > MsgPackCode.Map32)
return unchecked((double)(sbyte)type); // negative fixint
if (type <= MsgPackCode.FixStrMax)
return double.Parse(ReadString((uint)type - (uint)MsgPackCode.FixStrMin));
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
return unchecked((double)(sbyte)type);
}

switch (type)
Expand Down Expand Up @@ -528,9 +528,9 @@ public string DeserializeAsString()
else
return ReadString((byte)type % 32u);
}
else if (type >= MsgPackCode.FixIntNegativeMin)
else if (type >= MsgPackCode.FixIntNegativeMin) // anything at the end of our byte
{
return unchecked((sbyte)type).ToString(); // fix negative number
return unchecked((sbyte)type).ToString();
}

switch (type)
Expand Down

0 comments on commit ca2a478

Please sign in to comment.