8333396: Use StringBuilder internally for java.text.Format.* formatting

Reviewed-by: naoto, liach, jlu
This commit is contained in:
lingjun.cg 2024-07-22 02:01:08 +00:00
parent fd741a88e8
commit 4da9915875
16 changed files with 764 additions and 73 deletions

View file

@ -968,11 +968,18 @@ public class SimpleDateFormat extends DateFormat {
FieldPosition pos)
{
pos.beginIndex = pos.endIndex = 0;
return format(date, StringBufFactory.of(toAppendTo), pos.getFieldDelegate()).asStringBuffer();
}
@Override
final StringBuf format(Date date, StringBuf toAppendTo,
FieldPosition pos) {
pos.beginIndex = pos.endIndex = 0;
return format(date, toAppendTo, pos.getFieldDelegate());
}
// Called from Format after creating a FieldDelegate
private StringBuffer format(Date date, StringBuffer toAppendTo,
private StringBuf format(Date date, StringBuf toAppendTo,
FieldDelegate delegate) {
// Convert input date to time field list
calendar.setTime(date);
@ -1024,7 +1031,7 @@ public class SimpleDateFormat extends DateFormat {
*/
@Override
public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
StringBuffer sb = new StringBuffer();
StringBuf sb = StringBufFactory.of();
CharacterIteratorFieldDelegate delegate = new
CharacterIteratorFieldDelegate();
@ -1130,7 +1137,7 @@ public class SimpleDateFormat extends DateFormat {
* Private member function that does the real date/time formatting.
*/
private void subFormat(int patternCharIndex, int count,
FieldDelegate delegate, StringBuffer buffer,
FieldDelegate delegate, StringBuf buffer,
boolean useDateFormatSymbols)
{
int maxIntCount = Integer.MAX_VALUE;
@ -1320,7 +1327,11 @@ public class SimpleDateFormat extends DateFormat {
}
int num = (value / 60) * 100 + (value % 60);
CalendarUtils.sprintf0d(buffer, num, width);
if (buffer.isProxyStringBuilder()) {
CalendarUtils.sprintf0d(buffer.asStringBuilder(), num, width);
} else {
CalendarUtils.sprintf0d(buffer.asStringBuffer(), num, width);
}
break;
case PATTERN_ISO_ZONE: // 'X'
@ -1340,7 +1351,11 @@ public class SimpleDateFormat extends DateFormat {
value = -value;
}
CalendarUtils.sprintf0d(buffer, value / 60, 2);
if (buffer.isProxyStringBuilder()) {
CalendarUtils.sprintf0d(buffer.asStringBuilder(), value / 60, 2);
} else {
CalendarUtils.sprintf0d(buffer.asStringBuffer(), value / 60, 2);
}
if (count == 1) {
break;
}
@ -1348,7 +1363,11 @@ public class SimpleDateFormat extends DateFormat {
if (count == 3) {
buffer.append(':');
}
CalendarUtils.sprintf0d(buffer, value % 60, 2);
if (buffer.isProxyStringBuilder()) {
CalendarUtils.sprintf0d(buffer.asStringBuilder(), value % 60, 2);
} else {
CalendarUtils.sprintf0d(buffer.asStringBuffer(), value % 60, 2);
}
break;
default:
@ -1382,7 +1401,7 @@ public class SimpleDateFormat extends DateFormat {
/**
* Formats a number with the specified minimum and maximum number of digits.
*/
private void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer buffer)
private void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuf buffer)
{
// Optimization for 1, 2 and 4 digit numbers. This should
// cover most cases of formatting date/time related items.
@ -1425,7 +1444,17 @@ public class SimpleDateFormat extends DateFormat {
numberFormat.setMinimumIntegerDigits(minDigits);
numberFormat.setMaximumIntegerDigits(maxDigits);
numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
if (buffer.isProxyStringBuilder()) {
//User can set numberFormat with a user-defined NumberFormat which
//not override format(long, StringBuf, FieldPosition).
if ("java.text".equals(numberFormat.getClass().getPackageName())) {
numberFormat.format((long) value, buffer, DontCareFieldPosition.INSTANCE);
} else {
buffer.append(numberFormat.format((long) value, new StringBuffer(), DontCareFieldPosition.INSTANCE));
}
} else {
numberFormat.format((long) value, buffer.asStringBuffer(), DontCareFieldPosition.INSTANCE);
}
}
@ -2565,5 +2594,4 @@ public class SimpleDateFormat extends DateFormat {
originalNumberFormat = numberFormat;
}
}
}