1 /* 2 * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTF_HELPER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTF_HELPER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <vector> 22 #include <string> 23 24 #include "base/utils/macros.h" 25 26 namespace OHOS::Ace::UtfUtils { 27 ACE_EXPORT extern const std::string DEFAULT_STR; 28 ACE_EXPORT extern const std::u16string DEFAULT_U16STR; 29 ACE_EXPORT extern const std::u32string DEFAULT_U32STR; 30 ACE_EXPORT extern const std::wstring DEFAULT_WSTR; 31 32 uint32_t DecodeUTF16(uint16_t const *utf16, size_t len, size_t *index); 33 34 size_t EncodeUTF8(uint32_t codepoint, uint8_t* utf8, size_t len, size_t index); 35 36 size_t Utf16ToUtf8Size(const uint16_t *utf16, uint32_t length); 37 38 size_t ConvertRegionUtf16ToUtf8(const uint16_t *utf16In, uint8_t *utf8Out, size_t utf16Len, 39 size_t utf8Len, size_t start); 40 41 size_t DebuggerConvertRegionUtf16ToUtf8(const uint16_t *utf16In, uint8_t *utf8Out, size_t utf16Len, size_t utf8Len, 42 size_t start); 43 44 uint32_t HandleAndDecodeInvalidUTF16(uint16_t const *utf16, size_t len, size_t *index); 45 46 size_t Utf8ToUtf16Size(const uint8_t *utf8, size_t utf8Len); 47 48 size_t ConvertRegionUtf8ToUtf16(const uint8_t *utf8In, uint16_t *utf16Out, size_t utf8Len, size_t utf16Len); 49 50 std::u16string ACE_FORCE_EXPORT Str8ToStr16(const std::string& str); 51 52 std::u16string ACE_FORCE_EXPORT Str8DebugToStr16(const std::string& str); 53 54 std::string ACE_FORCE_EXPORT Str16ToStr8(const std::u16string& str); 55 56 std::string ACE_FORCE_EXPORT Str16DebugToStr8(const std::u16string& str); 57 58 std::u32string ACE_FORCE_EXPORT Str16ToStr32(const std::u16string& str); 59 60 std::u16string ACE_FORCE_EXPORT Str32ToStr16(const std::u32string& str); 61 62 void HandleInvalidUTF16(uint16_t* utf16In, size_t utf16Len, size_t start); 63 64 bool IsIndexInPairedSurrogates(int32_t index, const std::u16string& utf16); 65 66 } // namespace OHOS::Ace::UtfUtils 67 68 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTF_HELPER_H