xref: /aosp_15_r20/libcore/luni/src/main/native/libcore_icu_ICU.cpp (revision 89a6322812dc8573315e60046e7959c50dad91d4)
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_NDEBUG 1
18 #define LOG_TAG "ICU"
19 
20 #include <memory>
21 #include <vector>
22 
23 #include <log/log.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <nativehelper/ScopedUtfChars.h>
26 #include <nativehelper/jni_macros.h>
27 #include <nativehelper/toStringArray.h>
28 
29 #include "IcuUtilities.h"
30 #include "JniException.h"
31 #include "ScopedIcuULoc.h"
32 #include "unicode/char16ptr.h"
33 #include "unicode/uchar.h"
34 #include "unicode/uloc.h"
35 #include "unicode/ulocdata.h"
36 #include "unicode/ustring.h"
37 #include "unicode/uversion.h"
38 
39 #define U_ICUDATA_CURR U_ICUDATA_NAME "-" "curr"
40 
ICU_getScript(JNIEnv * env,jclass,jstring javaLocaleName)41 static jstring ICU_getScript(JNIEnv* env, jclass, jstring javaLocaleName) {
42   ScopedIcuULoc icuLocale(env, javaLocaleName);
43   if (!icuLocale.valid()) {
44     return NULL;
45   }
46   // Normal script part is 4-char long. Being conservative for allocation size
47   // because if the locale contains script part, it should not be longer than the locale itself.
48   int32_t capacity = std::max(ULOC_SCRIPT_CAPACITY, icuLocale.locale_length() + 1);
49   auto buffer = std::make_unique<char[]>(capacity);
50   UErrorCode status = U_ZERO_ERROR;
51   uloc_getScript(icuLocale.locale(), buffer.get(), capacity, &status);
52   if (U_FAILURE(status)) {
53     return NULL;
54   }
55   return env->NewStringUTF(buffer.get());
56 }
57 
ICU_getISO3Country(JNIEnv * env,jclass,jstring javaLanguageTag)58 static jstring ICU_getISO3Country(JNIEnv* env, jclass, jstring javaLanguageTag) {
59   ScopedIcuULoc icuLocale(env, javaLanguageTag);
60   if (!icuLocale.valid()) {
61     return NULL;
62   }
63   return env->NewStringUTF(uloc_getISO3Country(icuLocale.locale()));
64 }
65 
ICU_getISO3Language(JNIEnv * env,jclass,jstring javaLanguageTag)66 static jstring ICU_getISO3Language(JNIEnv* env, jclass, jstring javaLanguageTag) {
67   ScopedIcuULoc icuLocale(env, javaLanguageTag);
68   if (!icuLocale.valid()) {
69     return NULL;
70   }
71   return env->NewStringUTF(uloc_getISO3Language(icuLocale.locale()));
72 }
73 
ICU_getISOCountriesNative(JNIEnv * env,jclass)74 static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) {
75     return toStringArray(env, uloc_getISOCountries());
76 }
77 
ICU_getISOLanguagesNative(JNIEnv * env,jclass)78 static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) {
79     return toStringArray(env, uloc_getISOLanguages());
80 }
81 
ICU_getAvailableLocalesNative(JNIEnv * env,jclass)82 static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) {
83     return toStringArray(env, uloc_countAvailable, uloc_getAvailable);
84 }
85 
ICU_getDefaultLocale(JNIEnv * env,jclass)86 static jstring ICU_getDefaultLocale(JNIEnv* env, jclass) {
87   return env->NewStringUTF(uloc_getDefault());
88 }
89 
versionString(JNIEnv * env,const UVersionInfo & version)90 static jstring versionString(JNIEnv* env, const UVersionInfo& version) {
91     char versionString[U_MAX_VERSION_STRING_LENGTH];
92     u_versionToString(const_cast<UVersionInfo&>(version), &versionString[0]);
93     return env->NewStringUTF(versionString);
94 }
95 
ICU_getCldrVersion(JNIEnv * env,jclass)96 static jstring ICU_getCldrVersion(JNIEnv* env, jclass) {
97   UErrorCode status = U_ZERO_ERROR;
98   UVersionInfo cldrVersion;
99   ulocdata_getCLDRVersion(cldrVersion, &status);
100   return versionString(env, cldrVersion);
101 }
102 
ICU_getIcuVersion(JNIEnv * env,jclass)103 static jstring ICU_getIcuVersion(JNIEnv* env, jclass) {
104     UVersionInfo icuVersion;
105     u_getVersion(icuVersion);
106     return versionString(env, icuVersion);
107 }
108 
ICU_getUnicodeVersion(JNIEnv * env,jclass)109 static jstring ICU_getUnicodeVersion(JNIEnv* env, jclass) {
110     UVersionInfo unicodeVersion;
111     u_getUnicodeVersion(unicodeVersion);
112     return versionString(env, unicodeVersion);
113 }
114 
115 static JNINativeMethod gMethods[] = {
116     NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"),
117     NATIVE_METHOD(ICU, getCldrVersion, "()Ljava/lang/String;"),
118     NATIVE_METHOD(ICU, getDefaultLocale, "()Ljava/lang/String;"),
119     NATIVE_METHOD(ICU, getIcuVersion, "()Ljava/lang/String;"),
120     NATIVE_METHOD(ICU, getISO3Country, "(Ljava/lang/String;)Ljava/lang/String;"),
121     NATIVE_METHOD(ICU, getISO3Language, "(Ljava/lang/String;)Ljava/lang/String;"),
122     NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"),
123     NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"),
124     NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"),
125     NATIVE_METHOD(ICU, getUnicodeVersion, "()Ljava/lang/String;"),
126 };
127 
register_libcore_icu_ICU(JNIEnv * env)128 void register_libcore_icu_ICU(JNIEnv* env) {
129   jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
130 }
131 
unregister_libcore_icu_ICU()132 void unregister_libcore_icu_ICU() {
133   // Skip unregistering JNI methods explicitly, class unloading takes care of
134   // it.
135 }
136