YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
chrproc.cpp
浏览该文件的文档.
1 /*
2  © 2009-2014 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/cstring.h>
36 #include <ystdex/memory.hpp> // for ystdex::make_unique;
37 #include YFM_CHRLib_Convert
38 
39 namespace CHRLib
40 {
41 
42 using std::malloc;
43 using std::size_t;
44 using std::tolower;
46 using ystdex::is_null;
47 using ystdex::ntctslen;
50 
52 MBCToUC(ucs2_t& uc, const char*& c, Encoding enc, ConversionState&& st)
53 {
54  if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
56  return ConvertCharacter(pfun, uc, c, std::move(st));
58 }
60 MBCToUC(ucs2_t& uc, std::FILE* fp, Encoding enc, ConversionState&& st)
61 {
62  yconstraint(fp);
63  if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
65  {
67  const auto r(ConvertCharacter(pfun, uc, i, std::move(st)));
68 
69  std::ungetc(*i, fp);
70  return r;
71  }
73 }
75 MBCToUC(const char*& c, Encoding enc, ConversionState&& st)
76 {
77  if(const auto pfun = FetchMapperPtr<ConversionResult(
79  return ConvertCharacter(pfun, c, std::move(st));
81 }
84 {
85  yconstraint(fp);
86  if(const auto pfun = FetchMapperPtr<ConversionResult(
88  {
90  const auto r(ConvertCharacter(pfun, i, std::move(st)));
91 
92  std::ungetc(*i, fp);
93  return r;
94  }
96 }
97 
98 size_t
99 UCToMBC(char* d, const ucs2_t& s, Encoding enc)
100 {
101  yconstraint(d);
102 
103  size_t l(0);
104 
105  if(const auto pfun = FetchMapperPtr<byte(char*, const ucs2_t&)>(enc))
106  l = pfun(d, s);
107  return l;
108 }
109 
110 
111 size_t
112 MBCSToUCS2(ucs2_t* d, const char* s, Encoding enc)
113 {
114  yconstraint(d),
115  yconstraint(s);
116 
117  const auto p(d);
118 
119  if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
121  while(!is_null(*s))
122  {
123  ConversionState st;
124 
125  ConvertCharacter(pfun, *d++, s, std::move(st));
126  }
127  *d = 0;
128  return d - p;
129 }
130 
131 size_t
132 UCS2ToMBCS(char* d, const ucs2_t* s, Encoding enc)
133 {
134  yconstraint(d),
135  yconstraint(s);
136 
137  const auto p(d);
138 
139  if(const auto pfun = FetchMapperPtr<byte(char*, const ucs2_t&)>(enc))
140  while(!is_null(*s))
141  d += pfun(d, *s++);
142  *d = 0;
143  return d - p;
144 }
145 
146 size_t
148 {
149  yconstraint(d),
150  yconstraint(s);
151 
152  const auto p(d);
153 
154  while(!is_null(*s))
155  *d++ = *s++;
156  *d = 0;
157  return d - p;
158 }
159 
160 
163 {
164  yconstraint(s);
165 
166  const auto str(make_unique<char[]>(
167  ntctslen(s) * sizeof(ucsint_t) / sizeof(ucs2_t) + 1));
168 
169  UCS2ToMBCS(&str[0], s, enc);
170  return &str[0];
171 }
172 
173 std::basic_string<ucs2_t>
174 ucsdup(const char* s, Encoding enc)
175 {
176  yconstraint(s);
177 
178  const auto str(make_unique<ucs2_t[]>(ntctslen(s) + 1));
179 
180  MBCSToUCS2(&str[0], s, enc);
181  return &str[0];
182 }
183 std::basic_string<ucs2_t>
184 ucsdup(const ucs2_t* s)
185 {
186  yconstraint(s);
187 
188  const size_t n(ntctslen(s) + 1U);
189  const auto str(make_unique<ucs2_t[]>(n));
190 
191  std::copy_n(s, n, &str[0]);
192  return &str[0];
193 }
194 std::basic_string<ucs2_t>
195 ucsdup(const ucs4_t* s)
196 {
197  yconstraint(s);
198 
199  const auto str(make_unique<ucs2_t[]>(ntctslen(s) + 1));
200 
201  UCS4ToUCS2(&str[0], s);
202  return &str[0];
203 }
204 
205 } // namespace CHRLib;
206 
std::FILE ConversionState fp
Definition: chrproc.h:88
YF_API std::string strdup(const ucs2_t *, Encoding=CS_Default)
复制 UCS-2 字符串为多字节字符串。
Definition: chrproc.cpp:162
ConversionResult
编码转换结果。
Definition: chrmap.h:75
ISO C 标准字符串扩展。
size_t ntctslen(const _tChar *s)
计算简单 NTCTS 长度。
Definition: cstring.h:109
ISO C 标准输入/输出扩展。
ConversionResult ConvertCharacter(_fConv f, ucs2_t &uc, _tIn &&i, ConversionState &&st)
Definition: Convert.hpp:40
编码转换状态。
Definition: chrmap.h:89
基于 ISO C 标准库的流只读迭代器。
Definition: cstdio.h:73
YF_API size_t MBCSToUCS2(ucs2_t *, const char *, Encoding=CS_Default)
按指定编码转换 MBCS 字符串为 UCS-2 字符串,返回转换的串长。
Definition: chrproc.cpp:112
unsigned char byte
字节类型。
Definition: ydef.h:555
YF_API size_t UCS2ToMBCS(char *, const ucs2_t *, Encoding=CS_Default)
按指定编码转换 UCS-2 字符串为 MBCS 字符串,返回转换的串长。
Definition: chrproc.cpp:132
YF_API ConversionResult MBCToUC(ucs2_t &, const char *&, Encoding, ConversionState &&={})
按指定编码和转换状态转换字符串中字符为 UCS-2 字符,返回转换的字节数。
Definition: chrproc.cpp:52
YF_API std::basic_string< ucs2_t > ucsdup(const char *, Encoding=CS_Default)
复制多字节字符串为 UCS-2 字符串。
Definition: chrproc.cpp:174
存储和智能指针特性。
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
#define yconstraint
约束:接口语义。
Definition: cassert.h:47
char16_t ucs2_t
UCS-2 字符类型。
Definition: chrdef.h:44
char32_t ucs4_t
UCS-4 字符类型。
Definition: chrdef.h:45
bounds & r
Definition: ydraw.h:220
size_t UCToMBC(char *d, const ucs2_t &s, Encoding enc)
Definition: chrproc.cpp:99
YF_API size_t UCS4ToUCS2(ucs2_t *, const ucs4_t *)
转换 UCS-4 字符串为 UCS-2 字符串,返回转换的串长。
Definition: chrproc.cpp:147
bool is_null(_tChar c)
使用 std::char_traits::eq 判断是否为空字符。
Definition: cstring.h:86
未处理(超过被处理的界限)。
any_input_iterator< void_ref, ptrdiff_t, void *, void_ref > input_monomorphic_iterator
enable_if_t<!is_array< _type >::value, std::unique_ptr< _type > > make_unique(_tParams &&...args)
使用 new 和指定参数构造指定类型的 std::unique_ptr 实例。
Definition: memory.hpp:213
_fCodemapTransform * FetchMapperPtr(Encoding enc)
取指定编码映射的转换函数指针。
Definition: MapEx.h:217
std::char_traits< ucs4_t >::int_type ucsint_t
UCS 整数类型。
Definition: chrdef.h:46