mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 22:44:49 +00:00
ICU-10640 Fix broken build. Fix MeasureFormat.format() to give correct begin and end index in FieldPosition even if the StringBuffer was initially non-empty.
X-SVN-Rev: 35035
This commit is contained in:
parent
0f59bff9aa
commit
b5a7244a29
5 changed files with 67 additions and 82 deletions
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2004-2013, International Business Machines
|
||||
* Copyright (c) 2004-2014, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* Author: Alan Liu
|
||||
|
@ -86,18 +86,9 @@ class CurrencyFormat extends MeasureFormat {
|
|||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public String formatMeasures(Measure... measures) {
|
||||
return mf.formatMeasures(measures);
|
||||
}
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public <T extends Appendable> T formatMeasures(
|
||||
T appendable, FieldPosition fieldPosition, Measure... measures) {
|
||||
return mf.formatMeasures(appendable, fieldPosition, measures);
|
||||
public StringBuilder formatMeasures(
|
||||
StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) {
|
||||
return mf.formatMeasures(appendTo, fieldPosition, measures);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,15 +100,6 @@ class CurrencyFormat extends MeasureFormat {
|
|||
return mf.getWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public ULocale getLocale() {
|
||||
return mf.getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
|
|
|
@ -272,20 +272,20 @@ final public class ListFormatter {
|
|||
throw new IllegalArgumentException("Need {0} and {1} only in pattern " + pattern);
|
||||
}
|
||||
if (recordOffset || offsetRecorded()) {
|
||||
SimplePatternFormatter.Formatted formatted = pattern.formatValues(new Object[]{current, next});
|
||||
int oneOffset = formatted.getOffset(1);
|
||||
int zeroOffset = formatted.getOffset(0);
|
||||
if (zeroOffset == -1 || oneOffset == -1) {
|
||||
throw new IllegalArgumentException("{0} or {1} missing from pattern " + pattern);
|
||||
int[] offsets = new int[2];
|
||||
current = pattern.format(
|
||||
new StringBuilder(), offsets, current, next.toString()).toString();
|
||||
if (offsets[0] == -1 || offsets[1] == -1) {
|
||||
throw new IllegalArgumentException(
|
||||
"{0} or {1} missing from pattern " + pattern);
|
||||
}
|
||||
if (recordOffset) {
|
||||
offset = oneOffset;
|
||||
offset = offsets[1];
|
||||
} else {
|
||||
offset += zeroOffset;
|
||||
offset += offsets[0];
|
||||
}
|
||||
current = formatted.toString();
|
||||
} else {
|
||||
current = pattern.format(current, next);
|
||||
current = pattern.format(current, next.toString());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -281,6 +281,9 @@ public class MeasureFormat extends UFormat {
|
|||
*/
|
||||
@Override
|
||||
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
|
||||
int prevLength = toAppendTo.length();
|
||||
FieldPosition fpos =
|
||||
new FieldPosition(pos.getFieldAttribute(), pos.getField());
|
||||
if (obj instanceof Collection) {
|
||||
Collection<?> coll = (Collection<?>) obj;
|
||||
Measure[] measures = new Measure[coll.size()];
|
||||
|
@ -291,14 +294,19 @@ public class MeasureFormat extends UFormat {
|
|||
}
|
||||
measures[idx++] = (Measure) o;
|
||||
}
|
||||
return toAppendTo.append(formatMeasures(new StringBuilder(), pos, measures));
|
||||
toAppendTo.append(formatMeasures(new StringBuilder(), fpos, measures));
|
||||
} else if (obj instanceof Measure[]) {
|
||||
return toAppendTo.append(formatMeasures(new StringBuilder(), pos, (Measure[]) obj));
|
||||
toAppendTo.append(formatMeasures(new StringBuilder(), fpos, (Measure[]) obj));
|
||||
} else if (obj instanceof Measure){
|
||||
return toAppendTo.append(formatMeasure((Measure) obj, new StringBuilder(), pos));
|
||||
toAppendTo.append(formatMeasure((Measure) obj, new StringBuilder(), fpos));
|
||||
} else {
|
||||
throw new IllegalArgumentException(obj.toString());
|
||||
}
|
||||
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
|
||||
pos.setBeginIndex(fpos.getBeginIndex() + prevLength);
|
||||
pos.setEndIndex(fpos.getEndIndex() + prevLength);
|
||||
}
|
||||
return toAppendTo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -326,14 +334,15 @@ public class MeasureFormat extends UFormat {
|
|||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
public String formatMeasures(Measure... measures) {
|
||||
StringBuilder result = this.formatMeasures(
|
||||
new StringBuilder(), DontCareFieldPosition.INSTANCE, measures);
|
||||
return result.toString();
|
||||
public final String formatMeasures(Measure... measures) {
|
||||
return formatMeasures(
|
||||
new StringBuilder(),
|
||||
DontCareFieldPosition.INSTANCE,
|
||||
measures).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a sequence of measures and adds to appendable.
|
||||
* Formats a sequence of measures.
|
||||
*
|
||||
* If the fieldPosition argument identifies a NumberFormat field,
|
||||
* then its indices are set to the beginning and end of the first such field
|
||||
|
@ -369,7 +378,7 @@ public class MeasureFormat extends UFormat {
|
|||
ListFormatter listFormatter = ListFormatter.getInstance(
|
||||
getLocale(), formatWidth.getListFormatterStyle());
|
||||
if (fieldPosition != DontCareFieldPosition.INSTANCE) {
|
||||
return appendTo.append(formatMeasuresSlowTrack(listFormatter, fieldPosition, measures));
|
||||
return formatMeasuresSlowTrack(listFormatter, appendTo, fieldPosition, measures);
|
||||
}
|
||||
// Fast track: No field position.
|
||||
String[] results = new String[measures.length];
|
||||
|
@ -426,7 +435,7 @@ public class MeasureFormat extends UFormat {
|
|||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
public ULocale getLocale() {
|
||||
public final ULocale getLocale() {
|
||||
return getLocale(ULocale.VALID_LOCALE);
|
||||
}
|
||||
|
||||
|
@ -618,14 +627,13 @@ public class MeasureFormat extends UFormat {
|
|||
Map<FormatWidth, QuantityFormatter> styleToCountToFormat = unitToStyleToCountToFormat.get(unit);
|
||||
QuantityFormatter countToFormat = styleToCountToFormat.get(formatWidth);
|
||||
SimplePatternFormatter formatter = countToFormat.getByVariant(keyword);
|
||||
SimplePatternFormatter.Formatted result = formatter.formatValues(new Object[] {formattedNumber});
|
||||
appendTo.append(result.toString());
|
||||
int offset = result.getOffset(0);
|
||||
if (offset != -1) { // there is a number (may not happen with, say, Arabic dual)
|
||||
int[] offsets = new int[1];
|
||||
formatter.format(appendTo, offsets, formattedNumber.toString());
|
||||
if (offsets[0] != -1) { // there is a number (may not happen with, say, Arabic dual)
|
||||
// Fix field position
|
||||
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
|
||||
fieldPosition.setBeginIndex(fpos.getBeginIndex() + offset);
|
||||
fieldPosition.setEndIndex(fpos.getEndIndex() + offset);
|
||||
fieldPosition.setBeginIndex(fpos.getBeginIndex() + offsets[0]);
|
||||
fieldPosition.setEndIndex(fpos.getEndIndex() + offsets[0]);
|
||||
}
|
||||
}
|
||||
return appendTo;
|
||||
|
@ -685,12 +693,16 @@ public class MeasureFormat extends UFormat {
|
|||
return new MeasureProxy(getLocale(), formatWidth, numberFormat.get(), CURRENCY_FORMAT);
|
||||
}
|
||||
|
||||
private String formatMeasuresSlowTrack(ListFormatter listFormatter, FieldPosition fieldPosition,
|
||||
private StringBuilder formatMeasuresSlowTrack(
|
||||
ListFormatter listFormatter,
|
||||
StringBuilder appendTo,
|
||||
FieldPosition fieldPosition,
|
||||
Measure... measures) {
|
||||
String[] results = new String[measures.length];
|
||||
|
||||
// Zero out our field position so that we can tell when we find our field.
|
||||
FieldPosition fpos = new FieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField());
|
||||
FieldPosition fpos = new FieldPosition(
|
||||
fieldPosition.getFieldAttribute(), fieldPosition.getField());
|
||||
|
||||
int fieldPositionFoundIndex = -1;
|
||||
for (int i = 0; i < measures.length; ++i) {
|
||||
|
@ -708,10 +720,10 @@ public class MeasureFormat extends UFormat {
|
|||
|
||||
// Fix up FieldPosition indexes if our field is found.
|
||||
if (builder.getOffset() != -1) {
|
||||
fieldPosition.setBeginIndex(fpos.getBeginIndex() + builder.getOffset());
|
||||
fieldPosition.setEndIndex(fpos.getEndIndex() + builder.getOffset());
|
||||
fieldPosition.setBeginIndex(fpos.getBeginIndex() + builder.getOffset() + appendTo.length());
|
||||
fieldPosition.setEndIndex(fpos.getEndIndex() + builder.getOffset() + appendTo.length());
|
||||
}
|
||||
return builder.toString();
|
||||
return appendTo.append(builder.toString());
|
||||
}
|
||||
|
||||
// type is one of "hm", "ms" or "hms"
|
||||
|
|
|
@ -517,23 +517,15 @@ if ( searchPluralCount.equals("other") ) {
|
|||
// boilerplate code to make TimeUnitFormat otherwise follow the contract of
|
||||
// MeasureFormat
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public String formatMeasures(Measure... measures) {
|
||||
return mf.formatMeasures(measures);
|
||||
}
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public <T extends Appendable> T formatMeasures(
|
||||
T appendable, FieldPosition fieldPosition, Measure... measures) {
|
||||
return mf.formatMeasures(appendable, fieldPosition, measures);
|
||||
public StringBuilder formatMeasures(
|
||||
StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) {
|
||||
return mf.formatMeasures(appendTo, fieldPosition, measures);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -545,16 +537,6 @@ if ( searchPluralCount.equals("other") ) {
|
|||
return mf.getWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
*/
|
||||
@Override
|
||||
public ULocale getLocale() {
|
||||
return mf.getLocale();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @draft ICU 53
|
||||
* @provisional
|
||||
|
|
|
@ -421,9 +421,9 @@ public class MeasureUnitTest extends TestFmwk {
|
|||
MeasureFormat fmt = MeasureFormat.getInstance(
|
||||
ULocale.ENGLISH, FormatWidth.SHORT);
|
||||
FieldPosition pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
|
||||
fmt.format(new Measure(43.5, MeasureUnit.FOOT), new StringBuffer(), pos);
|
||||
assertEquals("beginIndex", 2, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 3, pos.getEndIndex());
|
||||
fmt.format(new Measure(43.5, MeasureUnit.FOOT), new StringBuffer("123456: "), pos);
|
||||
assertEquals("beginIndex", 10, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 11, pos.getEndIndex());
|
||||
|
||||
pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
|
||||
fmt.format(new Measure(43, MeasureUnit.FOOT), new StringBuffer(), pos);
|
||||
|
@ -450,14 +450,14 @@ public class MeasureUnitTest extends TestFmwk {
|
|||
|
||||
pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
|
||||
result = fmt.formatMeasures(
|
||||
new StringBuilder(),
|
||||
new StringBuilder("123456: "),
|
||||
pos,
|
||||
new Measure(354, MeasureUnit.METER),
|
||||
new Measure(23, MeasureUnit.CENTIMETER),
|
||||
new Measure(5.4, MeasureUnit.MILLIMETER)).toString();
|
||||
assertEquals("result", "354 m, 23 cm, 5.4 mm", result);
|
||||
assertEquals("beginIndex", 15, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 16, pos.getEndIndex());
|
||||
assertEquals("result", "123456: 354 m, 23 cm, 5.4 mm", result);
|
||||
assertEquals("beginIndex", 23, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 24, pos.getEndIndex());
|
||||
|
||||
result = fmt.formatMeasures(
|
||||
new StringBuilder(),
|
||||
|
@ -471,15 +471,24 @@ public class MeasureUnitTest extends TestFmwk {
|
|||
|
||||
pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
|
||||
result = fmt.formatMeasures(
|
||||
new StringBuilder(),
|
||||
new StringBuilder("123456: "),
|
||||
pos,
|
||||
new Measure(3, MeasureUnit.METER),
|
||||
new Measure(23, MeasureUnit.CENTIMETER),
|
||||
new Measure(5, MeasureUnit.MILLIMETER)).toString();
|
||||
assertEquals("result", "3 m, 23 cm, 5 mm", result);
|
||||
assertEquals("result", "123456: 3 m, 23 cm, 5 mm", result);
|
||||
assertEquals("beginIndex", 0, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 0, pos.getEndIndex());
|
||||
|
||||
pos = new FieldPosition(NumberFormat.Field.INTEGER);
|
||||
result = fmt.formatMeasures(
|
||||
new StringBuilder("123456: "),
|
||||
pos,
|
||||
new Measure(57, MeasureUnit.MILLIMETER)).toString();
|
||||
assertEquals("result", "123456: 57 mm", result);
|
||||
assertEquals("beginIndex", 8, pos.getBeginIndex());
|
||||
assertEquals("endIndex", 10, pos.getEndIndex());
|
||||
|
||||
}
|
||||
|
||||
public void testOldFormatWithList() {
|
||||
|
|
Loading…
Add table
Reference in a new issue