summaryrefslogtreecommitdiffstats
path: root/icu/src/main/java
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-01-21 10:52:50 -0800
committerElliott Hughes <enh@google.com>2010-01-21 10:58:41 -0800
commitb56d4f145a425910db1f6201edc593b61127ca83 (patch)
tree5c05dac4092cabaad94b493c916197107e6a43da /icu/src/main/java
parentb2d899b733e6f9f130a13d3684a9318da7ef9b2f (diff)
downloadlibcore-b56d4f145a425910db1f6201edc593b61127ca83.zip
libcore-b56d4f145a425910db1f6201edc593b61127ca83.tar.gz
libcore-b56d4f145a425910db1f6201edc593b61127ca83.tar.bz2
Improve the DecimalFormat JNI.
We don't need two identical copies of the code for double and long; ICU uses overloading, and we should take advantage of that. We can also improve the code to remove unnecessary heap allocation, remove unnecessary temporary copies, and only make JNI calls and ask for the attribute data when necessary. I've also switched the code from the thread-unsafe strtok(3) to strtok_r(3). I've also removed unnecessary temporary char[]s and copying in DecimalFormat. I've also fixed another instance of the "if (doubleValue == longValue) longPath" anti-pattern that gets -0.0 wrong. (It's also worth noting that caliper says the difference between the double and long paths is very small, on the order of 2us.) (The new code takes about 20us per call compared to 60us for the old code, measured on passion-eng.)
Diffstat (limited to 'icu/src/main/java')
-rw-r--r--icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java20
1 files changed, 7 insertions, 13 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java b/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java
index df76385..4b296d5 100644
--- a/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java
+++ b/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java
@@ -176,8 +176,7 @@ public class DecimalFormat extends NumberFormat {
throw new NullPointerException();
}
String fieldType = getFieldType(field.getFieldAttribute());
- String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);
- buffer.append(result.toCharArray(), 0, result.length());
+ buffer.append(NativeDecimalFormat.format(this.addr, value, field, fieldType, null));
return buffer;
}
@@ -187,8 +186,7 @@ public class DecimalFormat extends NumberFormat {
throw new NullPointerException();
}
String fieldType = getFieldType(field.getFieldAttribute());
- String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);
- buffer.append(result.toCharArray(), 0, result.length());
+ buffer.append(NativeDecimalFormat.format(this.addr, value, field, fieldType, null));
return buffer;
}
@@ -240,19 +238,15 @@ public class DecimalFormat extends NumberFormat {
scale = makeScalePositive(scale, val);
text = NativeDecimalFormat.format(this.addr, val.toString(), null,
null, attributes, scale);
- } else {
+ } else if (number instanceof Double || number instanceof Float) {
double dv = number.doubleValue();
+ text = NativeDecimalFormat.format(this.addr, dv, null, null, attributes);
+ } else {
long lv = number.longValue();
- if (dv == lv) {
- text = NativeDecimalFormat.format(this.addr, lv, null,
- null, attributes);
- } else {
- text = NativeDecimalFormat.format(this.addr, dv, null,
- null, attributes);
- }
+ text = NativeDecimalFormat.format(this.addr, lv, null, null, attributes);
}
- AttributedString as = new AttributedString(text.toString());
+ AttributedString as = new AttributedString(text);
String[] attrs = attributes.toString().split(";");
// add NumberFormat field attributes to the AttributedString