diff options
author | Elliott Hughes <enh@google.com> | 2010-01-28 17:47:00 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-01-28 17:47:00 -0800 |
commit | 5d593ea1361bd7c5ef3353b66d63ac64e95d9d41 (patch) | |
tree | a1212d037a0540cd8d05cf925cfb5ddd93cfe8df /icu/src/main | |
parent | be815a27c0dc051faf5a9642a5387f091f519efa (diff) | |
download | libcore-5d593ea1361bd7c5ef3353b66d63ac64e95d9d41.zip libcore-5d593ea1361bd7c5ef3353b66d63ac64e95d9d41.tar.gz libcore-5d593ea1361bd7c5ef3353b66d63ac64e95d9d41.tar.bz2 |
Use DecimalFormatSymbols' new default constructor for speed.
This brings "new DecimalFormat" down to ~80us (from ~260us before this patch,
or ~600us this time last week). Also remove some dead code and tighten up some
accessibility.
Depends on https://android-git.corp.google.com/g/38877.
Diffstat (limited to 'icu/src/main')
-rw-r--r-- | icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java | 49 | ||||
-rw-r--r-- | icu/src/main/native/NativeDecimalFormat.cpp | 143 |
2 files changed, 80 insertions, 112 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java b/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java index 7cb5de2..d1da72f 100644 --- a/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java +++ b/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java @@ -32,7 +32,7 @@ import java.util.Locale; public class NativeDecimalFormat { /** - * Constants corresponding to the native type UNumberFormatSymbol, for getSymbol/setSymbol. + * Constants corresponding to the native type UNumberFormatSymbol, for setSymbol. */ private static final int UNUM_DECIMAL_SEPARATOR_SYMBOL = 0; private static final int UNUM_GROUPING_SEPARATOR_SYMBOL = 1; @@ -117,10 +117,19 @@ public class NativeDecimalFormat { */ private BigDecimal multiplierBigDecimal = null; - public NativeDecimalFormat(String pattern, Locale locale, DecimalFormatSymbols symbols) { - this.addr = openDecimalFormat(pattern); - this.lastPattern = pattern; - setDecimalFormatSymbols(symbols); + public NativeDecimalFormat(String pattern, DecimalFormatSymbols dfs) { + try { + this.addr = openDecimalFormatImpl(pattern, dfs.getCurrencySymbol(), + dfs.getDecimalSeparator(), dfs.getDigit(), dfs.getGroupingSeparator(), + dfs.getInfinity(), dfs.getInternationalCurrencySymbol(), dfs.getMinusSign(), + dfs.getMonetaryDecimalSeparator(), dfs.getNaN(), dfs.getPatternSeparator(), + dfs.getPercent(), dfs.getPerMill(), dfs.getZeroDigit()); + this.lastPattern = pattern; + } catch (NullPointerException npe) { + throw npe; + } catch (RuntimeException re) { + throw new IllegalArgumentException("syntax error: " + re.getMessage() + ": " + pattern); + } } // Used to implement clone. @@ -183,8 +192,7 @@ public class NativeDecimalFormat { obj.getMaximumFractionDigits() == this.getMaximumFractionDigits() && obj.getMinimumIntegerDigits() == this.getMinimumIntegerDigits() && obj.getMinimumFractionDigits() == this.getMinimumFractionDigits() && - obj.isGroupingUsed() == this.isGroupingUsed() && - obj.getCurrency() == this.getCurrency(); + obj.isGroupingUsed() == this.isGroupingUsed(); } /** @@ -350,14 +358,6 @@ public class NativeDecimalFormat { return getAttribute(this.addr, UNUM_MIN_INTEGER_DIGITS); } - public Currency getCurrency() { - String curr = getSymbol(this.addr, UNUM_INTL_CURRENCY_SYMBOL); - if (curr.equals("") || curr.equals("\u00a4\u00a4")) { - return null; - } - return Currency.getInstance(curr); - } - public int getGroupingSize() { return getAttribute(this.addr, UNUM_GROUPING_SIZE); } @@ -560,17 +560,6 @@ public class NativeDecimalFormat { return null; } - private static int openDecimalFormat(String pattern) { - try { - // FIXME: if we're about to override everything, should we just ask for the cheapest locale (presumably the root locale)? - return openDecimalFormatImpl(pattern); - } catch (NullPointerException npe) { - throw npe; - } catch (RuntimeException re) { - throw new IllegalArgumentException("syntax error: " + re.getMessage() + ": " + pattern); - } - } - private static void applyPattern(int addr, boolean localized, String pattern) { try { applyPatternImpl(addr, localized, pattern); @@ -588,17 +577,17 @@ public class NativeDecimalFormat { private static native String format(int addr, double value, FieldPosition position, String fieldType, StringBuffer attributes); private static native String format(int addr, String value, FieldPosition position, String fieldType, StringBuffer attributes, int scale); private static native int getAttribute(int addr, int symbol); - // FIXME: do we need getSymbol any more? the Java-side object should be the canonical source. - private static native String getSymbol(int addr, int symbol); private static native String getTextAttribute(int addr, int symbol); - private static native int openDecimalFormatImpl(String pattern); + private static native int openDecimalFormatImpl(String pattern, String currencySymbol, + char decimalSeparator, char digit, char groupingSeparator, String infinity, + String internationalCurrencySymbol, char minusSign, char monetaryDecimalSeparator, + String nan, char patternSeparator, char percent, char perMill, char zeroDigit); private static native Number parse(int addr, String string, ParsePosition position); private static native void setDecimalFormatSymbols(int addr, String currencySymbol, char decimalSeparator, char digit, char groupingSeparator, String infinity, String internationalCurrencySymbol, char minusSign, char monetaryDecimalSeparator, String nan, char patternSeparator, char percent, char perMill, char zeroDigit); private static native void setSymbol(int addr, int symbol, String str); - private static native void setSymbol(int addr, int symbol, char ch); private static native void setAttribute(int addr, int symbol, int i); private static native void setTextAttribute(int addr, int symbol, String str); private static native String toPatternImpl(int addr, boolean localized); diff --git a/icu/src/main/native/NativeDecimalFormat.cpp b/icu/src/main/native/NativeDecimalFormat.cpp index 9170f27..413ffdd 100644 --- a/icu/src/main/native/NativeDecimalFormat.cpp +++ b/icu/src/main/native/NativeDecimalFormat.cpp @@ -33,16 +33,68 @@ static DecimalFormat* toDecimalFormat(jint addr) { return reinterpret_cast<DecimalFormat*>(static_cast<uintptr_t>(addr)); } -static jint openDecimalFormatImpl(JNIEnv* env, jclass clazz, jstring pattern0) { +static DecimalFormatSymbols* makeDecimalFormatSymbols(JNIEnv* env, + jstring currencySymbol0, jchar decimalSeparator, jchar digit, + jchar groupingSeparator0, jstring infinity0, + jstring internationalCurrencySymbol0, jchar minusSign, + jchar monetaryDecimalSeparator, jstring nan0, jchar patternSeparator, + jchar percent, jchar perMill, jchar zeroDigit) { + ScopedJavaUnicodeString currencySymbol(env, currencySymbol0); + ScopedJavaUnicodeString infinity(env, infinity0); + ScopedJavaUnicodeString internationalCurrencySymbol(env, internationalCurrencySymbol0); + ScopedJavaUnicodeString nan(env, nan0); + UnicodeString groupingSeparator(groupingSeparator0); + + DecimalFormatSymbols* result = new DecimalFormatSymbols; + result->setSymbol(DecimalFormatSymbols::kCurrencySymbol, currencySymbol.unicodeString()); + result->setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, UnicodeString(decimalSeparator)); + result->setSymbol(DecimalFormatSymbols::kDigitSymbol, UnicodeString(digit)); + result->setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, groupingSeparator); + result->setSymbol(DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, groupingSeparator); + result->setSymbol(DecimalFormatSymbols::kInfinitySymbol, infinity.unicodeString()); + result->setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, internationalCurrencySymbol.unicodeString()); + result->setSymbol(DecimalFormatSymbols::kMinusSignSymbol, UnicodeString(minusSign)); + result->setSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol, UnicodeString(monetaryDecimalSeparator)); + result->setSymbol(DecimalFormatSymbols::kNaNSymbol, nan.unicodeString()); + result->setSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol, UnicodeString(patternSeparator)); + result->setSymbol(DecimalFormatSymbols::kPercentSymbol, UnicodeString(percent)); + result->setSymbol(DecimalFormatSymbols::kPerMillSymbol, UnicodeString(perMill)); + result->setSymbol(DecimalFormatSymbols::kZeroDigitSymbol, UnicodeString(zeroDigit)); + return result; +} + +static void setDecimalFormatSymbols(JNIEnv* env, jclass, jint addr, + jstring currencySymbol, jchar decimalSeparator, jchar digit, + jchar groupingSeparator, jstring infinity, + jstring internationalCurrencySymbol, jchar minusSign, + jchar monetaryDecimalSeparator, jstring nan, jchar patternSeparator, + jchar percent, jchar perMill, jchar zeroDigit) { + DecimalFormatSymbols* symbols = makeDecimalFormatSymbols(env, + currencySymbol, decimalSeparator, digit, groupingSeparator, + infinity, internationalCurrencySymbol, minusSign, + monetaryDecimalSeparator, nan, patternSeparator, percent, perMill, + zeroDigit); + toDecimalFormat(addr)->adoptDecimalFormatSymbols(symbols); +} + +static jint openDecimalFormatImpl(JNIEnv* env, jclass clazz, jstring pattern0, + jstring currencySymbol, jchar decimalSeparator, jchar digit, + jchar groupingSeparator, jstring infinity, + jstring internationalCurrencySymbol, jchar minusSign, + jchar monetaryDecimalSeparator, jstring nan, jchar patternSeparator, + jchar percent, jchar perMill, jchar zeroDigit) { if (pattern0 == NULL) { jniThrowNullPointerException(env, NULL); return 0; } - UErrorCode status = U_ZERO_ERROR; UParseError parseError; ScopedJavaUnicodeString pattern(env, pattern0); - DecimalFormatSymbols* symbols = new DecimalFormatSymbols(status); + DecimalFormatSymbols* symbols = makeDecimalFormatSymbols(env, + currencySymbol, decimalSeparator, digit, groupingSeparator, + infinity, internationalCurrencySymbol, minusSign, + monetaryDecimalSeparator, nan, patternSeparator, percent, perMill, + zeroDigit); DecimalFormat* fmt = new DecimalFormat(pattern.unicodeString(), symbols, parseError, status); if (fmt == NULL) { delete symbols; @@ -55,87 +107,16 @@ static void closeDecimalFormatImpl(JNIEnv *env, jclass clazz, jint addr) { delete toDecimalFormat(addr); } -static void setSymbol(JNIEnv *env, uintptr_t addr, jint symbol, const UChar* chars, int32_t charCount) { +static void setSymbol(JNIEnv* env, jclass, jint addr, jint symbol, jstring s) { + const UChar* chars = env->GetStringChars(s, NULL); + const int32_t charCount = env->GetStringLength(s); UErrorCode status = U_ZERO_ERROR; UNumberFormat* fmt = reinterpret_cast<UNumberFormat*>(static_cast<uintptr_t>(addr)); - unum_setSymbol(fmt, static_cast<UNumberFormatSymbol>(symbol), - chars, charCount, &status); + unum_setSymbol(fmt, static_cast<UNumberFormatSymbol>(symbol), chars, charCount, &status); icu4jni_error(env, status); -} - -static void setSymbol_String(JNIEnv *env, jclass, jint addr, jint symbol, jstring s) { - const UChar* chars = env->GetStringChars(s, NULL); - const int32_t charCount = env->GetStringLength(s); - setSymbol(env, addr, symbol, chars, charCount); env->ReleaseStringChars(s, chars); } -static void setSymbol_char(JNIEnv *env, jclass, jint addr, jint symbol, jchar ch) { - setSymbol(env, addr, symbol, &ch, 1); -} - -static void setDecimalFormatSymbols(JNIEnv* env, jclass, jint addr, jstring currencySymbol0, jchar decimalSeparator, jchar digit, jchar groupingSeparator0, jstring infinity0, jstring internationalCurrencySymbol0, jchar minusSign, jchar monetaryDecimalSeparator, jstring nan0, jchar patternSeparator, jchar percent, jchar perMill, jchar zeroDigit) { - ScopedJavaUnicodeString currencySymbol(env, currencySymbol0); - ScopedJavaUnicodeString infinity(env, infinity0); - ScopedJavaUnicodeString internationalCurrencySymbol(env, internationalCurrencySymbol0); - ScopedJavaUnicodeString nan(env, nan0); - UnicodeString groupingSeparator(groupingSeparator0); - - DecimalFormat* fmt = toDecimalFormat(addr); - DecimalFormatSymbols newSymbols(*fmt->getDecimalFormatSymbols()); - newSymbols.setSymbol(DecimalFormatSymbols::kCurrencySymbol, currencySymbol.unicodeString()); - newSymbols.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, UnicodeString(decimalSeparator)); - newSymbols.setSymbol(DecimalFormatSymbols::kDigitSymbol, UnicodeString(digit)); - newSymbols.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, groupingSeparator); - newSymbols.setSymbol(DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, groupingSeparator); - newSymbols.setSymbol(DecimalFormatSymbols::kInfinitySymbol, infinity.unicodeString()); - newSymbols.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, internationalCurrencySymbol.unicodeString()); - newSymbols.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, UnicodeString(minusSign)); - newSymbols.setSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol, UnicodeString(monetaryDecimalSeparator)); - newSymbols.setSymbol(DecimalFormatSymbols::kNaNSymbol, nan.unicodeString()); - newSymbols.setSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol, UnicodeString(patternSeparator)); - newSymbols.setSymbol(DecimalFormatSymbols::kPercentSymbol, UnicodeString(percent)); - newSymbols.setSymbol(DecimalFormatSymbols::kPerMillSymbol, UnicodeString(perMill)); - newSymbols.setSymbol(DecimalFormatSymbols::kZeroDigitSymbol, UnicodeString(zeroDigit)); - fmt->setDecimalFormatSymbols(newSymbols); -} - -static jstring getSymbol(JNIEnv *env, jclass clazz, jint addr, jint symbol) { - - uint32_t resultlength, reslenneeded; - - // the errorcode returned by unum_getSymbol - UErrorCode status = U_ZERO_ERROR; - - // get the pointer to the number format - UNumberFormat *fmt = (UNumberFormat *)(int)addr; - - UChar* result = NULL; - resultlength=0; - - // find out how long the result will be - reslenneeded=unum_getSymbol(fmt, (UNumberFormatSymbol) symbol, result, - resultlength, &status); - - result = NULL; - if(status==U_BUFFER_OVERFLOW_ERROR) { - status=U_ZERO_ERROR; - resultlength=reslenneeded+1; - result=(UChar*)malloc(sizeof(UChar) * resultlength); - reslenneeded=unum_getSymbol(fmt, (UNumberFormatSymbol) symbol, result, - resultlength, &status); - } - if (icu4jni_error(env, status) != FALSE) { - return NULL; - } - - jstring res = env->NewString(result, reslenneeded); - - free(result); - - return res; -} - static void setAttribute(JNIEnv *env, jclass clazz, jint addr, jint symbol, jint value) { @@ -606,14 +587,12 @@ static JNINativeMethod gMethods[] = { {"format", "(IJLjava/text/FieldPosition;Ljava/lang/String;Ljava/lang/StringBuffer;)Ljava/lang/String;", (void*) formatLong}, {"format", "(ILjava/lang/String;Ljava/text/FieldPosition;Ljava/lang/String;Ljava/lang/StringBuffer;I)Ljava/lang/String;", (void*) formatDigitList}, {"getAttribute", "(II)I", (void*) getAttribute}, - {"getSymbol", "(II)Ljava/lang/String;", (void*) getSymbol}, {"getTextAttribute", "(II)Ljava/lang/String;", (void*) getTextAttribute}, - {"openDecimalFormatImpl", "(Ljava/lang/String;)I", (void*) openDecimalFormatImpl}, + {"openDecimalFormatImpl", "(Ljava/lang/String;Ljava/lang/String;CCCLjava/lang/String;Ljava/lang/String;CCLjava/lang/String;CCCC)I", (void*) openDecimalFormatImpl}, {"parse", "(ILjava/lang/String;Ljava/text/ParsePosition;)Ljava/lang/Number;", (void*) parse}, {"setAttribute", "(III)V", (void*) setAttribute}, {"setDecimalFormatSymbols", "(ILjava/lang/String;CCCLjava/lang/String;Ljava/lang/String;CCLjava/lang/String;CCCC)V", (void*) setDecimalFormatSymbols}, - {"setSymbol", "(IIC)V", (void*) setSymbol_char}, - {"setSymbol", "(IILjava/lang/String;)V", (void*) setSymbol_String}, + {"setSymbol", "(IILjava/lang/String;)V", (void*) setSymbol}, {"setTextAttribute", "(IILjava/lang/String;)V", (void*) setTextAttribute}, {"toPatternImpl", "(IZ)Ljava/lang/String;", (void*) toPatternImpl}, }; |