@@ -116,7 +116,10 @@ public static void e(final String tag,
116116
117117 /**
118118 * Simple string format method for easier logging in the form of
119- * {@code ExtractorLogger.d("Hello my name {Name} {}", name, surname)}
119+ * {@code ExtractorLogger.d("Hello my name {Name} {}", name, surname)}<br><br>
120+ * Braces can be escaped by double braces:
121+ * <code>{{ -> {</code> and
122+ * <code>}} -> }</code>
120123 * @param template The template string to format
121124 * @param args Arguments to replace identifiers with in {@code template}
122125 * @return Formatted string with arguments replaced
@@ -125,33 +128,55 @@ private static String format(final String template, final Object... args) {
125128 if (template == null || args == null || args .length == 0 ) {
126129 return template ;
127130 }
128- final var out = new StringBuilder (template .length () + Math .min (32 , 16 * args .length ));
131+ final var result = new StringBuilder (template .length () + Math .min (32 , 16 * args .length ));
129132 int cursorIndex = 0 ;
130133 int argIndex = 0 ;
131134 final int n = template .length ();
132135 while (cursorIndex < n ) {
133- // Find first/next open brace
134- final int openBraceIndex = template .indexOf ('{' , cursorIndex );
135- if (openBraceIndex < 0 ) {
136- // If none found then there's no more arguments to replace
137- out .append (template , cursorIndex , n ); break ;
136+ final char ch = template .charAt (cursorIndex );
137+
138+ // {{ -> {
139+ // If current char is { and next char is {, append { and skip 2 chars
140+ if (ch == '{' && cursorIndex + 1 < n && template .charAt (cursorIndex + 1 ) == '{' ) {
141+ result .append ('{' );
142+ cursorIndex += 2 ;
143+ continue ;
138144 }
139145
140- // Find matching closing brace
141- final int close = template . indexOf ( '}' , openBraceIndex + 1 );
142- if ( close < 0 ) {
143- // If none found then there's no more arguments to replace
144- out . append ( template , cursorIndex , n ); break ;
146+ // }} -> }
147+ if ( ch == '}' && cursorIndex + 1 < n && template . charAt ( cursorIndex + 1 ) == '}' ) {
148+ result . append ( '}' );
149+ cursorIndex += 2 ;
150+ continue ;
145151 }
146- // Append everything from cursor up to before the open brace
147- out .append (template , cursorIndex , openBraceIndex );
148- // Append arguments in the brace
149- out .append (argIndex < args .length
150- ? String .valueOf (args [argIndex ++])
151- : template .substring (openBraceIndex , close + 1 ));
152- cursorIndex = close + 1 ;
152+
153+ // {name}, {} placeholders
154+ if (ch == '{' ) {
155+ // Find first } after the current {
156+ final int closeBraceIndex = template .indexOf ('}' , cursorIndex + 1 );
157+ if (closeBraceIndex < 0 ) {
158+ // If none found then there's no more arguments to replace
159+ // Append rest of the string
160+ result .append (template , cursorIndex , n );
161+ break ;
162+ }
163+
164+ // Found a }
165+ if (argIndex < args .length ) {
166+ // Append the argument
167+ result .append (args [argIndex ++]);
168+ } else {
169+ // No args left to append; append text as is
170+ result .append (template , cursorIndex , closeBraceIndex + 1 );
171+ }
172+ cursorIndex = closeBraceIndex + 1 ;
173+ continue ;
174+ }
175+
176+ result .append (ch );
177+ cursorIndex ++;
153178 }
154- return out .toString ();
179+ return result .toString ();
155180 }
156181
157182 private static final class EmptyLogger implements Logger {
0 commit comments