ifesdjeen commented on code in PR #253:
URL: https://github.com/apache/cassandra-accord/pull/253#discussion_r2376389658


##########
accord-core/src/main/java/accord/api/Tracing.java:
##########
@@ -34,35 +30,61 @@ default void trace(CommandStore store, String fmt, Object 
... args)
 
     static String safeFormat(String fmt, Object ... args)
     {
-        try
-        {
-            return String.format(fmt, args);
-        }
-        catch (Throwable t)
+        if (args.length == 0)
+            return fmt;
+
+        StringBuilder out = new StringBuilder();
+        int prev = 0;
+        for (int argIndex = 0 ; argIndex < args.length ; ++argIndex)
         {
+            Object arg = args[argIndex];
+            int next = fmt.indexOf('%', prev);
+            if (next < 0)
+                break;
+
+            out.append(fmt, prev, next);
+            if (++next == fmt.length())
+                throw new IllegalArgumentException("Invalid substitution 
declaration: % not followed by d, s or %");
+
+            char ch = fmt.charAt(next);
+            prev = next + 1;
+
+            if (ch == '%')
+            {
+                out.append('%');
+                prev = next + 1;

Review Comment:
   To be honest, I am not sure why we want to parse inputs here. I think it was 
reasonable to just use exception id there. 



##########
accord-core/src/main/java/accord/api/Tracing.java:
##########
@@ -34,35 +30,61 @@ default void trace(CommandStore store, String fmt, Object 
... args)
 
     static String safeFormat(String fmt, Object ... args)
     {
-        try
-        {
-            return String.format(fmt, args);
-        }
-        catch (Throwable t)
+        if (args.length == 0)
+            return fmt;
+
+        StringBuilder out = new StringBuilder();
+        int prev = 0;
+        for (int argIndex = 0 ; argIndex < args.length ; ++argIndex)
         {
+            Object arg = args[argIndex];
+            int next = fmt.indexOf('%', prev);
+            if (next < 0)
+                break;
+
+            out.append(fmt, prev, next);
+            if (++next == fmt.length())
+                throw new IllegalArgumentException("Invalid substitution 
declaration: % not followed by d, s or %");
+
+            char ch = fmt.charAt(next);
+            prev = next + 1;
+
+            if (ch == '%')
+            {
+                out.append('%');
+                prev = next + 1;

Review Comment:
   Looks like `%` handling might not work the way you expect it to:
   
   ```
   public class TracingTest
   {
       @Test
       public void testFormatting()
       {
           assertTracingFormatIsEquivalentToStringFormat("%% %s %s", "arg1", 
"arg2");
           assertTracingFormatIsEquivalentToStringFormat("%% %% %s %s", "arg1", 
"arg2");
       }
   
       private static void assertTracingFormatIsEquivalentToStringFormat(String 
format, Object... args)
       {
           String tracing = Tracing.safeFormat(format, args);
           String string = String.format(format, args);
           Assertions.assertEquals(tracing, string);
       }
   }
   ```
   
   ```
   org.opentest4j.AssertionFailedError: 
   Expected :% arg2 %s
   Actual   :% arg1 arg2
   ```
   
   (assuming you intended to implement `%%` substitution)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to