Browse Source

Update: string.format support `+` flag

AnnulusGames 1 year ago
parent
commit
f69161f158
1 changed files with 14 additions and 1 deletions
  1. 14 1
      src/Lua/Standard/Text/FormatFunction.cs

+ 14 - 1
src/Lua/Standard/Text/FormatFunction.cs

@@ -33,6 +33,7 @@ public sealed class FormatFunction : LuaFunction
                 }
                 
                 var leftJustify = false;
+                var plusSign = false;
                 var zeroPadding = false;
                 var alternateForm = false;
                 var blank = false;
@@ -48,6 +49,9 @@ public sealed class FormatFunction : LuaFunction
                         case '-':
                             leftJustify = true;
                             break;
+                        case '+':
+                            plusSign = true;
+                            break;
                         case '0':
                             zeroPadding = true;
                             break;
@@ -64,7 +68,7 @@ public sealed class FormatFunction : LuaFunction
                     i++;
                 }
 
-                PROCESS_WIDTH:
+            PROCESS_WIDTH:
 
                 // Process width
                 if (char.IsDigit(format[i]))
@@ -130,6 +134,10 @@ public sealed class FormatFunction : LuaFunction
                                 break;
                         }
 
+                        if (plusSign && f >= 0)
+                        {
+                            formattedValue = $"+{formattedValue}";
+                        }
                         break;
                     case 's':
                         using (var strBuffer = new PooledArray<LuaValue>(1))
@@ -209,6 +217,11 @@ public sealed class FormatFunction : LuaFunction
                                 formattedValue = Convert.ToString(integer, 8);
                                 break;
                         }
+
+                        if (plusSign && integer >= 0)
+                        {
+                            formattedValue = $"+{formattedValue}";
+                        }
                         break;
                     default:
                         throw new LuaRuntimeException(context.State.GetTraceback(), $"invalid option '%{specifier}' to 'format'");