Created
November 29, 2018 15:47
-
-
Save reyabreu/33345c6c8a049e1111546722c0988afd to your computer and use it in GitHub Desktop.
Adds format index specifiers to format strings that don't have them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
//helper class that adds format index specifiers to plain specifiers in format strings | |
public class FormatStringIndexer { | |
private static Pattern pattern = Pattern.compile("%[-#+\\s0,(]?\\d*(\\.\\d)?[bBhHsScCdDoxXeEfgGaAtT%n]"); | |
public static String index(final String formatString, int start) { | |
final Matcher matcher = pattern.matcher(formatString); | |
final StringBuffer sb = new StringBuffer(); | |
while (matcher.find()) { | |
String replacement = "%" + start++ + "\\$" + matcher.group().substring(1); | |
matcher.appendReplacement(sb, replacement); | |
} | |
matcher.appendTail(sb); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment