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 #include "IntlCommon.h"
16 #include "libpandabase/macros.h"
17 #include "stdlib_ani_helpers.h"
18 #include <cassert>
19
20 namespace ark::ets::stdlib::intl {
21
StdStrToUnicode(const std::string & str)22 icu::UnicodeString StdStrToUnicode(const std::string &str)
23 {
24 return icu::UnicodeString::fromUTF8(icu::StringPiece(str));
25 }
26
StdStrToAni(ani_env * env,const std::string & str)27 ani_string StdStrToAni(ani_env *env, const std::string &str)
28 {
29 return CreateUtf8String(env, str.data(), str.size());
30 }
31
UnicodeToAniStr(ani_env * env,icu::UnicodeString & ustr)32 ani_string UnicodeToAniStr(ani_env *env, icu::UnicodeString &ustr)
33 {
34 return CreateUtf16String(env, reinterpret_cast<const uint16_t *>(ustr.getBuffer()), ustr.length());
35 }
36
AniToUnicodeStr(ani_env * env,ani_string aniStr)37 icu::UnicodeString AniToUnicodeStr(ani_env *env, ani_string aniStr)
38 {
39 ani_size aniStrSize = 0;
40 ANI_FATAL_IF_ERROR(env->String_GetUTF16Size(aniStr, &aniStrSize));
41
42 ani_size copiedCharsCount = 0;
43 std::vector<uint16_t> buf(aniStrSize + 1);
44 ANI_FATAL_IF_ERROR(env->String_GetUTF16(aniStr, buf.data(), buf.size(), &copiedCharsCount));
45 ANI_FATAL_IF(copiedCharsCount != aniStrSize);
46
47 return icu::UnicodeString(buf.data(), aniStrSize);
48 }
49
50 } // namespace ark::ets::stdlib::intl
51