@react-native-ohos/react-native-text-input-mask 3.1.6-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +134 -0
  4. package/dist/babel.config.d.ts +7 -0
  5. package/dist/babel.config.js +16 -0
  6. package/dist/babel.config.js.map +1 -0
  7. package/dist/index.d.ts +138 -0
  8. package/dist/index.js +92 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/src/RNNativeTextInputMask.d.ts +129 -0
  11. package/dist/src/RNNativeTextInputMask.js +8 -0
  12. package/dist/src/RNNativeTextInputMask.js.map +1 -0
  13. package/dist/src/index.d.ts +8 -0
  14. package/dist/src/index.harmony.d.ts +127 -0
  15. package/dist/src/index.harmony.js +23 -0
  16. package/dist/src/index.harmony.js.map +1 -0
  17. package/dist/src/index.js +27 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/tsconfig.tsbuildinfo +1 -0
  20. package/harmony/text_input_mask/BuildProfile.ets +17 -0
  21. package/harmony/text_input_mask/Index.ets +7 -0
  22. package/harmony/text_input_mask/build-profile.json5 +31 -0
  23. package/harmony/text_input_mask/consumer-rules.txt +0 -0
  24. package/harmony/text_input_mask/hvigorfile.ts +6 -0
  25. package/harmony/text_input_mask/obfuscation-rules.txt +23 -0
  26. package/harmony/text_input_mask/oh-package.json5 +11 -0
  27. package/harmony/text_input_mask/src/main/cpp/CMakeLists.txt +9 -0
  28. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.cpp +415 -0
  29. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.h +93 -0
  30. package/harmony/text_input_mask/src/main/cpp/RNTextInputMaskPackage.h +32 -0
  31. package/harmony/text_input_mask/src/main/cpp/common/Compiler.h +175 -0
  32. package/harmony/text_input_mask/src/main/cpp/common/FormatError.h +23 -0
  33. package/harmony/text_input_mask/src/main/cpp/common/FormatSanitizer.h +231 -0
  34. package/harmony/text_input_mask/src/main/cpp/common/Mask.h +378 -0
  35. package/harmony/text_input_mask/src/main/cpp/common/RTLMask.h +79 -0
  36. package/harmony/text_input_mask/src/main/cpp/common/model/AffinityCalculationStrategy.h +58 -0
  37. package/harmony/text_input_mask/src/main/cpp/common/model/CaretString.h +76 -0
  38. package/harmony/text_input_mask/src/main/cpp/common/model/CaretStringIterator.h +58 -0
  39. package/harmony/text_input_mask/src/main/cpp/common/model/Next.h +25 -0
  40. package/harmony/text_input_mask/src/main/cpp/common/model/Notation.h +25 -0
  41. package/harmony/text_input_mask/src/main/cpp/common/model/RTLCaretStringIterator.h +23 -0
  42. package/harmony/text_input_mask/src/main/cpp/common/model/State.h +302 -0
  43. package/harmony/text_input_mask/src/main/cpp/common/model/common.h +95 -0
  44. package/harmony/text_input_mask/src/main/ets/RNTextInputMaskPackage.ts +29 -0
  45. package/harmony/text_input_mask/src/main/ets/RNTextInputMaskTurboModle.ts +33 -0
  46. package/harmony/text_input_mask/src/main/module.json5 +11 -0
  47. package/harmony/text_input_mask/src/main/resources/base/element/string.json +8 -0
  48. package/harmony/text_input_mask/src/main/resources/en_US/element/string.json +8 -0
  49. package/harmony/text_input_mask/src/main/resources/zh_CN/element/string.json +8 -0
  50. package/harmony/text_input_mask/ts.ts +8 -0
  51. package/harmony/text_input_mask.har +0 -0
  52. package/index.tsx +258 -0
  53. package/package.json +50 -0
  54. package/src/RNNativeTextInputMask.ts +143 -0
  55. package/src/index.harmony.ts +147 -0
  56. package/src/index.ts +36 -0
@@ -0,0 +1,175 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+ #include <string>
9
+ #include <vector>
10
+ #include <string>
11
+ #include <memory>
12
+ #include "model/common.h"
13
+ #include "model/Notation.h"
14
+ #include "model/State.h"
15
+ #include "FormatError.h"
16
+ #include "FormatSanitizer.h"
17
+ #include <glog/logging.h>
18
+
19
+ namespace TinpMask {
20
+ class FormatSanitizer;
21
+ class Compiler {
22
+ private:
23
+ std::vector<Notation> customNotations;
24
+
25
+ public:
26
+ Compiler(const std::vector<Notation> &notations) : customNotations(notations) {}
27
+
28
+ std::shared_ptr<State> compile(const std::string &formatString) {
29
+ FormatSanitizer sanitizer;
30
+ std::string sanitizedString = sanitizer.sanitize(formatString);
31
+ return compile(sanitizedString, false, false, '\0');
32
+ }
33
+
34
+ std::shared_ptr<State> compile(const std::string &formatString, bool valuable, bool fixed, char lastCharacter) {
35
+ if (formatString.empty()) {
36
+ return std::make_shared<EOLState>();
37
+ }
38
+
39
+ char ch = formatString.front();
40
+ switch (ch) {
41
+ case '[':
42
+ if (lastCharacter != '\\') {
43
+ return compile(formatString.substr(1), true, false, ch);
44
+ }
45
+ break;
46
+
47
+ case '{':
48
+ if (lastCharacter != '\\') {
49
+ return compile(formatString.substr(1), false, true, ch);
50
+ }
51
+ break;
52
+
53
+ case ']':
54
+ case '}':
55
+ if (lastCharacter != '\\') {
56
+ return compile(formatString.substr(1), false, false, ch);
57
+ }
58
+ break;
59
+
60
+ case '\\':
61
+ if (lastCharacter != '\\') {
62
+ return compile(formatString.substr(1), valuable, fixed, ch);
63
+ }
64
+ break;
65
+
66
+ default:
67
+ break;
68
+ }
69
+
70
+ if (valuable) {
71
+ switch (ch) {
72
+ case '0':
73
+ return std::make_shared<ValueState>(
74
+ this->compile(formatString.substr(1), true, false, ch),
75
+ // std::static_pointer_cast<ValueState::StateType>( std::make_shared<ValueState::Numeric>())
76
+ std::make_shared<ValueState::Numeric>());
77
+
78
+ case 'A':
79
+ return std::make_shared<ValueState>(this->compile(formatString.substr(1), true, false, ch),
80
+ std::make_shared<ValueState::Literal>());
81
+
82
+ case '_':
83
+ return std::make_shared<ValueState>(this->compile(formatString.substr(1), true, false, ch),
84
+ std::make_shared<ValueState::AlphaNumeric>());
85
+
86
+ case '...':
87
+ return std::make_unique<ValueState>(determineInheritedType(lastCharacter));
88
+
89
+ case '9':
90
+ return std::make_shared<OptionalValueState>(this->compile(formatString.substr(1), true, false, ch),
91
+ std::make_shared<OptionalValueState::Numeric>()
92
+
93
+ );
94
+
95
+ case 'a':
96
+ return std::make_shared<OptionalValueState>(this->compile(formatString.substr(1), true, false, ch),
97
+ std::make_shared<OptionalValueState::Literal>());
98
+
99
+ case '-':
100
+ return std::make_shared<OptionalValueState>(this->compile(formatString.substr(1), true, false, ch),
101
+ std::make_shared<OptionalValueState::AlphaNumeric>());
102
+
103
+ default:
104
+ return compileWithCustomNotations(ch, formatString);
105
+ }
106
+ }
107
+ if (fixed) {
108
+ return std::make_shared<FixedState>(compile(formatString.substr(1), false, true, ch), ch);
109
+ }
110
+ return std::make_shared<FreeState>(compile(formatString.substr(1), false, false, ch), ch);
111
+ }
112
+
113
+ std::unique_ptr<State> compileWithCustomNotations(char c, const std::string &str) {
114
+ DLOG(ERROR) << "====== compileWithCustomNotations customNotations size: " << customNotations.size();
115
+ for (const auto &customNotation : customNotations) {
116
+ if (customNotation.character == c) {
117
+ std::shared_ptr<State> compiledState = compile(str.substr(1), true, false, c);
118
+ if (customNotation.isOptional) {
119
+ DLOG(ERROR) << "====== compileWithCustomNotations customNotations isOptional :" << customNotation.isOptional;
120
+ return std::make_unique<OptionalValueState>(
121
+ std::move(compiledState),
122
+ std::make_shared<OptionalValueState::Custom>(c, customNotation.characterSet));
123
+ } else {
124
+ DLOG(ERROR) << "======= compileWithCustomNotations customNotations isOptional :" << customNotation.isOptional;
125
+ return std::make_unique<ValueState>(
126
+ std::move(compiledState), std::make_shared<ValueState::Custom>(c, customNotation.characterSet));
127
+ }
128
+ }
129
+ }
130
+ DLOG(INFO) << "compileWithCustomNotations No Match Found ";
131
+ throw FormatError("compileWithCustomNotations No Match Found");
132
+ }
133
+ std::shared_ptr<ValueState::ValueStateType> determineInheritedType(std::optional<char> lastCharacter) {
134
+ if (!lastCharacter.has_value()) {
135
+ throw FormatError(); // 处理空字符情况,抛出异常
136
+ }
137
+ char character = lastCharacter.value();
138
+ switch (character) {
139
+ case '0':
140
+ case '9':
141
+ return std::make_shared<ValueState::Numeric>();
142
+
143
+ case 'A':
144
+ case 'a':
145
+ return std::make_shared<ValueState::Literal>();
146
+
147
+ case '_':
148
+ case '-':
149
+ case '...':
150
+ case '[':
151
+ return std::make_shared<ValueState::AlphaNumeric>();
152
+
153
+ default:
154
+ return determineTypeWithCustomNotations(lastCharacter);
155
+ }
156
+ }
157
+
158
+ std::shared_ptr<ValueState::ValueStateType> determineTypeWithCustomNotations(std::optional<char> lastCharacter) {
159
+ if (!lastCharacter.has_value()) {
160
+ throw FormatError(); // 处理空字符情况,抛出异常
161
+ }
162
+ DLOG(INFO) << "======determineTypeWithCustomNotations customNotations size: "<<customNotations.size();
163
+ char character = lastCharacter.value();
164
+ for (const auto &customNotation : customNotations) {
165
+ if (customNotation.character == character) {
166
+ // 返回 Custom 状态
167
+ return std::make_shared<ValueState::Custom>(lastCharacter.value(),
168
+ customNotation.characterSet); // 可以根据需要调整返回内容
169
+ }
170
+ }
171
+ DLOG(INFO) << "determineTypeWithCustomNotations No Match Found ";
172
+ throw FormatError("determineTypeWithCustomNotations No Match Found"); // 未找到匹配项,抛出异常
173
+ }
174
+ };
175
+ } // namespace TinpMask
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+ // 定义 FormatError 类
9
+ #include <string>
10
+
11
+ namespace TinpMask {
12
+ class FormatError : public std::exception {
13
+ public:
14
+ // 构造函数接受错误消息
15
+ explicit FormatError(const std::string &message = "An error occurred") : msg(message) {}
16
+
17
+ // 重写 what() 方法,返回错误消息
18
+ const char *what() const noexcept { return msg.c_str(); }
19
+
20
+ private:
21
+ std::string msg; // 存储错误消息
22
+ };
23
+ } // namespace TinpMask
@@ -0,0 +1,231 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+ #include <iostream>
9
+ #include <vector>
10
+ #include <string>
11
+ #include <algorithm>
12
+ #include "Compiler.h"
13
+
14
+ namespace TinpMask {
15
+
16
+
17
+ class Compiler;
18
+ class FormatSanitizer {
19
+ public:
20
+ std::string sanitize(const std::string &formatString) {
21
+ checkOpenBraces(formatString);
22
+ std::vector<std::string> blocks = divideBlocksWithMixedCharacters(getFormatBlocks(formatString));
23
+ return sortFormatBlocks(blocks);
24
+ }
25
+
26
+ std::vector<std::string> getFormatBlocks(const std::string &formatString) {
27
+ std::vector<std::string> blocks;
28
+ std::string currentBlock;
29
+ bool escape = false;
30
+
31
+ for (char ch : formatString) {
32
+ if (ch == '\\') {
33
+ if (!escape) {
34
+ escape = true;
35
+ currentBlock += ch;
36
+ continue;
37
+ }
38
+ }
39
+
40
+ if ((ch == '[' || ch == '{') && !escape) {
41
+ if (!currentBlock.empty()) {
42
+ blocks.push_back(currentBlock);
43
+ }
44
+ currentBlock.clear();
45
+ }
46
+
47
+ currentBlock += ch;
48
+
49
+ if ((ch == ']' || ch == '}') && !escape) {
50
+ blocks.push_back(currentBlock);
51
+ currentBlock.clear();
52
+ }
53
+
54
+ escape = false;
55
+ }
56
+
57
+ if (!currentBlock.empty()) {
58
+ blocks.push_back(currentBlock);
59
+ }
60
+
61
+ return blocks;
62
+ }
63
+
64
+
65
+ public:
66
+ std::vector<std::string> divideBlocksWithMixedCharacters(const std::vector<std::string> &blocks) {
67
+ std::vector<std::string> resultingBlocks;
68
+
69
+ for (const auto &block : blocks) {
70
+ if (!block.empty() && block.substr(0, 1) == "[") { // 使用 substr 来检查开头
71
+ std::string blockBuffer;
72
+ for (char blockCharacter : block) {
73
+ if (blockCharacter == '[') {
74
+ blockBuffer += blockCharacter;
75
+ continue;
76
+ }
77
+
78
+ if (blockCharacter == ']' && !endsWithEscapeCharacter(blockBuffer)) {
79
+ blockBuffer += blockCharacter;
80
+ resultingBlocks.push_back(blockBuffer);
81
+ break;
82
+ }
83
+
84
+ if (isDigit(blockCharacter)) {
85
+ if (containsAny(blockBuffer, {'A', 'a', '-', '_'})) {
86
+ blockBuffer += "]";
87
+ resultingBlocks.push_back(blockBuffer);
88
+ blockBuffer = "[" + std::string(1, blockCharacter);
89
+ continue;
90
+ }
91
+ }
92
+
93
+ if (isAlpha(blockCharacter)) {
94
+ if (containsAny(blockBuffer, {'0', '9', '-', '_'})) {
95
+ blockBuffer += "]";
96
+ resultingBlocks.push_back(blockBuffer);
97
+ blockBuffer = "[" + std::string(1, blockCharacter);
98
+ continue;
99
+ }
100
+ }
101
+
102
+ if (isSpecialCharacter(blockCharacter)) {
103
+ if (containsAny(blockBuffer, {'0', '9', 'A', 'a'})) {
104
+ blockBuffer += "]";
105
+ resultingBlocks.push_back(blockBuffer);
106
+ blockBuffer = "[" + std::string(1, blockCharacter);
107
+ continue;
108
+ }
109
+ }
110
+
111
+ blockBuffer += blockCharacter;
112
+ }
113
+ } else {
114
+ resultingBlocks.push_back(block);
115
+ }
116
+ }
117
+
118
+ return resultingBlocks;
119
+ }
120
+
121
+ private:
122
+ bool endsWithEscapeCharacter(const std::string &str) const { return !str.empty() && str.back() == '\\'; }
123
+
124
+ bool isDigit(char ch) const { return ch >= '0' && ch <= '9'; }
125
+
126
+ bool isAlpha(char ch) const { return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); }
127
+
128
+ bool isSpecialCharacter(char ch) const { return ch == '-' || ch == '_'; }
129
+
130
+ bool containsAny(const std::string &str, const std::vector<char> &characters) const {
131
+ for (char c : characters) {
132
+ if (str.find(c) != std::string::npos) {
133
+ return true;
134
+ }
135
+ }
136
+ return false;
137
+ }
138
+
139
+ public:
140
+ std::string sortFormatBlocks(const std::vector<std::string> &blocks) {
141
+ std::vector<std::string> sortedBlocks;
142
+
143
+ for (const auto &block : blocks) {
144
+ std::string sortedBlock;
145
+
146
+ if (startsWith(block, "[")) {
147
+ std::string cleanedBlock = block.substr(1, block.size() - 2); // Remove the surrounding brackets
148
+ if (cleanedBlock.find("0") != std::string::npos || cleanedBlock.find("9") != std::string::npos ||
149
+ cleanedBlock.find("a") != std::string::npos || cleanedBlock.find("A") != std::string::npos) {
150
+ sortedBlock = "[" + sortString(cleanedBlock) + "]";
151
+ } else {
152
+ cleanedBlock = replaceChars(cleanedBlock);
153
+ sortedBlock = "[" + sortString(cleanedBlock) + "]";
154
+ sortedBlock = replaceCharsBack(sortedBlock);
155
+ }
156
+ } else {
157
+ sortedBlock = block;
158
+ }
159
+
160
+ sortedBlocks.push_back(sortedBlock);
161
+ }
162
+
163
+ return join(sortedBlocks); // Ensure this is returning std::vector<std::string>
164
+ }
165
+
166
+ void checkOpenBraces(const std::string &inputString) {
167
+ bool escape = false;
168
+ bool squareBraceOpen = false;
169
+ bool curlyBraceOpen = false;
170
+
171
+ for (const char &ch : inputString) {
172
+ if (ch == '\\') {
173
+ escape = !escape;
174
+ continue;
175
+ }
176
+
177
+ if (ch == '[') {
178
+ if (squareBraceOpen) {
179
+ throw FormatError();
180
+ }
181
+ squareBraceOpen = !escape;
182
+ }
183
+
184
+ if (ch == ']' && !escape) {
185
+ squareBraceOpen = false;
186
+ }
187
+
188
+ if (ch == '{') {
189
+ if (curlyBraceOpen) {
190
+ throw FormatError();
191
+ }
192
+ curlyBraceOpen = !escape;
193
+ }
194
+
195
+ if (ch == '}' && !escape) {
196
+ curlyBraceOpen = false;
197
+ }
198
+ escape = false;
199
+ }
200
+ }
201
+
202
+ private:
203
+ bool startsWith(const std::string &str, const std::string &prefix) const { return str.rfind(prefix, 0) == 0; }
204
+
205
+ std::string sortString(std::string str) {
206
+ std::sort(str.begin(), str.end());
207
+ return str;
208
+ }
209
+
210
+ std::string replaceChars(const std::string &str) {
211
+ std::string newStr = str;
212
+ std::replace(newStr.begin(), newStr.end(), '_', 'A');
213
+ std::replace(newStr.begin(), newStr.end(), '-', 'a');
214
+ return newStr;
215
+ }
216
+
217
+ std::string replaceCharsBack(const std::string &str) {
218
+ std::string newStr = str;
219
+ std::replace(newStr.begin(), newStr.end(), 'A', '_');
220
+ std::replace(newStr.begin(), newStr.end(), 'a', '-');
221
+ return newStr;
222
+ }
223
+ std::string join(const std::vector<std::string> &strings) {
224
+ std::string result;
225
+ for (const auto &str : strings) {
226
+ result += str; // 将所有块连接成一个字符串
227
+ }
228
+ return result;
229
+ }
230
+ };
231
+ } // namespace TinpMask