Browse Source

Fix: StringHelper.TryFromStringLiteral

AnnulusGames 1 year ago
parent
commit
b1012e2ee1
2 changed files with 13 additions and 27 deletions
  1. 8 22
      src/Lua/CodeAnalysis/Syntax/Lexer.cs
  2. 5 5
      src/Lua/Internal/StringHelper.cs

+ 8 - 22
src/Lua/CodeAnalysis/Syntax/Lexer.cs

@@ -7,7 +7,7 @@ public ref struct Lexer
 {
     public required ReadOnlyMemory<char> Source { get; init; }
     public string? ChunkName { get; init; }
-    
+
     SyntaxToken current;
     SourcePosition position = new(1, 0);
     int offset;
@@ -205,7 +205,7 @@ public ref struct Lexer
                     return true;
                 }
 
-                if (!IsNumeric(c2))
+                if (!StringHelper.IsNumber(c2))
                 {
                     current = SyntaxToken.Dot(position);
                     return true;
@@ -224,7 +224,7 @@ public ref struct Lexer
         }
 
         // numeric literal
-        if (c1 is '.' || IsNumeric(c1))
+        if (c1 is '.' || StringHelper.IsNumber(c1))
         {
             if (c1 is '0' && c2 is 'x' or 'X') // hex 0x
             {
@@ -320,7 +320,7 @@ public ref struct Lexer
                 {
                     break;
                 }
-               
+
                 if (c is '\\')
                 {
                     Advance(1);
@@ -444,7 +444,7 @@ public ref struct Lexer
     }
 
     (int Start, int End, bool IsTerminated) ReadUntilLongBracketEnd(ref ReadOnlySpan<char> span)
-    {   
+    {
         var c = span[offset];
         var level = 0;
         while (c is '=')
@@ -510,7 +510,7 @@ public ref struct Lexer
     void ReadDigit(ref ReadOnlySpan<char> span, ref int offset, out int readCount)
     {
         readCount = 0;
-        while (span.Length > offset && IsDigit(span[offset]))
+        while (span.Length > offset && StringHelper.IsDigit(span[offset]))
         {
             Advance(1);
             readCount++;
@@ -521,33 +521,19 @@ public ref struct Lexer
     void ReadNumber(ref ReadOnlySpan<char> span, ref int offset, out int readCount)
     {
         readCount = 0;
-        while (span.Length > offset && IsNumeric(span[offset]))
+        while (span.Length > offset && StringHelper.IsNumber(span[offset]))
         {
             Advance(1);
             readCount++;
         }
     }
 
-    [MethodImpl(MethodImplOptions.AggressiveInlining)]
-    static bool IsDigit(char c)
-    {
-        return IsNumeric(c) ||
-            ('a' <= c && c <= 'f') ||
-            ('A' <= c && c <= 'F');
-    }
-
-    [MethodImpl(MethodImplOptions.AggressiveInlining)]
-    static bool IsNumeric(char c)
-    {
-        return '0' <= c && c <= '9';
-    }
-
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     static bool IsIdentifier(char c)
     {
         return c == '_' ||
             ('A' <= c && c <= 'Z') ||
             ('a' <= c && c <= 'z') ||
-            IsNumeric(c);
+            StringHelper.IsNumber(c);
     }
 }

+ 5 - 5
src/Lua/Internal/StringHelper.cs

@@ -99,7 +99,7 @@ internal static class StringHelper
                         }
                         break;
                     default:
-                        if (IsNumeric(c))
+                        if (IsNumber(c))
                         {
                             var start = i;
                             for (int j = 0; j < 3; j++)
@@ -107,7 +107,7 @@ internal static class StringHelper
                                 i++;
                                 if (i >= literal.Length) break;
                                 c = literal[i];
-                                if (!IsNumeric(c)) break;
+                                if (!IsNumber(c)) break;
                             }
 
                             builder.Append((char)int.Parse(literal[start..i]));
@@ -296,15 +296,15 @@ internal static class StringHelper
     }
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
-    static bool IsNumeric(char c)
+    public static bool IsNumber(char c)
     {
         return '0' <= c && c <= '9';
     }
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
-    static bool IsDigit(char c)
+    public static bool IsDigit(char c)
     {
-        return IsNumeric(c) ||
+        return IsNumber(c) ||
             ('a' <= c && c <= 'f') ||
             ('A' <= c && c <= 'F');
     }