| 1349 | |
| 1350 | |
| 1351 | static String format(String template, @Nullable Object... args) { |
| 1352 | template = String.valueOf(template); // null -> "null" |
| 1353 | |
| 1354 | // start substituting the arguments into the '%s' placeholders |
| 1355 | StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); |
| 1356 | int templateStart = 0; |
| 1357 | int i = 0; |
| 1358 | while (i < args.length) { |
| 1359 | int placeholderStart = template.indexOf("%s", templateStart); |
| 1360 | if (placeholderStart == -1) { |
| 1361 | break; |
| 1362 | } |
| 1363 | builder.append(template, templateStart, placeholderStart); |
| 1364 | builder.append(args[i++]); |
| 1365 | templateStart = placeholderStart + 2; |
| 1366 | } |
| 1367 | builder.append(template, templateStart, template.length()); |
| 1368 | |
| 1369 | // if we run out of placeholders, append the extra args in square braces |
| 1370 | if (i < args.length) { |
| 1371 | builder.append(" ["); |
| 1372 | builder.append(args[i++]); |
| 1373 | while (i < args.length) { |
| 1374 | builder.append(", "); |
| 1375 | builder.append(args[i++]); |
| 1376 | } |
| 1377 | builder.append(']'); |
| 1378 | } |
| 1379 | return builder.toString(); |
| 1380 | } |
| 1381 | } |