YSTest: MappingEx.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

MappingEx.h
浏览该文件的文档.
1 /*
2 © 2012-2016 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_MapEx_h_
29 #define INC_CHRLib_MapEx_h_ 1
30
31 #include "YModules.h"
32 #include YFM_CHRLib_StaticMapping
33
34 namespace CHRLib
35 {
36
42 #ifndef CHRLib_NoDynamicMapping
43 # define CHRLib_NoDynamicMapping 0
44 #endif
45
46 #if CHRLib_NoDynamicMapping
47
52 yconstexpr const byte cp17[]{0};
53 extern "C"
54 {
55 extern const byte cp113[];
56 //extern const byte cp2026[13658];
57 }
58 yconstexpr const byte cp2026[]{0};
60 #else
61
69 extern byte* cp17;
70 extern byte* cp113;
71 extern byte* cp2026;
73
74 #endif
75
81 extern char16_t(*cp113_lkp)(byte, byte);
82
83
89 template<>
90 struct GUCSMapper<CharSet::SHIFT_JIS> : UCSMapperBase
91 {
92 using UCSMapperBase::Assign;
93
94 #if 0
95 template<typename _tObj, typename _tIn, typename _tState>
96 static byte
97 Decode(_tObj&& uc, _tIn&& i, _tState&& st) ynoexcept(noexcept(GetSequenceOf(
98 st)) && noexcept(GetIndexOf(st)) && noexcept(!FillByte(i, st)))
99 {
100 uint_least16_t row(0), col(0), ln(188); // (7E-40 + 1 + FC-80 + 1)
101 const auto c(FillByte(i, st));
102
103 if(c >= 0xA1 && c <= 0xC6)
104 {
105 const auto d(FillByte(i, st));
106
107 row = c - 0xA1 ;
108 if(d >= 0x40 && d <= 0x7E)
109 col = d - 0x40 ;
110 else if(d >= 0xA1 && d <= 0xFE)
111 col = d - 0x62;
112 Assign(uc, cp17[row * ln + col]);
113 }
114 else if(c >= 0xC9 && c <= 0xF9)
115 {
116 const auto d(FillByte(i, st));
117
118 row = c - 0xA3;
119 if(d >= 0x40 && d <= 0x7E)
120 col = d - 0x40 ;
121 else if(d >= 0xA1 && d <= 0xFE)
122 col = d - 0x62;
123 Assign(uc, cp17[row * ln + col]);
124 }
125 else if(c < 0x80)
126 {
127 Assign(uc, c);
128 return 1;
129 }
130 else
131 uc = 0xFFFE;
132 return 2;
133 }
134 #endif
135 };
136
137 template<>
138 struct GUCSMapper<CharSet::GBK> : UCSMapperBase
139 {
140 using UCSMapperBase::Assign;
141
146 template<typename _tObj, typename _tIn, typename _tState>
147 static ConversionResult
148 Decode(_tObj&& uc, _tIn&& i, _tState&& st) ynoexcept(noexcept(GetSequenceOf(
149 st)) && noexcept(GetIndexOf(st)) && noexcept(!FillByte(i, st)))
150 {
151 yassume(cp113_lkp);
152
153 const auto seq(GetSequenceOf(st));
154
155 switch(GetIndexOf(st))
156 {
157 case 0:
158 if(YB_UNLIKELY(!FillByte(i, st)))
159 return ConversionResult::BadSource;
160 if(seq[0] < 0x80)
161 {
162 Assign(uc, seq[0]);
163 break;
164 }
165 case 1:
166 if(YB_UNLIKELY(!FillByte(i, st)))
167 return ConversionResult::BadSource;
168 {
169 const auto t(cp113_lkp(seq[0], seq[1]));
170
171 if(YB_LIKELY(t != 0))
172 {
173 Assign(uc, t);
174 break;
175 }
176 }
177 return ConversionResult::Unhandled;
178 default:
179 return ConversionResult::BadState;
180 }
181 return ConversionResult::OK;
182 }
183 };
184
185 template<>
186 struct GUCSMapper<CharSet::Big5> : UCSMapperBase
187 {
188 using UCSMapperBase::Assign;
189
190 #if 0
191 template<typename _tObj, typename _tIn, typename _tState>
192 static byte
193 Decode(_tObj&& uc, _tIn&& i, _tState&& st) ynoexcept(noexcept(GetSequenceOf(
194 st)) && noexcept(GetIndexOf(st)) && noexcept(!FillByte(i, st)))
195 {
196 uint_least16_t row(0), col(0), ln(157); // (7E-40 + FE-A1)
197 const auto c(FillByte(i, st));
198
199 if(c >= 0xA1 && c <= 0xC6)
200 {
201 const auto d(FillByte(i, st));
202
203 row = c - 0xA1;
204 if(d >= 0x40 && d <= 0x7E)
205 col = d - 0x40;
206 else if(d >= 0xA1 && d <= 0xFE)
207 col = d - 0x62;
208 Assign(uc, cp2026[row * ln + col]);
209 return 2;
210 }
211 else if(c >= 0xC9 && c <= 0xF9)
212 {
213 const auto d(FillByte(i, st));
214
215 row = c - 0xA3;
216 if(d >= 0x40 && d <= 0x7E)
217 col = c - 0x40;
218 else if(d >= 0xA1 && d <= 0xFE)
219 col = d - 0x62;
220 Assign(uc, cp2026[row * ln + col]);
221 return 2;
222 }
223 else if(c < 0x80)
224 {
225 Assign(uc, c);
226 return 1;
227 }
228 else
229 Assign(uc, 0xFFFE);
230 return 2;
231 }
232 #endif
233 };
235
236
237 // NOTE: See https://llvm.org/bugs/show_bug.cgi?id=25306.
238 #if YB_IMPL_CLANGPP
239 # pragma GCC diagnostic push
240 # pragma GCC diagnostic ignored "-Wignored-attributes"
241 #endif
242
244
245 template<Encoding, typename _tRet, typename... _tParams>
246 yconstfn YB_STATELESS ystdex::add_pointer_t<_tRet(_tParams...)>
247 FetchMapperPtr_TryUCSMapper(...) ynothrow
248 {
249 return {};
250 }
251 template<Encoding _vEnc, typename _tRet, typename... _tParams>
252 yconstfn YB_ATTR_returns_nonnull YB_STATELESS
253 ystdex::add_pointer_t<_tRet(_tParams...)>
254 FetchMapperPtr_TryUCSMapper(yimpl(ystdex::enable_if_convertible_t<
255 decltype(GUCSMapper<_vEnc>::Decode(std::declval<_tParams>()...)), _tRet>*
256 = {})) ynothrow
257 {
258 return GUCSMapper<_vEnc>::Decode;
259 }
260 template<Encoding _vEnc, typename _tRet, typename... _tParams>
261 yconstfn YB_ATTR_returns_nonnull YB_STATELESS
262 ystdex::add_pointer_t<_tRet(_tParams...)>
263 FetchMapperPtr_TryUCSMapper(yimpl(ystdex::enable_if_convertible_t<
264 decltype(GUCSMapper<_vEnc>::Encode(std::declval<_tParams>()...)), _tRet,
265 int>* = {})) ynothrow
266 {
267 return GUCSMapper<_vEnc>::Encode;
268 }
269
270 #if YB_IMPL_CLANGPP
271 # pragma GCC diagnostic pop
272 #endif
273
275 template<typename _tRet, typename... _tParams>
276 yconstfn YB_STATELESS ystdex::add_lvalue_reference_t<_tRet(_tParams...)>
277 FetchMapper_Default() ynothrow
278 {
279 return *FetchMapperPtr_TryUCSMapper<CS_Default, _tRet, _tParams...>(nullptr);
280 }
281
283 template<typename _tRet, typename... _tParams>
284 yconstfn_relaxed ystdex::add_pointer_t<_tRet(_tParams...)>
285 FetchMapperPtr(Encoding enc)
286 {
287 using namespace CharSet;
288
289 #define CHR_MapItem(enc) \
290 case enc: \
291 return FetchMapperPtr_TryUCSMapper<enc, _tRet, _tParams...>(nullptr);
292
293 switch(enc)
294 {
295 CHR_MapItem(SHIFT_JIS)
296 CHR_MapItem(UTF_8)
297 CHR_MapItem(GBK)
298 CHR_MapItem(UTF_16BE)
299 CHR_MapItem(UTF_16LE)
300 CHR_MapItem(UTF_32BE)
301 CHR_MapItem(UTF_32LE)
302 CHR_MapItem(Big5)
303 default:
304 break;
305 }
306 #undef CHR_MapItem
307 return {};
308 }
310
311 } // namespace CHRLib;
312
313 #endif
314
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:99
CHRLib::enc
enc
Definition: CharacterProcessing.h:81
CHRLib::cp113_lkp
char16_t(* cp113_lkp)(byte, byte)
查找编码转换序列的实现。
Definition: MappingEx.cpp:64
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:522
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:653
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
CHRLib::GUCSMapper
静态编码操作模板及 Unicode 编码特化。
Definition: StaticMapping.hpp:157
ystdex::enable_if_convertible_t
enable_if_t< is_convertible< _tFrom, _tTo >::value, _type > enable_if_convertible_t
Definition: type_traits.hpp:993
CHRLib::Encoding
std::FILE Encoding
Definition: CharacterProcessing.h:89
CHRLib::CharSet::UTF_8
yconstexpr const Encoding UTF_8(csUTF8)
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
CHRLib::CS_Default
yconstexpr const Encoding CS_Default(CharSet::UTF_8)
默认字符编码。
CHRLib::FetchMapperPtr_TryUCSMapper
yconstfn YB_STATELESS ystdex::add_pointer_t< _tRet(_tParams...)> FetchMapperPtr_TryUCSMapper(...) ynothrow
Definition: MappingEx.h:247
CHRLib::c
c
Definition: CharacterProcessing.h:81
CHRLib::CharSet::Encoding
Encoding
字符流编码标识。
Definition: Encoding.h:58
CHRLib::GUCSMapper< CharSet::GBK >::Decode
static ConversionResult Decode(_tObj &&uc, _tIn &&i, _tState &&st) ynoexcept(noexcept(GetSequenceOf(st))&&noexcept(GetIndexOf(st))&&noexcept(!FillByte(i, st)))
Definition: MappingEx.h:148
CHRLib::CharSet::UTF_32LE
yconstexpr const Encoding UTF_32LE(csUTF32LE)
ystdex::cpp2014::add_pointer_t
typename add_pointer< _type >::type add_pointer_t
Definition: type_traits.hpp:331
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
CHRLib::char16_t
char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
CHRLib::FetchMapperPtr
yconstfn_relaxed ystdex::add_pointer_t< _tRet(_tParams...)> FetchMapperPtr(Encoding enc)
取指定编码映射的转换函数指针。
Definition: MappingEx.h:285
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:438
ystdex::cpp2014::add_lvalue_reference_t
typename add_lvalue_reference< _type >::type add_lvalue_reference_t
Definition: type_traits.hpp:307
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:666
CHRLib::CharSet::UTF_32BE
yconstexpr const Encoding UTF_32BE(csUTF32BE)
CHRLib::GetSequenceOf
yconstfn ConversionState &st GetSequenceOf
Definition: CharacterMapping.h:138
CHRLib::FetchMapper_Default
yconstfn YB_STATELESS ystdex::add_lvalue_reference_t< _tRet(_tParams...)> FetchMapper_Default() ynothrow
Definition: MappingEx.h:277
CHRLib::CharSet::UTF_16BE
yconstexpr const Encoding UTF_16BE(csUTF16BE)
CHRLib::cp17
byte * cp17
动态加载的编码转换表。
YModules.h
CHRLib::cp2026
byte * cp2026
YSLib::i
return i
Definition: YCoreUtilities.h:176
CHRLib
Definition: CharacterDefinition.h:38
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:720
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:437
CHRLib::CharSet::UTF_16LE
yconstexpr const Encoding UTF_16LE(csUTF16LE)
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
CHRLib::CharSet::Big5
yconstexpr const Encoding Big5(csBig5)
< 仅 EUC-CN ，GB2312 最常用实现。
CHRLib::CharSet::SHIFT_JIS
yconstexpr const Encoding SHIFT_JIS(csShiftJIS)
CHRLib::cp113
byte * cp113
Definition: MappingEx.cpp:57
CHRLib::FillByte
bool FillByte(_tIn &i, byte &b) ynoexcept(noexcept(byte(*i))&&noexcept(++i))
以输入迭代器指向内容填充有效输入迭代器指定的字节。
Definition: StaticMapping.hpp:57
YB_ATTR_returns_nonnull
#define YB_ATTR_returns_nonnull
指示返回非空属性。
Definition: ydef.h:390
CHRLib::CharSet::GBK
yconstexpr const Encoding GBK(csGBK)
CHR_MapItem
#define CHR_MapItem(enc)