OSDN Git Service

More ICU cleanup.
authorElliott Hughes <enh@google.com>
Tue, 22 Dec 2009 04:24:12 +0000 (20:24 -0800)
committerElliott Hughes <enh@google.com>
Tue, 22 Dec 2009 04:24:12 +0000 (20:24 -0800)
Don't duplicate SimpleDateFormat.patternChars in native code when we can just
access it directly.

Also remove a bit more duplication in the native code, and remove the early
scope closure in getCurrencyCodeNative. If we're going to waste space in
that function, let's waste it on an explanation of why a seemingly useless
assignment is actually totally necessary.

libcore/icu/src/main/native/ResourceInterface.cpp
libcore/text/src/main/java/java/text/DateFormatSymbols.java
libcore/text/src/main/java/java/text/SimpleDateFormat.java

index c0fedbf..abb00a0 100644 (file)
@@ -122,15 +122,16 @@ static jstring getCurrencyCodeNative(JNIEnv* env, jclass clazz, jstring key) {
         return env->NewStringUTF("None");
     }
 
-    // check if there is a to date. If there is, the currency isn't used anymore.
-    {
-        ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status));
-        if (!U_FAILURE(status)) {
-            // return and let the ResourceBundle throw an exception
-            return NULL;
-        }
-        status = U_ZERO_ERROR;
+    // check if there is a 'to' date. If there is, the currency isn't used anymore.
+    ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status));
+    if (!U_FAILURE(status)) {
+        // return and let the caller throw an exception
+        return NULL;
     }
+    // We need to reset 'status'. It works like errno in that ICU doesn't set it
+    // to U_ZERO_ERROR on success: it only touches it on error, and the test
+    // above means it now holds a failure code.
+    status = U_ZERO_ERROR;
 
     ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status));
     if (U_FAILURE(status)) {
@@ -255,53 +256,38 @@ static jobjectArray getAvailableLocalesNative(JNIEnv* env, jclass clazz) {
     return result;
 }
 
+static TimeZone* timeZoneFromId(JNIEnv* env, jstring id) {
+    const jchar* chars = env->GetStringChars(id, NULL);
+    const UnicodeString zoneID(reinterpret_cast<const UChar*>(chars), env->GetStringLength(id));
+    env->ReleaseStringChars(id, chars);
+    return TimeZone::createTimeZone(zoneID);
+}
+
 static void getTimeZonesNative(JNIEnv* env, jclass clazz,
         jobjectArray outerArray, jstring locale) {
-    // LOGI("ENTER getTimeZonesNative");
-    
-    UErrorCode status = U_ZERO_ERROR;
 
-    jobjectArray zoneIdArray;
-    jobjectArray longStdTimeArray;
-    jobjectArray shortStdTimeArray;
-    jobjectArray longDlTimeArray;
-    jobjectArray shortDlTimeArray;
-
-    jstring content;
-    jstring strObj;
-    const jchar* res;
-    UnicodeString resU;
-    jint length;
-    const UnicodeString* zoneID;
-    DateFormat* df;
+    // get all timezone objects
+    jobjectArray zoneIdArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 0);
+    int count = env->GetArrayLength(zoneIdArray);
+    TimeZone* zones[count];
+    for(int i = 0; i < count; i++) {
+        jstring id = (jstring) env->GetObjectArrayElement(zoneIdArray, i);
+        zones[i] = timeZoneFromId(env, id);
+        env->DeleteLocalRef(id);
+    }
 
-    UnicodeString longPattern("zzzz","");
-    UnicodeString shortPattern("z","");
-      
     Locale loc = getLocale(env, locale);
 
+    UErrorCode status = U_ZERO_ERROR;
+    UnicodeString longPattern("zzzz","");
     SimpleDateFormat longFormat(longPattern, loc, status);
+    UnicodeString shortPattern("z","");
     SimpleDateFormat shortFormat(shortPattern, loc, status);
 
-
-    zoneIdArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 0);
-    longStdTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 1);
-    shortStdTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 2);
-    longDlTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 3);
-    shortDlTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 4);
-
-    // get all timezone objects
-    int count = env->GetArrayLength(zoneIdArray);
-    TimeZone* zones[count];
-    for(int i = 0; i < count; i++) {
-        strObj = (jstring) env->GetObjectArrayElement(zoneIdArray, i);
-        length = env->GetStringLength(strObj);
-        res = env->GetStringChars(strObj, NULL);
-        const UnicodeString zoneID((UChar*)res, length);
-        env->ReleaseStringChars(strObj, res);
-        zones[i] = TimeZone::createTimeZone(zoneID);
-        env->DeleteLocalRef(strObj);
-    }
+    jobjectArray longStdTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 1);
+    jobjectArray shortStdTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 2);
+    jobjectArray longDlTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 3);
+    jobjectArray shortDlTimeArray = (jobjectArray) env->GetObjectArrayElement(outerArray, 4);
 
     // 15th January 2008
     UDate date1 = 1203105600000.0;
@@ -331,7 +317,7 @@ static void getTimeZonesNative(JNIEnv* env, jclass clazz,
 
         UnicodeString str;
         shortFormat.format(daylightSavingDate, str);
-        content = env->NewString(str.getBuffer(), str.length());
+        jstring content = env->NewString(str.getBuffer(), str.length());
         env->SetObjectArrayElement(shortDlTimeArray, i, content);
         env->DeleteLocalRef(content);
 
@@ -355,16 +341,10 @@ static void getTimeZonesNative(JNIEnv* env, jclass clazz,
 }
 
 static jstring getDisplayTimeZoneNative(JNIEnv* env, jclass clazz,
-        jstring zoneID, jboolean isDST, jint style, jstring localeID) {
-
-    // Build TimeZone object
-    const jchar* idChars = env->GetStringChars(zoneID, NULL);
-    jint idLength = env->GetStringLength(zoneID);
-    UnicodeString idString((UChar*)idChars, idLength);
-    TimeZone* zone = TimeZone::createTimeZone(idString);
-    env->ReleaseStringChars(zoneID, idChars);
+        jstring zoneId, jboolean isDST, jint style, jstring localeId) {
 
-    Locale locale = getLocale(env, localeID);
+    TimeZone* zone = timeZoneFromId(env, zoneId);
+    Locale locale = getLocale(env, localeId);
 
     // Try to get the display name of the TimeZone according to the Locale
     UnicodeString displayName;
@@ -617,7 +597,6 @@ static jobjectArray getContentImpl(JNIEnv* env, jclass clazz, jstring locale) {
     jobject minimalDaysInFirstWeek = NULL;
     jobjectArray amPmMarkers = NULL;
     jobjectArray eras = NULL;
-    jstring localPatternChars = NULL;
     jobjectArray weekdays = NULL;
     jobjectArray shortWeekdays = NULL;
     jobjectArray months = NULL;
@@ -695,12 +674,6 @@ static jobjectArray getContentImpl(JNIEnv* env, jclass clazz, jstring locale) {
     }
 
 
-    // local pattern chars are initially always the same
-    localPatternChars = env->NewStringUTF("GyMdkHmsSEDFwWahKzZ");
-    // adding local pattern chars string to the result
-    counter++;
-
-
     // adding month names string array to the result
     months = getLongMonthNames(env, gregorian);
     if(months != NULL) {
@@ -967,9 +940,6 @@ zones:
     if(eras != NULL && index < counter) {
         addObject(env, result, "eras", eras, index++);
     }
-    if(localPatternChars != NULL && index < counter) {
-        addObject(env, result, "LocalPatternChars", localPatternChars, index++);
-    }
     if(weekdays != NULL && index < counter) {
         addObject(env, result, "weekdays", weekdays, index++);
     }
index ad25bd8..e8e1f69 100644 (file)
@@ -122,8 +122,8 @@ public class DateFormatSymbols implements Serializable, Cloneable {
      */
     public DateFormatSymbols(Locale locale) {
         // BEGIN android-changed
+        localPatternChars = SimpleDateFormat.patternChars;
         ResourceBundle bundle = Format.getBundle(locale);
-        localPatternChars = bundle.getString("LocalPatternChars"); //$NON-NLS-1$
         ampms = bundle.getStringArray("ampm"); //$NON-NLS-1$
         eras = bundle.getStringArray("eras"); //$NON-NLS-1$
         months = bundle.getStringArray("months"); //$NON-NLS-1$
index 290616c..47ff930 100644 (file)
@@ -316,8 +316,7 @@ public class SimpleDateFormat extends DateFormat {
     private static final long serialVersionUID = 4774881970558875024L;
 
     // BEGIN android-changed
-    // private static final String patternChars = "GyMdkHmsSEDFwWahKzYeugAZvcLQqV"; //$NON-NLS-1$
-    private static final String patternChars = "GyMdkHmsSEDFwWahKzZ"; //$NON-NLS-1$
+    static final String patternChars = "GyMdkHmsSEDFwWahKzZ"; //$NON-NLS-1$
     // END android-changed
 
     private String pattern;