Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

fxchar.h
1 /********************************************************************************
2 * *
3 * U n i c o d e C h a r a c t e r E n c o d i n g S u p p o r t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXCHAR_H
22 #define FXCHAR_H
23 
24 
25 namespace FX {
26 
27 
28 /*************************** UTF8 Support Functions **************************/
29 
31 static inline FXbool isUTF8(FXchar c){
32  return (c&0xC0)!=0x80;
33  }
34 
36 static inline FXbool leadUTF8(FXchar c){
37  return (c&0xC0)==0xC0;
38  }
39 
41 static inline FXbool followUTF8(FXchar c){
42  return (c&0xC0)==0x80;
43  }
44 
46 static inline FXbool seqUTF8(FXchar c){
47  return (c&0x80)==0x80;
48  }
49 
51 static inline FXival lenUTF8(FXchar c){
52  return ((0xE5000000>>((c>>3)&0x1E))&3)+1;
53  }
54 
56 static inline FXival wclen(const FXchar *ptr){
57  return lenUTF8(*ptr);
58  }
59 
61 static inline FXwchar wc(const FXchar* ptr){
62  FXwchar w=(FXuchar)ptr[0];
63  if(0xC0<=w){ w = (w<<6) ^ (FXuchar)ptr[1] ^ 0x3080;
64  if(0x800<=w){ w = (w<<6) ^ (FXuchar)ptr[2] ^ 0x20080;
65  if(0x10000<=w){ w = (w<<6) ^ (FXuchar)ptr[3] ^ 0x400080; }}}
66  return w;
67  }
68 
70 static inline FXwchar wc1(const FXchar* ptr){
71  return (FXuchar)ptr[0];
72  }
73 
75 static inline FXwchar wc2(const FXchar* ptr){
76  return ((FXuchar)ptr[0]<<6)^((FXuchar)ptr[1])^0x3080;
77  }
78 
80 static inline FXwchar wc3(const FXchar* ptr){
81  return ((FXuchar)ptr[0]<<12)^((FXuchar)ptr[1]<<6)^((FXuchar)ptr[2])^0x0E2080;
82  }
83 
85 static inline FXwchar wc4(const FXchar* ptr){
86  return ((FXuchar)ptr[0]<<18)^((FXuchar)ptr[1]<<12)^((FXuchar)ptr[2]<<6)^((FXuchar)ptr[3])^0x3C82080;
87  }
88 
90 static inline FXwchar wcnxt(const FXchar*& ptr){
91  FXwchar w=(FXuchar)*ptr++;
92  if(0xC0<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x3080;
93  if(0x800<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x20080;
94  if(0x10000<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x400080; }}}
95  return w;
96  }
97 
99 static inline FXwchar wcnxt(FXchar*& ptr){
100  FXwchar w=(FXuchar)*ptr++;
101  if(0xC0<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x3080;
102  if(0x800<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x20080;
103  if(0x10000<=w){ w = (w<<6) ^ (FXuchar)*ptr++ ^ 0x400080; }}}
104  return w;
105  }
106 
108 static inline FXwchar wcprv(const FXchar*& ptr){
109  FXwchar w=(FXuchar)*--ptr;
110  if(0x80<=w){ w = ((FXuchar)*--ptr<<6) ^ w ^ 0x3080;
111  if(0x1000<=w){ w = ((FXuchar)*--ptr<<12) ^ w ^ 0xE1000;
112  if(0x20000<=w){ w = ((FXuchar)*--ptr<<18) ^ w ^ 0x3C60000; }}}
113  return w;
114  }
115 
117 static inline FXwchar wcprv(FXchar*& ptr){
118  FXwchar w=(FXuchar)*--ptr;
119  if(0x80<=w){ w = ((FXuchar)*--ptr<<6) ^ w ^ 0x3080;
120  if(0x1000<=w){ w = ((FXuchar)*--ptr<<12) ^ w ^ 0xE1000;
121  if(0x20000<=w){ w = ((FXuchar)*--ptr<<18) ^ w ^ 0x3C60000; }}}
122  return w;
123  }
124 
126 static inline const FXchar* wcinc(const FXchar* ptr){
127  return (isUTF8(*++ptr) || isUTF8(*++ptr) || isUTF8(*++ptr) || ++ptr), ptr;
128  }
129 
131 static inline FXchar* wcinc(FXchar* ptr){
132  return (isUTF8(*++ptr) || isUTF8(*++ptr) || isUTF8(*++ptr) || ++ptr), ptr;
133  }
134 
136 static inline const FXchar* wcdec(const FXchar* ptr){
137  return (isUTF8(*--ptr) || isUTF8(*--ptr) || isUTF8(*--ptr) || --ptr), ptr;
138  }
139 
141 static inline FXchar* wcdec(FXchar* ptr){
142  return (isUTF8(*--ptr) || isUTF8(*--ptr) || isUTF8(*--ptr) || --ptr), ptr;
143  }
144 
146 static inline const FXchar* wcstart(const FXchar* ptr){
147  return (isUTF8(*ptr) || isUTF8(*--ptr) || isUTF8(*--ptr) || --ptr), ptr;
148  }
149 
151 static inline FXchar* wcstart(FXchar* ptr){
152  return (isUTF8(*ptr) || isUTF8(*--ptr) || isUTF8(*--ptr) || --ptr), ptr;
153  }
154 
155 /************************** UTF16 Support Functions **************************/
156 
158 static inline FXbool isUTF16(FXnchar c){
159  return (c&0xFC00)!=0xDC00;
160  }
161 
163 static inline FXbool leadUTF16(FXnchar c){
164  return (c&0xFC00)==0xD800;
165  }
166 
168 static inline FXbool followUTF16(FXnchar c){
169  return (c&0xFC00)==0xDC00;
170  }
171 
173 static inline FXbool seqUTF16(FXnchar c){
174  return (c&0xF800)==0xD800;
175  }
176 
178 static inline FXival lenUTF16(FXnchar c){
179  return leadUTF16(c)+1;
180  }
181 
183 static inline FXival wclen(const FXnchar *ptr){
184  return lenUTF16(*ptr);
185  }
186 
188 static inline FXwchar wc(const FXnchar* ptr){
189  FXwchar w=ptr[0];
190  if(leadUTF16(w)){ w = (w<<10) + ptr[1] - 0x35FDC00; }
191  return w;
192  }
193 
195 static inline FXwchar wc1(const FXnchar* ptr){
196  return ptr[0];
197  }
198 
200 static inline FXwchar wc2(const FXnchar* ptr){
201  return (ptr[0]<<10)+ptr[1]-0x35FDC00;
202  }
203 
205 static inline FXwchar wcnxt(const FXnchar*& ptr){
206  FXwchar w=*ptr++;
207  if(leadUTF16(w)){ w = (w<<10) + *ptr++ - 0x35FDC00; }
208  return w;
209  }
210 
212 static inline FXwchar wcnxt(FXnchar*& ptr){
213  FXwchar w=*ptr++;
214  if(leadUTF16(w)){ w = (w<<10) + *ptr++ - 0x35FDC00; }
215  return w;
216  }
217 
219 static inline FXwchar wcprv(const FXnchar*& ptr){
220  FXwchar w=*--ptr;
221  if(followUTF16(w)){ w = (*--ptr<<10) + w - 0x35FDC00; }
222  return w;
223  }
224 
226 static inline FXwchar wcprv(FXnchar*& ptr){
227  FXwchar w=*--ptr;
228  if(followUTF16(w)){ w = (*--ptr<<10) + w - 0x35FDC00; }
229  return w;
230  }
231 
233 static inline const FXnchar* wcinc(const FXnchar* ptr){
234  return (isUTF16(*++ptr) || ++ptr), ptr;
235  }
236 
238 static inline FXnchar* wcinc(FXnchar* ptr){
239  return (isUTF16(*++ptr) || ++ptr), ptr;
240  }
241 
243 static inline const FXnchar* wcdec(const FXnchar* ptr){
244  return (isUTF16(*--ptr) || --ptr), ptr;
245  }
246 
248 static inline FXnchar* wcdec(FXnchar* ptr){
249  return (isUTF16(*--ptr) || --ptr), ptr;
250  }
251 
253 static inline const FXnchar* wcstart(const FXnchar *ptr){
254  return (isUTF16(*ptr) || --ptr), ptr;
255  }
256 
258 static inline FXnchar* wcstart(FXnchar *ptr){
259  return (isUTF16(*ptr) || --ptr), ptr;
260  }
261 
263 static inline FXbool isUTF32(FXwchar c){
264  return c<0x110000;
265  }
266 
267 /*********************** Measure Encoding Conversions **********************/
268 
270 static inline FXival wc2utf(FXwchar w){ return 1+(0x80<=w)+(0x800<=w)+(0x10000<=w); }
271 
273 static inline FXival wc2nc(FXwchar w){ return 1+(0x10000<=w); }
274 
276 extern FXAPI FXival wcs2utf(const FXwchar* src,FXival srclen);
277 extern FXAPI FXival wcs2utf(const FXwchar* src);
278 
280 extern FXAPI FXival ncs2utf(const FXnchar* src,FXival srclen);
281 extern FXAPI FXival ncs2utf(const FXnchar* src);
282 
284 extern FXAPI FXival utf2wcs(const FXchar src,FXival srclen);
285 extern FXAPI FXival utf2wcs(const FXchar *src);
286 
288 extern FXAPI FXival utf2ncs(const FXchar *src,FXival srclen);
289 extern FXAPI FXival utf2ncs(const FXchar *src);
290 
291 
292 /************************ Encoding Conversions ******************************/
293 
295 extern FXAPI FXival wc2utf(FXchar *dst,FXwchar w);
296 
298 extern FXAPI FXival wc2nc(FXnchar *dst,FXwchar w);
299 
301 extern FXAPI FXival wcs2utf(FXchar *dst,const FXwchar* src,FXival dstlen,FXival srclen);
302 extern FXAPI FXival wcs2utf(FXchar *dst,const FXwchar* src,FXival dstlen);
303 
305 extern FXAPI FXival ncs2utf(FXchar *dst,const FXnchar* src,FXival dstlen,FXival srclen);
306 extern FXAPI FXival ncs2utf(FXchar *dst,const FXnchar* src,FXival dstlen);
307 
309 extern FXAPI FXival utf2wcs(FXwchar *dst,const FXchar* src,FXival dstlen,FXival srclen);
310 extern FXAPI FXival utf2wcs(FXwchar *dst,const FXchar* src,FXival dstlen);
311 
313 extern FXAPI FXival utf2ncs(FXnchar *dst,const FXchar* src,FXival dstlen,FXival srclen);
314 extern FXAPI FXival utf2ncs(FXnchar *dst,const FXchar* src,FXival dstlen);
315 
316 }
317 
318 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp