• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "string_util.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace SPText {
21 
22 static constexpr size_t SURROGATE_LENGTH = 2;
23 static constexpr uint16_t LOW_SURROGATE_MIN = 0xDC00;
24 static constexpr uint16_t LOW_SURROGATE_MAX = 0xDFFF;
25 static constexpr uint16_t HIGHT_SURROGATE_MIN = 0xD800;
26 static constexpr uint16_t HIGHT_SURROGATE_MAX = 0xDBFF;
27 static constexpr uint16_t UTF16_REPLACEMENT_CHARACTER = 0xFFFD;
28 
IsUTF16LowSurrogate(uint16_t ch)29 bool Utf16Utils::IsUTF16LowSurrogate(uint16_t ch)
30 {
31     return ch >= LOW_SURROGATE_MIN && ch <= LOW_SURROGATE_MAX;
32 }
33 
IsUTF16HighSurrogate(uint16_t ch)34 bool Utf16Utils::IsUTF16HighSurrogate(uint16_t ch)
35 {
36     return ch >= HIGHT_SURROGATE_MIN && ch <= HIGHT_SURROGATE_MAX;
37 }
38 
HandleIncompleteSurrogatePairs(std::u16string & str)39 void Utf16Utils::HandleIncompleteSurrogatePairs(std::u16string& str)
40 {
41     size_t i = 0;
42     size_t length = str.size();
43 
44     while (i < length) {
45         char16_t ch = str[i];
46         if (IsUTF16HighSurrogate(ch)) {
47             if (i + 1 < length && IsUTF16LowSurrogate(str[i + 1])) {
48                 i += SURROGATE_LENGTH;
49                 continue;
50             }
51             str[i] = UTF16_REPLACEMENT_CHARACTER;
52         } else if (IsUTF16LowSurrogate(ch)) {
53             if (i == 0 || !IsUTF16HighSurrogate(str[i - 1])) {
54                 str[i] = UTF16_REPLACEMENT_CHARACTER;
55             }
56         }
57 
58         ++i;
59     }
60 }
61 }
62 }
63 }