Browse Source

Fix: HexConverter

AnnulusGames 1 year ago
parent
commit
a463ba0fc5
1 changed files with 30 additions and 6 deletions
  1. 30 6
      src/Lua/Internal/HexConverter.cs

+ 30 - 6
src/Lua/Internal/HexConverter.cs

@@ -9,7 +9,14 @@ public static class HexConverter
     {
     {
         var sign = 1;
         var sign = 1;
         text = text.Trim();
         text = text.Trim();
-        if (text[0] == '-')
+        var first = text[0];
+        if (first == '+')
+        {
+            // Remove the "+0x"
+            sign = 1;
+            text = text[3..];
+        }
+        else if (first == '-')
         {
         {
             // Remove the "-0x"
             // Remove the "-0x"
             sign = -1;
             sign = -1;
@@ -34,11 +41,28 @@ public static class HexConverter
             return sign * (double)BigInteger.Parse(buffer.AsSpan()[..(text.Length + 1)], NumberStyles.AllowHexSpecifier);
             return sign * (double)BigInteger.Parse(buffer.AsSpan()[..(text.Length + 1)], NumberStyles.AllowHexSpecifier);
         }
         }
 
 
-        var intPart = dotIndex == -1 ? [] : text[..dotIndex];
-        var decimalPart = expIndex == -1
-            ? text.Slice(dotIndex + 1)
-            : text.Slice(dotIndex + 1, expIndex - dotIndex - 1);
-        var expPart = expIndex == -1 ? [] : text[(expIndex + 1)..];
+        ReadOnlySpan<char> intPart;
+        ReadOnlySpan<char> decimalPart;
+        ReadOnlySpan<char> expPart;
+
+        if (dotIndex == -1)
+        {
+            intPart = text[..expIndex];
+            decimalPart = [];
+            expPart = text[(expIndex + 1)..];
+        }
+        else if (expIndex == -1)
+        {
+            intPart = text[..dotIndex];
+            decimalPart = text[(dotIndex + 1)..];
+            expPart = [];
+        }
+        else
+        {
+            intPart = text[..dotIndex];
+            decimalPart = text.Slice(dotIndex + 1, expIndex - dotIndex - 1);
+            expPart = text[(expIndex + 1)..];
+        }
 
 
         var value = intPart.Length == 0
         var value = intPart.Length == 0
             ? 0
             ? 0