1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "i18n_unicode_addon.h"
17
18 #include "character.h"
19 #include "i18n_hilog.h"
20 #include "variable_converter.h"
21
22 using namespace OHOS;
23 using namespace Global;
24 using namespace I18n;
25
IsDigitAddon(ani_env * env,ani_object object,ani_string ch)26 ani_boolean I18nUnicodeAddon::IsDigitAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
27 {
28 return IsDigit(VariableConverter::AniStrToString(env, ch));
29 }
30
IsSpaceCharAddon(ani_env * env,ani_object object,ani_string ch)31 ani_boolean I18nUnicodeAddon::IsSpaceCharAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
32 {
33 return IsSpaceChar(VariableConverter::AniStrToString(env, ch));
34 }
35
IsWhitespaceAddon(ani_env * env,ani_object object,ani_string ch)36 ani_boolean I18nUnicodeAddon::IsWhitespaceAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
37 {
38 return IsWhiteSpace(VariableConverter::AniStrToString(env, ch));
39 }
40
IsRTLAddon(ani_env * env,ani_object object,ani_string ch)41 ani_boolean I18nUnicodeAddon::IsRTLAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
42 {
43 return IsRTLCharacter(VariableConverter::AniStrToString(env, ch));
44 }
45
IsIdeographAddon(ani_env * env,ani_object object,ani_string ch)46 ani_boolean I18nUnicodeAddon::IsIdeographAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
47 {
48 return IsIdeoGraphic(VariableConverter::AniStrToString(env, ch));
49 }
50
IsLetterAddon(ani_env * env,ani_object object,ani_string ch)51 ani_boolean I18nUnicodeAddon::IsLetterAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
52 {
53 return IsLetter(VariableConverter::AniStrToString(env, ch));
54 }
55
IsLowerCaseAddon(ani_env * env,ani_object object,ani_string ch)56 ani_boolean I18nUnicodeAddon::IsLowerCaseAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
57 {
58 return IsLowerCase(VariableConverter::AniStrToString(env, ch));
59 }
60
IsUpperCaseAddon(ani_env * env,ani_object object,ani_string ch)61 ani_boolean I18nUnicodeAddon::IsUpperCaseAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
62 {
63 return IsUpperCase(VariableConverter::AniStrToString(env, ch));
64 }
65
GetTypeAddon(ani_env * env,ani_object object,ani_string ch)66 ani_string I18nUnicodeAddon::GetTypeAddon(ani_env *env, [[maybe_unused]] ani_object object, ani_string ch)
67 {
68 std::string type = GetType(VariableConverter::AniStrToString(env, ch));
69 return VariableConverter::StringToAniStr(env, type);
70 }
71
BindContextUnicode(ani_env * env)72 ani_status I18nUnicodeAddon::BindContextUnicode(ani_env *env)
73 {
74 static const char *className = "L@ohos/i18n/i18n/Unicode;";
75 ani_class cls;
76 if (ANI_OK != env->FindClass(className, &cls)) {
77 HILOG_ERROR_I18N("Find class '%{public}s' failed", className);
78 return ANI_ERROR;
79 }
80
81 std::array methods = {
82 ani_native_function { "isDigit", nullptr, reinterpret_cast<void *>(IsDigitAddon) },
83 ani_native_function { "isSpaceChar", nullptr, reinterpret_cast<void *>(IsSpaceCharAddon) },
84 ani_native_function { "isWhitespace", nullptr, reinterpret_cast<void *>(IsWhitespaceAddon) },
85 ani_native_function { "isRTL", nullptr, reinterpret_cast<void *>(IsRTLAddon) },
86 ani_native_function { "isIdeograph", nullptr, reinterpret_cast<void *>(IsIdeographAddon) },
87 ani_native_function { "isLetter", nullptr, reinterpret_cast<void *>(IsLetterAddon) },
88 ani_native_function { "isLowerCase", nullptr, reinterpret_cast<void *>(IsLowerCaseAddon) },
89 ani_native_function { "isUpperCase", nullptr, reinterpret_cast<void *>(IsUpperCaseAddon) },
90 ani_native_function { "getType", nullptr, reinterpret_cast<void *>(GetTypeAddon) },
91 };
92
93 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
94 HILOG_ERROR_I18N("Cannot bind native methods to '%{public}s'", className);
95 return ANI_ERROR;
96 };
97
98 return ANI_OK;
99 }
100