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

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 (40) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +15 -134
  4. package/dist/tsconfig.tsbuildinfo +1 -1
  5. package/harmony/text_input_mask/BuildProfile.ets +16 -16
  6. package/harmony/text_input_mask/Index.ets +7 -7
  7. package/harmony/text_input_mask/build-profile.json5 +31 -31
  8. package/harmony/text_input_mask/hvigorfile.ts +6 -6
  9. package/harmony/text_input_mask/obfuscation-rules.txt +22 -22
  10. package/harmony/text_input_mask/oh-package.json5 +11 -11
  11. package/harmony/text_input_mask/src/main/cpp/CMakeLists.txt +9 -9
  12. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.cpp +421 -415
  13. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.h +93 -93
  14. package/harmony/text_input_mask/src/main/cpp/RNTextInputMaskPackage.h +31 -31
  15. package/harmony/text_input_mask/src/main/cpp/common/Compiler.h +174 -174
  16. package/harmony/text_input_mask/src/main/cpp/common/FormatError.h +22 -22
  17. package/harmony/text_input_mask/src/main/cpp/common/FormatSanitizer.h +230 -230
  18. package/harmony/text_input_mask/src/main/cpp/common/Mask.h +377 -377
  19. package/harmony/text_input_mask/src/main/cpp/common/RTLMask.h +78 -78
  20. package/harmony/text_input_mask/src/main/cpp/common/model/AffinityCalculationStrategy.h +57 -57
  21. package/harmony/text_input_mask/src/main/cpp/common/model/CaretString.h +75 -75
  22. package/harmony/text_input_mask/src/main/cpp/common/model/CaretStringIterator.h +58 -58
  23. package/harmony/text_input_mask/src/main/cpp/common/model/Next.h +24 -24
  24. package/harmony/text_input_mask/src/main/cpp/common/model/Notation.h +24 -24
  25. package/harmony/text_input_mask/src/main/cpp/common/model/RTLCaretStringIterator.h +22 -22
  26. package/harmony/text_input_mask/src/main/cpp/common/model/State.h +302 -302
  27. package/harmony/text_input_mask/src/main/cpp/common/model/common.h +94 -94
  28. package/harmony/text_input_mask/src/main/ets/RNTextInputMaskPackage.ts +28 -28
  29. package/harmony/text_input_mask/src/main/ets/RNTextInputMaskTurboModle.ts +32 -32
  30. package/harmony/text_input_mask/src/main/module.json5 +11 -11
  31. package/harmony/text_input_mask/src/main/resources/base/element/string.json +8 -8
  32. package/harmony/text_input_mask/src/main/resources/en_US/element/string.json +8 -8
  33. package/harmony/text_input_mask/src/main/resources/zh_CN/element/string.json +8 -8
  34. package/harmony/text_input_mask/ts.ts +8 -8
  35. package/harmony/text_input_mask.har +0 -0
  36. package/index.tsx +258 -258
  37. package/package.json +48 -50
  38. package/src/RNNativeTextInputMask.ts +142 -142
  39. package/src/index.harmony.ts +147 -147
  40. package/src/index.ts +36 -36
@@ -1,93 +1,93 @@
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
- /**
8
- * This code was generated by "react-native codegen-harmony"
9
- *
10
- * Do not edit this file as changes may cause incorrect behavior and will be
11
- * lost once the code is regenerated.
12
- *
13
- * @generatorVersion: 1
14
- */
15
- #pragma once
16
-
17
- #include "RNOH/ArkTSTurboModule.h"
18
- #include "RNOH/arkui/NativeNodeApi.h"
19
- #include "RNOH/arkui/TextInputNode.h"
20
- #include "common/model/Notation.h"
21
- #include "common/RTLMask.h"
22
- #include "common/model/AffinityCalculationStrategy.h"
23
- using namespace rnoh;
24
- using namespace facebook;
25
- using namespace TinpMask;
26
-
27
- void myEventReceiver(ArkUI_NodeEvent *event);
28
-
29
- namespace rnoh {
30
-
31
- class RNTextInputMask;
32
-
33
- struct MaskParams {
34
- std::string mask; // mask的格式
35
- std::string value; // 待mask的值
36
- std::optional<bool> autocomplete; // 布尔值,表示是否自动完成
37
- MaskParams() = default;
38
- MaskParams(const std::string &m, const std::string &v, std::optional<bool> autoComplete = std::nullopt)
39
- : mask(m), value(v), autocomplete(autoComplete) {} // 构造函数
40
- MaskParams &operator=(const MaskParams &input) {
41
- this->mask = input.mask;
42
- this->value = input.value;
43
- this->autocomplete = input.autocomplete;
44
- return *this;
45
- }
46
- };
47
-
48
- struct MaskOptions {
49
- std::optional<std::vector<std::string>> affineFormats; // 可选的字符串数组
50
- std::optional<std::vector<Notation>> customNotations; // 可选的 Notation 数组
51
- std::optional<std::string> affinityCalculationStrategy; // 可选字符串
52
- std::optional<bool> autocomplete; // 可选布尔值
53
- std::optional<bool> autoskip; // 可选布尔值
54
- std::optional<bool> rightToLeft; // 可选布尔值
55
-
56
- MaskOptions()
57
- : affineFormats(std::vector<std::string>()), customNotations(std::vector<Notation>()),
58
- affinityCalculationStrategy(std::nullopt), autocomplete(true), autoskip(false), rightToLeft(false) {}
59
- MaskOptions(const std::vector<std::string> &formats, const std::vector<Notation> &notations,
60
- const std::string &strategy, bool autoComp, bool autoSkip, bool rtl)
61
- : affineFormats(formats), customNotations(notations),
62
- affinityCalculationStrategy(strategy.empty() ? std::make_optional("WHOLE_STRING") : std::make_optional(strategy)),
63
- autocomplete(autoComp), autoskip(autoSkip), rightToLeft(rtl) {}
64
- };
65
-
66
- typedef struct {
67
- ArkUI_NodeHandle data;
68
- MaskOptions maskOptions;
69
- std::string primaryFormat;
70
- int node;
71
- std::string lastInputText;
72
- RNTextInputMask *instance;
73
- } UserData;
74
-
75
- class JSI_EXPORT RNTextInputMask : public ArkTSTurboModule {
76
- public:
77
- RNTextInputMask(const ArkTSTurboModule::Context ctx, const std::string name);
78
- void setMask(int reactNode, std::string primaryFormat, MaskOptions options);
79
- jsi::Value mask(std::string mask, std::string value, bool autocomplete);
80
- jsi::Value unmask(std::string mask, std::string value, bool autocomplete);
81
- jsi::Value unmaskWithRightToLeft(std::string mask, std::string value, bool autocomplete, bool rightToLeft);
82
- jsi::Runtime *grt = nullptr;
83
- std::shared_ptr<Mask> pickMask(const CaretString &text, MaskOptions maskOptions, std::string primaryMask);
84
-
85
- // 释放资源
86
- ~RNTextInputMask();
87
-
88
- private:
89
- std::unordered_set<UserData *> m_userDatas;
90
- };
91
-
92
-
93
- } // namespace rnoh
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
+ /**
8
+ * This code was generated by "react-native codegen-harmony"
9
+ *
10
+ * Do not edit this file as changes may cause incorrect behavior and will be
11
+ * lost once the code is regenerated.
12
+ *
13
+ * @generatorVersion: 1
14
+ */
15
+ #pragma once
16
+
17
+ #include "RNOH/ArkTSTurboModule.h"
18
+ #include "RNOH/arkui/NativeNodeApi.h"
19
+ #include "RNOH/arkui/TextInputNode.h"
20
+ #include "common/model/Notation.h"
21
+ #include "common/RTLMask.h"
22
+ #include "common/model/AffinityCalculationStrategy.h"
23
+ using namespace rnoh;
24
+ using namespace facebook;
25
+ using namespace TinpMask;
26
+
27
+ void myEventReceiver(ArkUI_NodeEvent *event);
28
+
29
+ namespace rnoh {
30
+
31
+ class RNTextInputMask;
32
+
33
+ struct MaskParams {
34
+ std::string mask; // mask的格式
35
+ std::string value; // 待mask的值
36
+ std::optional<bool> autocomplete; // 布尔值,表示是否自动完成
37
+ MaskParams() = default;
38
+ MaskParams(const std::string &m, const std::string &v, std::optional<bool> autoComplete = std::nullopt)
39
+ : mask(m), value(v), autocomplete(autoComplete) {} // 构造函数
40
+ MaskParams &operator=(const MaskParams &input) {
41
+ this->mask = input.mask;
42
+ this->value = input.value;
43
+ this->autocomplete = input.autocomplete;
44
+ return *this;
45
+ }
46
+ };
47
+
48
+ struct MaskOptions {
49
+ std::optional<std::vector<std::string>> affineFormats; // 可选的字符串数组
50
+ std::optional<std::vector<Notation>> customNotations; // 可选的 Notation 数组
51
+ std::optional<std::string> affinityCalculationStrategy; // 可选字符串
52
+ std::optional<bool> autocomplete; // 可选布尔值
53
+ std::optional<bool> autoskip; // 可选布尔值
54
+ std::optional<bool> rightToLeft; // 可选布尔值
55
+
56
+ MaskOptions()
57
+ : affineFormats(std::vector<std::string>()), customNotations(std::vector<Notation>()),
58
+ affinityCalculationStrategy(std::nullopt), autocomplete(true), autoskip(false), rightToLeft(false) {}
59
+ MaskOptions(const std::vector<std::string> &formats, const std::vector<Notation> &notations,
60
+ const std::string &strategy, bool autoComp, bool autoSkip, bool rtl)
61
+ : affineFormats(formats), customNotations(notations),
62
+ affinityCalculationStrategy(strategy.empty() ? std::make_optional("WHOLE_STRING") : std::make_optional(strategy)),
63
+ autocomplete(autoComp), autoskip(autoSkip), rightToLeft(rtl) {}
64
+ };
65
+
66
+ typedef struct {
67
+ ArkUI_NodeHandle data;
68
+ MaskOptions maskOptions;
69
+ std::string primaryFormat;
70
+ int node;
71
+ std::string lastInputText;
72
+ RNTextInputMask *instance;
73
+ } UserData;
74
+
75
+ class JSI_EXPORT RNTextInputMask : public ArkTSTurboModule {
76
+ public:
77
+ RNTextInputMask(const ArkTSTurboModule::Context ctx, const std::string name);
78
+ void setMask(int reactNode, std::string primaryFormat, MaskOptions options);
79
+ jsi::Value mask(std::string mask, std::string value, bool autocomplete);
80
+ jsi::Value unmask(std::string mask, std::string value, bool autocomplete);
81
+ jsi::Value unmaskWithRightToLeft(std::string mask, std::string value, bool autocomplete, bool rightToLeft);
82
+ jsi::Runtime *grt = nullptr;
83
+ std::shared_ptr<Mask> pickMask(const CaretString &text, MaskOptions maskOptions, std::string primaryMask);
84
+
85
+ // 释放资源
86
+ ~RNTextInputMask();
87
+
88
+ private:
89
+ std::unordered_set<UserData *> m_userDatas;
90
+ };
91
+
92
+
93
+ } // namespace rnoh
@@ -1,32 +1,32 @@
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 "RNOH/Package.h"
9
- #include "RNTextInputMask.h"
10
-
11
- using namespace rnoh;
12
- using namespace facebook;
13
- namespace rnoh {
14
- class RNTextInputMaskTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
15
- public:
16
- SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
17
- if (name == "RNTextInputMask") {
18
- return std::make_shared<RNTextInputMask>(ctx, name);
19
- }
20
- return nullptr;
21
- }
22
- };
23
-
24
- class RNTextInputMaskPackage : public Package {
25
- public:
26
- RNTextInputMaskPackage(Package::Context ctx) : Package(ctx) {}
27
- std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
28
- return std::make_unique<RNTextInputMaskTurboModuleFactoryDelegate>();
29
- }
30
- // std::vector<ArkTSMessageHandler::Shared> createArkTSMessageHandlers() override;
31
- };
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 "RNOH/Package.h"
9
+ #include "RNTextInputMask.h"
10
+
11
+ using namespace rnoh;
12
+ using namespace facebook;
13
+ namespace rnoh {
14
+ class RNTextInputMaskTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
15
+ public:
16
+ SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
17
+ if (name == "RNTextInputMask") {
18
+ return std::make_shared<RNTextInputMask>(ctx, name);
19
+ }
20
+ return nullptr;
21
+ }
22
+ };
23
+
24
+ class RNTextInputMaskPackage : public Package {
25
+ public:
26
+ RNTextInputMaskPackage(Package::Context ctx) : Package(ctx) {}
27
+ std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
28
+ return std::make_unique<RNTextInputMaskTurboModuleFactoryDelegate>();
29
+ }
30
+ // std::vector<ArkTSMessageHandler::Shared> createArkTSMessageHandlers() override;
31
+ };
32
32
  } // namespace rnoh
@@ -1,175 +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
- };
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
175
  } // namespace TinpMask
@@ -1,23 +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
- };
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
23
  } // namespace TinpMask