| 1277 | */ |
| 1278 | // Note that this is somewhat-improperly used from Verify.java as well. |
| 1279 | static String format(String template, @Nullable Object... args) { |
| 1280 | template = String.valueOf(template); // null -> "null" |
| 1281 | |
| 1282 | // start substituting the arguments into the '%s' placeholders |
| 1283 | StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); |
| 1284 | int templateStart = 0; |
| 1285 | int i = 0; |
| 1286 | while (i < args.length) { |
| 1287 | int placeholderStart = template.indexOf("%s", templateStart); |
| 1288 | if (placeholderStart == -1) { |
| 1289 | break; |
| 1290 | } |
| 1291 | builder.append(template, templateStart, placeholderStart); |
| 1292 | builder.append(args[i++]); |
| 1293 | templateStart = placeholderStart + 2; |
| 1294 | } |
| 1295 | builder.append(template, templateStart, template.length()); |
| 1296 | |
| 1297 | // if we run out of placeholders, append the extra args in square braces |
| 1298 | if (i < args.length) { |
| 1299 | builder.append(" ["); |
| 1300 | builder.append(args[i++]); |
| 1301 | while (i < args.length) { |
| 1302 | builder.append(", "); |
| 1303 | builder.append(args[i++]); |
| 1304 | } |
| 1305 | builder.append(']'); |
| 1306 | } |
| 1307 | |
| 1308 | return builder.toString(); |
| 1309 | } |
| 1310 | } |