YSTest: CharacterProcessing.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

CharacterProcessing.cpp
浏览该文件的文档.
1 /*
2 © 2009-2015 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 #include "CHRLib/YModules.h"
29 #include YFM_CHRLib_CharacterProcessing
30 #include YFM_CHRLib_MappingEx
31 #include <cctype>
32 #include <cstdlib>
33 #include <cwchar>
34 #include <ystdex/cstdio.h>
35 #include <ystdex/memory.hpp> // for ystdex::make_unique;
36 #include YFM_CHRLib_Convert
37 #include <ystdex/algorithm.hpp> // for ystdex::copy_when,
38 // ystdex::transform_when;
39
40 namespace CHRLib
41 {
42
43 using std::malloc;
44 using std::size_t;
45 using std::tolower;
46 using ystdex::is_null;
47 using ystdex::ntctslen;
49 using ystdex::make_unique;
50
51 ConversionResult
52 MBCToUC(char16_t& uc, const char*& c, Encoding enc, ConversionState&& st)
53 {
54 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&, const char*&,
55 ConversionState&&>(enc))
56 return pfun(uc, c, std::move(st));
57 return ConversionResult::Unhandled;
58 }
59 ConversionResult
60 MBCToUC(char16_t& uc, const char*& c, const char* e, Encoding enc,
61 ConversionState&& st)
62 {
63 yconstraint(c <= e);
64 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
65 GuardPair<const char*>&&, ConversionState&&>(enc))
66 return pfun(uc, {c, e}, std::move(st));
67 return ConversionResult::Unhandled;
68 }
69 ConversionResult
70 MBCToUC(char16_t& uc, std::FILE* fp, Encoding enc, ConversionState&& st)
71 {
72 yconstraint(fp);
73 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
74 ystdex::ifile_iterator&, ConversionState&&>(enc))
75 {
76 ystdex::ifile_iterator i(fp);
77 const auto r(pfun(uc, i, std::move(st)));
78
79 i.sungetc(fp);
80 return r;
81 }
82 return ConversionResult::Unhandled;
83 }
84 ConversionResult
85 MBCToUC(const char*& c, Encoding enc, ConversionState&& st)
86 {
87 if(const auto pfun = FetchMapperPtr<ConversionResult,
88 ystdex::pseudo_output&&, const char*&, ConversionState&&>(enc))
89 return pfun(ystdex::pseudo_output(), c, std::move(st));
90 return ConversionResult::Unhandled;
91 }
92 ConversionResult
93 MBCToUC(const char*& c, const char* e, Encoding enc, ConversionState&& st)
94 {
95 yconstraint(c <= e);
96 if(const auto pfun = FetchMapperPtr<ConversionResult,
97 ystdex::pseudo_output&&, GuardPair<const char*>&&,
98 ConversionState&&>(enc))
99 return pfun(ystdex::pseudo_output(), {c, e}, std::move(st));
100 return ConversionResult::Unhandled;
101 }
102 ConversionResult
103 MBCToUC(std::FILE* fp, Encoding enc, ConversionState&& st)
104 {
105 yconstraint(fp);
106 if(const auto pfun = FetchMapperPtr<ConversionResult,
107 ystdex::pseudo_output&&, ystdex::ifile_iterator&,
108 ConversionState&&>(enc))
109 {
110 ystdex::ifile_iterator i(fp);
111 const auto r(pfun(ystdex::pseudo_output(), i, std::move(st)));
112
113 i.sungetc(fp);
114 return r;
115 }
116 return ConversionResult::Unhandled;
117 }
118
119 size_t
120 UCToMBC(char* d, const char16_t& s, Encoding enc)
121 {
122 yconstraint(d);
123
124 size_t l(0);
125
126 if(const auto pfun = FetchMapperPtr<size_t, char*, char32_t>(enc))
127 l = pfun(d, s);
128 return l;
129 }
130
131
132 size_t
133 MBCSToUCS2(char16_t* d, const char* s)
134 {
135 return MBCSToUCS2(FetchMapper_Default<ConversionResult, char16_t&,
136 const char*&, ConversionState&&>(), d, s);
137 }
138 size_t
139 MBCSToUCS2(char16_t* d, const char* s, Encoding enc)
140 {
141 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
142 const char*&, ConversionState&&>(enc))
143 return MBCSToUCS2(pfun, d, s);
144 else
145 yconstraint(d && s);
146 return 0;
147 }
148 size_t
149 MBCSToUCS2(char16_t* d, const char* s, const char* e)
150 {
151 return MBCSToUCS2(FetchMapper_Default<ConversionResult, char16_t&,
152 GuardPair<const char*>&&, ConversionState&&>(), d, s, e);
153 }
154 size_t
155 MBCSToUCS2(char16_t* d, const char* s, const char* e, Encoding enc)
156 {
157 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
158 GuardPair<const char*>&&, ConversionState&&>(enc))
159 return MBCSToUCS2(pfun, d, s, e);
160 else
161 yconstraint(d && s && e && s <= e);
162 return 0;
163 }
164
165 size_t
166 MBCSToUCS4(char32_t* d, const char* s)
167 {
168 // TODO: Use UCS-4 internal conversion directly?
169 return MBCSToUCS4(FetchMapper_Default<ConversionResult, char16_t&,
170 const char*&, ConversionState&&>(), d, s);
171 }
172 size_t
173 MBCSToUCS4(char32_t* d, const char* s, Encoding enc)
174 {
175 // TODO: Use UCS-4 internal conversion directly?
176 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
177 const char*&, ConversionState&&>(enc))
178 return MBCSToUCS4(pfun, d, s);
179 else
180 yconstraint(d && s);
181 return 0;
182 }
183 size_t
184 MBCSToUCS4(char32_t* d, const char* s, const char* e)
185 {
186 // TODO: Use UCS-4 internal conversion directly?
187 return MBCSToUCS4(FetchMapper_Default<ConversionResult, char16_t&,
188 GuardPair<const char*>&&, ConversionState&&>(), d, s, e);
189 }
190 size_t
191 MBCSToUCS4(char32_t* d, const char* s, const char* e, Encoding enc)
192 {
193 // TODO: Use UCS-4 internal conversion directly?
194 if(const auto pfun = FetchMapperPtr<ConversionResult, char16_t&,
195 GuardPair<const char*>&&, ConversionState&&>(enc))
196 return MBCSToUCS4(pfun, d, s, e);
197 else
198 yconstraint(d && s && e && s <= e);
199 return 0;
200 }
201
202 size_t
203 UCS2ToMBCS(char* d, const char16_t* s)
204 {
205 return UCS2ToMBCS(FetchMapper_Default<size_t, char*, char32_t>(), d, s);
206 }
207 size_t
208 UCS2ToMBCS(char* d, const char16_t* s, Encoding enc)
209 {
210 if(const auto pfun = FetchMapperPtr<size_t, char*, char32_t>(enc))
211 return UCS2ToMBCS(pfun, d, s);
212 else
213 yconstraint(d && s);
214 return 0;
215 }
216 size_t
217 UCS2ToMBCS(char* d, const char16_t* s, const char16_t* e)
218 {
219 // TODO: Deferred. Use guard for encoding.
220 return UCS2ToMBCS(FetchMapper_Default<size_t, char*, char32_t>(), d, s, e);
221 }
222 size_t
223 UCS2ToMBCS(char* d, const char16_t* s, const char16_t* e, Encoding enc)
224 {
225 // TODO: Deferred. Use guard for encoding.
226 if(const auto pfun = FetchMapperPtr<size_t, char*, char32_t>(enc))
227 return UCS2ToMBCS(pfun, d, s, e);
228 else
229 yconstraint(d && s && e && s <= e);
230 return 0;
231 }
232
233 size_t
234 UCS2ToUCS4(char32_t* d, const char16_t* s)
235 {
236 const auto p(ystdex::copy_when(s, d, [](char16_t c) ynothrow{
237 return !is_null(c);
238 }));
239
240 *p = char32_t();
241 return size_t(p - d);
242 }
243 size_t
244 UCS2ToUCS4(char32_t* d, const char16_t* s, const char16_t* e)
245 {
246 yconstraint(d),
247 yconstraint(s),
248 yconstraint(s <= e);
249
250 return size_t(std::copy(s, e, d) - d);
251 }
252
253 size_t
254 UCS4ToMBCS(char* d, const char32_t* s)
255 {
256 return UCS4ToMBCS(FetchMapper_Default<size_t, char*, char32_t>(), d, s);
257 }
258 size_t
259 UCS4ToMBCS(char* d, const char32_t* s, Encoding enc)
260 {
261 if(const auto pfun = FetchMapperPtr<size_t, char*, char32_t>(enc))
262 return UCS4ToMBCS(pfun, d, s);
263 else
264 yconstraint(d && s);
265 return 0;
266 }
267 size_t
268 UCS4ToMBCS(char* d, const char32_t* s, const char32_t* e)
269 {
270 // TODO: Deferred. Use guard for encoding.
271 return UCS4ToMBCS(FetchMapper_Default<size_t, char*, char32_t>(), d, s, e);
272 }
273 size_t
274 UCS4ToMBCS(char* d, const char32_t* s, const char32_t* e, Encoding enc)
275 {
276 // TODO: Deferred. Use guard for encoding.
277 if(const auto pfun = FetchMapperPtr<size_t, char*, char32_t>(enc))
278 return UCS4ToMBCS(pfun, d, s, e);
279 else
280 yconstraint(d && s && e && s <= e);
281 return 0;
282 }
283
284 size_t
285 UCS4ToUCS2(char16_t* d, const char32_t* s)
286 {
287 yconstraint(d),
288 yconstraint(s);
289
290 const auto p(ystdex::transform_when(s, d, [](char32_t c) ynothrow{
291 return !is_null(c);
292 }, [](char32_t c) ynothrow{
293 return char16_t(c);
294 }));
295
296 *p = char16_t();
297 return size_t(p - d);
298 }
299 size_t
300 UCS4ToUCS2(char16_t* d, const char32_t* s, const char32_t* e)
301 {
302 yconstraint(d),
303 yconstraint(s),
304 yconstraint(s <= e);
305
306 return size_t(std::transform(s, e, d, [](char32_t c) ynothrow{
307 return char16_t(c);
308 }) - d);
309 }
310
311 } // namespace CHRLib;
312
CHRLib::enc
enc
Definition: CharacterProcessing.h:81
ystdex::is_null
bool is_null(_tChar c)
使用 std::char_traits::eq 判断是否为空字符。
Definition: cstring.h:127
CHRLib::ConversionState
编码转换状态。
Definition: CharacterMapping.h:106
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
CHRLib::GuardPair
std::pair< ystdex::lref< _tIn >, const _tIn > GuardPair
Definition: CharacterMapping.h:185
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:82
cstdio.h
ystdex::ifile_iterator
基于 ISO C 标准库的流只读迭代器。
Definition: ystdex/cstdio.h:114
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
platform::size_t
YF_API size_t
Definition: FileIO.h:1262
ystdex::make_unique
std::unique_ptr< _type > make_unique(std::initializer_list< _tValue > il)
使用指定类型的初始化列表构造指定类型的 std::unique_ptr 对象。
Definition: memory.hpp:780
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
YSLib::Drawing::char32_t
tr TextRegion &tr char32_t
Definition: TextLayout.h:92
CHRLib::size_t
return size_t(d-p)
CHRLib::c
c
Definition: CharacterProcessing.h:81
CHRLib::CharSet::Encoding
Encoding
字符流编码标识。
Definition: Encoding.h:58
CHRLib::UCS4ToUCS2
size_t UCS4ToUCS2(char16_t *d, const char32_t *s)
Definition: CharacterProcessing.cpp:285
CHRLib::char16_t
char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
ystdex::copy_when
_tOut copy_when(_tIn first, _tOut result, _fPred pred)
复制满足谓词的序列的连续元素。
Definition: algorithm.hpp:315
CHRLib::p
const auto p(d)
CHRLib::MBCSToUCS2
size_t MBCSToUCS2(char16_t *d, const char *s)
Definition: CharacterProcessing.cpp:133
CHRLib::FetchMapperPtr
yconstfn_relaxed ystdex::add_pointer_t< _tRet(_tParams...)> FetchMapperPtr(Encoding enc)
取指定编码映射的转换函数指针。
Definition: MappingEx.h:285
CHRLib::UCS2ToUCS4
size_t UCS2ToUCS4(char32_t *d, const char16_t *s)
Definition: CharacterProcessing.cpp:234
ystdex::transform_when
_tOut transform_when(_tIn first, _tOut result, _fPred pred, _fOp op)
变换满足谓词的序列的连续元素。
Definition: algorithm.hpp:302
ystdex::ifile_iterator::sungetc
int_type sungetc()
Definition: ystdex/cstdio.h:243
CHRLib::MBCToUC
MBCToUC
Definition: CharacterProcessing.h:109
CHRLib::MBCSToUCS4
size_t MBCSToUCS4(char32_t *d, const char *s)
Definition: CharacterProcessing.cpp:166
memory.hpp
存储和智能指针特性。
CHRLib::FetchMapper_Default
yconstfn YB_STATELESS ystdex::add_lvalue_reference_t< _tRet(_tParams...)> FetchMapper_Default() ynothrow
Definition: MappingEx.h:277
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YModules.h
ystdex::pseudo_output
伪输出对象。
Definition: ref.hpp:254
CHRLib::UCToMBC
size_t UCToMBC(char *d, const char16_t &s, Encoding enc)
Definition: CharacterProcessing.cpp:120
YSLib::i
return i
Definition: YCoreUtilities.h:176
CHRLib
Definition: CharacterDefinition.h:38
CHRLib::UCS4ToMBCS
size_t UCS4ToMBCS(char *d, const char32_t *s)
Definition: CharacterProcessing.cpp:254
algorithm.hpp
泛型算法。
ystdex::tolower
_tChar tolower(_tChar c)
转换大小写字符。
Definition: cctype.h:137
ystdex::ntctslen
size_t ntctslen(const _tChar *s)
计算简单 NTCTS 长度。
Definition: cstring.h:456
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
CHRLib::UCS2ToMBCS
size_t UCS2ToMBCS(char *d, const char16_t *s)
Definition: CharacterProcessing.cpp:203
CHRLib::fp
std::FILE ConversionState fp
Definition: CharacterProcessing.h:92