fasdfsa commited on
Commit
37d46fe
·
1 Parent(s): 7b882c9

// 虽说是单字结果,但是有可能是一个单词的多个字。这种情况拆出每一个字符,让它们在图片中有相同的坐标和宽高。既是整个单词的大框

Browse files
src/WeChatOcrCpp/WeChatOcrCpp.vcxproj CHANGED
@@ -157,6 +157,7 @@
157
  <ClInclude Include="MmmojoDll.h" />
158
  <ClInclude Include="OcrManager.h" />
159
  <ClInclude Include="protobuf\ocr_protobuf.pb.h" />
 
160
  <ClInclude Include="Utilities.h" />
161
  <ClInclude Include="Wchtcr.h" />
162
  <ClInclude Include="WeChatOcrResult.h" />
 
157
  <ClInclude Include="MmmojoDll.h" />
158
  <ClInclude Include="OcrManager.h" />
159
  <ClInclude Include="protobuf\ocr_protobuf.pb.h" />
160
+ <ClInclude Include="utf8.h" />
161
  <ClInclude Include="Utilities.h" />
162
  <ClInclude Include="Wchtcr.h" />
163
  <ClInclude Include="WeChatOcrResult.h" />
src/WeChatOcrCpp/WeChatOcrResult.cpp CHANGED
@@ -13,6 +13,8 @@
13
 
14
  #include "WeChatOcrResult.h"
15
 
 
 
16
 
17
 
18
 
@@ -92,7 +94,6 @@ WeChatOcrResult* ParseOcrResult::ParseJson(const std::string& jsonResponseStr) {
92
  const auto& protoSingleResult = protoOcrResult.single_result(i);
93
  SingleResult singleResult;
94
 
95
- // 解析单行文本
96
  // 解析单行文本
97
  if (!protoSingleResult.single_str_utf8().empty()) {
98
  singleResult.SingleStrUtf8 = Base64Decode(protoSingleResult.single_str_utf8());
@@ -231,6 +232,7 @@ std::string OcrResult::ToJson(std::string& pth_img) const {
231
  nlohmann::json oneResultArray = nlohmann::json::array();
232
 
233
  for (int i = 0; i < singleResult.OneResult.size(); i++) {
 
234
  const auto& oneResult = singleResult.OneResult[i];
235
  nlohmann::ordered_json jsonOneResult;
236
 
@@ -238,34 +240,77 @@ std::string OcrResult::ToJson(std::string& pth_img) const {
238
  continue;
239
  }
240
 
241
- jsonOneResult["word"] = oneResult.OneStrUtf8; // 单个字文本
 
 
 
242
 
243
- int one_x = -1, one_y = -1, one_w = -1, one_h = h;
244
-
245
- if (oneResult.OnePos) {
246
- nlohmann::json posArray = nlohmann::json::array();
247
- for (const auto& pos : oneResult.OnePos->Pos) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
248
- //posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
249
- one_x = pos.X;
250
- one_y = pos.Y;
251
- break;
 
 
 
 
252
  }
253
- //jsonOneResult["position"] = posArray;
254
- }
255
 
256
- if (i == singleResult.OneResult.size() - 1) {
257
- one_w = rht - one_x;
258
- }
259
- else {
260
- one_w = singleResult.OneResult[i + 1].OnePos->Pos[0].X - one_x; // 获取下一个单字的左上角坐标
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  }
262
 
263
- jsonOneResult["x"] = one_x;
264
- jsonOneResult["y"] = one_y;
265
- jsonOneResult["w"] = one_w;
266
- jsonOneResult["h"] = one_h;
267
-
268
- oneResultArray.push_back(jsonOneResult);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
 
271
  //cv::Mat tmt = mt.clone();
 
13
 
14
  #include "WeChatOcrResult.h"
15
 
16
+ #include "utf8.h"
17
+
18
 
19
 
20
 
 
94
  const auto& protoSingleResult = protoOcrResult.single_result(i);
95
  SingleResult singleResult;
96
 
 
97
  // 解析单行文本
98
  if (!protoSingleResult.single_str_utf8().empty()) {
99
  singleResult.SingleStrUtf8 = Base64Decode(protoSingleResult.single_str_utf8());
 
232
  nlohmann::json oneResultArray = nlohmann::json::array();
233
 
234
  for (int i = 0; i < singleResult.OneResult.size(); i++) {
235
+
236
  const auto& oneResult = singleResult.OneResult[i];
237
  nlohmann::ordered_json jsonOneResult;
238
 
 
240
  continue;
241
  }
242
 
243
+ // 虽说是字结果,但是有可能是一单词的多个。这种情况拆出每一个字符,让它们在图片中有相同的坐标和宽高。既是整个单词的大框
244
+ for (size_t j = 0; j < oneResult.OneStrUtf8.size();) {
245
+ size_t len = utf8codepointcalcsize(&oneResult.OneStrUtf8[j]); // 计算字符字节数
246
+ std::string word = oneResult.OneStrUtf8.substr(j, len);
247
 
248
+ jsonOneResult["word"] = word; // 单个字文本
249
+
250
+ int one_x = -1, one_y = -1, one_w = -1, one_h = h;
251
+
252
+ if (oneResult.OnePos) {
253
+ nlohmann::json posArray = nlohmann::json::array();
254
+ for (const auto& pos : oneResult.OnePos->Pos) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
255
+ //posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
256
+ one_x = pos.X;
257
+ one_y = pos.Y;
258
+ break;
259
+ }
260
+ //jsonOneResult["position"] = posArray;
261
  }
 
 
262
 
263
+ if (i == singleResult.OneResult.size() - 1) {
264
+ one_w = rht - one_x;
265
+ }
266
+ else {
267
+ one_w = singleResult.OneResult[i + 1].OnePos->Pos[0].X - one_x; // 获取下一个单字的左上角坐标
268
+ }
269
+
270
+ jsonOneResult["x"] = one_x;
271
+ jsonOneResult["y"] = one_y;
272
+ jsonOneResult["w"] = one_w;
273
+ jsonOneResult["h"] = one_h;
274
+
275
+ oneResultArray.push_back(jsonOneResult);
276
+
277
+ //cv::Mat tmt = mt.clone();
278
+ //cv::rectangle(tmt, cv::Rect(one_x, one_y, one_w, one_h), cv::Scalar(0, 0, 255));
279
+ //cv::imshow("line", tmt);
280
+ //cv::waitKey(0);
281
+
282
+ j += len;
283
  }
284
 
285
+
286
+ //jsonOneResult["word"] = oneResult.OneStrUtf8; // 单个字文本
287
+
288
+ //int one_x = -1, one_y = -1, one_w = -1, one_h = h;
289
+ //
290
+ // if (oneResult.OnePos) {
291
+ // nlohmann::json posArray = nlohmann::json::array();
292
+ // for (const auto& pos : oneResult.OnePos->Pos) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
293
+ // //posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
294
+ // one_x = pos.X;
295
+ // one_y = pos.Y;
296
+ // break;
297
+ // }
298
+ // //jsonOneResult["position"] = posArray;
299
+ // }
300
+
301
+ // if (i == singleResult.OneResult.size() - 1) {
302
+ // one_w = rht - one_x;
303
+ // }
304
+ // else {
305
+ // one_w = singleResult.OneResult[i + 1].OnePos->Pos[0].X - one_x; // 获取下一个单字的左上角坐标
306
+ // }
307
+
308
+ // jsonOneResult["x"] = one_x;
309
+ // jsonOneResult["y"] = one_y;
310
+ // jsonOneResult["w"] = one_w;
311
+ // jsonOneResult["h"] = one_h;
312
+ //
313
+ // oneResultArray.push_back(jsonOneResult);
314
 
315
 
316
  //cv::Mat tmt = mt.clone();
src/WeChatOcrCpp/utf8.h ADDED
@@ -0,0 +1,1717 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* The latest version of this library is available on GitHub;
2
+ * https://github.com/sheredom/utf8.h */
3
+
4
+ /* This is free and unencumbered software released into the public domain.
5
+ *
6
+ * Anyone is free to copy, modify, publish, use, compile, sell, or
7
+ * distribute this software, either in source code form or as a compiled
8
+ * binary, for any purpose, commercial or non-commercial, and by any
9
+ * means.
10
+ *
11
+ * In jurisdictions that recognize copyright laws, the author or authors
12
+ * of this software dedicate any and all copyright interest in the
13
+ * software to the public domain. We make this dedication for the benefit
14
+ * of the public at large and to the detriment of our heirs and
15
+ * successors. We intend this dedication to be an overt act of
16
+ * relinquishment in perpetuity of all present and future rights to this
17
+ * software under copyright law.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ * OTHER DEALINGS IN THE SOFTWARE.
26
+ *
27
+ * For more information, please refer to <http://unlicense.org/> */
28
+
29
+ #ifndef SHEREDOM_UTF8_H_INCLUDED
30
+ #define SHEREDOM_UTF8_H_INCLUDED
31
+
32
+ #if defined(_MSC_VER)
33
+ #pragma warning(push)
34
+
35
+ /* disable warning: no function prototype given: converting '()' to '(void)' */
36
+ #pragma warning(disable : 4255)
37
+
38
+ /* disable warning: '__cplusplus' is not defined as a preprocessor macro,
39
+ * replacing with '0' for '#if/#elif' */
40
+ #pragma warning(disable : 4668)
41
+
42
+ /* disable warning: bytes padding added after construct */
43
+ #pragma warning(disable : 4820)
44
+ #endif
45
+
46
+ #if defined(__cplusplus)
47
+ #if defined(_MSC_VER)
48
+ #define utf8_cplusplus _MSVC_LANG
49
+ #else
50
+ #define utf8_cplusplus __cplusplus
51
+ #endif
52
+ #endif
53
+
54
+ #include <stddef.h>
55
+ #include <stdlib.h>
56
+
57
+ #if defined(_MSC_VER)
58
+ #pragma warning(pop)
59
+ #endif
60
+
61
+ #if defined(_MSC_VER) && (_MSC_VER < 1920)
62
+ typedef __int32 utf8_int32_t;
63
+ #else
64
+ #include <stdint.h>
65
+ typedef int32_t utf8_int32_t;
66
+ #endif
67
+
68
+ #if defined(__clang__)
69
+ #pragma clang diagnostic push
70
+ #pragma clang diagnostic ignored "-Wold-style-cast"
71
+ #pragma clang diagnostic ignored "-Wcast-qual"
72
+
73
+ #if __has_warning("-Wunsafe-buffer-usage")
74
+ #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
75
+ #endif
76
+ #endif
77
+
78
+ #ifdef utf8_cplusplus
79
+ extern "C" {
80
+ #endif
81
+
82
+ #if defined(__TINYC__)
83
+ #define UTF8_ATTRIBUTE(a) __attribute((a))
84
+ #else
85
+ #define UTF8_ATTRIBUTE(a) __attribute__((a))
86
+ #endif
87
+
88
+ #if defined(_MSC_VER)
89
+ #define utf8_nonnull
90
+ #define utf8_pure
91
+ #define utf8_restrict __restrict
92
+ #define utf8_weak __inline
93
+ #elif defined(__clang__) || defined(__GNUC__)
94
+ #define utf8_nonnull UTF8_ATTRIBUTE(nonnull)
95
+ #define utf8_pure UTF8_ATTRIBUTE(pure)
96
+ #define utf8_restrict __restrict__
97
+ #define utf8_weak UTF8_ATTRIBUTE(weak)
98
+ #elif defined(__TINYC__)
99
+ #define utf8_nonnull UTF8_ATTRIBUTE(nonnull)
100
+ #define utf8_pure UTF8_ATTRIBUTE(pure)
101
+ #define utf8_restrict
102
+ #define utf8_weak UTF8_ATTRIBUTE(weak)
103
+ #elif defined(__IAR_SYSTEMS_ICC__)
104
+ #define utf8_nonnull
105
+ #define utf8_pure UTF8_ATTRIBUTE(pure)
106
+ #define utf8_restrict __restrict
107
+ #define utf8_weak UTF8_ATTRIBUTE(weak)
108
+ #else
109
+ #error Non clang, non gcc, non MSVC, non tcc, non iar compiler found!
110
+ #endif
111
+
112
+ #ifdef utf8_cplusplus
113
+ #define utf8_null NULL
114
+ #else
115
+ #define utf8_null 0
116
+ #endif
117
+
118
+ #if defined(utf8_cplusplus) && utf8_cplusplus >= 201402L && (!defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER >= 1910))
119
+ #define utf8_constexpr14 constexpr
120
+ #define utf8_constexpr14_impl constexpr
121
+ #else
122
+ /* constexpr and weak are incompatible. so only enable one of them */
123
+ #define utf8_constexpr14 utf8_weak
124
+ #define utf8_constexpr14_impl
125
+ #endif
126
+
127
+ #if defined(utf8_cplusplus) && utf8_cplusplus >= 202002L && defined(__cpp_char8_t)
128
+ using utf8_int8_t = char8_t; /* Introduced in C++20 */
129
+ #else
130
+ typedef char utf8_int8_t;
131
+ #endif
132
+
133
+ /* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 >
134
+ * src2 respectively, case insensitive. */
135
+ utf8_constexpr14 utf8_nonnull utf8_pure int
136
+ utf8casecmp(const utf8_int8_t *src1, const utf8_int8_t *src2);
137
+
138
+ /* Append the utf8 string src onto the utf8 string dst. */
139
+ utf8_nonnull utf8_weak utf8_int8_t *
140
+ utf8cat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src);
141
+
142
+ /* Find the first match of the utf8 codepoint chr in the utf8 string src. */
143
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
144
+ utf8chr(const utf8_int8_t *src, utf8_int32_t chr);
145
+
146
+ /* Return less than 0, 0, greater than 0 if src1 < src2,
147
+ * src1 == src2, src1 > src2 respectively. */
148
+ utf8_constexpr14 utf8_nonnull utf8_pure int utf8cmp(const utf8_int8_t *src1,
149
+ const utf8_int8_t *src2);
150
+
151
+ /* Copy the utf8 string src onto the memory allocated in dst. */
152
+ utf8_nonnull utf8_weak utf8_int8_t *
153
+ utf8cpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src);
154
+
155
+ /* Number of utf8 codepoints in the utf8 string src that consists entirely
156
+ * of utf8 codepoints not from the utf8 string reject. */
157
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t
158
+ utf8cspn(const utf8_int8_t *src, const utf8_int8_t *reject);
159
+
160
+ /* Duplicate the utf8 string src by getting its size, malloc'ing a new buffer
161
+ * copying over the data, and returning that. Or 0 if malloc failed. */
162
+ utf8_weak utf8_int8_t *utf8dup(const utf8_int8_t *src);
163
+
164
+ /* Number of utf8 codepoints in the utf8 string str,
165
+ * excluding the null terminating byte. */
166
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8len(const utf8_int8_t *str);
167
+
168
+ /* Similar to utf8len, except that only at most n bytes of src are looked. */
169
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8nlen(const utf8_int8_t *str,
170
+ size_t n);
171
+
172
+ /* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 >
173
+ * src2 respectively, case insensitive. Checking at most n bytes of each utf8
174
+ * string. */
175
+ utf8_constexpr14 utf8_nonnull utf8_pure int
176
+ utf8ncasecmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n);
177
+
178
+ /* Append the utf8 string src onto the utf8 string dst,
179
+ * writing at most n+1 bytes. Can produce an invalid utf8
180
+ * string if n falls partway through a utf8 codepoint. */
181
+ utf8_nonnull utf8_weak utf8_int8_t *
182
+ utf8ncat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src,
183
+ size_t n);
184
+
185
+ /* Return less than 0, 0, greater than 0 if src1 < src2,
186
+ * src1 == src2, src1 > src2 respectively. Checking at most n
187
+ * bytes of each utf8 string. */
188
+ utf8_constexpr14 utf8_nonnull utf8_pure int
189
+ utf8ncmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n);
190
+
191
+ /* Copy the utf8 string src onto the memory allocated in dst.
192
+ * Copies at most n bytes. If n falls partway through a utf8
193
+ * codepoint, or if dst doesn't have enough room for a null
194
+ * terminator, the final string will be cut short to preserve
195
+ * utf8 validity. */
196
+
197
+ utf8_nonnull utf8_weak utf8_int8_t *
198
+ utf8ncpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src,
199
+ size_t n);
200
+
201
+ /* Similar to utf8dup, except that at most n bytes of src are copied. If src is
202
+ * longer than n, only n bytes are copied and a null byte is added.
203
+ *
204
+ * Returns a new string if successful, 0 otherwise */
205
+ utf8_weak utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n);
206
+
207
+ /* Locates the first occurrence in the utf8 string str of any byte in the
208
+ * utf8 string accept, or 0 if no match was found. */
209
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
210
+ utf8pbrk(const utf8_int8_t *str, const utf8_int8_t *accept);
211
+
212
+ /* Find the last match of the utf8 codepoint chr in the utf8 string src. */
213
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
214
+ utf8rchr(const utf8_int8_t *src, int chr);
215
+
216
+ /* Number of bytes in the utf8 string str,
217
+ * including the null terminating byte. */
218
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8size(const utf8_int8_t *str);
219
+
220
+ /* Similar to utf8size, except that the null terminating byte is excluded. */
221
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t
222
+ utf8size_lazy(const utf8_int8_t *str);
223
+
224
+ /* Similar to utf8size, except that only at most n bytes of src are looked and
225
+ * the null terminating byte is excluded. */
226
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t
227
+ utf8nsize_lazy(const utf8_int8_t *str, size_t n);
228
+
229
+ /* Number of utf8 codepoints in the utf8 string src that consists entirely
230
+ * of utf8 codepoints from the utf8 string accept. */
231
+ utf8_constexpr14 utf8_nonnull utf8_pure size_t
232
+ utf8spn(const utf8_int8_t *src, const utf8_int8_t *accept);
233
+
234
+ /* The position of the utf8 string needle in the utf8 string haystack. */
235
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
236
+ utf8str(const utf8_int8_t *haystack, const utf8_int8_t *needle);
237
+
238
+ /* The position of the utf8 string needle in the utf8 string haystack, case
239
+ * insensitive. */
240
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
241
+ utf8casestr(const utf8_int8_t *haystack, const utf8_int8_t *needle);
242
+
243
+ /* Return 0 on success, or the position of the invalid
244
+ * utf8 codepoint on failure. */
245
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
246
+ utf8valid(const utf8_int8_t *str);
247
+
248
+ /* Similar to utf8valid, except that only at most n bytes of src are looked. */
249
+ utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t *
250
+ utf8nvalid(const utf8_int8_t *str, size_t n);
251
+
252
+ /* Given a null-terminated string, makes the string valid by replacing invalid
253
+ * codepoints with a 1-byte replacement. Returns 0 on success. */
254
+ utf8_nonnull utf8_weak int utf8makevalid(utf8_int8_t *str,
255
+ const utf8_int32_t replacement);
256
+
257
+ /* Sets out_codepoint to the current utf8 codepoint in str, and returns the
258
+ * address of the next utf8 codepoint after the current one in str. */
259
+ utf8_constexpr14 utf8_nonnull utf8_int8_t *
260
+ utf8codepoint(const utf8_int8_t *utf8_restrict str,
261
+ utf8_int32_t *utf8_restrict out_codepoint);
262
+
263
+ /* Calculates the size of the next utf8 codepoint in str. */
264
+ utf8_constexpr14 utf8_nonnull size_t
265
+ utf8codepointcalcsize(const utf8_int8_t *str);
266
+
267
+ /* Returns the size of the given codepoint in bytes. */
268
+ utf8_constexpr14 size_t utf8codepointsize(utf8_int32_t chr);
269
+
270
+ /* Write a codepoint to the given string, and return the address to the next
271
+ * place after the written codepoint. Pass how many bytes left in the buffer to
272
+ * n. If there is not enough space for the codepoint, this function returns
273
+ * null. */
274
+ utf8_nonnull utf8_weak utf8_int8_t *
275
+ utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n);
276
+
277
+ /* Returns 1 if the given character is lowercase, or 0 if it is not. */
278
+ utf8_constexpr14 int utf8islower(utf8_int32_t chr);
279
+
280
+ /* Returns 1 if the given character is uppercase, or 0 if it is not. */
281
+ utf8_constexpr14 int utf8isupper(utf8_int32_t chr);
282
+
283
+ /* Transform the given string into all lowercase codepoints. */
284
+ utf8_nonnull utf8_weak void utf8lwr(utf8_int8_t *utf8_restrict str);
285
+
286
+ /* Transform the given string into all uppercase codepoints. */
287
+ utf8_nonnull utf8_weak void utf8upr(utf8_int8_t *utf8_restrict str);
288
+
289
+ /* Make a codepoint lower case if possible. */
290
+ utf8_constexpr14 utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp);
291
+
292
+ /* Make a codepoint upper case if possible. */
293
+ utf8_constexpr14 utf8_int32_t utf8uprcodepoint(utf8_int32_t cp);
294
+
295
+ /* Sets out_codepoint to the current utf8 codepoint in str, and returns the
296
+ * address of the previous utf8 codepoint before the current one in str. */
297
+ utf8_constexpr14 utf8_nonnull utf8_int8_t *
298
+ utf8rcodepoint(const utf8_int8_t *utf8_restrict str,
299
+ utf8_int32_t *utf8_restrict out_codepoint);
300
+
301
+ /* Duplicate the utf8 string src by getting its size, calling alloc_func_ptr to
302
+ * copy over data to a new buffer, and returning that. Or 0 if alloc_func_ptr
303
+ * returned null. */
304
+ utf8_weak utf8_int8_t *utf8dup_ex(const utf8_int8_t *src,
305
+ utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *,
306
+ size_t),
307
+ utf8_int8_t *user_data);
308
+
309
+ /* Similar to utf8dup, except that at most n bytes of src are copied. If src is
310
+ * longer than n, only n bytes are copied and a null byte is added.
311
+ *
312
+ * Returns a new string if successful, 0 otherwise. */
313
+ utf8_weak utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n,
314
+ utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *,
315
+ size_t),
316
+ utf8_int8_t *user_data);
317
+
318
+ #undef utf8_weak
319
+ #undef utf8_pure
320
+ #undef utf8_nonnull
321
+
322
+ utf8_constexpr14_impl int utf8casecmp(const utf8_int8_t *src1,
323
+ const utf8_int8_t *src2) {
324
+ utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0,
325
+ src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0;
326
+
327
+ for (;;) {
328
+ src1 = utf8codepoint(src1, &src1_orig_cp);
329
+ src2 = utf8codepoint(src2, &src2_orig_cp);
330
+
331
+ /* lower the srcs if required */
332
+ src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp);
333
+ src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp);
334
+
335
+ /* lower the srcs if required */
336
+ src1_upr_cp = utf8uprcodepoint(src1_orig_cp);
337
+ src2_upr_cp = utf8uprcodepoint(src2_orig_cp);
338
+
339
+ /* check if the lowered codepoints match */
340
+ if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) {
341
+ return 0;
342
+ } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) {
343
+ continue;
344
+ }
345
+
346
+ /* if they don't match, then we return the difference between the characters
347
+ */
348
+ return src1_lwr_cp - src2_lwr_cp;
349
+ }
350
+ }
351
+
352
+ utf8_int8_t *utf8cat(utf8_int8_t *utf8_restrict dst,
353
+ const utf8_int8_t *utf8_restrict src) {
354
+ utf8_int8_t *d = dst;
355
+ /* find the null terminating byte in dst */
356
+ while ('\0' != *d) {
357
+ d++;
358
+ }
359
+
360
+ /* overwriting the null terminating byte in dst, append src byte-by-byte */
361
+ while ('\0' != *src) {
362
+ *d++ = *src++;
363
+ }
364
+
365
+ /* write out a new null terminating byte into dst */
366
+ *d = '\0';
367
+
368
+ return dst;
369
+ }
370
+
371
+ utf8_constexpr14_impl utf8_int8_t *utf8chr(const utf8_int8_t *src,
372
+ utf8_int32_t chr) {
373
+ utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'};
374
+
375
+ if (0 == chr) {
376
+ /* being asked to return position of null terminating byte, so
377
+ * just run s to the end, and return! */
378
+ while ('\0' != *src) {
379
+ src++;
380
+ }
381
+ return (utf8_int8_t *)src;
382
+ } else if (0 == ((utf8_int32_t)0xffffff80 & chr)) {
383
+ /* 1-byte/7-bit ascii
384
+ * (0b0xxxxxxx) */
385
+ c[0] = (utf8_int8_t)chr;
386
+ } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) {
387
+ /* 2-byte/11-bit utf8 code point
388
+ * (0b110xxxxx 0b10xxxxxx) */
389
+ c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6));
390
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
391
+ } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) {
392
+ /* 3-byte/16-bit utf8 code point
393
+ * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */
394
+ c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12));
395
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
396
+ c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
397
+ } else { /* if (0 == ((int)0xffe00000 & chr)) { */
398
+ /* 4-byte/21-bit utf8 code point
399
+ * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */
400
+ c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18));
401
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f));
402
+ c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
403
+ c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
404
+ }
405
+
406
+ /* we've made c into a 2 utf8 codepoint string, one for the chr we are
407
+ * seeking, another for the null terminating byte. Now use utf8str to
408
+ * search */
409
+ return utf8str(src, c);
410
+ }
411
+
412
+ utf8_constexpr14_impl int utf8cmp(const utf8_int8_t *src1,
413
+ const utf8_int8_t *src2) {
414
+ while (('\0' != *src1) || ('\0' != *src2)) {
415
+ if (*src1 < *src2) {
416
+ return -1;
417
+ } else if (*src1 > *src2) {
418
+ return 1;
419
+ }
420
+
421
+ src1++;
422
+ src2++;
423
+ }
424
+
425
+ /* both utf8 strings matched */
426
+ return 0;
427
+ }
428
+
429
+ utf8_constexpr14_impl int utf8coll(const utf8_int8_t *src1,
430
+ const utf8_int8_t *src2);
431
+
432
+ utf8_int8_t *utf8cpy(utf8_int8_t *utf8_restrict dst,
433
+ const utf8_int8_t *utf8_restrict src) {
434
+ utf8_int8_t *d = dst;
435
+
436
+ /* overwriting anything previously in dst, write byte-by-byte
437
+ * from src */
438
+ while ('\0' != *src) {
439
+ *d++ = *src++;
440
+ }
441
+
442
+ /* append null terminating byte */
443
+ *d = '\0';
444
+
445
+ return dst;
446
+ }
447
+
448
+ utf8_constexpr14_impl size_t utf8cspn(const utf8_int8_t *src,
449
+ const utf8_int8_t *reject) {
450
+ size_t chars = 0;
451
+
452
+ while ('\0' != *src) {
453
+ const utf8_int8_t *r = reject;
454
+ size_t offset = 0;
455
+
456
+ while ('\0' != *r) {
457
+ /* checking that if *r is the start of a utf8 codepoint
458
+ * (it is not 0b10xxxxxx) and we have successfully matched
459
+ * a previous character (0 < offset) - we found a match */
460
+ if ((0x80 != (0xc0 & *r)) && (0 < offset)) {
461
+ return chars;
462
+ } else {
463
+ if (*r == src[offset]) {
464
+ /* part of a utf8 codepoint matched, so move our checking
465
+ * onwards to the next byte */
466
+ offset++;
467
+ r++;
468
+ } else {
469
+ /* r could be in the middle of an unmatching utf8 code point,
470
+ * so we need to march it on to the next character beginning, */
471
+
472
+ do {
473
+ r++;
474
+ } while (0x80 == (0xc0 & *r));
475
+
476
+ /* reset offset too as we found a mismatch */
477
+ offset = 0;
478
+ }
479
+ }
480
+ }
481
+
482
+ /* found a match at the end of *r, so didn't get a chance to test it */
483
+ if (0 < offset) {
484
+ return chars;
485
+ }
486
+
487
+ /* the current utf8 codepoint in src did not match reject, but src
488
+ * could have been partway through a utf8 codepoint, so we need to
489
+ * march it onto the next utf8 codepoint starting byte */
490
+ do {
491
+ src++;
492
+ } while ((0x80 == (0xc0 & *src)));
493
+ chars++;
494
+ }
495
+
496
+ return chars;
497
+ }
498
+
499
+ utf8_int8_t *utf8dup(const utf8_int8_t *src) {
500
+ return utf8dup_ex(src, utf8_null, utf8_null);
501
+ }
502
+
503
+ utf8_int8_t *utf8dup_ex(const utf8_int8_t *src,
504
+ utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t),
505
+ utf8_int8_t *user_data) {
506
+ utf8_int8_t *n = utf8_null;
507
+
508
+ /* figure out how many bytes (including the terminator) we need to copy first
509
+ */
510
+ size_t bytes = utf8size(src);
511
+
512
+ if (alloc_func_ptr) {
513
+ n = alloc_func_ptr(user_data, bytes);
514
+ } else {
515
+ #if !defined(UTF8_NO_STD_MALLOC)
516
+ n = (utf8_int8_t *)malloc(bytes);
517
+ #else
518
+ return utf8_null;
519
+ #endif
520
+ }
521
+
522
+ if (utf8_null == n) {
523
+ /* out of memory so we bail */
524
+ return utf8_null;
525
+ } else {
526
+ bytes = 0;
527
+
528
+ /* copy src byte-by-byte into our new utf8 string */
529
+ while ('\0' != src[bytes]) {
530
+ n[bytes] = src[bytes];
531
+ bytes++;
532
+ }
533
+
534
+ /* append null terminating byte */
535
+ n[bytes] = '\0';
536
+ return n;
537
+ }
538
+ }
539
+
540
+ utf8_constexpr14_impl utf8_int8_t *utf8fry(const utf8_int8_t *str);
541
+
542
+ utf8_constexpr14_impl size_t utf8len(const utf8_int8_t *str) {
543
+ return utf8nlen(str, SIZE_MAX);
544
+ }
545
+
546
+ utf8_constexpr14_impl size_t utf8nlen(const utf8_int8_t *str, size_t n) {
547
+ const utf8_int8_t *t = str;
548
+ size_t length = 0;
549
+
550
+ while ((size_t)(str - t) < n && '\0' != *str) {
551
+ if (0xf0 == (0xf8 & *str)) {
552
+ /* 4-byte utf8 code point (began with 0b11110xxx) */
553
+ str += 4;
554
+ } else if (0xe0 == (0xf0 & *str)) {
555
+ /* 3-byte utf8 code point (began with 0b1110xxxx) */
556
+ str += 3;
557
+ } else if (0xc0 == (0xe0 & *str)) {
558
+ /* 2-byte utf8 code point (began with 0b110xxxxx) */
559
+ str += 2;
560
+ } else { /* if (0x00 == (0x80 & *s)) { */
561
+ /* 1-byte ascii (began with 0b0xxxxxxx) */
562
+ str += 1;
563
+ }
564
+
565
+ /* no matter the bytes we marched s forward by, it was
566
+ * only 1 utf8 codepoint */
567
+ length++;
568
+ }
569
+
570
+ if ((size_t)(str - t) > n) {
571
+ length--;
572
+ }
573
+ return length;
574
+ }
575
+
576
+ utf8_constexpr14_impl int utf8ncasecmp(const utf8_int8_t *src1,
577
+ const utf8_int8_t *src2, size_t n) {
578
+ utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0,
579
+ src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0;
580
+
581
+ do {
582
+ const utf8_int8_t *const s1 = src1;
583
+ const utf8_int8_t *const s2 = src2;
584
+
585
+ /* first check that we have enough bytes left in n to contain an entire
586
+ * codepoint */
587
+ if (0 == n) {
588
+ return 0;
589
+ }
590
+
591
+ if ((1 == n) && ((0xc0 == (0xe0 & *s1)) || (0xc0 == (0xe0 & *s2)))) {
592
+ const utf8_int32_t c1 = (0xe0 & *s1);
593
+ const utf8_int32_t c2 = (0xe0 & *s2);
594
+
595
+ if (c1 != c2) {
596
+ return c1 - c2;
597
+ } else {
598
+ return 0;
599
+ }
600
+ }
601
+
602
+ if ((2 >= n) && ((0xe0 == (0xf0 & *s1)) || (0xe0 == (0xf0 & *s2)))) {
603
+ const utf8_int32_t c1 = (0xf0 & *s1);
604
+ const utf8_int32_t c2 = (0xf0 & *s2);
605
+
606
+ if (c1 != c2) {
607
+ return c1 - c2;
608
+ } else {
609
+ return 0;
610
+ }
611
+ }
612
+
613
+ if ((3 >= n) && ((0xf0 == (0xf8 & *s1)) || (0xf0 == (0xf8 & *s2)))) {
614
+ const utf8_int32_t c1 = (0xf8 & *s1);
615
+ const utf8_int32_t c2 = (0xf8 & *s2);
616
+
617
+ if (c1 != c2) {
618
+ return c1 - c2;
619
+ } else {
620
+ return 0;
621
+ }
622
+ }
623
+
624
+ src1 = utf8codepoint(src1, &src1_orig_cp);
625
+ src2 = utf8codepoint(src2, &src2_orig_cp);
626
+ n -= utf8codepointsize(src1_orig_cp);
627
+
628
+ src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp);
629
+ src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp);
630
+
631
+ src1_upr_cp = utf8uprcodepoint(src1_orig_cp);
632
+ src2_upr_cp = utf8uprcodepoint(src2_orig_cp);
633
+
634
+ /* check if the lowered codepoints match */
635
+ if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) {
636
+ return 0;
637
+ } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) {
638
+ continue;
639
+ }
640
+
641
+ /* if they don't match, then we return the difference between the characters
642
+ */
643
+ return src1_lwr_cp - src2_lwr_cp;
644
+ } while (0 < n);
645
+
646
+ /* both utf8 strings matched */
647
+ return 0;
648
+ }
649
+
650
+ utf8_int8_t *utf8ncat(utf8_int8_t *utf8_restrict dst,
651
+ const utf8_int8_t *utf8_restrict src, size_t n) {
652
+ utf8_int8_t *d = dst;
653
+
654
+ /* find the null terminating byte in dst */
655
+ while ('\0' != *d) {
656
+ d++;
657
+ }
658
+
659
+ /* overwriting the null terminating byte in dst, append src byte-by-byte
660
+ * stopping if we run out of space */
661
+ while (('\0' != *src) && (0 != n--)) {
662
+ *d++ = *src++;
663
+ }
664
+
665
+ /* write out a new null terminating byte into dst */
666
+ *d = '\0';
667
+
668
+ return dst;
669
+ }
670
+
671
+ utf8_constexpr14_impl int utf8ncmp(const utf8_int8_t *src1,
672
+ const utf8_int8_t *src2, size_t n) {
673
+ while ((0 != n--) && (('\0' != *src1) || ('\0' != *src2))) {
674
+ if (*src1 < *src2) {
675
+ return -1;
676
+ } else if (*src1 > *src2) {
677
+ return 1;
678
+ }
679
+
680
+ src1++;
681
+ src2++;
682
+ }
683
+
684
+ /* both utf8 strings matched */
685
+ return 0;
686
+ }
687
+
688
+ utf8_int8_t *utf8ncpy(utf8_int8_t *utf8_restrict dst,
689
+ const utf8_int8_t *utf8_restrict src, size_t n) {
690
+ utf8_int8_t *d = dst;
691
+ size_t index = 0, check_index = 0;
692
+
693
+ if (n == 0) {
694
+ return dst;
695
+ }
696
+
697
+ /* overwriting anything previously in dst, write byte-by-byte
698
+ * from src */
699
+ for (index = 0; index < n; index++) {
700
+ d[index] = src[index];
701
+ if ('\0' == src[index]) {
702
+ break;
703
+ }
704
+ }
705
+
706
+ for (check_index = index - 1;
707
+ check_index > 0 && 0x80 == (0xc0 & d[check_index]); check_index--) {
708
+ /* just moving the index */
709
+ }
710
+
711
+ if (check_index < index &&
712
+ ((index - check_index) < utf8codepointcalcsize(&d[check_index]) ||
713
+ (index - check_index) == n)) {
714
+ index = check_index;
715
+ }
716
+
717
+ /* append null terminating byte */
718
+ for (; index < n; index++) {
719
+ d[index] = 0;
720
+ }
721
+
722
+ return dst;
723
+ }
724
+
725
+ utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n) {
726
+ return utf8ndup_ex(src, n, utf8_null, utf8_null);
727
+ }
728
+
729
+ utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n,
730
+ utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t),
731
+ utf8_int8_t *user_data) {
732
+ utf8_int8_t *c = utf8_null;
733
+ size_t bytes = 0;
734
+
735
+ /* Find the end of the string or stop when n is reached */
736
+ while ('\0' != src[bytes] && bytes < n) {
737
+ bytes++;
738
+ }
739
+
740
+ /* In case bytes is actually less than n, we need to set it
741
+ * to be used later in the copy byte by byte. */
742
+ n = bytes;
743
+
744
+ if (alloc_func_ptr) {
745
+ c = alloc_func_ptr(user_data, bytes + 1);
746
+ } else {
747
+ #if !defined(UTF8_NO_STD_MALLOC)
748
+ c = (utf8_int8_t *)malloc(bytes + 1);
749
+ #else
750
+ c = utf8_null;
751
+ #endif
752
+ }
753
+
754
+ if (utf8_null == c) {
755
+ /* out of memory so we bail */
756
+ return utf8_null;
757
+ }
758
+
759
+ bytes = 0;
760
+
761
+ /* copy src byte-by-byte into our new utf8 string */
762
+ while ('\0' != src[bytes] && bytes < n) {
763
+ c[bytes] = src[bytes];
764
+ bytes++;
765
+ }
766
+
767
+ /* append null terminating byte */
768
+ c[bytes] = '\0';
769
+ return c;
770
+ }
771
+
772
+ utf8_constexpr14_impl utf8_int8_t *utf8rchr(const utf8_int8_t *src, int chr) {
773
+
774
+ utf8_int8_t *match = utf8_null;
775
+ utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'};
776
+
777
+ if (0 == chr) {
778
+ /* being asked to return position of null terminating byte, so
779
+ * just run s to the end, and return! */
780
+ while ('\0' != *src) {
781
+ src++;
782
+ }
783
+ return (utf8_int8_t *)src;
784
+ } else if (0 == ((int)0xffffff80 & chr)) {
785
+ /* 1-byte/7-bit ascii
786
+ * (0b0xxxxxxx) */
787
+ c[0] = (utf8_int8_t)chr;
788
+ } else if (0 == ((int)0xfffff800 & chr)) {
789
+ /* 2-byte/11-bit utf8 code point
790
+ * (0b110xxxxx 0b10xxxxxx) */
791
+ c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6));
792
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
793
+ } else if (0 == ((int)0xffff0000 & chr)) {
794
+ /* 3-byte/16-bit utf8 code point
795
+ * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */
796
+ c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12));
797
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
798
+ c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
799
+ } else { /* if (0 == ((int)0xffe00000 & chr)) { */
800
+ /* 4-byte/21-bit utf8 code point
801
+ * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */
802
+ c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18));
803
+ c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f));
804
+ c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
805
+ c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
806
+ }
807
+
808
+ /* we've created a 2 utf8 codepoint string in c that is
809
+ * the utf8 character asked for by chr, and a null
810
+ * terminating byte */
811
+
812
+ while ('\0' != *src) {
813
+ size_t offset = 0;
814
+
815
+ while ((src[offset] == c[offset]) && ('\0' != src[offset])) {
816
+ offset++;
817
+ }
818
+
819
+ if ('\0' == c[offset]) {
820
+ /* we found a matching utf8 code point */
821
+ match = (utf8_int8_t *)src;
822
+ src += offset;
823
+
824
+ if ('\0' == *src) {
825
+ break;
826
+ }
827
+ } else {
828
+ src += offset;
829
+
830
+ /* need to march s along to next utf8 codepoint start
831
+ * (the next byte that doesn't match 0b10xxxxxx) */
832
+ if ('\0' != *src) {
833
+ do {
834
+ src++;
835
+ } while (0x80 == (0xc0 & *src));
836
+ }
837
+ }
838
+ }
839
+
840
+ /* return the last match we found (or 0 if no match was found) */
841
+ return match;
842
+ }
843
+
844
+ utf8_constexpr14_impl utf8_int8_t *utf8pbrk(const utf8_int8_t *str,
845
+ const utf8_int8_t *accept) {
846
+ while ('\0' != *str) {
847
+ const utf8_int8_t *a = accept;
848
+ size_t offset = 0;
849
+
850
+ while ('\0' != *a) {
851
+ /* checking that if *a is the start of a utf8 codepoint
852
+ * (it is not 0b10xxxxxx) and we have successfully matched
853
+ * a previous character (0 < offset) - we found a match */
854
+ if ((0x80 != (0xc0 & *a)) && (0 < offset)) {
855
+ return (utf8_int8_t *)str;
856
+ } else {
857
+ if (*a == str[offset]) {
858
+ /* part of a utf8 codepoint matched, so move our checking
859
+ * onwards to the next byte */
860
+ offset++;
861
+ a++;
862
+ } else {
863
+ /* r could be in the middle of an unmatching utf8 code point,
864
+ * so we need to march it on to the next character beginning, */
865
+
866
+ do {
867
+ a++;
868
+ } while (0x80 == (0xc0 & *a));
869
+
870
+ /* reset offset too as we found a mismatch */
871
+ offset = 0;
872
+ }
873
+ }
874
+ }
875
+
876
+ /* we found a match on the last utf8 codepoint */
877
+ if (0 < offset) {
878
+ return (utf8_int8_t *)str;
879
+ }
880
+
881
+ /* the current utf8 codepoint in src did not match accept, but src
882
+ * could have been partway through a utf8 codepoint, so we need to
883
+ * march it onto the next utf8 codepoint starting byte */
884
+ do {
885
+ str++;
886
+ } while ((0x80 == (0xc0 & *str)));
887
+ }
888
+
889
+ return utf8_null;
890
+ }
891
+
892
+ utf8_constexpr14_impl size_t utf8size(const utf8_int8_t *str) {
893
+ return utf8size_lazy(str) + 1;
894
+ }
895
+
896
+ utf8_constexpr14_impl size_t utf8size_lazy(const utf8_int8_t *str) {
897
+ return utf8nsize_lazy(str, SIZE_MAX);
898
+ }
899
+
900
+ utf8_constexpr14_impl size_t utf8nsize_lazy(const utf8_int8_t *str, size_t n) {
901
+ size_t size = 0;
902
+ while (size < n && '\0' != str[size]) {
903
+ size++;
904
+ }
905
+ return size;
906
+ }
907
+
908
+ utf8_constexpr14_impl size_t utf8spn(const utf8_int8_t *src,
909
+ const utf8_int8_t *accept) {
910
+ size_t chars = 0;
911
+
912
+ while ('\0' != *src) {
913
+ const utf8_int8_t *a = accept;
914
+ size_t offset = 0;
915
+
916
+ while ('\0' != *a) {
917
+ /* checking that if *r is the start of a utf8 codepoint
918
+ * (it is not 0b10xxxxxx) and we have successfully matched
919
+ * a previous character (0 < offset) - we found a match */
920
+ if ((0x80 != (0xc0 & *a)) && (0 < offset)) {
921
+ /* found a match, so increment the number of utf8 codepoints
922
+ * that have matched and stop checking whether any other utf8
923
+ * codepoints in a match */
924
+ chars++;
925
+ src += offset;
926
+ offset = 0;
927
+ break;
928
+ } else {
929
+ if (*a == src[offset]) {
930
+ offset++;
931
+ a++;
932
+ } else {
933
+ /* a could be in the middle of an unmatching utf8 codepoint,
934
+ * so we need to march it on to the next character beginning, */
935
+ do {
936
+ a++;
937
+ } while (0x80 == (0xc0 & *a));
938
+
939
+ /* reset offset too as we found a mismatch */
940
+ offset = 0;
941
+ }
942
+ }
943
+ }
944
+
945
+ /* found a match at the end of *a, so didn't get a chance to test it */
946
+ if (0 < offset) {
947
+ chars++;
948
+ src += offset;
949
+ continue;
950
+ }
951
+
952
+ /* if a got to its terminating null byte, then we didn't find a match.
953
+ * Return the current number of matched utf8 codepoints */
954
+ if ('\0' == *a) {
955
+ return chars;
956
+ }
957
+ }
958
+
959
+ return chars;
960
+ }
961
+
962
+ utf8_constexpr14_impl utf8_int8_t *utf8str(const utf8_int8_t *haystack,
963
+ const utf8_int8_t *needle) {
964
+ utf8_int32_t throwaway_codepoint = 0;
965
+
966
+ /* if needle has no utf8 codepoints before the null terminating
967
+ * byte then return haystack */
968
+ if ('\0' == *needle) {
969
+ return (utf8_int8_t *)haystack;
970
+ }
971
+
972
+ while ('\0' != *haystack) {
973
+ const utf8_int8_t *maybeMatch = haystack;
974
+ const utf8_int8_t *n = needle;
975
+
976
+ while (*haystack == *n && (*haystack != '\0' && *n != '\0')) {
977
+ n++;
978
+ haystack++;
979
+ }
980
+
981
+ if ('\0' == *n) {
982
+ /* we found the whole utf8 string for needle in haystack at
983
+ * maybeMatch, so return it */
984
+ return (utf8_int8_t *)maybeMatch;
985
+ } else {
986
+ /* h could be in the middle of an unmatching utf8 codepoint,
987
+ * so we need to march it on to the next character beginning
988
+ * starting from the current character */
989
+ haystack = utf8codepoint(maybeMatch, &throwaway_codepoint);
990
+ }
991
+ }
992
+
993
+ /* no match */
994
+ return utf8_null;
995
+ }
996
+
997
+ utf8_constexpr14_impl utf8_int8_t *utf8casestr(const utf8_int8_t *haystack,
998
+ const utf8_int8_t *needle) {
999
+ /* if needle has no utf8 codepoints before the null terminating
1000
+ * byte then return haystack */
1001
+ if ('\0' == *needle) {
1002
+ return (utf8_int8_t *)haystack;
1003
+ }
1004
+
1005
+ for (;;) {
1006
+ const utf8_int8_t *maybeMatch = haystack;
1007
+ const utf8_int8_t *n = needle;
1008
+ utf8_int32_t h_cp = 0, n_cp = 0;
1009
+
1010
+ /* Get the next code point and track it */
1011
+ const utf8_int8_t *nextH = haystack = utf8codepoint(haystack, &h_cp);
1012
+ n = utf8codepoint(n, &n_cp);
1013
+
1014
+ while ((0 != h_cp) && (0 != n_cp)) {
1015
+ h_cp = utf8lwrcodepoint(h_cp);
1016
+ n_cp = utf8lwrcodepoint(n_cp);
1017
+
1018
+ /* if we find a mismatch, bail out! */
1019
+ if (h_cp != n_cp) {
1020
+ break;
1021
+ }
1022
+
1023
+ haystack = utf8codepoint(haystack, &h_cp);
1024
+ n = utf8codepoint(n, &n_cp);
1025
+ }
1026
+
1027
+ if (0 == n_cp) {
1028
+ /* we found the whole utf8 string for needle in haystack at
1029
+ * maybeMatch, so return it */
1030
+ return (utf8_int8_t *)maybeMatch;
1031
+ }
1032
+
1033
+ if (0 == h_cp) {
1034
+ /* no match */
1035
+ return utf8_null;
1036
+ }
1037
+
1038
+ /* Roll back to the next code point in the haystack to test */
1039
+ haystack = nextH;
1040
+ }
1041
+ }
1042
+
1043
+ utf8_constexpr14_impl utf8_int8_t *utf8valid(const utf8_int8_t *str) {
1044
+ return utf8nvalid(str, SIZE_MAX);
1045
+ }
1046
+
1047
+ utf8_constexpr14_impl utf8_int8_t *utf8nvalid(const utf8_int8_t *str,
1048
+ size_t n) {
1049
+ const utf8_int8_t *t = str;
1050
+ size_t consumed = 0;
1051
+
1052
+ while ((void)(consumed = (size_t)(str - t)), consumed < n && '\0' != *str) {
1053
+ const size_t remaining = n - consumed;
1054
+
1055
+ if (0xf0 == (0xf8 & *str)) {
1056
+ /* ensure that there's 4 bytes or more remaining */
1057
+ if (remaining < 4) {
1058
+ return (utf8_int8_t *)str;
1059
+ }
1060
+
1061
+ /* ensure each of the 3 following bytes in this 4-byte
1062
+ * utf8 codepoint began with 0b10xxxxxx */
1063
+ if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2])) ||
1064
+ (0x80 != (0xc0 & str[3]))) {
1065
+ return (utf8_int8_t *)str;
1066
+ }
1067
+
1068
+ /* ensure that our utf8 codepoint ended after 4 bytes */
1069
+ if ((remaining != 4) && (0x80 == (0xc0 & str[4]))) {
1070
+ return (utf8_int8_t *)str;
1071
+ }
1072
+
1073
+ /* ensure that the top 5 bits of this 4-byte utf8
1074
+ * codepoint were not 0, as then we could have used
1075
+ * one of the smaller encodings */
1076
+ if ((0 == (0x07 & str[0])) && (0 == (0x30 & str[1]))) {
1077
+ return (utf8_int8_t *)str;
1078
+ }
1079
+
1080
+ /* 4-byte utf8 code point (began with 0b11110xxx) */
1081
+ str += 4;
1082
+ } else if (0xe0 == (0xf0 & *str)) {
1083
+ /* ensure that there's 3 bytes or more remaining */
1084
+ if (remaining < 3) {
1085
+ return (utf8_int8_t *)str;
1086
+ }
1087
+
1088
+ /* ensure each of the 2 following bytes in this 3-byte
1089
+ * utf8 codepoint began with 0b10xxxxxx */
1090
+ if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2]))) {
1091
+ return (utf8_int8_t *)str;
1092
+ }
1093
+
1094
+ /* ensure that our utf8 codepoint ended after 3 bytes */
1095
+ if ((remaining != 3) && (0x80 == (0xc0 & str[3]))) {
1096
+ return (utf8_int8_t *)str;
1097
+ }
1098
+
1099
+ /* ensure that the top 5 bits of this 3-byte utf8
1100
+ * codepoint were not 0, as then we could have used
1101
+ * one of the smaller encodings */
1102
+ if ((0 == (0x0f & str[0])) && (0 == (0x20 & str[1]))) {
1103
+ return (utf8_int8_t *)str;
1104
+ }
1105
+
1106
+ /* 3-byte utf8 code point (began with 0b1110xxxx) */
1107
+ str += 3;
1108
+ } else if (0xc0 == (0xe0 & *str)) {
1109
+ /* ensure that there's 2 bytes or more remaining */
1110
+ if (remaining < 2) {
1111
+ return (utf8_int8_t *)str;
1112
+ }
1113
+
1114
+ /* ensure the 1 following byte in this 2-byte
1115
+ * utf8 codepoint began with 0b10xxxxxx */
1116
+ if (0x80 != (0xc0 & str[1])) {
1117
+ return (utf8_int8_t *)str;
1118
+ }
1119
+
1120
+ /* ensure that our utf8 codepoint ended after 2 bytes */
1121
+ if ((remaining != 2) && (0x80 == (0xc0 & str[2]))) {
1122
+ return (utf8_int8_t *)str;
1123
+ }
1124
+
1125
+ /* ensure that the top 4 bits of this 2-byte utf8
1126
+ * codepoint were not 0, as then we could have used
1127
+ * one of the smaller encodings */
1128
+ if (0 == (0x1e & str[0])) {
1129
+ return (utf8_int8_t *)str;
1130
+ }
1131
+
1132
+ /* 2-byte utf8 code point (began with 0b110xxxxx) */
1133
+ str += 2;
1134
+ } else if (0x00 == (0x80 & *str)) {
1135
+ /* 1-byte ascii (began with 0b0xxxxxxx) */
1136
+ str += 1;
1137
+ } else {
1138
+ /* we have an invalid 0b1xxxxxxx utf8 code point entry */
1139
+ return (utf8_int8_t *)str;
1140
+ }
1141
+ }
1142
+
1143
+ return utf8_null;
1144
+ }
1145
+
1146
+ int utf8makevalid(utf8_int8_t *str, const utf8_int32_t replacement) {
1147
+ utf8_int8_t *read = str;
1148
+ utf8_int8_t *write = read;
1149
+ const utf8_int8_t r = (utf8_int8_t)replacement;
1150
+ utf8_int32_t codepoint = 0;
1151
+
1152
+ if (replacement > 0x7f) {
1153
+ return -1;
1154
+ }
1155
+
1156
+ while ('\0' != *read) {
1157
+ if (0xf0 == (0xf8 & *read)) {
1158
+ /* ensure each of the 3 following bytes in this 4-byte
1159
+ * utf8 codepoint began with 0b10xxxxxx */
1160
+ if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2])) ||
1161
+ (0x80 != (0xc0 & read[3]))) {
1162
+ *write++ = r;
1163
+ read++;
1164
+ continue;
1165
+ }
1166
+
1167
+ /* 4-byte utf8 code point (began with 0b11110xxx) */
1168
+ read = utf8codepoint(read, &codepoint);
1169
+ write = utf8catcodepoint(write, codepoint, 4);
1170
+ } else if (0xe0 == (0xf0 & *read)) {
1171
+ /* ensure each of the 2 following bytes in this 3-byte
1172
+ * utf8 codepoint began with 0b10xxxxxx */
1173
+ if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2]))) {
1174
+ *write++ = r;
1175
+ read++;
1176
+ continue;
1177
+ }
1178
+
1179
+ /* 3-byte utf8 code point (began with 0b1110xxxx) */
1180
+ read = utf8codepoint(read, &codepoint);
1181
+ write = utf8catcodepoint(write, codepoint, 3);
1182
+ } else if (0xc0 == (0xe0 & *read)) {
1183
+ /* ensure the 1 following byte in this 2-byte
1184
+ * utf8 codepoint began with 0b10xxxxxx */
1185
+ if (0x80 != (0xc0 & read[1])) {
1186
+ *write++ = r;
1187
+ read++;
1188
+ continue;
1189
+ }
1190
+
1191
+ /* 2-byte utf8 code point (began with 0b110xxxxx) */
1192
+ read = utf8codepoint(read, &codepoint);
1193
+ write = utf8catcodepoint(write, codepoint, 2);
1194
+ } else if (0x00 == (0x80 & *read)) {
1195
+ /* 1-byte ascii (began with 0b0xxxxxxx) */
1196
+ read = utf8codepoint(read, &codepoint);
1197
+ write = utf8catcodepoint(write, codepoint, 1);
1198
+ } else {
1199
+ /* if we got here then we've got a dangling continuation (0b10xxxxxx) */
1200
+ *write++ = r;
1201
+ read++;
1202
+ continue;
1203
+ }
1204
+ }
1205
+
1206
+ *write = '\0';
1207
+
1208
+ return 0;
1209
+ }
1210
+
1211
+ utf8_constexpr14_impl utf8_int8_t *
1212
+ utf8codepoint(const utf8_int8_t *utf8_restrict str,
1213
+ utf8_int32_t *utf8_restrict out_codepoint) {
1214
+ if (0xf0 == (0xf8 & str[0])) {
1215
+ /* 4 byte utf8 codepoint */
1216
+ *out_codepoint = ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) |
1217
+ ((0x3f & str[2]) << 6) | (0x3f & str[3]);
1218
+ str += 4;
1219
+ } else if (0xe0 == (0xf0 & str[0])) {
1220
+ /* 3 byte utf8 codepoint */
1221
+ *out_codepoint =
1222
+ ((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]);
1223
+ str += 3;
1224
+ } else if (0xc0 == (0xe0 & str[0])) {
1225
+ /* 2 byte utf8 codepoint */
1226
+ *out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]);
1227
+ str += 2;
1228
+ } else {
1229
+ /* 1 byte utf8 codepoint otherwise */
1230
+ *out_codepoint = str[0];
1231
+ str += 1;
1232
+ }
1233
+
1234
+ return (utf8_int8_t *)str;
1235
+ }
1236
+
1237
+ utf8_constexpr14_impl size_t utf8codepointcalcsize(const utf8_int8_t *str) {
1238
+ if (0xf0 == (0xf8 & str[0])) {
1239
+ /* 4 byte utf8 codepoint */
1240
+ return 4;
1241
+ } else if (0xe0 == (0xf0 & str[0])) {
1242
+ /* 3 byte utf8 codepoint */
1243
+ return 3;
1244
+ } else if (0xc0 == (0xe0 & str[0])) {
1245
+ /* 2 byte utf8 codepoint */
1246
+ return 2;
1247
+ }
1248
+
1249
+ /* 1 byte utf8 codepoint otherwise */
1250
+ return 1;
1251
+ }
1252
+
1253
+ utf8_constexpr14_impl size_t utf8codepointsize(utf8_int32_t chr) {
1254
+ if (0 == ((utf8_int32_t)0xffffff80 & chr)) {
1255
+ return 1;
1256
+ } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) {
1257
+ return 2;
1258
+ } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) {
1259
+ return 3;
1260
+ } else { /* if (0 == ((int)0xffe00000 & chr)) { */
1261
+ return 4;
1262
+ }
1263
+ }
1264
+
1265
+ utf8_int8_t *utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n) {
1266
+ if (0 == ((utf8_int32_t)0xffffff80 & chr)) {
1267
+ /* 1-byte/7-bit ascii
1268
+ * (0b0xxxxxxx) */
1269
+ if (n < 1) {
1270
+ return utf8_null;
1271
+ }
1272
+ str[0] = (utf8_int8_t)chr;
1273
+ str += 1;
1274
+ } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) {
1275
+ /* 2-byte/11-bit utf8 code point
1276
+ * (0b110xxxxx 0b10xxxxxx) */
1277
+ if (n < 2) {
1278
+ return utf8_null;
1279
+ }
1280
+ str[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)((chr >> 6) & 0x1f));
1281
+ str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
1282
+ str += 2;
1283
+ } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) {
1284
+ /* 3-byte/16-bit utf8 code point
1285
+ * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */
1286
+ if (n < 3) {
1287
+ return utf8_null;
1288
+ }
1289
+ str[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)((chr >> 12) & 0x0f));
1290
+ str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
1291
+ str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
1292
+ str += 3;
1293
+ } else { /* if (0 == ((int)0xffe00000 & chr)) { */
1294
+ /* 4-byte/21-bit utf8 code point
1295
+ * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */
1296
+ if (n < 4) {
1297
+ return utf8_null;
1298
+ }
1299
+ str[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)((chr >> 18) & 0x07));
1300
+ str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f));
1301
+ str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f));
1302
+ str[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f));
1303
+ str += 4;
1304
+ }
1305
+
1306
+ return str;
1307
+ }
1308
+
1309
+ utf8_constexpr14_impl int utf8islower(utf8_int32_t chr) {
1310
+ return chr != utf8uprcodepoint(chr);
1311
+ }
1312
+
1313
+ utf8_constexpr14_impl int utf8isupper(utf8_int32_t chr) {
1314
+ return chr != utf8lwrcodepoint(chr);
1315
+ }
1316
+
1317
+ void utf8lwr(utf8_int8_t *utf8_restrict str) {
1318
+ utf8_int32_t cp = 0;
1319
+ utf8_int8_t *pn = utf8codepoint(str, &cp);
1320
+
1321
+ while (cp != 0) {
1322
+ const utf8_int32_t lwr_cp = utf8lwrcodepoint(cp);
1323
+ const size_t size = utf8codepointsize(lwr_cp);
1324
+
1325
+ if (lwr_cp != cp) {
1326
+ utf8catcodepoint(str, lwr_cp, size);
1327
+ }
1328
+
1329
+ str = pn;
1330
+ pn = utf8codepoint(str, &cp);
1331
+ }
1332
+ }
1333
+
1334
+ void utf8upr(utf8_int8_t *utf8_restrict str) {
1335
+ utf8_int32_t cp = 0;
1336
+ utf8_int8_t *pn = utf8codepoint(str, &cp);
1337
+
1338
+ while (cp != 0) {
1339
+ const utf8_int32_t lwr_cp = utf8uprcodepoint(cp);
1340
+ const size_t size = utf8codepointsize(lwr_cp);
1341
+
1342
+ if (lwr_cp != cp) {
1343
+ utf8catcodepoint(str, lwr_cp, size);
1344
+ }
1345
+
1346
+ str = pn;
1347
+ pn = utf8codepoint(str, &cp);
1348
+ }
1349
+ }
1350
+
1351
+ utf8_constexpr14_impl utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp) {
1352
+ if (((0x0041 <= cp) && (0x005a >= cp)) ||
1353
+ ((0x00c0 <= cp) && (0x00d6 >= cp)) ||
1354
+ ((0x00d8 <= cp) && (0x00de >= cp)) ||
1355
+ ((0x0391 <= cp) && (0x03a1 >= cp)) ||
1356
+ ((0x03a3 <= cp) && (0x03ab >= cp)) ||
1357
+ ((0x0410 <= cp) && (0x042f >= cp))) {
1358
+ cp += 32;
1359
+ } else if ((0x0400 <= cp) && (0x040f >= cp)) {
1360
+ cp += 80;
1361
+ } else if (((0x0100 <= cp) && (0x012f >= cp)) ||
1362
+ ((0x0132 <= cp) && (0x0137 >= cp)) ||
1363
+ ((0x014a <= cp) && (0x0177 >= cp)) ||
1364
+ ((0x0182 <= cp) && (0x0185 >= cp)) ||
1365
+ ((0x01a0 <= cp) && (0x01a5 >= cp)) ||
1366
+ ((0x01de <= cp) && (0x01ef >= cp)) ||
1367
+ ((0x01f8 <= cp) && (0x021f >= cp)) ||
1368
+ ((0x0222 <= cp) && (0x0233 >= cp)) ||
1369
+ ((0x0246 <= cp) && (0x024f >= cp)) ||
1370
+ ((0x03d8 <= cp) && (0x03ef >= cp)) ||
1371
+ ((0x0460 <= cp) && (0x0481 >= cp)) ||
1372
+ ((0x048a <= cp) && (0x04ff >= cp))) {
1373
+ cp |= 0x1;
1374
+ } else if (((0x0139 <= cp) && (0x0148 >= cp)) ||
1375
+ ((0x0179 <= cp) && (0x017e >= cp)) ||
1376
+ ((0x01af <= cp) && (0x01b0 >= cp)) ||
1377
+ ((0x01b3 <= cp) && (0x01b6 >= cp)) ||
1378
+ ((0x01cd <= cp) && (0x01dc >= cp))) {
1379
+ cp += 1;
1380
+ cp &= ~0x1;
1381
+ } else {
1382
+ switch (cp) {
1383
+ default:
1384
+ break;
1385
+ case 0x0178:
1386
+ cp = 0x00ff;
1387
+ break;
1388
+ case 0x0243:
1389
+ cp = 0x0180;
1390
+ break;
1391
+ case 0x018e:
1392
+ cp = 0x01dd;
1393
+ break;
1394
+ case 0x023d:
1395
+ cp = 0x019a;
1396
+ break;
1397
+ case 0x0220:
1398
+ cp = 0x019e;
1399
+ break;
1400
+ case 0x01b7:
1401
+ cp = 0x0292;
1402
+ break;
1403
+ case 0x01c4:
1404
+ cp = 0x01c6;
1405
+ break;
1406
+ case 0x01c7:
1407
+ cp = 0x01c9;
1408
+ break;
1409
+ case 0x01ca:
1410
+ cp = 0x01cc;
1411
+ break;
1412
+ case 0x01f1:
1413
+ cp = 0x01f3;
1414
+ break;
1415
+ case 0x01f7:
1416
+ cp = 0x01bf;
1417
+ break;
1418
+ case 0x0187:
1419
+ cp = 0x0188;
1420
+ break;
1421
+ case 0x018b:
1422
+ cp = 0x018c;
1423
+ break;
1424
+ case 0x0191:
1425
+ cp = 0x0192;
1426
+ break;
1427
+ case 0x0198:
1428
+ cp = 0x0199;
1429
+ break;
1430
+ case 0x01a7:
1431
+ cp = 0x01a8;
1432
+ break;
1433
+ case 0x01ac:
1434
+ cp = 0x01ad;
1435
+ break;
1436
+ case 0x01b8:
1437
+ cp = 0x01b9;
1438
+ break;
1439
+ case 0x01bc:
1440
+ cp = 0x01bd;
1441
+ break;
1442
+ case 0x01f4:
1443
+ cp = 0x01f5;
1444
+ break;
1445
+ case 0x023b:
1446
+ cp = 0x023c;
1447
+ break;
1448
+ case 0x0241:
1449
+ cp = 0x0242;
1450
+ break;
1451
+ case 0x03fd:
1452
+ cp = 0x037b;
1453
+ break;
1454
+ case 0x03fe:
1455
+ cp = 0x037c;
1456
+ break;
1457
+ case 0x03ff:
1458
+ cp = 0x037d;
1459
+ break;
1460
+ case 0x037f:
1461
+ cp = 0x03f3;
1462
+ break;
1463
+ case 0x0386:
1464
+ cp = 0x03ac;
1465
+ break;
1466
+ case 0x0388:
1467
+ cp = 0x03ad;
1468
+ break;
1469
+ case 0x0389:
1470
+ cp = 0x03ae;
1471
+ break;
1472
+ case 0x038a:
1473
+ cp = 0x03af;
1474
+ break;
1475
+ case 0x038c:
1476
+ cp = 0x03cc;
1477
+ break;
1478
+ case 0x038e:
1479
+ cp = 0x03cd;
1480
+ break;
1481
+ case 0x038f:
1482
+ cp = 0x03ce;
1483
+ break;
1484
+ case 0x0370:
1485
+ cp = 0x0371;
1486
+ break;
1487
+ case 0x0372:
1488
+ cp = 0x0373;
1489
+ break;
1490
+ case 0x0376:
1491
+ cp = 0x0377;
1492
+ break;
1493
+ case 0x03f4:
1494
+ cp = 0x03b8;
1495
+ break;
1496
+ case 0x03cf:
1497
+ cp = 0x03d7;
1498
+ break;
1499
+ case 0x03f9:
1500
+ cp = 0x03f2;
1501
+ break;
1502
+ case 0x03f7:
1503
+ cp = 0x03f8;
1504
+ break;
1505
+ case 0x03fa:
1506
+ cp = 0x03fb;
1507
+ break;
1508
+ }
1509
+ }
1510
+
1511
+ return cp;
1512
+ }
1513
+
1514
+ utf8_constexpr14_impl utf8_int32_t utf8uprcodepoint(utf8_int32_t cp) {
1515
+ if (((0x0061 <= cp) && (0x007a >= cp)) ||
1516
+ ((0x00e0 <= cp) && (0x00f6 >= cp)) ||
1517
+ ((0x00f8 <= cp) && (0x00fe >= cp)) ||
1518
+ ((0x03b1 <= cp) && (0x03c1 >= cp)) ||
1519
+ ((0x03c3 <= cp) && (0x03cb >= cp)) ||
1520
+ ((0x0430 <= cp) && (0x044f >= cp))) {
1521
+ cp -= 32;
1522
+ } else if ((0x0450 <= cp) && (0x045f >= cp)) {
1523
+ cp -= 80;
1524
+ } else if (((0x0100 <= cp) && (0x012f >= cp)) ||
1525
+ ((0x0132 <= cp) && (0x0137 >= cp)) ||
1526
+ ((0x014a <= cp) && (0x0177 >= cp)) ||
1527
+ ((0x0182 <= cp) && (0x0185 >= cp)) ||
1528
+ ((0x01a0 <= cp) && (0x01a5 >= cp)) ||
1529
+ ((0x01de <= cp) && (0x01ef >= cp)) ||
1530
+ ((0x01f8 <= cp) && (0x021f >= cp)) ||
1531
+ ((0x0222 <= cp) && (0x0233 >= cp)) ||
1532
+ ((0x0246 <= cp) && (0x024f >= cp)) ||
1533
+ ((0x03d8 <= cp) && (0x03ef >= cp)) ||
1534
+ ((0x0460 <= cp) && (0x0481 >= cp)) ||
1535
+ ((0x048a <= cp) && (0x04ff >= cp))) {
1536
+ cp &= ~0x1;
1537
+ } else if (((0x0139 <= cp) && (0x0148 >= cp)) ||
1538
+ ((0x0179 <= cp) && (0x017e >= cp)) ||
1539
+ ((0x01af <= cp) && (0x01b0 >= cp)) ||
1540
+ ((0x01b3 <= cp) && (0x01b6 >= cp)) ||
1541
+ ((0x01cd <= cp) && (0x01dc >= cp))) {
1542
+ cp -= 1;
1543
+ cp |= 0x1;
1544
+ } else {
1545
+ switch (cp) {
1546
+ default:
1547
+ break;
1548
+ case 0x00ff:
1549
+ cp = 0x0178;
1550
+ break;
1551
+ case 0x0180:
1552
+ cp = 0x0243;
1553
+ break;
1554
+ case 0x01dd:
1555
+ cp = 0x018e;
1556
+ break;
1557
+ case 0x019a:
1558
+ cp = 0x023d;
1559
+ break;
1560
+ case 0x019e:
1561
+ cp = 0x0220;
1562
+ break;
1563
+ case 0x0292:
1564
+ cp = 0x01b7;
1565
+ break;
1566
+ case 0x01c6:
1567
+ cp = 0x01c4;
1568
+ break;
1569
+ case 0x01c9:
1570
+ cp = 0x01c7;
1571
+ break;
1572
+ case 0x01cc:
1573
+ cp = 0x01ca;
1574
+ break;
1575
+ case 0x01f3:
1576
+ cp = 0x01f1;
1577
+ break;
1578
+ case 0x01bf:
1579
+ cp = 0x01f7;
1580
+ break;
1581
+ case 0x0188:
1582
+ cp = 0x0187;
1583
+ break;
1584
+ case 0x018c:
1585
+ cp = 0x018b;
1586
+ break;
1587
+ case 0x0192:
1588
+ cp = 0x0191;
1589
+ break;
1590
+ case 0x0199:
1591
+ cp = 0x0198;
1592
+ break;
1593
+ case 0x01a8:
1594
+ cp = 0x01a7;
1595
+ break;
1596
+ case 0x01ad:
1597
+ cp = 0x01ac;
1598
+ break;
1599
+ case 0x01b9:
1600
+ cp = 0x01b8;
1601
+ break;
1602
+ case 0x01bd:
1603
+ cp = 0x01bc;
1604
+ break;
1605
+ case 0x01f5:
1606
+ cp = 0x01f4;
1607
+ break;
1608
+ case 0x023c:
1609
+ cp = 0x023b;
1610
+ break;
1611
+ case 0x0242:
1612
+ cp = 0x0241;
1613
+ break;
1614
+ case 0x037b:
1615
+ cp = 0x03fd;
1616
+ break;
1617
+ case 0x037c:
1618
+ cp = 0x03fe;
1619
+ break;
1620
+ case 0x037d:
1621
+ cp = 0x03ff;
1622
+ break;
1623
+ case 0x03f3:
1624
+ cp = 0x037f;
1625
+ break;
1626
+ case 0x03ac:
1627
+ cp = 0x0386;
1628
+ break;
1629
+ case 0x03ad:
1630
+ cp = 0x0388;
1631
+ break;
1632
+ case 0x03ae:
1633
+ cp = 0x0389;
1634
+ break;
1635
+ case 0x03af:
1636
+ cp = 0x038a;
1637
+ break;
1638
+ case 0x03cc:
1639
+ cp = 0x038c;
1640
+ break;
1641
+ case 0x03cd:
1642
+ cp = 0x038e;
1643
+ break;
1644
+ case 0x03ce:
1645
+ cp = 0x038f;
1646
+ break;
1647
+ case 0x0371:
1648
+ cp = 0x0370;
1649
+ break;
1650
+ case 0x0373:
1651
+ cp = 0x0372;
1652
+ break;
1653
+ case 0x0377:
1654
+ cp = 0x0376;
1655
+ break;
1656
+ case 0x03d1:
1657
+ cp = 0x0398;
1658
+ break;
1659
+ case 0x03d7:
1660
+ cp = 0x03cf;
1661
+ break;
1662
+ case 0x03f2:
1663
+ cp = 0x03f9;
1664
+ break;
1665
+ case 0x03f8:
1666
+ cp = 0x03f7;
1667
+ break;
1668
+ case 0x03fb:
1669
+ cp = 0x03fa;
1670
+ break;
1671
+ }
1672
+ }
1673
+
1674
+ return cp;
1675
+ }
1676
+
1677
+ utf8_constexpr14_impl utf8_int8_t *
1678
+ utf8rcodepoint(const utf8_int8_t *utf8_restrict str,
1679
+ utf8_int32_t *utf8_restrict out_codepoint) {
1680
+ const utf8_int8_t *s = (const utf8_int8_t *)str;
1681
+
1682
+ if (0xf0 == (0xf8 & s[0])) {
1683
+ /* 4 byte utf8 codepoint */
1684
+ *out_codepoint = ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) |
1685
+ ((0x3f & s[2]) << 6) | (0x3f & s[3]);
1686
+ } else if (0xe0 == (0xf0 & s[0])) {
1687
+ /* 3 byte utf8 codepoint */
1688
+ *out_codepoint =
1689
+ ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]);
1690
+ } else if (0xc0 == (0xe0 & s[0])) {
1691
+ /* 2 byte utf8 codepoint */
1692
+ *out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]);
1693
+ } else {
1694
+ /* 1 byte utf8 codepoint otherwise */
1695
+ *out_codepoint = s[0];
1696
+ }
1697
+
1698
+ do {
1699
+ s--;
1700
+ } while ((0 != (0x80 & s[0])) && (0x80 == (0xc0 & s[0])));
1701
+
1702
+ return (utf8_int8_t *)s;
1703
+ }
1704
+
1705
+ #undef utf8_restrict
1706
+ #undef utf8_constexpr14
1707
+ #undef utf8_null
1708
+
1709
+ #ifdef utf8_cplusplus
1710
+ } /* extern "C" */
1711
+ #endif
1712
+
1713
+ #if defined(__clang__)
1714
+ #pragma clang diagnostic pop
1715
+ #endif
1716
+
1717
+ #endif /* SHEREDOM_UTF8_H_INCLUDED */
src/WpfEditor/TextEditorControl.cs CHANGED
@@ -524,6 +524,7 @@ namespace WpfEditor
524
  {
525
  if (HasSelection())
526
  {
 
527
  // 保存文本内容到剪贴板(兼容性考虑)
528
  Clipboard.SetText(GetSelectedText());
529
 
 
524
  {
525
  if (HasSelection())
526
  {
527
+ _isCut = false;
528
  // 保存文本内容到剪贴板(兼容性考虑)
529
  Clipboard.SetText(GetSelectedText());
530
 
src/WpfEditor/readme.txt CHANGED
@@ -1,2 +1,8 @@
1
 
2
  see huggingface\itrans\src\iTrans\App.xaml.cs
 
 
 
 
 
 
 
1
 
2
  see huggingface\itrans\src\iTrans\App.xaml.cs
3
+
4
+
5
+ WpfEditor\MainWindow.xaml.cs
6
+ OpenFile_Click
7
+ 如果是图片,且没有被识别过,用 wechatocr 识别
8
+