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

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 (51) hide show
  1. package/.eslintrc +16 -0
  2. package/.gitattributes +1 -0
  3. package/.mtslconfig.json +1 -0
  4. package/OAT.xml +79 -0
  5. package/README.md +6 -8
  6. package/dist/index.d.ts +0 -2
  7. package/dist/index.js +6 -25
  8. package/dist/index.js.map +1 -1
  9. package/dist/src/RNNativeTextInputMask.d.ts +0 -1
  10. package/dist/src/RNNativeTextInputMask.js.map +1 -1
  11. package/dist/src/index.harmony.d.ts +0 -1
  12. package/dist/src/index.harmony.js +0 -3
  13. package/dist/src/index.harmony.js.map +1 -1
  14. package/dist/tsconfig.tsbuildinfo +1 -1
  15. package/harmony/text_input_mask/Index.ets +3 -1
  16. package/harmony/text_input_mask/oh-package.json5 +2 -2
  17. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.cpp +65 -96
  18. package/harmony/text_input_mask/src/main/cpp/RNTextInputMask.h +10 -9
  19. package/harmony/text_input_mask/src/main/cpp/RNTextInputMaskPackage.h +4 -2
  20. package/harmony/text_input_mask/src/main/cpp/common/Compiler.h +84 -81
  21. package/harmony/text_input_mask/src/main/cpp/common/FormatError.h +5 -5
  22. package/harmony/text_input_mask/src/main/cpp/common/FormatSanitizer.h +26 -28
  23. package/harmony/text_input_mask/src/main/cpp/common/Mask.h +51 -45
  24. package/harmony/text_input_mask/src/main/cpp/common/RTLMask.h +18 -20
  25. package/harmony/text_input_mask/src/main/cpp/common/model/AffinityCalculationStrategy.h +23 -19
  26. package/harmony/text_input_mask/src/main/cpp/common/model/CaretString.h +11 -13
  27. package/harmony/text_input_mask/src/main/cpp/common/model/CaretStringIterator.h +9 -10
  28. package/harmony/text_input_mask/src/main/cpp/common/model/Next.h +8 -8
  29. package/harmony/text_input_mask/src/main/cpp/common/model/Notation.h +12 -7
  30. package/harmony/text_input_mask/src/main/cpp/common/model/RTLCaretStringIterator.h +2 -2
  31. package/harmony/text_input_mask/src/main/cpp/common/model/State.h +43 -32
  32. package/harmony/text_input_mask/src/main/cpp/common/model/common.h +19 -11
  33. package/harmony/text_input_mask/src/main/ets/{RNTextInputMaskPackage.ts → RNTextInputMaskPackage.ets} +2 -1
  34. package/harmony/text_input_mask/src/main/ets/RNTextInputMaskTurboModle.ts +0 -4
  35. package/harmony/text_input_mask/src/ohosTest/ets/test/Ability.test.ets +41 -0
  36. package/harmony/text_input_mask/src/ohosTest/ets/test/List.test.ets +11 -0
  37. package/harmony/text_input_mask/src/ohosTest/module.json5 +13 -0
  38. package/harmony/text_input_mask/src/test/List.test.ets +11 -0
  39. package/harmony/text_input_mask/src/test/LocalUnit.test.ets +39 -0
  40. package/harmony/text_input_mask.har +0 -0
  41. package/index.tsx +5 -24
  42. package/package.json +17 -11
  43. package/src/RNNativeTextInputMask.ts +0 -1
  44. package/src/index.harmony.ts +0 -3
  45. package/tsconfig.json +43 -0
  46. package/README.OpenSource +0 -11
  47. package/dist/babel.config.d.ts +0 -7
  48. package/dist/babel.config.js +0 -16
  49. package/dist/babel.config.js.map +0 -1
  50. package/harmony/text_input_mask/BuildProfile.ets +0 -17
  51. /package/harmony/text_input_mask/{ts.ts → ts.ets} +0 -0
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef FORMATERROR_H
7
+ #define FORMATERROR_H
7
8
  #pragma once
8
9
  // 定义 FormatError 类
9
10
  #include <string>
@@ -13,11 +14,10 @@ class FormatError : public std::exception {
13
14
  public:
14
15
  // 构造函数接受错误消息
15
16
  explicit FormatError(const std::string &message = "An error occurred") : msg(message) {}
16
-
17
17
  // 重写 what() 方法,返回错误消息
18
- const char *what() const noexcept { return msg.c_str(); }
19
-
18
+ const char *what() const noexcept override { return msg.c_str(); }
20
19
  private:
21
20
  std::string msg; // 存储错误消息
22
21
  };
23
- } // namespace TinpMask
22
+ } // namespace TinpMask
23
+ #endif
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef FORMATSANITIZER_H
7
+ #define FORMATSANITIZER_H
7
8
  #pragma once
8
9
  #include <iostream>
9
10
  #include <vector>
@@ -17,13 +18,15 @@ namespace TinpMask {
17
18
  class Compiler;
18
19
  class FormatSanitizer {
19
20
  public:
20
- std::string sanitize(const std::string &formatString) {
21
+ std::string sanitize(const std::string &formatString)
22
+ {
21
23
  checkOpenBraces(formatString);
22
24
  std::vector<std::string> blocks = divideBlocksWithMixedCharacters(getFormatBlocks(formatString));
23
25
  return sortFormatBlocks(blocks);
24
26
  }
25
27
 
26
- std::vector<std::string> getFormatBlocks(const std::string &formatString) {
28
+ std::vector<std::string> getFormatBlocks(const std::string &formatString)
29
+ {
27
30
  std::vector<std::string> blocks;
28
31
  std::string currentBlock;
29
32
  bool escape = false;
@@ -36,36 +39,30 @@ public:
36
39
  continue;
37
40
  }
38
41
  }
39
-
40
42
  if ((ch == '[' || ch == '{') && !escape) {
41
43
  if (!currentBlock.empty()) {
42
44
  blocks.push_back(currentBlock);
43
45
  }
44
46
  currentBlock.clear();
45
47
  }
46
-
47
48
  currentBlock += ch;
48
49
 
49
50
  if ((ch == ']' || ch == '}') && !escape) {
50
51
  blocks.push_back(currentBlock);
51
52
  currentBlock.clear();
52
53
  }
53
-
54
54
  escape = false;
55
55
  }
56
56
 
57
57
  if (!currentBlock.empty()) {
58
58
  blocks.push_back(currentBlock);
59
59
  }
60
-
61
60
  return blocks;
62
61
  }
63
-
64
-
65
62
  public:
66
- std::vector<std::string> divideBlocksWithMixedCharacters(const std::vector<std::string> &blocks) {
63
+ std::vector<std::string> divideBlocksWithMixedCharacters(const std::vector<std::string> &blocks)
64
+ {
67
65
  std::vector<std::string> resultingBlocks;
68
-
69
66
  for (const auto &block : blocks) {
70
67
  if (!block.empty() && block.substr(0, 1) == "[") { // 使用 substr 来检查开头
71
68
  std::string blockBuffer;
@@ -114,7 +111,6 @@ public:
114
111
  resultingBlocks.push_back(block);
115
112
  }
116
113
  }
117
-
118
114
  return resultingBlocks;
119
115
  }
120
116
 
@@ -127,7 +123,8 @@ private:
127
123
 
128
124
  bool isSpecialCharacter(char ch) const { return ch == '-' || ch == '_'; }
129
125
 
130
- bool containsAny(const std::string &str, const std::vector<char> &characters) const {
126
+ bool containsAny(const std::string &str, const std::vector<char> &characters) const
127
+ {
131
128
  for (char c : characters) {
132
129
  if (str.find(c) != std::string::npos) {
133
130
  return true;
@@ -135,9 +132,9 @@ private:
135
132
  }
136
133
  return false;
137
134
  }
138
-
139
135
  public:
140
- std::string sortFormatBlocks(const std::vector<std::string> &blocks) {
136
+ std::string sortFormatBlocks(const std::vector<std::string> &blocks)
137
+ {
141
138
  std::vector<std::string> sortedBlocks;
142
139
 
143
140
  for (const auto &block : blocks) {
@@ -162,12 +159,11 @@ public:
162
159
 
163
160
  return join(sortedBlocks); // Ensure this is returning std::vector<std::string>
164
161
  }
165
-
166
- void checkOpenBraces(const std::string &inputString) {
162
+ void checkOpenBraces(const std::string &inputString)
163
+ {
167
164
  bool escape = false;
168
165
  bool squareBraceOpen = false;
169
166
  bool curlyBraceOpen = false;
170
-
171
167
  for (const char &ch : inputString) {
172
168
  if (ch == '\\') {
173
169
  escape = !escape;
@@ -191,7 +187,6 @@ public:
191
187
  }
192
188
  curlyBraceOpen = !escape;
193
189
  }
194
-
195
190
  if (ch == '}' && !escape) {
196
191
  curlyBraceOpen = false;
197
192
  }
@@ -200,27 +195,29 @@ public:
200
195
  }
201
196
 
202
197
  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) {
198
+ bool startsWith(const std::string &str,
199
+ const std::string &prefix) const { return str.rfind(prefix, 0) == 0; }
200
+ std::string sortString(std::string str)
201
+ {
206
202
  std::sort(str.begin(), str.end());
207
203
  return str;
208
204
  }
209
-
210
- std::string replaceChars(const std::string &str) {
205
+ std::string replaceChars(const std::string &str)
206
+ {
211
207
  std::string newStr = str;
212
208
  std::replace(newStr.begin(), newStr.end(), '_', 'A');
213
209
  std::replace(newStr.begin(), newStr.end(), '-', 'a');
214
210
  return newStr;
215
211
  }
216
-
217
- std::string replaceCharsBack(const std::string &str) {
212
+ std::string replaceCharsBack(const std::string &str)
213
+ {
218
214
  std::string newStr = str;
219
215
  std::replace(newStr.begin(), newStr.end(), 'A', '_');
220
216
  std::replace(newStr.begin(), newStr.end(), 'a', '-');
221
217
  return newStr;
222
218
  }
223
- std::string join(const std::vector<std::string> &strings) {
219
+ std::string join(const std::vector<std::string> &strings)
220
+ {
224
221
  std::string result;
225
222
  for (const auto &str : strings) {
226
223
  result += str; // 将所有块连接成一个字符串
@@ -228,4 +225,5 @@ private:
228
225
  return result;
229
226
  }
230
227
  };
231
- } // namespace TinpMask
228
+ } // namespace TinpMask
229
+ #endif
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef MASK_H
7
+ #define MASK_H
7
8
  #pragma once
8
9
  #include <string>
9
10
  #include <vector>
@@ -15,7 +16,6 @@
15
16
  #include "model/State.h"
16
17
 
17
18
  namespace TinpMask {
18
-
19
19
  class Mask {
20
20
  protected:
21
21
  std::vector<Notation> customNotations;
@@ -28,27 +28,33 @@ private:
28
28
 
29
29
  public:
30
30
  // 主构造函数
31
- Mask(const std::string &format, const std::vector<Notation> &customNotations) : customNotations(customNotations) {
31
+ Mask(const std::string &format, const std::vector<Notation> &customNotations) : customNotations(customNotations)
32
+ {
32
33
  this->format = format;
33
34
  this->customNotations = customNotations;
34
35
  this->initialState = Compiler(customNotations).compile(format);
35
36
  }
36
37
 
37
38
  // 便利构造函数
38
- Mask(const std::string &format) : Mask(format, {}) { // 调用主构造函数,传入空的 customNotations
39
+ Mask(const std::string &format) : Mask(format, {})
40
+ { // 调用主构造函数,传入空的 customNotations
39
41
  std::vector<Notation> emptyVector;
40
42
  this->format = format;
41
43
  this->customNotations = emptyVector;
42
44
  this->initialState = Compiler(this->customNotations).compile(this->format);
43
45
  }
44
46
 
47
+ bool hasSameCustomNotations(const std::vector<Notation> &targetNotations) const
48
+ {
49
+ return this->customNotations == targetNotations;
50
+ }
45
51
 
46
52
  class MaskFactory {
47
53
  public:
48
54
  static std::unordered_map<std::string, std::shared_ptr<Mask>> maskCache;
49
55
 
50
56
  public:
51
- /**
57
+ /* *
52
58
  * Factory constructor.
53
59
  *
54
60
  * Operates over own ``Mask`` cache where initialized ``Mask`` objects are stored under
@@ -58,9 +64,10 @@ public:
58
64
  * doesn't exist in cache, the object is constructed, cached and returned.
59
65
  */
60
66
  static std::shared_ptr<Mask> getOrCreate(const std::string &format,
61
- const std::vector<Notation> &customNotations) {
67
+ const std::vector<Notation> &customNotations)
68
+ {
62
69
  auto cachedMask = maskCache.find(format);
63
- if (cachedMask == maskCache.end()) {
70
+ if (cachedMask == maskCache.end() || !cachedMask->second->hasSameCustomNotations(customNotations)) {
64
71
  auto newMask = std::make_shared<Mask>(format, customNotations);
65
72
  maskCache[format] = newMask;
66
73
  return newMask;
@@ -68,7 +75,7 @@ public:
68
75
  return cachedMask->second;
69
76
  }
70
77
 
71
- /**
78
+ /* *
72
79
  * Check your mask format is valid.
73
80
  *
74
81
  * @param format mask format.
@@ -77,7 +84,8 @@ public:
77
84
  * @returns `true` if this format coupled with custom notations will compile into a working ``Mask`` object.
78
85
  * Otherwise `false`.
79
86
  */
80
- static bool isValid(const std::string &format, const std::vector<Notation> &customNotations) {
87
+ static bool isValid(const std::string &format, const std::vector<Notation> &customNotations)
88
+ {
81
89
  try {
82
90
  Mask(format, customNotations);
83
91
  return true;
@@ -86,14 +94,15 @@ public:
86
94
  }
87
95
  }
88
96
  };
89
- /**
97
+ /* *
90
98
  * Apply mask to the user input string.
91
99
  *
92
100
  * @param text user input string with current cursor position
93
101
  *
94
102
  * @returns Formatted text with extracted value an adjusted cursor position.
95
103
  */
96
- virtual Result apply(const CaretString &text) {
104
+ virtual Result apply(const CaretString &text)
105
+ {
97
106
  auto iterator = makeIterator(text); // Assume this function is defined
98
107
 
99
108
  int affinity = 0;
@@ -109,7 +118,6 @@ public:
109
118
  char character = iterator->next();
110
119
  while (character != '\0') {
111
120
  auto next = state->accept(character);
112
-
113
121
  if (next != nullptr) {
114
122
  if (deletionAffectsCaret) {
115
123
  auto opt = state->autocomplete();
@@ -152,9 +160,9 @@ public:
152
160
 
153
161
  while (text.caretGravity->autocomplete() && insertionAffectsCaret) {
154
162
  auto next = state->autocomplete();
155
- if (next == nullptr)
163
+ if (next == nullptr) {
156
164
  break;
157
-
165
+ }
158
166
  state = next->state;
159
167
  if (next->insert == '\0') {
160
168
  modifiedString += "";
@@ -194,34 +202,35 @@ public:
194
202
  std::string tailPlaceholder = appendPlaceholder(tailState.get(), tail); // Assume this function is defined
195
203
 
196
204
  return Result(CaretString(modifiedString, modifiedCaretPosition, text.caretGravity), extractedValue, affinity,
197
- noMandatoryCharactersLeftAfterState(state.get()), // Assume this function is defined
198
- tailPlaceholder);
205
+ noMandatoryCharactersLeftAfterState(state.get()), tailPlaceholder);
199
206
  }
200
207
 
201
-
202
208
  public:
203
- std::shared_ptr<CaretStringIterator> makeIterator(const CaretString &text) const {
209
+ std::shared_ptr<CaretStringIterator> makeIterator(const CaretString &text) const
210
+ {
204
211
  return std::make_shared<CaretStringIterator>(text);
205
212
  }
206
-
207
- /**
213
+ /* *
208
214
  * Generate placeholder.
209
215
  *
210
216
  * @return Placeholder string.
211
217
  */
212
218
  public:
213
- std::string placeholder() { return appendPlaceholder(initialState.get(), ""); }
214
- /**
219
+ std::string placeholder()
220
+ {
221
+ return appendPlaceholder(initialState.get(), "");
222
+ }
223
+ /* *
215
224
  * Minimal length of the text inside the field to fill all mandatory characters in the mask.
216
225
  *
217
226
  * @return Minimal satisfying count of characters inside the text field.
218
227
  */
219
228
  public:
220
- int acceptableTextLength() {
229
+ int acceptableTextLength()
230
+ {
221
231
  std::shared_ptr<State> state = std::move(initialState);
222
232
  ;
223
233
  int length = 0;
224
-
225
234
  while (state != nullptr && dynamic_cast<EOLState *>(state.get()) == nullptr) {
226
235
  if (dynamic_cast<FixedState *>(state.get()) != nullptr ||
227
236
  dynamic_cast<FreeState *>(state.get()) != nullptr ||
@@ -230,18 +239,16 @@ public:
230
239
  }
231
240
  state = state->child; // 移动到下一个子状态
232
241
  }
233
-
234
242
  return length;
235
243
  }
236
-
237
-
238
- /**
244
+ /* *
239
245
  * Maximal length of the text inside the field.
240
246
  *
241
247
  * @return Total available count of mandatory and optional characters inside the text field.
242
248
  */
243
249
  public:
244
- int totalTextLength() const {
250
+ int totalTextLength() const
251
+ {
245
252
  std::shared_ptr<State> state = initialState;
246
253
  ;
247
254
  int length = 0;
@@ -255,16 +262,16 @@ public:
255
262
  }
256
263
  state = state->child; // 移动到下一个子状态
257
264
  }
258
-
259
265
  return length;
260
266
  }
261
- /**
267
+ /* *
262
268
  * Minimal length of the extracted value with all mandatory characters filled.\
263
269
  *
264
270
  * @return Minimal satisfying count of characters in extracted value.
265
271
  */
266
272
  public:
267
- int acceptableValueLength() const {
273
+ int acceptableValueLength() const
274
+ {
268
275
  std::shared_ptr<State> state = initialState;
269
276
  int length = 0;
270
277
  while (state != nullptr && dynamic_cast<EOLState *>(state.get()) == nullptr) {
@@ -274,16 +281,16 @@ public:
274
281
  }
275
282
  state = state->child; // 移动到下一个子状态
276
283
  }
277
-
278
284
  return length;
279
285
  }
280
- /**
286
+ /* *
281
287
  * Maximal length of the extracted value.
282
288
  *
283
289
  * @return Total available count of mandatory and optional characters for extracted value.
284
290
  */
285
291
  public:
286
- int totalValueLength() const {
292
+ int totalValueLength() const
293
+ {
287
294
  std::shared_ptr<State> state = initialState;
288
295
  int length = 0;
289
296
  while (state != nullptr && dynamic_cast<EOLState *>(state.get()) == nullptr) {
@@ -298,7 +305,8 @@ public:
298
305
  }
299
306
 
300
307
  private:
301
- std::string appendPlaceholder(State *state, const std::string &placeholder) const {
308
+ std::string appendPlaceholder(State *state, const std::string &placeholder) const
309
+ {
302
310
  if (state == nullptr) {
303
311
  return placeholder;
304
312
  }
@@ -334,7 +342,6 @@ private:
334
342
  return placeholder; // 未知类型,返回原始 placeholder
335
343
  }
336
344
  }
337
-
338
345
  if (auto valueState = dynamic_cast<ValueState *>(state)) {
339
346
  if (!state) {
340
347
  return placeholder; // 如果 state 为 nullptr,直接返回原始 placeholder
@@ -351,19 +358,17 @@ private:
351
358
  if (customValueState->type.get()) {
352
359
  auto customStateType = dynamic_cast<ValueState::Custom *>(customValueState->type.get());
353
360
  return appendPlaceholder(state->child.get(), placeholder + customStateType->character);
354
- }else {
355
- throw FormatError("appendPlaceholder customValueState type is null"); // 未找到匹配项,抛出异常
361
+ } else {
362
+ throw FormatError("appendPlaceholder customValueState type is null");
356
363
  }
357
-
358
364
  } else {
359
- return placeholder; // 未知类型,返回原始 placeholder
365
+ return placeholder;
360
366
  }
361
367
  }
362
-
363
368
  return placeholder;
364
369
  }
365
-
366
- bool noMandatoryCharactersLeftAfterState(State *state) const {
370
+ bool noMandatoryCharactersLeftAfterState(State *state) const
371
+ {
367
372
  if (dynamic_cast<EOLState *>(state)) {
368
373
  return true;
369
374
  } else if (auto valueState = dynamic_cast<ValueState *>(state)) {
@@ -375,4 +380,5 @@ private:
375
380
  }
376
381
  }
377
382
  };
378
- } // namespace TinpMask
383
+ } // namespace TinpMask
384
+ #endif
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef RTLMASK_H
7
+ #define RTLMASK_H
7
8
  #pragma once
8
9
  #include "common/Mask.h"
9
10
  #include "common/model/CaretString.h"
@@ -13,11 +14,12 @@ namespace TinpMask {
13
14
  class RTLMask : public Mask {
14
15
 
15
16
  public:
16
- static std::unordered_map<std::string, std::shared_ptr<RTLMask>> cache ;
17
+ static std::unordered_map<std::string, std::shared_ptr<RTLMask>> cache ;
17
18
  RTLMask(const std::string& format, const std::vector<Notation>& customNotations)
18
19
  : Mask(reversedFormat(format), customNotations) {}
19
-
20
- static std::shared_ptr<RTLMask> getOrCreate(const std::string& format, const std::vector<Notation>& customNotations) {
20
+ static std::shared_ptr<RTLMask> getOrCreate(const std::string& format,
21
+ const std::vector<Notation>& customNotations)
22
+ {
21
23
  std::string reversed = reversedFormat(format);
22
24
  auto it = cache.find(reversed);
23
25
  if (it != cache.end()) {
@@ -29,40 +31,35 @@ public:
29
31
  cache[reversed] = newMask;
30
32
  return newMask;
31
33
  }
32
-
33
- Result apply(const CaretString& text) override {
34
+ Result apply(const CaretString& text) override
35
+ {
34
36
  return Mask::apply(text.reversed()).reversed(); // Assuming the Result class has a reversed method
35
37
  }
36
-
37
-
38
38
  private:
39
-
40
-
41
- static std::string reversedFormat(const std::string& format) {
39
+ static constexpr size_t ESCAPED_BRACKET_LENGTH = 2;
40
+ static std::string reversedFormat(const std::string& format)
41
+ {
42
42
  std::string reversed = std::string(format.rbegin(), format.rend());
43
-
44
- // Replace logic (equivalent to Kotlin's replace)
45
43
  size_t pos = 0;
46
44
  while ((pos = reversed.find("\\[", pos)) != std::string::npos) {
47
- reversed.replace(pos, 2, "\\");
48
- pos += 1; // Advance position to prevent infinite loop
45
+ reversed.replace(pos, ESCAPED_BRACKET_LENGTH, "\\");
46
+ pos += 1;
49
47
  }
50
48
  pos = 0;
51
49
  while ((pos = reversed.find("]\\", pos)) != std::string::npos) {
52
- reversed.replace(pos, 2, "\\[");
50
+ reversed.replace(pos, ESCAPED_BRACKET_LENGTH, "\\[");
53
51
  pos += 1;
54
52
  }
55
53
  pos = 0;
56
54
  while ((pos = reversed.find("\\{", pos)) != std::string::npos) {
57
- reversed.replace(pos, 2, "\\}");
55
+ reversed.replace(pos, ESCAPED_BRACKET_LENGTH, "\\}");
58
56
  pos += 1;
59
57
  }
60
58
  pos = 0;
61
59
  while ((pos = reversed.find("}\\", pos)) != std::string::npos) {
62
- reversed.replace(pos, 2, "\\{");
60
+ reversed.replace(pos, ESCAPED_BRACKET_LENGTH, "\\{");
63
61
  pos += 1;
64
62
  }
65
-
66
63
  // Map logic for brackets
67
64
  for (char& ch : reversed) {
68
65
  switch (ch) {
@@ -76,4 +73,5 @@ private:
76
73
  return reversed;
77
74
  }
78
75
  };
79
- }
76
+ }
77
+ #endif
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef AFFINITYCALCULATIONSTRATEGY_H
7
+ #define AFFINITYCALCULATIONSTRATEGY_H
7
8
  #pragma once
8
9
  #include <string>
9
10
  #include <stdexcept>
@@ -18,32 +19,34 @@ enum class AffinityCalculationStrategy { WHOLE_STRING, PREFIX, CAPACITY, EXTRACT
18
19
  class AffinityCalculator {
19
20
  public:
20
21
  static int calculateAffinityOfMask(AffinityCalculationStrategy strategy, Mask &mask,
21
- const CaretString &text) {
22
+ const CaretString &text)
23
+ {
22
24
  switch (strategy) {
23
- case AffinityCalculationStrategy::WHOLE_STRING:
24
- return mask.apply(text).affinity;
25
+ case AffinityCalculationStrategy::WHOLE_STRING:
26
+ return mask.apply(text).affinity;
25
27
 
26
- case AffinityCalculationStrategy::PREFIX:
27
- return prefixIntersection(mask.apply(text).formattedText.string, text.string).length();
28
+ case AffinityCalculationStrategy::PREFIX:
29
+ return prefixIntersection(mask.apply(text).formattedText.string, text.string).length();
28
30
 
29
- case AffinityCalculationStrategy::CAPACITY:
30
- return text.string.length() > mask.totalTextLength() ? std::numeric_limits<int>::min()
31
- : text.string.length() - mask.totalTextLength();
31
+ case AffinityCalculationStrategy::CAPACITY:
32
+ return text.string.length() > mask.totalTextLength() ? std::numeric_limits<int>::min()
33
+ : text.string.length() - mask.totalTextLength();
32
34
 
33
- case AffinityCalculationStrategy::EXTRACTED_VALUE_CAPACITY: {
34
- const auto &extractedValue = mask.apply(text).extractedValue;
35
- return extractedValue.length() > mask.totalValueLength()
36
- ? std::numeric_limits<int>::min()
37
- : extractedValue.length() - mask.totalValueLength();
38
- }
39
- default:
40
- throw std::invalid_argument("Unknown AffinityCalculationStrategy");
35
+ case AffinityCalculationStrategy::EXTRACTED_VALUE_CAPACITY: {
36
+ const auto &extractedValue = mask.apply(text).extractedValue;
37
+ return extractedValue.length() > mask.totalValueLength()
38
+ ? std::numeric_limits<int>::min()
39
+ : extractedValue.length() - mask.totalValueLength();
40
+ }
41
+ default:
42
+ throw std::invalid_argument("Unknown AffinityCalculationStrategy");
41
43
  }
42
44
  }
43
45
 
44
46
  private:
45
47
  // Helper function to find prefix intersection
46
- static std::string prefixIntersection(const std::string &str1, const std::string &str2) {
48
+ static std::string prefixIntersection(const std::string &str1, const std::string &str2)
49
+ {
47
50
  size_t endIndex = 0;
48
51
  while (endIndex < str1.length() && endIndex < str2.length()) {
49
52
  if (str1[endIndex] == str2[endIndex]) {
@@ -55,4 +58,5 @@ private:
55
58
  return str1.substr(0, endIndex);
56
59
  }
57
60
  };
58
- } // namespace TinpMask
61
+ } // namespace TinpMask
62
+ #endif
@@ -3,7 +3,8 @@
3
3
  * Use of this source code is governed by a MIT license that can be
4
4
  * found in the LICENSE file.
5
5
  */
6
-
6
+ #ifndef CARETSTRING_H
7
+ #define CARETSTRING_H
7
8
  #pragma once
8
9
  #include <string>
9
10
  #include <memory>
@@ -26,7 +27,7 @@ public:
26
27
  // 向前的光标重力
27
28
  class Forward : public CaretGravity {
28
29
  public:
29
- Forward(bool autoCompleteValue) : autocompleteValue(autoCompleteValue) {}
30
+ explicit Forward(bool autoCompleteValue) : autocompleteValue(autoCompleteValue) {}
30
31
 
31
32
  bool autocomplete() const override { return autocompleteValue; }
32
33
 
@@ -37,7 +38,7 @@ public:
37
38
  // 向后的光标重力
38
39
  class Backward : public CaretGravity {
39
40
  public:
40
- Backward(bool autoSkipValue) : autoskipValue(autoSkipValue) {}
41
+ explicit Backward(bool autoSkipValue) : autoskipValue(autoSkipValue) {}
41
42
 
42
43
  bool autoskip() const override { return autoskipValue; }
43
44
 
@@ -51,26 +52,23 @@ public:
51
52
  : string(str), caretPosition(caretPos), caretGravity(caretGrav) {}
52
53
 
53
54
  // 反转字符串并返回新的 CaretString 对象
54
- CaretString reversed() const {
55
+ CaretString reversed() const
56
+ {
55
57
  // 创建一个反转后的字符串
56
58
  std::string reversedStr(string.rbegin(), string.rend());
57
59
  // 计算新的 caretPosition
58
60
  int newCaretPos = string.length() - caretPosition;
59
61
  return CaretString(reversedStr, newCaretPos, caretGravity);
60
62
  }
61
-
62
- // 获取字符串
63
63
  const std::string &getString() const { return string; }
64
64
  // 获取光标位置
65
65
  int getCaretPosition() const { return caretPosition; }
66
66
  // 获取光标重力
67
67
  std::shared_ptr<CaretGravity> getCaretGravity() const { return caretGravity; }
68
-
69
-
70
68
  public:
71
- std::string string; // 字符串内容
72
- int caretPosition; // 光标位置
73
- std::shared_ptr<CaretGravity> caretGravity; // 光标重力
69
+ std::string string;
70
+ int caretPosition;
71
+ std::shared_ptr<CaretGravity> caretGravity;
74
72
  };
75
-
76
- } // namespace TinpMask
73
+ } // namespace TinpMask
74
+ #endif