8285932: Implementation of JEP 430 String Templates (Preview)

Reviewed-by: mcimadamore, rriggs, darcy
This commit is contained in:
Jim Laskey 2023-05-10 11:34:01 +00:00
parent da2c930262
commit 4aa65cbeef
74 changed files with 9309 additions and 99 deletions

View file

@ -36,6 +36,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandle;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
@ -60,6 +61,7 @@ import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;
import java.time.temporal.UnsupportedTemporalTypeException;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.math.DoubleConsts;
import jdk.internal.math.FormattedFPDecimal;
import sun.util.locale.provider.LocaleProviderAdapter;
@ -2770,8 +2772,7 @@ public final class Formatter implements Closeable, Flushable {
int lasto = -1;
List<FormatString> fsa = parse(format);
for (int i = 0; i < fsa.size(); i++) {
var fs = fsa.get(i);
for (FormatString fs : fsa) {
int index = fs.index();
try {
switch (index) {
@ -2789,7 +2790,7 @@ public final class Formatter implements Closeable, Flushable {
throw new MissingFormatArgumentException(fs.toString());
fs.print(this, (args == null ? null : args[lasto]), l);
}
default -> { // explicit index
default -> { // explicit index
last = index - 1;
if (args != null && last > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
@ -2804,15 +2805,15 @@ public final class Formatter implements Closeable, Flushable {
}
// %[argument_index$][flags][width][.precision][t]conversion
private static final String formatSpecifier
static final String FORMAT_SPECIFIER
= "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
private static final Pattern fsPattern = Pattern.compile(formatSpecifier);
static final Pattern FORMAT_SPECIFIER_PATTERN = Pattern.compile(FORMAT_SPECIFIER);
/**
* Finds format specifiers in the format string.
*/
private List<FormatString> parse(String s) {
static List<FormatString> parse(String s) {
ArrayList<FormatString> al = new ArrayList<>();
int i = 0;
int max = s.length();
@ -2840,7 +2841,7 @@ public final class Formatter implements Closeable, Flushable {
i++;
} else {
if (m == null) {
m = fsPattern.matcher(s);
m = FORMAT_SPECIFIER_PATTERN.matcher(s);
}
// We have already parsed a '%' at n, so we either have a
// match or the specifier at n is invalid
@ -2855,7 +2856,7 @@ public final class Formatter implements Closeable, Flushable {
return al;
}
private interface FormatString {
interface FormatString {
int index();
void print(Formatter fmt, Object arg, Locale l) throws IOException;
String toString();
@ -2891,14 +2892,15 @@ public final class Formatter implements Closeable, Flushable {
DECIMAL_FLOAT
};
private static class FormatSpecifier implements FormatString {
static class FormatSpecifier implements FormatString {
private static final double SCALEUP = Math.scalb(1.0, 54);
private int index = 0;
private int flags = Flags.NONE;
private int width = -1;
private int precision = -1;
private boolean dt = false;
private char c;
int index = 0;
int flags = Flags.NONE;
int width = -1;
int precision = -1;
boolean dt = false;
char c;
private void index(String s, int start, int end) {
if (start >= 0) {
@ -3548,8 +3550,8 @@ public final class Formatter implements Closeable, Flushable {
if (width != -1) {
newW = adjustWidth(width - exp.length - 1, flags, neg);
}
localizedMagnitude(fmt, sb, mant, 0, flags, newW, l);
localizedMagnitude(fmt, sb, mant, 0, flags, newW, l);
sb.append(Flags.contains(flags, Flags.UPPERCASE) ? 'E' : 'e');
char sign = exp[0];
@ -3719,8 +3721,7 @@ public final class Formatter implements Closeable, Flushable {
// If this is subnormal input so normalize (could be faster to
// do as integer operation).
if (subnormal) {
double scaleUp = Math.scalb(1.0, 54);
d *= scaleUp;
d *= SCALEUP;
// Calculate the exponent. This is not just exponent + 54
// since the former is not the normalized exponent.
exponent = Math.getExponent(d);
@ -4623,7 +4624,7 @@ public final class Formatter implements Closeable, Flushable {
}
}
private static class Flags {
static class Flags {
static final int NONE = 0; // ''
@ -4701,7 +4702,7 @@ public final class Formatter implements Closeable, Flushable {
}
}
private static class Conversion {
static class Conversion {
// Byte, Short, Integer, Long, BigInteger
// (and associated primitives due to autoboxing)
static final char DECIMAL_INTEGER = 'd';
@ -4826,7 +4827,7 @@ public final class Formatter implements Closeable, Flushable {
}
}
private static class DateTime {
static class DateTime {
static final char HOUR_OF_DAY_0 = 'H'; // (00 - 23)
static final char HOUR_0 = 'I'; // (01 - 12)
static final char HOUR_OF_DAY = 'k'; // (0 - 23) -- like H
@ -4877,4 +4878,5 @@ public final class Formatter implements Closeable, Flushable {
};
}
}
}