• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetRangeOfSameType(const std::string & str,int32_t position)51 inline TextSelection GetRangeOfSameType(const std::string& str, int32_t position)
52 {
53     TextSelection selection = TextSelection(position, position);
54     auto wstring = StringUtils::ToWstring(str);
55     int32_t length = static_cast<int32_t>(wstring.size());
56     CharType type = GetCharType(wstring, position);
57     if (type != CharType::NUMBER && type != CharType::LETTER) {
58         selection.extentOffset = position + 1;
59         return selection;
60     }
61 
62     // find start position.
63     for (int32_t i = position - 1; i >= 0; --i) {
64         if (GetCharType(wstring, i) != type) {
65             selection.baseOffset = i + 1;
66             break;
67         }
68         if (i == 0) {
69             selection.baseOffset = 0;
70         }
71     }
72 
73     // find end position.
74     for (int32_t i = position; i < length; ++i) {
75         if (GetCharType(wstring, i) != type) {
76             selection.extentOffset = i;
77             break;
78         }
79         if (i == (length - 1)) {
80             selection.extentOffset = length;
81         }
82     }
83     return selection;
84 }
85 
86 } // namespace OHOS::Ace::TextUtils
87 
88 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_UTILS_H
89