Advertisement
bobmarley12345

java string interpolation

Oct 2nd, 2021
1,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. /**
  2.  * The same as {@link java.text.MessageFormat}'s format() method, only more efficient
  3.  * <p>
  4.  *     Uses a number between {@code {}'s} to index an object in the given arguments. Null objects are handled by the StringBuilder
  5.  * </p>
  6.  * @param value The value to be formatted
  7.  * @param args The values to splice in
  8.  * @return A string where all of the {...} cases have been replaced with the given arguments
  9.  * @throws BadFormatException If the value between the { and } is empty, or not an integer
  10.  * @throws IndexOutOfBoundsException If the index if out of range (smaller than 0 or bigger than the array's length)
  11.  */
  12. public static String format(String value, Object... args) {
  13.     if (value == null) {
  14.         return null;
  15.     }
  16.     if (value.isEmpty()) {
  17.         return value;
  18.     }
  19.     int firstOpenIndex = value.indexOf('{', 0);
  20.     if (firstOpenIndex == -1) {
  21.         return value;
  22.     }
  23.     char[] chars = value.toCharArray();
  24.     int lastCloseIndex = StringHelper.charsLastIndexOf(chars, '}', firstOpenIndex);
  25.     if (lastCloseIndex == -1) {
  26.         if (firstOpenIndex == 0 || chars[firstOpenIndex - 1] != '\\') { // check if the format is escaped using \\{
  27.             throw new BadFormatException("Initial format check - missing closing '}' after the first opening bracket '{' at index " + firstOpenIndex);
  28.         }
  29.         return value;
  30.     }
  31.     int maxIndex = args.length - 1;
  32.     StringBuilder builder = new StringBuilder(value.length() * 2);
  33.     builder.append(value, 0, firstOpenIndex);
  34.     for (int i = firstOpenIndex, end = chars.length, endIndex = end - 1; i < end; i++) {
  35.         char c = chars[i];
  36.         if (c == '{') {
  37.             // last character
  38.             if (i == endIndex) {
  39.                 builder.append(c);
  40.                 break;
  41.             }
  42.             // escape
  43.             if (i > 0 && chars[i - 1] == '\\') {
  44.                 builder.setCharAt(i - 1, '{');
  45.                 continue;
  46.             }
  47.             int nextClose = StringHelper.charsIndexOf(chars, '}', i + 1);
  48.             if (nextClose == -1) {
  49.                 throw new BadFormatException("Missing a closing '}' after an opening bracket '{' at index " + i);
  50.                 // builder.append(chars, i, chars.length - i - 1);
  51.                 // break;
  52.             }
  53.             int parseIndex = i + 1;
  54.             if (parseIndex == nextClose) {
  55.                 throw new BadFormatException("Value between '{' and '}' was empty! At index " + parseIndex);
  56.                 // builder.append('{');
  57.                 // continue;
  58.             }
  59.             Integer index = StringHelper.parseInteger(chars, parseIndex, nextClose);
  60.             if (index == null) {
  61.                 throw new BadFormatException("Value between '{' and '}' was not an integer! At index " + parseIndex);
  62.             }
  63.             if (index < 0 || index > maxIndex) {
  64.                 throw new IndexOutOfBoundsException("The index (" + index + ") was out of range (array size was " + args.length + ')');
  65.             }
  66.             // let the sb handle null objects :)
  67.             builder.append(args[index]);
  68.             i = nextClose;
  69.         }
  70.         else {
  71.             builder.append(c);
  72.         }
  73.     }
  74.     return builder.toString();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement