-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextmodule_string.cpp
More file actions
483 lines (384 loc) · 11.1 KB
/
textmodule_string.cpp
File metadata and controls
483 lines (384 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "textmodule_string.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <exception>
#include <codecvt>
#include <windows.h>
#include <atlconv.h>
#include <unicode/datefmt.h>
#include <unicode/dtfmtsym.h>
#include <unicode/gregocal.h>
#include <unicode/timezone.h>
#include <unicode/unistr.h>
#include <unicode/ustring.h>
#include <unicode/dtptngen.h>
#include <unicode/dtitvfmt.h>
#include <unicode/translit.h>
#include <unicode/ucnv.h>
#include <boost/shared_ptr.hpp>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "textmodule_exception.hpp"
#include "textmodule_type.hpp"
#pragma comment(lib, "atls.lib")
#pragma comment(lib, "afxnmcd.lib")
#pragma comment(lib, "icuuc.lib")
#define isU16charLowSurrogate(ch) (0xDC00 <= ch && ch < 0xE000)
#define isU16charHighSurrogate(ch) (0xD800 <= ch && ch < 0xDC00)
using namespace icu;
//ワイド文字 - マルチバイト文字 相互変換
std::string WstrToStr(std::wstring str) {
USES_CONVERSION_EX;
const auto pwstr = W2A_EX(str.c_str(), str.length());
if (pwstr != NULL) {
std::string dst(pwstr);
return dst;
}
throw std::runtime_error("string conversion failed");
}
std::wstring StrToWstr(std::string str) {
USES_CONVERSION_EX;
const auto pwstr = A2W_EX(str.c_str(), str.length());
if (pwstr != NULL) {
std::wstring dst(pwstr);
return dst;
}
throw std::runtime_error(STRING_COVERSION_FAILED);
}
std::string UstrToStr(icu::UnicodeString str) {
int length = str.extract(0, str.length(), NULL, "shift_jis");
std::vector<char> result(length + 1);
str.extract(0, str.length(), &result[0], "shift_jis");
return std::string(result.begin(), result.end() - 1);
}
std::wstring UstrToWstr(icu::UnicodeString str) {
return StrToWstr(UstrToStr(str));
}
icu::UnicodeString StrToUstr(std::string str) {
return icu::UnicodeString(str.c_str());
}
icu::UnicodeString WstrToUstr(std::wstring str) {
return icu::UnicodeString(str.c_str());
}
std::u8string StrToU8str(std::string str) {
icu::UnicodeString d(str.c_str(), "shift_jis");
int length = d.extract(0, str.length(), NULL, "utf8");
std::vector<char> result(length + 1);
d.extract(0, str.length(), &result[0], "utf8");
return std::u8string(result.begin(), result.end() - 1);
}
std::u8string WstrToU8str(std::wstring str) {
return StrToU8str(WstrToStr(str));
}
std::string U8strToStr(std::u8string str) {
icu::UnicodeString d((const char*)str.c_str(), "utf8");
int length = d.extract(0, str.length(), NULL, "shift_jis");
std::vector<char> result(length + 1);
d.extract(0, str.length(), &result[0], "shift_jis");
return std::string(result.begin(), result.end() - 1);
}
std::wstring U8strToWstr(std::u8string str) {
return StrToWstr(U8strToStr(str));
}
bool convU32charToU16char(const char32_t u32Ch, std::array<char16_t, 2>& u16Ch) {
if (u32Ch < 0 || u32Ch > 0x10FFFF) {
return false;
}
if (u32Ch < 0x10000) {
u16Ch[0] = char16_t(u32Ch);
u16Ch[1] = 0;
}
else {
u16Ch[0] = char16_t((u32Ch - 0x10000) / 0x400 + 0xD800);
u16Ch[1] = char16_t((u32Ch - 0x10000) % 0x400 + 0xDC00);
}
return true;
}
bool convU16charToU32char(const std::array<char16_t, 2>& u16Ch, char32_t& u32Ch) {
if (isU16charHighSurrogate(u16Ch[0])) {
if (isU16charLowSurrogate(u16Ch[1])) {
u32Ch = 0x10000 + (char32_t(u16Ch[0]) - 0xD800) * 0x400 +
(char32_t(u16Ch[1]) - 0xDC00);
}
else if (u16Ch[1] == 0) {
u32Ch = u16Ch[0];
}
else {
return false;
}
}
else if (isU16charLowSurrogate(u16Ch[0])) {
if (u16Ch[1] == 0) {
u32Ch = u16Ch[0];
}
else {
return false;
}
}
else {
u32Ch = u16Ch[0];
}
return true;
}
std::u32string StrToU32str(std::string str) {
return WstrToU32str(StrToWstr(str));
}
std::u32string WstrToU32str(std::wstring str) {
std::u32string ret;
for (auto u16It = str.begin(); u16It != str.end(); ++u16It) {
std::array<char16_t, 2> u16Ch;
if (isU16charHighSurrogate((*u16It))) {
u16Ch[0] = (*u16It);
++u16It;
if (u16It == str.end())
throw std::runtime_error(STRING_COVERSION_FAILED);
u16Ch[1] = (*u16It);
}
else {
u16Ch[0] = (*u16It);
u16Ch[1] = 0;
}
char32_t u32Ch;
if (!convU16charToU32char(u16Ch, u32Ch))
throw std::runtime_error(STRING_COVERSION_FAILED);
ret.push_back(u32Ch);
}
return ret;
}
std::string U32strToStr(std::u32string str) {
return WstrToStr(U32strToWstr(str));
}
std::wstring U32strToWstr(std::u32string str) {
std::wstring ret;
for (auto u32It = str.begin(); u32It != str.end(); ++u32It) {
std::array<char16_t, 2> u16Ch;
if (!convU32charToU16char((*u32It), u16Ch))
throw std::runtime_error(STRING_COVERSION_FAILED);
if (u16Ch[0] != 0)
ret.push_back(u16Ch[0]);
if (u16Ch[1] != 0)
ret.push_back(u16Ch[1]);
}
return ret;
}
std::string StrToEUCstr(std::string str) {
icu::UnicodeString d(str.c_str(), "shift_jis");
int length = d.extract(0, str.length(), NULL, "euc_jp");
std::vector<char> result(length + 1);
d.extract(0, str.length(), &result[0], "euc_jp");
return std::string(result.begin(), result.end() - 1);
}
std::string EUCstrToStr(std::string str) {
icu::UnicodeString d(str.c_str(), "euc_jp");
int length = d.extract(0, str.length(), NULL, "shift_jis");
std::vector<char> result(length + 1);
d.extract(0, str.length(), &result[0], "shift_jis");
return std::string(result.begin(), result.end() - 1);
}
//置換
std::wstring _jreplace(std::wstring String1, std::wstring String2, std::wstring String3, bool invert)
{
if (invert) {
std::wstring temp = String3;
String3 = String2;
String2 = temp;
}
std::string::size_type Pos(String1.find(String2));
while (Pos != std::string::npos)
{
String1.replace(Pos, String2.length(), String3);
Pos = String1.find(String2, Pos + String3.length());
}
return String1;
}
icu::UnicodeString StrTransliterate(icu::UnicodeString str, const char* conversion) {
UErrorCode status = U_ZERO_ERROR;
boost::shared_ptr<Transliterator> trans(
Transliterator::createInstance(conversion, UTRANS_FORWARD, status)
);
trans->transliterate(str);
return str;
}
unsigned long long UnicodeToUTF8(unsigned long long code) {
std::string bin;
unsigned int mask = (int)1 << (sizeof(unsigned int) * CHAR_BIT - 1);
do bin += (mask & code ? '1' : '0');
while (mask >>= 1);
int m = bin.size();
std::string ret;
if (0x0000 <= code && code <= 0x007F) {
ret = "0" + bin.substr(m - 7, 7);
}
else if (0x0080 <= code && code <= 0x07FF) {
ret = "110" + bin.substr(m - 11, 5) + "10" + bin.substr(m - 6, 6);
}
else if (0x0890 <= code && code <= 0xFFFF) {
ret = "1110" + bin.substr(m - 16, 4) + "10" + bin.substr(m - 12, 6) + "10" + bin.substr(m - 6, 6);
}
else if (0x10000 <= code && code <= 0x10FFFF) {
ret = "11110" + bin.substr(m - 21, 3) + "10" + bin.substr(m - 18, 6) + "10" + bin.substr(m - 12, 6) + "10" + bin.substr(m - 6, 6);
}
else {
return -1;
}
return std::stoull(ret, 0, 2);
}
unsigned long long UTF8ToUnicode(unsigned long long code) {
std::string bin;
unsigned int mask = (int)1 << (sizeof(unsigned int) * CHAR_BIT - 1);
do bin += (mask & code ? '1' : '0');
while (mask >>= 1);
int m = bin.size();
std::string ret;
if (0x0000 <= code && code <= 0x007F) { //U+0000 - U+007F
ret = bin.substr(m - 7, 7);
}
else if (0x0080 <= code && code <= 0xDFBF) { //U+0080 - U+07FF
ret = bin.substr(m - 13, 5) + bin.substr(m - 6, 6);
}
else if (0xE08080 <= code && code <= 0xEFBFBF) { //U+0800 - U+FFFF
ret = bin.substr(m - 20, 4) + bin.substr(m - 14, 6) + bin.substr(m - 6, 6);
}
else if (0xF0808080 <= code && code <= 0xF7BFBFBF) { //U+10000 - U+10FFFF
ret = bin.substr(m - 27, 3) + bin.substr(m - 22, 6) + bin.substr(m - 14, 6) + bin.substr(m - 6, 6);
}
else {
return -1;
}
return std::stoull(ret, 0, 2);
}
//文字列ローカライズ
icu::UnicodeString HiraganaToKatakana(icu::UnicodeString str) {
return StrTransliterate(str, "Hiragana-Katakana");
}
icu::UnicodeString KatakanaToHiragana(icu::UnicodeString str) {
return StrTransliterate(str, "Hiragana-Katakana");
}
icu::UnicodeString LatinToHiragana(icu::UnicodeString str) {
return StrTransliterate(str, "Latin-Hiragana");
}
icu::UnicodeString LatinToKatakana(icu::UnicodeString str) {
return StrTransliterate(str, "Latin-Katakana");
}
icu::UnicodeString LatinBGNToHiragana(icu::UnicodeString str) {
return StrTransliterate(str, "Latin/BGN-Hiragana");
}
icu::UnicodeString LatinBGNToKatakana(icu::UnicodeString str) {
return StrTransliterate(str, "Latin/BGN-Katakana");
}
icu::UnicodeString HiraganaToLatin(icu::UnicodeString str) {
return StrTransliterate(str, "Hiragana-Latin");
}
icu::UnicodeString KatakanaToLatin(icu::UnicodeString str) {
return StrTransliterate(str, "Katakana-Latin");
}
icu::UnicodeString HiraganaToLatinBGN(icu::UnicodeString str) {
return StrTransliterate(str, "Hiragana-Latin/BGN");
}
icu::UnicodeString KatakanaToLatinBGN(icu::UnicodeString str) {
return StrTransliterate(str, "Katakana-Latin/BGN");
}
icu::UnicodeString HalfwidthToFullwidth(icu::UnicodeString str) {
return StrTransliterate(str, "Halfwidth-Fullwidth");
}
icu::UnicodeString FullwidthToHalfwidth(icu::UnicodeString str) {
return StrTransliterate(str, "Fullwidth-Halfwidth");
}
//型変換
std::wstring DecToHex(int num) {
std::ostringstream os;
os << std::hex << num;
return L"0x" + StrToWstr(os.str());
}
int HexToDec(std::wstring num) {
return std::stoi(num, nullptr, 16);
}
std::wstring tostring(double num) {
return StrToWstr(std::to_string(num));
}
std::wstring tostring_n(double num) {
std::wstring str = StrToWstr(std::to_string(num));
if (str.find(L".") == std::string::npos) {
return str;
}
int l = str.length();
for (int i = 0; i < l; i++) {
const wchar_t s = str[l - (i + 1)];
if (s == L'0') {
str.pop_back();
}
else if (s == L'.') {
str.pop_back();
break;
}
else if (s == L'\0') {
str.pop_back();
}
else {
break;
}
}
return str;
}
//大文字小文字変換
std::wstring lowerString(std::wstring type) {
std::transform(type.cbegin(), type.cend(), type.begin(), tolower);
return type;
}
std::string lowerString(std::string type) {
std::transform(type.cbegin(), type.cend(), type.begin(), tolower);
return type;
}
std::wstring upperString(std::wstring type) {
std::transform(type.cbegin(), type.cend(), type.begin(), toupper);
return type;
}
std::string upperString(std::string type) {
std::transform(type.cbegin(), type.cend(), type.begin(), toupper);
return type;
}
//文字列分割
std::vector<std::wstring> split(std::wstring str, wchar_t del) {
std::vector<std::wstring> result;
std::wstring subStr;
for (const wchar_t c : str) {
if (c == del) {
result.push_back(subStr);
subStr.clear();
}
else {
subStr += c;
}
}
result.push_back(subStr);
return result;
}
std::vector<std::string> split(std::string str, char del) {
std::vector<std::string> result;
std::string subStr;
for (const char c : str) {
if (c == del) {
result.push_back(subStr);
subStr.clear();
}
else {
subStr += c;
}
}
result.push_back(subStr);
return result;
}
//デバッグ用関数
void debug_string(std::wstring message) {
OutputDebugString(WstrToStr(message).c_str());
}
void debug_string(std::string message) {
OutputDebugString(message.c_str());
}
void debug_string(double message) {
OutputDebugString(WstrToStr(tostring(message)).c_str());
}