1 /*
2 * Copyright (c) 2022 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_CORE_COMPONENTS_TEXT_TEXT_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_UTILS_H
18
19 #include <cmath>
20 #include <string>
21
22 #include "base/utils/utils.h"
23 #include "base/utils/string_utils.h"
24 #include "core/common/ime/text_selection.h"
25
26 namespace OHOS::Ace::TextUtils {
27
28 enum class CharType {
29 NUMBER = 0,
30 LETTER,
31 OTHERS,
32 NONE
33 };
34
GetCharType(const std::wstring & value,int32_t position)35 inline CharType GetCharType(const std::wstring& value, int32_t position)
36 {
37 if (position < 0 || position >= static_cast<int32_t>(value.size())) {
38 return CharType::NONE;
39 }
40
41 CharType result = CharType::OTHERS;
42 auto positionChar = value[position];
43 if (positionChar >= '0' && positionChar <= '9') {
44 result = CharType::NUMBER;
45 } else if ((positionChar >= 'a' && positionChar <= 'z') || (positionChar >= 'A' && positionChar <= 'Z')) {
46 result = CharType::LETTER;
47 }
48 return result;
49 }
50
IsSameCharType(const std::wstring & wstring,int32_t position,CharType type)51 inline bool IsSameCharType(const std::wstring& wstring, int32_t position, CharType type)
52 {
53 return GetCharType(wstring, position) == type;
54 }
55
FindSameCharTypeStart(const std::wstring & wstring,int32_t position,CharType type)56 inline int32_t FindSameCharTypeStart(const std::wstring& wstring, int32_t position, CharType type)
57 {
58 for (int32_t i = position - 1; i >= 0; --i) {
59 if (!IsSameCharType(wstring, i, type)) {
60 return i + 1;
61 }
62 }
63 return 0;
64 }
65
FindSameCharTypeEnd(const std::wstring & wstring,int32_t position,int32_t length,CharType type)66 inline int32_t FindSameCharTypeEnd(const std::wstring& wstring, int32_t position, int32_t length, CharType type)
67 {
68 for (int32_t i = position; i < length; ++i) {
69 if (!IsSameCharType(wstring, i, type)) {
70 return i;
71 }
72 }
73 return length;
74 }
75
GetRangeOfSameType(const std::string & str,int32_t position)76 inline TextSelection GetRangeOfSameType(const std::string& str, int32_t position)
77 {
78 TextSelection selection = TextSelection(position, position);
79 auto wstring = StringUtils::ToWstring(str);
80 int32_t length = static_cast<int32_t>(wstring.size());
81 CharType type = GetCharType(wstring, position);
82 if (type != CharType::NUMBER && type != CharType::LETTER) {
83 selection.extentOffset = position + 1;
84 return selection;
85 }
86
87 selection.baseOffset = FindSameCharTypeStart(wstring, position, type);
88 selection.extentOffset = FindSameCharTypeEnd(wstring, position, length, type);
89 return selection;
90 }
91
92 } // namespace OHOS::Ace::TextUtils
93
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_UTILS_H
95