YSTest: CharacterProcessing.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

CharacterProcessing.h
浏览该文件的文档.
1 /*
2 © 2009-2015, 2018-2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef INC_CHRLib_CharacterProcessing_h_
29 #define INC_CHRLib_CharacterProcessing_h_ 1
30
31 #include "YModules.h"
32 #include YFM_CHRLib_CharacterMapping
33 #include <ystdex/cstring.h> // for ystdex::uchar_t;
34 #include <ystdex/string.hpp> // for ystdex::is_null, ystdex::ntctslen,
35 // ystdex::string_traits, std::allocator_arg_t,
36 // ystdex::make_obj_using_allocator, std::basic_string, std::allocator_arg;
37 #include <cstdio> // for std::FILE;
38 #include <memory> // for std::move;
39
40 namespace CHRLib
41 {
42
44 using ystdex::uchar_t;
45
46
51 template<typename _tChar>
52 YB_ATTR_nodiscard YB_STATELESS yconstfn bool
53 IsASCII(_tChar c)
54 {
55 return !(c & ~0x7F);
56 }
57
62 template<typename _tChar>
63 YB_ATTR_nodiscard YB_STATELESS yconstfn char
64 ToASCII(_tChar c)
65 {
66 static_assert(std::is_integral<_tChar>(), "Invalid type found.");
67
68 return c & 0x7F;
69 }
70
71
73
74
76
78 YF_API ConversionResult
79 MBCToUC(char16_t&, const char*&, Encoding, ConversionState&& = {});
80 YF_API ConversionResult
81 MBCToUC(char16_t&, const char*&, const char*, Encoding, ConversionState&& = {});
82 inline PDefH(ConversionResult, MBCToUC, char16_t& uc, const char*& c,
83 Encoding enc, ConversionState& st)
84 ImplRet(MBCToUC(uc, c, enc, std::move(st)))
86
91 YF_API YB_NONNULL(2) ConversionResult
92 MBCToUC(char16_t&, std::FILE*, Encoding, ConversionState&& = {});
93 inline PDefH(ConversionResult, MBCToUC, char16_t& uc, std::FILE* fp, Encoding enc,
94 ConversionState& st)
95 ImplRet(MBCToUC(uc, fp, enc, std::move(st)))
97
98
103 YF_API ConversionResult
104 MBCToUC(const char*&, Encoding, ConversionState&& = {});
106 YF_API ConversionResult
107 MBCToUC(const char*&, const char*, Encoding, ConversionState&& = {});
108 inline PDefH(ConversionResult, MBCToUC, const char*& c, Encoding enc,
109 ConversionState& st)
110 ImplRet(MBCToUC(c, enc, std::move(st)))
112 inline PDefH(ConversionResult, MBCToUC, const char*& c, const char* e,
113 Encoding enc, ConversionState& st)
114 ImplRet(MBCToUC(c, e, enc, std::move(st)))
116
117 YF_API YB_NONNULL(1) ConversionResult
118 MBCToUC(std::FILE*, Encoding, ConversionState&& = {});
119 YB_NONNULL(1) inline PDefH(ConversionResult, MBCToUC, std::FILE* fp,
120 Encoding enc, ConversionState& st)
121 ImplRet(MBCToUC(fp, enc, std::move(st)))
123
124
131 YF_API YB_NONNULL(1) size_t
132 UCToMBC(char*, const char16_t&, Encoding);
134
135
137
138
140 template<typename _func, typename _tDst, typename _tSrc>
141 YB_NONNULL(2, 3) size_t
142 TranscodeWith(_func f, _tDst* d, const _tSrc* s)
143 {
144 yconstraint(d),
145 yconstraint(s);
146
147 const auto p(d);
148
149 f(d, s);
150 *d = _tDst();
151 return size_t(d - p);
152 }
153 template<typename _func, typename _tDst, typename _tSrc>
154 YB_NONNULL(2, 3, 4) size_t
155 TranscodeWith(_func f, _tDst* d, const _tSrc* s, const _tSrc* e)
156 {
157 yconstraint(d),
158 yconstraint(s),
159 yconstraint(e),
160 yconstraint(s <= e);
161
162 const auto p(d);
163
164 f(d, s, e);
165 return size_t(d - p);
166 }
168
170
171 template<typename _func, typename _tDst, typename _tSrc>
172 YB_NONNULL(2, 3) size_t
173 DecodeWith(_func f, _tDst* d, const _tSrc* s)
174 {
175 return CHRLib::TranscodeWith([&](_tDst*& dst, const _tSrc*& src){
176 while(!ystdex::is_null(*src) && f(*dst, src))
177 ++dst;
178 }, d, s);
179 }
180 template<typename _func, typename _tDst, typename _tSrc>
181 YB_NONNULL(2, 3, 4) size_t
182 DecodeWith(_func f, _tDst* d, const _tSrc* s, const _tSrc* e)
183 {
184 return CHRLib::TranscodeWith(
185 [&](_tDst*& dst, const _tSrc*& src, const _tSrc*& end){
186 while(!ystdex::is_null(*src) && f(*dst, src, end))
187 ++dst;
188 }, d, s, e);
189 }
190
191 template<typename _func, typename _tDst, typename _tSrc>
192 YB_NONNULL(2, 3) size_t
193 EncodeWith(_func f, _tDst* d, const _tSrc* s)
194 {
195 return CHRLib::TranscodeWith([&](char*& dst, const _tSrc*& src){
196 while(!ystdex::is_null(*src))
197 f(dst, src);
198 }, d, s);
199 }
200 template<typename _func, typename _tDst, typename _tSrc>
201 YB_NONNULL(2, 3, 4) size_t
202 EncodeWith(_func f, _tDst* d, const _tSrc* s, const _tSrc* e)
203 {
204 return CHRLib::TranscodeWith(
205 [&](_tDst*& dst, const _tSrc*& src, const _tSrc* end){
206 // TODO: Deferred. Use guard for encoding.
207 while(src < end)
208 f(dst, src, end);
209 }, d, s, e);
210 }
212
221
223 template<typename _func>
224 YB_NONNULL(2, 3) size_t
225 MBCSToUCS2(_func f, char16_t* d, const char* s)
226 {
227 return CHRLib::DecodeWith([&](char16_t& dc, const char*& src) -> bool{
228 return f(dc, src, ConversionState()) == ConversionResult::OK;
229 }, d, s);
230 }
231 template<typename _func>
232 YB_NONNULL(2, 3, 4) size_t
233 MBCSToUCS2(_func f, char16_t* d, const char* s, const char* e)
234 {
235 return CHRLib::DecodeWith(
236 [&](char16_t& dc, const char*& src, const char*& end) -> bool{
237 return f(dc, {src, end}, ConversionState()) == ConversionResult::OK;
238 }, d, s, e);
239 }
240 YF_API YB_FLATTEN YB_NONNULL(1, 2) size_t
241 MBCSToUCS2(char16_t*, const char*);
242 YF_API YB_NONNULL(1, 2) size_t
243 MBCSToUCS2(char16_t*, const char*, Encoding);
244 YF_API YB_FLATTEN YB_NONNULL(1, 2, 3) size_t
245 MBCSToUCS2(char16_t*, const char*, const char* e);
246 YF_API YB_NONNULL(1, 2, 3) size_t
247 MBCSToUCS2(char16_t*, const char*, const char* e, Encoding);
249
251
252 template<typename _func>
253 YB_NONNULL(2, 3) size_t
254 MBCSToUCS4(_func f, char32_t* d, const char* s)
255 {
256 return CHRLib::DecodeWith([&](char32_t& dc, const char*& src) -> bool{
257 // TODO: Necessary initialization?
258 char16_t c;
259
260 if(f(c, src, ConversionState()) == ConversionResult::OK)
261 {
262 dc = c;
263 return true;
264 }
265 return {};
266 }, d, s);
267 }
268 template<typename _func>
269 YB_NONNULL(2, 3, 4) size_t
270 MBCSToUCS4(_func f, char32_t* d, const char* s, const char* e)
271 {
272 return CHRLib::DecodeWith(
273 [&](char32_t& dc, const char*& src, const char*& end) -> bool{
274 // TODO: Necessary initialization?
275 char16_t c;
276
277 if(f(c, {src, end}, ConversionState()) == ConversionResult::OK)
278 {
279 dc = c;
280 return true;
281 }
282 return {};
283 }, d, s, e);
284 }
285 YF_API YB_FLATTEN YB_NONNULL(1, 2) size_t
286 MBCSToUCS4(char32_t*, const char*);
287 YF_API YB_NONNULL(1, 2) size_t
288 MBCSToUCS4(char32_t*, const char*, Encoding);
289 YF_API YB_FLATTEN YB_NONNULL(1, 2, 3) size_t
290 MBCSToUCS4(char32_t*, const char*, const char*);
291 YF_API YB_NONNULL(1, 2, 3) size_t
292 MBCSToUCS4(char32_t*, const char*, const char*, Encoding);
294
296
297
299 template<typename _func>
300 YB_NONNULL(2, 3) size_t
301 UCS2ToMBCS(_func f, char* d, const char16_t* s)
302 {
303 return CHRLib::EncodeWith([&](char*& dst, const char16_t*& src){
304 dst += f(dst, *src++);
305 }, d, s);
306 }
307 template<typename _func>
308 YB_NONNULL(2, 3, 4) size_t
309 UCS2ToMBCS(_func f, char* d, const char16_t* s, const char16_t* e)
310 {
311 return CHRLib::EncodeWith(
312 [&](char*& dst, const char16_t*& src, const char16_t*){
313 dst += f(dst, *src++);
314 }, d, s, e);
315 }
317 YF_API YB_FLATTEN YB_NONNULL(1, 2) size_t
318 UCS2ToMBCS(char*, const char16_t*);
319 YF_API YB_NONNULL(1, 2) size_t
320 UCS2ToMBCS(char*, const char16_t*, Encoding);
321 YF_API YB_FLATTEN YB_NONNULL(1, 2, 3) size_t
322 UCS2ToMBCS(char*, const char16_t*, const char16_t*);
323 YF_API YB_NONNULL(1, 2, 3) size_t
324 UCS2ToMBCS(char*, const char16_t*, const char16_t*, Encoding);
326
332 YF_API YB_NONNULL(1, 2) size_t
333 UCS2ToUCS4(char32_t*, const char16_t*);
334 YF_API YB_NONNULL(1, 2, 3) size_t
335 UCS2ToUCS4(char32_t*, const char16_t*, const char16_t*);
337
339
340
342 template<typename _func>
343 YB_NONNULL(2, 3) size_t
344 UCS4ToMBCS(_func f, char* d, const char32_t* s)
345 {
346 return CHRLib::EncodeWith([&](char*& dst, const char32_t*& src){
347 dst += f(dst, char16_t(*src++));
348 }, d, s);
349 }
350 template<typename _func>
351 YB_NONNULL(2, 3, 4) size_t
352 UCS4ToMBCS(_func f, char* d, const char32_t* s, const char32_t* e)
353 {
354 return CHRLib::EncodeWith(
355 [&](char*& dst, const char32_t*& src, const char32_t*){
356 dst += f(dst, char16_t(*src++));
357 }, d, s, e);
358 }
360 YF_API YB_FLATTEN YB_NONNULL(1, 2) size_t
361 UCS4ToMBCS(char*, const char32_t*);
362 YF_API YB_NONNULL(1, 2) size_t
363 UCS4ToMBCS(char*, const char32_t*, Encoding);
364 YF_API YB_FLATTEN YB_NONNULL(1, 2, 3) size_t
365 UCS4ToMBCS(char*, const char32_t*, const char32_t*);
366 YF_API YB_NONNULL(1, 2, 3) size_t
367 UCS4ToMBCS(char*, const char32_t*, const char32_t*, Encoding);
369
375 YF_API YB_NONNULL(1, 2) size_t
376 UCS4ToUCS2(char16_t*, const char32_t*);
377 YF_API YB_NONNULL(1, 2, 3) size_t
378 UCS4ToUCS2(char16_t*, const char32_t*, const char32_t*);
380
381
382
384
385 template<Encoding, Encoding>
386 struct DispatchTranscoding;
387
388 #define CHRLib_Impl_DispatchTranscoding(_vFrom, _vTo, _n) \
389 template<> \
390 struct DispatchTranscoding<CharSet::_vFrom, CharSet::_vTo> \
391 { \
392 template<typename... _tParams> \
393 static size_t \
394 Transcode(_tParams&&... args) \
395 { \
396 return _n(yforward(args)...); \
397 } \
398 };
399
400 CHRLib_Impl_DispatchTranscoding(Null, ISO_10646_UCS_2, MBCSToUCS2)
401
402 CHRLib_Impl_DispatchTranscoding(Null, ISO_10646_UCS_4, MBCSToUCS4)
403
404 CHRLib_Impl_DispatchTranscoding(ISO_10646_UCS_2, Null, UCS2ToMBCS)
405
406 CHRLib_Impl_DispatchTranscoding(ISO_10646_UCS_2, ISO_10646_UCS_4, UCS2ToUCS4)
407
408 CHRLib_Impl_DispatchTranscoding(ISO_10646_UCS_4, Null, UCS4ToMBCS)
409
410 CHRLib_Impl_DispatchTranscoding(ISO_10646_UCS_4, ISO_10646_UCS_2, UCS4ToUCS2)
411
412 #undef CHRLib_Impl_DispatchTranscoding
413
414
415 template<Encoding _vFrom, Encoding _vTo,
416 class _tDispatcher = DispatchTranscoding<_vFrom, _vTo>>
417 struct Transcoding
418 {
419 template<typename _tDst, typename _tChar, typename... _tParams>
420 YB_ATTR_nodiscard YB_PURE static _tDst
421 MakeTranscoded(const _tChar* s, _tParams&&... args)
422 {
423 _tDst str(ystdex::ntctslen(s),
424 typename ystdex::string_traits<_tDst>::value_type());
425
426 str.resize(_tDispatcher::Transcode(&str[0], s, yforward(args)...));
427 return str;
428 }
430 template<typename _tDst, typename _tChar, typename... _tParams>
431 YB_ATTR_nodiscard YB_PURE static _tDst
432 MakeTranscoded(std::allocator_arg_t, const typename
433 _tDst::allocator_type& a, const _tChar* s, _tParams&&... args)
434 {
435 auto str(ystdex::make_obj_using_allocator<_tDst>(a, ystdex::ntctslen(s),
436 typename ystdex::string_traits<_tDst>::value_type()));
437
438 str.resize(_tDispatcher::Transcode(&str[0], s, yforward(args)...));
439 return str;
440 }
441
442 template<typename _tDst, typename _type, typename... _tParams>
443 YB_ATTR_nodiscard YB_PURE static _tDst
444 MakeViewTranscoded(_type sv, _tParams&&... args)
445 {
446 const auto s(sv.data());
447
448 yconstraint(s);
449
450 const auto l(sv.length());
451 _tDst str(l, typename ystdex::string_traits<_tDst>::value_type());
452
453 str.resize(_tDispatcher::Transcode(&str[0], s, s + l,
454 yforward(args)...));
455 return str;
456 }
458 template<typename _tDst, typename _type, typename... _tParams>
459 YB_ATTR_nodiscard YB_PURE static _tDst
460 MakeViewTranscoded(std::allocator_arg_t,
461 const typename _tDst::allocator_type& a, _type sv, _tParams&&... args)
462 {
463 const auto s(sv.data());
464
465 yconstraint(s);
466
467 const auto l(sv.length());
468 auto str(ystdex::make_obj_using_allocator<_tDst>(a, l,
469 typename ystdex::string_traits<_tDst>::value_type()));
470
471 str.resize(_tDispatcher::Transcode(&str[0], s, s + l,
472 yforward(args)...));
473 return str;
474 }
475 };
476
477 template<Encoding _vFrom, class _tDispatcher>
478 struct Transcoding<_vFrom, CharSet::Null, _tDispatcher>
479 {
480 template<typename _tDst, typename _tChar, typename... _tParams>
481 YB_ATTR_nodiscard YB_PURE static _tDst
482 MakeTranscoded(const _tChar* s)
483 {
484 const auto w(FetchMaxCharWidth(CS_Default));
485 _tDst str(ystdex::ntctslen(s) * (w == 0 ? sizeof(ucsint_t) : w),
486 typename ystdex::string_traits<_tDst>::value_type());
487
488 str.resize(_tDispatcher::Transcode(&str[0], s));
489 return str;
490 }
491 template<typename _tDst, typename _tChar, typename... _tParams>
492 YB_ATTR_nodiscard YB_PURE static _tDst
493 MakeTranscoded(const _tChar* s, Encoding enc, _tParams&&... args)
494 {
495 const auto w(FetchMaxCharWidth(enc));
496 _tDst str(ystdex::ntctslen(s) * (w == 0 ? sizeof(ucsint_t) : w),
497 typename ystdex::string_traits<_tDst>::value_type());
498
499 str.resize(_tDispatcher::Transcode(&str[0], s, enc, yforward(args)...));
500 return str;
501 }
503 template<typename _tDst, typename _tChar, typename... _tParams>
504 YB_ATTR_nodiscard YB_PURE static _tDst
505 MakeTranscoded(std::allocator_arg_t,
506 const typename _tDst::allocator_type& a, const _tChar* s)
507 {
508 const auto w(FetchMaxCharWidth(CS_Default));
509 auto str(ystdex::make_obj_using_allocator<_tDst>(a,
510 ystdex::ntctslen(s) * (w == 0 ? sizeof(ucsint_t) : w),
511 typename ystdex::string_traits<_tDst>::value_type()));
512
513 str.resize(_tDispatcher::Transcode(&str[0], s));
514 return str;
515 }
517 template<typename _tDst, typename _tChar, typename... _tParams>
518 YB_ATTR_nodiscard YB_PURE static _tDst
519 MakeTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type&
520 a, const _tChar* s, Encoding enc, _tParams&&... args)
521 {
522 const auto w(FetchMaxCharWidth(enc));
523 auto str(ystdex::make_obj_using_allocator<_tDst>(a, ystdex::ntctslen(s)
524 * (w == 0 ? sizeof(ucsint_t) : w),
525 typename ystdex::string_traits<_tDst>::value_type()));
526
527 str.resize(_tDispatcher::Transcode(&str[0], s, enc, yforward(args)...));
528 return str;
529 }
530
531 template<typename _tDst, typename _type, typename... _tParams>
532 YB_ATTR_nodiscard YB_PURE static _tDst
533 MakeViewTranscoded(_type sv)
534 {
535 const auto s(sv.data());
536
537 yconstraint(s);
538
539 const auto l(sv.length());
540 const auto w(FetchMaxCharWidth(CS_Default));
541 _tDst str(l * (w == 0 ? sizeof(ucsint_t) : w),
542 typename ystdex::string_traits<_tDst>::value_type());
543
544 str.resize(_tDispatcher::Transcode(&str[0], s, s + l));
545 return str;
546 }
547 template<typename _tDst, typename _type, typename... _tParams>
548 YB_ATTR_nodiscard YB_PURE static _tDst
549 MakeViewTranscoded(_type sv, Encoding enc, _tParams&&... args)
550 {
551 const auto s(sv.data());
552
553 yconstraint(s);
554
555 const auto l(sv.length());
556 const auto w(FetchMaxCharWidth(enc));
557 _tDst str(l * (w == 0 ? sizeof(ucsint_t) : w),
558 typename ystdex::string_traits<_tDst>::value_type());
559
560 str.resize(_tDispatcher::Transcode(&str[0], s, s + l, enc,
561 yforward(args)...));
562 return str;
563 }
565 template<typename _tDst, typename _type, typename... _tParams>
566 YB_ATTR_nodiscard YB_PURE static _tDst
567 MakeViewTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type& a, _type sv)
568 {
569 const auto s(sv.data());
570
571 yconstraint(s);
572
573 const auto l(sv.length());
574 const auto w(FetchMaxCharWidth(CS_Default));
575 auto str(ystdex::make_obj_using_allocator<_tDst>(a,
576 l * (w == 0 ? sizeof(ucsint_t) : w),
577 typename ystdex::string_traits<_tDst>::value_type()));
578
579 str.resize(_tDispatcher::Transcode(&str[0], s, s + l));
580 return str;
581 }
583 template<typename _tDst, typename _type, typename... _tParams>
584 YB_ATTR_nodiscard YB_PURE static _tDst
585 MakeViewTranscoded(std::allocator_arg_t, const typename
586 _tDst::allocator_type& a, _type sv, Encoding enc, _tParams&&... args)
587 {
588 const auto s(sv.data());
589
590 yconstraint(s);
591
592 const auto l(sv.length());
593 const auto w(FetchMaxCharWidth(enc));
594 auto str(ystdex::make_obj_using_allocator<_tDst>(a,
595 l * (w == 0 ? sizeof(ucsint_t) : w),
596 typename ystdex::string_traits<_tDst>::value_type()));
597
598 str.resize(_tDispatcher::Transcode(&str[0], s, s + l, enc,
599 yforward(args)...));
600 return str;
601 }
602 };
604
605
612 #define CHRLib_Impl_Transcode(_vFrom, _vTo, ...) \
613 return Transcoding<CharSet::_vFrom, CharSet::_vTo> \
614 ::MakeTranscoded<_tDst>(__VA_ARGS__);
615 #define CHRLib_Impl_TranscodeView(_vFrom, _vTo, ...) \
616 return Transcoding<CharSet::_vFrom, CharSet::_vTo> \
617 ::MakeViewTranscoded<_tDst>(__VA_ARGS__);
618 #define CHRLib_Impl_MakeTrans(_n, _tSrcChar, _tDstChar, _vFrom, _vTo, _vDeEnc) \
619 template<class _tDst = std::basic_string<_tDstChar>, typename... _tParams> \
620 YB_ATTR_nodiscard YB_NONNULL(1) YB_PURE _tDst \
621 Make##_n(const _tSrcChar* s, _tParams&&... args) \
622 { \
623 CHRLib_Impl_Transcode(_vFrom, _vTo, s, yforward(args)...); \
624 } \
625 template<class _tDst = std::basic_string<_tDstChar>, typename... _tParams> \
626 YB_ATTR_nodiscard YB_PURE _tDst \
627 Make##_n(basic_string_view<_tSrcChar> sv, _tParams&&... args) \
628 { \
629 CHRLib_Impl_TranscodeView(_vFrom, _vTo, sv, yforward(args)...); \
630 } \
631 template<class _tDst = std::basic_string<_tDstChar>, typename... _tParams> \
632 YB_ATTR_nodiscard YB_NONNULL(3) YB_PURE _tDst \
633 Make##_n(std::allocator_arg_t, const typename _tDst::allocator_type& a, \
634 const _tSrcChar* s, _tParams&&... args) \
635 { \
636 CHRLib_Impl_Transcode(_vFrom, _vTo, std::allocator_arg, a, s, \
637 yforward(args)...); \
638 } \
639 template<class _tDst = std::basic_string<_tDstChar>, typename... _tParams> \
640 YB_ATTR_nodiscard YB_PURE _tDst \
641 Make##_n(std::allocator_arg_t, const typename _tDst::allocator_type& a, \
642 basic_string_view<_tSrcChar> sv, _tParams&&... args) \
643 { \
644 CHRLib_Impl_TranscodeView(_vFrom, _vTo, std::allocator_arg, a, sv, \
645 yforward(args)...); \
646 }
647
649 CHRLib_Impl_MakeTrans(UCS2LE, char, char16_t, Null, ISO_10646_UCS_2, CS_Default)
651
655 template<class _tDst = std::basic_string<char16_t>>
656 YB_ATTR_nodiscard YB_PURE inline _tDst
657 MakeUCS2LE(u16string_view sv, Encoding = CharSet::ISO_10646_UCS_2)
658 {
659 const auto s(sv.data());
660
661 yconstraint(s);
662 // FIXME: Correct conversion for encoding other than UCS-2LE.
663 return {s, sv.length()};
664 }
669 template<class _tDst = std::basic_string<char16_t>>
670 YB_ATTR_nodiscard YB_PURE inline _tDst
671 MakeUCS2LE(std::allocator_arg_t, const typename _tDst::allocator_type& a,
672 u16string_view sv, Encoding = CharSet::ISO_10646_UCS_2)
673 {
674 const auto s(sv.data());
675
676 yconstraint(s);
677 // FIXME: Correct conversion for encoding other than UCS-2LE.
678 return ystdex::make_obj_using_allocator<_tDst>(a, s, sv.length());
679 }
685 CHRLib_Impl_MakeTrans(UCS2LE, char32_t, char16_t, ISO_10646_UCS_4,
686 ISO_10646_UCS_2, CharSet::ISO_10646_UCS_4)
688
690
691 CHRLib_Impl_MakeTrans(UCS4LE, char, char32_t, Null, ISO_10646_UCS_4, CS_Default)
693
698 CHRLib_Impl_MakeTrans(UCS4LE, char16_t, char32_t, ISO_10646_UCS_2,
699 ISO_10646_UCS_4, CharSet::ISO_10646_UCS_2)
701
705 template<class _tDst = std::basic_string<char32_t>>
706 YB_ATTR_nodiscard YB_PURE inline _tDst
707 MakeUCS4LE(u32string_view sv, Encoding = CharSet::ISO_10646_UCS_4)
708 {
709 const auto s(sv.data());
710
711 yconstraint(s);
712 // FIXME: Correct conversion for encoding other than UCS-4LE.
713 return {s, sv.length()};
714 }
719 template<class _tAlloc, class _tDst = std::basic_string<char32_t>>
720 YB_ATTR_nodiscard YB_PURE inline _tDst
721 MakeUCS4LE(std::allocator_arg_t, const typename _tDst::allocator_type& a,
722 u32string_view sv, Encoding = CharSet::ISO_10646_UCS_4)
723 {
724 const auto s(sv.data());
725
726 yconstraint(s);
727 // FIXME: Correct conversion for encoding other than UCS-4LE.
728 return ystdex::make_obj_using_allocator<_tDst>(a, s, sv.length());
729 }
730
732
734 template<class _tDst = std::string>
736 YB_ATTR_nodiscard YB_NONNULL(1) YB_PURE inline _tDst
737 MakeMBCS(const char* s, Encoding enc)
738 {
739 return enc = CS_Default ? MakeMBCS<_tDst>(s)
740 : MakeMBCS<_tDst>(MakeUCS2LE(s, CS_Default), enc);
741 }
742 template<class _tDst = std::string>
743 YB_ATTR_nodiscard YB_PURE inline _tDst
744 MakeMBCS(string_view sv, Encoding enc)
745 {
746 return enc = CS_Default ? MakeMBCS<_tDst>(sv)
747 : MakeMBCS<_tDst>(MakeUCS2LE(sv, CS_Default), enc);
748 }
750 template<class _tDst = std::string>
751 YB_ATTR_nodiscard YB_NONNULL(3) YB_PURE inline _tDst
752 MakeMBCS(std::allocator_arg_t, const typename _tDst::allocator_type& a, char* s,
753 Encoding enc)
754 {
755 return enc = CS_Default ? MakeMBCS<_tDst>(s)
756 : MakeMBCS<_tDst>(std::allocator_arg, a,
757 MakeUCS2LE(std::allocator_arg, a, s, CS_Default), enc);
758 }
760 template<class _tDst = std::string>
761 YB_ATTR_nodiscard YB_PURE inline _tDst
762 MakeMBCS(std::allocator_arg_t, const typename _tDst::allocator_type& a,
763 string_view sv, Encoding enc)
764 {
765 return enc = CS_Default ? MakeMBCS<_tDst>(sv)
766 : MakeMBCS<_tDst>(std::allocator_arg, a,
767 MakeUCS2LE(std::allocator_arg, a, sv, CS_Default), enc);
768 }
770
772 CHRLib_Impl_MakeTrans(MBCS, char16_t, char, ISO_10646_UCS_2, Null, CS_Default)
774
776 CHRLib_Impl_MakeTrans(MBCS, char32_t, char, ISO_10646_UCS_4, Null, CS_Default)
778
779 #undef CHRLib_Impl_MakeTrans
780 #undef CHRLib_Impl_Transcode
781 #undef CHRLib_Impl_TranscodeView
782
783
784 } // namespace CHRLib;
785
786 #endif
787
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
CHRLib::CharSet::ISO_10646_UCS_2
yconstexpr const Encoding ISO_10646_UCS_2(csUnicode)
string.hpp
ISO C++ 标准字符串扩展。
platform::strings::basic_string
ystdex::basic_string< _tChar, _tTraits, _tAlloc > basic_string
满足 ISO C++03 std::basic_string 但不保证满足 ISO C++11 的实现。
Definition: Container.h:175
CHRLib::ToASCII
YB_ATTR_nodiscard YB_STATELESS yconstfn char ToASCII(_tChar c)
任意整数类型字符转换为 ASCII 取值范围兼容的字符。
Definition: CharacterProcessing.h:64
CHRLib::ConversionState
编码转换状态。
Definition: CharacterMapping.h:109
CHRLib::c
c
Definition: CharacterProcessing.h:84
CHRLib::fp
std::FILE ConversionState fp
Definition: CharacterProcessing.h:95
CHRLib::CharSet::ISO_10646_UCS_4
yconstexpr const Encoding ISO_10646_UCS_4(csUCS4)
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, _type sv)
Definition: CharacterProcessing.h:567
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
CHRLib::size_t
return size_t(d - p)
cstring.h
ISO C 标准字符串扩展。
CHRLib::PDefH
YB_ATTR_nodiscard YB_PURE PDefH(char16_t, FetchBiCharBE, const char *c_ptr) ImplRet(yconstraint(c_ptr)
取 c_ptr 指向的大端序双字节字符。
CHRLib::Transcoding::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, const _tChar *s, _tParams &&... args)
Definition: CharacterProcessing.h:432
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, const _tChar *s)
Definition: CharacterProcessing.h:505
ystdex::uchar_t
cond_t< is_trivially_replaceable< wchar_t, char16_t >, char16_t, cond_t< is_trivially_replaceable< wchar_t, char32_t >, char32_t, void > > uchar_t
指定和 wchar_t 互相替换存储的非 char 内建字符类型。
Definition: cstring.h:118
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
CHRLib::DispatchTranscoding
Definition: CharacterProcessing.h:386
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(_type sv, Encoding enc, _tParams &&... args)
Definition: CharacterProcessing.h:549
CHRLib::MBCSToUCS4
size_t MBCSToUCS4(char32_t *d, const char *s)
Definition: CharacterProcessing.cpp:155
YB_FLATTEN
#define YB_FLATTEN
标记函数尽可能内联定义内的调用。
Definition: ydef.h:843
CHRLib::ImplRet
YB_ATTR_nodiscard YB_PURE const char *c_ptr ImplRet(yconstraint(c_ptr), char16_t((c_ptr[1]<< CHAR_BIT)| *c_ptr)) enum class ConversionResult
编码转换结果。
Definition: CharacterMapping.h:86
CHRLib
Definition: CharacterDefinition.h:39
CHRLib::CharSet::Null
@ Null
空字符集。
Definition: Encoding.h:63
CHRLib::CharSet::Encoding
Encoding
字符流编码标识。
Definition: Encoding.h:59
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(_type sv)
Definition: CharacterProcessing.h:533
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:142
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, const _tChar *s, Encoding enc, _tParams &&... args)
Definition: CharacterProcessing.h:519
YF_API
#define YF_API
Definition: Platform.h:56
CHRLib::Transcoding::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, _type sv, _tParams &&... args)
Definition: CharacterProcessing.h:460
CHRLib::f
f(d, s)
CHRLib::MakeMBCS
YB_ATTR_nodiscard YB_PURE _tDst MakeMBCS(string_view sv, Encoding enc)
Definition: CharacterProcessing.h:744
ystdex::ntctslen
size_t ntctslen(const _tChar *s)
计算简单 NTCTS 长度。
Definition: cstring.h:456
CHRLib::MakeUCS2LE
YB_ATTR_nodiscard YB_PURE _tDst MakeUCS2LE(basic_string_view< char > sv, _tParams &&... args)
Definition: CharacterProcessing.h:649
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
ystdex::is_null
bool is_null(_tChar c)
使用 std::char_traits::eq 判断是否为空字符。
Definition: cstring.h:127
CHRLib::UCS4ToUCS2
size_t UCS4ToUCS2(char16_t *d, const char32_t *s)
Definition: CharacterProcessing.cpp:274
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
YSLib::Drawing::src
src
Definition: YGDI.h:442
CHRLib::Encoding
std::FILE Encoding
Definition: CharacterProcessing.h:92
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:941
ystdex::cpp2017::end
auto end(_tRange &c) -> decltype(c.end())
Definition: range.hpp:345
std
Definition: optional.h:982
CHRLib::Transcoding
Definition: CharacterProcessing.h:418
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(std::allocator_arg_t, const typename _tDst::allocator_type &a, _type sv, Encoding enc, _tParams &&... args)
Definition: CharacterProcessing.h:585
CHRLib::UCToMBC
size_t UCToMBC(char *d, const char16_t &s, Encoding enc)
Definition: CharacterProcessing.cpp:109
CHRLib::MBCToUC
MBCToUC
Definition: CharacterProcessing.h:112
CHRLib::YB_NONNULL
YB_NONNULL(1) inline PDefH(ConversionResult
转换指定编码的多字节字符串为指定类型的 UCS-2 字符串。
CHRLib_Impl_DispatchTranscoding
#define CHRLib_Impl_DispatchTranscoding(_vFrom, _vTo, _n)
Definition: CharacterProcessing.h:388
CHRLib::p
const auto p(d)
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:125
CHRLib::MBCSToUCS2
size_t MBCSToUCS2(char16_t *d, const char *s)
Definition: CharacterProcessing.cpp:122
ystdex::cpp2017::u16string_view
basic_string_view< char16_t > u16string_view
Definition: string_view.hpp:438
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
CHRLib::CS_Default
yconstexpr const Encoding CS_Default(CharSet::UTF_8)
默认字符编码。
YModules.h
YSLib 用户界面模块配置文件。
CHRLib::UCS4ToMBCS
size_t UCS4ToMBCS(char *d, const char32_t *s)
Definition: CharacterProcessing.cpp:243
CHRLib::char16_t
YB_ATTR_nodiscard YB_PURE char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) YB_ATTR_nodiscard YB_PURE inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
CHRLib::Transcoding::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(const _tChar *s, _tParams &&... args)
Definition: CharacterProcessing.h:421
YSLib::Drawing::w
The boundary is out of the const Rect SDst w
Definition: YDraw.h:127
ystdex::string_traits::value_type
remove_cvref_t< decltype(std::declval< string_type >()[0])> value_type
Definition: string.hpp:790
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(const _tChar *s, Encoding enc, _tParams &&... args)
Definition: CharacterProcessing.h:493
CHRLib::IsASCII
YB_ATTR_nodiscard YB_STATELESS yconstfn bool IsASCII(_tChar c)
判断整数类型字符在 ASCII 字符取值范围内。
Definition: CharacterProcessing.h:53
CHRLib_Impl_MakeTrans
#define CHRLib_Impl_MakeTrans(_n, _tSrcChar, _tDstChar, _vFrom, _vTo, _vDeEnc)
Definition: CharacterProcessing.h:618
CHRLib::enc
enc
Definition: CharacterProcessing.h:84
YSLib::Text::str
YB_ATTR_nodiscard YB_PURE const char const char * str
Definition: TextFile.h:83
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
CHRLib::Transcoding< _vFrom, CharSet::Null, _tDispatcher >::MakeTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeTranscoded(const _tChar *s)
Definition: CharacterProcessing.h:482
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:96
CHRLib::ucsint_t
std::char_traits< char32_t >::int_type ucsint_t
UCS 整数类型。
Definition: CharacterDefinition.h:47
CHRLib::MakeUCS4LE
YB_ATTR_nodiscard YB_PURE _tDst MakeUCS4LE(basic_string_view< char > sv, _tParams &&... args)
Definition: CharacterProcessing.h:691
YSLib::Drawing::char32_t
YB_ATTR_nodiscard YB_PURE tr TextRegion &tr char32_t
Definition: TextLayout.h:93
CHRLib::UCS2ToMBCS
size_t UCS2ToMBCS(char *d, const char16_t *s)
Definition: CharacterProcessing.cpp:192
CHRLib::FetchMaxCharWidth
YF_API YB_STATELESS size_t FetchMaxCharWidth(Encoding)
取指定编码的最大字符宽度。
Definition: chrmap.cpp:59
CHRLib::UCS2ToUCS4
size_t UCS2ToUCS4(char32_t *d, const char16_t *s)
Definition: CharacterProcessing.cpp:223
ystdex::cpp2017::u32string_view
basic_string_view< char32_t > u32string_view
Definition: string_view.hpp:439
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:168
platform::l
return l
Definition: YCLib/FileSystem.h:402
CHRLib::Transcoding::MakeViewTranscoded
YB_ATTR_nodiscard static YB_PURE _tDst MakeViewTranscoded(_type sv, _tParams &&... args)
Definition: CharacterProcessing.h:444