File: fxchar.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (595 lines) | stat: -rw-r--r-- 15,293 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/********************************************************************************
*                                                                               *
*     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       *
*                                                                               *
*********************************************************************************
* Copyright (C) 1997,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#define __NEW_DEFS__ 1
#include "fxdefs.h"
#include "fxchar.h"
#include "fxmath.h"
#include "fxascii.h"
#include "fxunicode.h"


/*
  Notes:

  - UTF-8 Encoding scheme:

      Hex Range                 Binary                          Encoding
      -----------------------   -----------------------------   -----------------------------------
      U-00000000 - U-0000007F   0000 0000 0000 0000 0xxx xxxx   0xxxxxxx
      U-00000080 - U-000007FF   0000 0000 0000 0yyy yyxx xxxx   110yyyyy 10xxxxxx
      U-00000800 - U-0000FFFF   0000 0000 zzzz yyyy yyxx xxxx   1110zzzz 10yyyyyy 10xxxxxx
      U-00010000 - U-001FFFFF   000u uuzz zzzz yyyy yyxx xxxx   11110uuu 10zzzzzz 10yyyyyy 10xxxxxx

  - UTF-16 Encoding scheme:

      Hex Range                 Binary                          Encoding
      -----------------------   -----------------------------   -----------------------------------
      U-00000000 - U-0000FFFF   0000 0000 xxxx xxxx xxxx xxxx   xxxxxxxx xxxxxxxx
      U-00010000 - U-0010FFFF   000y yyyy yyyy yyxx xxxx xxxx   110110zz zzzzzzzz 110111xx xxxxxxxx

    The range U-D800 - U-DFFF is reserved for surrogate pairs; Leading-surrogates or high-surrogates
    are from U-D800 to U-DBFF, and trailing-surrogates or low-surrogates are from U-DC00 to U-DFFF.
    Surrogates CH and CL are computed as follows for U > 0x10000:

      CH = (U >> 10) + 0xD800
      CL = (U & 0x3FF) + 0xDC00

*/


using namespace FX;

/*******************************************************************************/

namespace FX {

// For conversion from UTF16 to UTF32
const FXint SURROGATE_OFFSET=0x10000-(0xD800<<10)-0xDC00;

// For conversion of UTF32 to UTF16
const FXint LEAD_OFFSET=0xD800-(0x10000>>10);

// For conversion of UTF32 to UTF16
const FXint TAIL_OFFSET=0xDC00;

/*******************************************************************************/

// Return non-zero if valid utf8 character sequence
FXival wcvalid(const FXchar* ptr){
  if((FXuchar)ptr[0]<0x80) return 1;
  if((FXuchar)ptr[0]<0xC0) return 0;
  if((FXuchar)ptr[1]<0x80) return 0;
  if((FXuchar)ptr[1]>0xBF) return 0;
  if((FXuchar)ptr[0]<0xE0) return 2;
  if((FXuchar)ptr[2]<0x80) return 0;
  if((FXuchar)ptr[2]>0xBF) return 0;
  if((FXuchar)ptr[0]<0xF0) return 3;
  if((FXuchar)ptr[0]>0xF7) return 0;
  if((FXuchar)ptr[3]<0x80) return 0;
  if((FXuchar)ptr[3]>0xBF) return 0;
  return 4;
  }


// Return non-zero if valid utf16 character sequence
FXival wcvalid(const FXnchar* ptr){
  if(ptr[0]<0xD800) return 1;
  if(ptr[0]>0xDFFF) return 1;
  if(ptr[0]>0xDBFF) return 0;
  if(ptr[1]<0xDC00) return 0;
  if(ptr[1]>0xDFFF) return 0;
  return 2;
  }

/*******************************************************************************/

// Return number of bytes for utf8 representation of wide character string
FXival wcs2utf(const FXwchar* src,FXival srclen){
  const FXwchar* srcend=src+srclen;
  FXival p=0;
  FXwchar w;
  while(src<srcend){
    w=*src++;
    p+=wc2utf(w);
    }
  return p;
  }


// Return number of bytes for utf8 representation of wide character string
FXival wcs2utf(const FXwchar *src){
  FXival p=0;
  FXwchar w;
  while((w=*src++)!=0){
    p+=wc2utf(w);
    }
  return p;
  }


// Return number of bytes for utf8 representation of narrow character string
FXival ncs2utf(const FXnchar *src,FXival srclen){
  const FXnchar* srcend=src+srclen;
  FXival p=0;
  FXnchar w;
  while(src<srcend){
    w=*src++;
    if(leadUTF16(w)){
      if(src>=srcend) break;
      if(!followUTF16(*src)) break;
      w=SURROGATE_OFFSET+(w<<10)+*src++;
      }
    p+=wc2utf(w);
    }
  return p;
  }


// Return number of bytes for utf8 representation of narrow character string
FXival ncs2utf(const FXnchar *src){
  FXival p=0;
  FXnchar w;
  while((w=*src++)!=0){
    if(leadUTF16(w)){
      if(!followUTF16(*src)) break;
      w=SURROGATE_OFFSET+(w<<10)+*src++;
      }
    p+=wc2utf(w);
    }
  return p;
  }


// Return number of wide characters for utf8 character string
FXival utf2wcs(const FXchar *src,FXival srclen){
  const FXchar* srcend=src+srclen;
  FXival p=0;
  FXuchar c;
  while(src<srcend){
    c=src[0];
    if(0xC0<=c){
      if(src+1>=srcend) break;
      if(!followUTF8(src[1])) break;
      if(0xE0<=c){
        if(src+2>=srcend) break;
        if(!followUTF8(src[2])) break;
        if(0xF0<=c){
          if(src+3>=srcend) break;
          if(!followUTF8(src[3])) break;
          src++;
          }
        src++;
        }
      src++;
      }
    src++;
    p++;
    }
  return p;
  }


// Return number of wide characters for utf8 character string
FXival utf2wcs(const FXchar *src){
  FXival p=0;
  FXuchar c;
  while((c=src[0])!=0){
    if(0xC0<=c){
      if(!followUTF8(src[1])) break;
      if(0xE0<=c){
        if(!followUTF8(src[2])) break;
        if(0xF0<=c){
          if(!followUTF8(src[3])) break;
          src++;
          }
        src++;
        }
      src++;
      }
    src++;
    p++;
    }
  return p;
  }


// Return number of narrow characters for utf8 character string
FXival utf2ncs(const FXchar *src,FXival len){
  const FXchar* end=src+len;
  FXival p=0;
  FXuchar c;
  while(src<end){
    c=src[0];
    if(0xC0<=c){
      if(src+1>=end) break;
      if(!followUTF8(src[1])) break;
      if(0xE0<=c){
        if(src+2>=end) break;
        if(!followUTF8(src[2])) break;
        if(0xF0<=c){
          if(src+3>=end) break;
          if(!followUTF8(src[3])) break;
          src++;
          p++;
          }
        src++;
        }
      src++;
      }
    src++;
    p++;
    }
  return p;
  }


// Return number of narrow characters for utf8 character string
FXival utf2ncs(const FXchar *src){
  FXival p=0;
  FXuchar c;
  while((c=src[0])!=0){
    if(0xC0<=c){
      if(!followUTF8(src[1])) break;
      if(0xE0<=c){
        if(!followUTF8(src[2])) break;
        if(0xF0<=c){
          if(!followUTF8(src[3])) break;
          src++;
          p++;
          }
        src++;
        }
      src++;
      }
    src++;
    p++;
    }
  return p;
  }

/*******************************************************************************/

// Convert wide character to utf8 string
FXival wc2utf(FXchar *dst,FXwchar w){
  if(0x80<=w){
    if(0x800<=w){
      if(0x10000<=w){
        dst[0]=(w>>18)|0xF0;
        dst[1]=((w>>12)&0x3F)|0x80;
        dst[2]=((w>>6)&0x3F)|0x80;
        dst[3]=(w&0x3F)|0x80;
        return 4;
        }
      dst[0]=(w>>12)|0xE0;
      dst[1]=((w>>6)&0x3F)|0x80;
      dst[2]=(w&0x3F)|0x80;
      return 3;
      }
    dst[0]=(w>>6)|0xC0;
    dst[1]=(w&0x3F)|0x80;
    return 2;
    }
  dst[0]=w;
  return 1;
  }


// Convert wide character to narrow character string
FXival wc2nc(FXnchar *dst,FXwchar w){
  if(0x10000<=w){
    dst[0]=LEAD_OFFSET+(w>>10);
    dst[1]=TAIL_OFFSET+(w&0x3FF);
    return 2;
    }
  dst[0]=w;
  return 1;
  }


// Convert wide character string to utf8 string
FXival wcs2utf(FXchar *dst,const FXwchar* src,FXival dstlen,FXival srclen){
  const FXwchar* srcend=src+srclen;
  FXchar* ptrend=dst+dstlen;
  FXchar* ptr=dst;
  FXwchar w;
  while(src<srcend){
    w=*src++;
    if(0x80<=w){
      if(0x800<=w){
        if(0x10000<=w){
          if(ptr+3>=ptrend) break;
          *ptr++=(w>>18)|0xF0;
          *ptr++=((w>>12)&0x3F)|0x80;
          *ptr++=((w>>6)&0x3F)|0x80;
          *ptr++=(w&0x3F)|0x80;
          continue;
          }
        if(ptr+2>=ptrend) break;
        *ptr++=(w>>12)|0xE0;
        *ptr++=((w>>6)&0x3F)|0x80;
        *ptr++=(w&0x3F)|0x80;
        continue;
        }
      if(ptr+1>=ptrend) break;
      *ptr++=(w>>6)|0xC0;
      *ptr++=(w&0x3F)|0x80;
      continue;
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr='\0';
  return ptr-dst;
  }


// Convert wide character string to utf8 string
FXival wcs2utf(FXchar *dst,const FXwchar* src,FXival dstlen){
  FXchar* ptrend=dst+dstlen;
  FXchar* ptr=dst;
  FXwchar w;
  while((w=*src++)!=0){
    if(0x80<=w){
      if(0x800<=w){
        if(0x10000<=w){
          if(ptr+3>=ptrend) break;
          *ptr++=(w>>18)|0xF0;
          *ptr++=((w>>12)&0x3F)|0x80;
          *ptr++=((w>>6)&0x3F)|0x80;
          *ptr++=(w&0x3F)|0x80;
          continue;
          }
        if(ptr+2>=ptrend) break;
        *ptr++=(w>>12)|0xE0;
        *ptr++=((w>>6)&0x3F)|0x80;
        *ptr++=(w&0x3F)|0x80;
        continue;
        }
      if(ptr+1>=ptrend) break;
      *ptr++=(w>>6)|0xC0;
      *ptr++=(w&0x3F)|0x80;
      continue;
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr='\0';
  return ptr-dst;
  }


// Convert narrow character string to utf8 string
FXival ncs2utf(FXchar *dst,const FXnchar* src,FXival dstlen,FXival srclen){
  const FXnchar* srcend=src+srclen;
  FXchar* ptrend=dst+dstlen;
  FXchar* ptr=dst;
  FXwchar w;
  while(src<srcend){
    w=*src++;
    if(0x80<=w){
      if(0x800<=w){
        if(leadUTF16(w)){
          if(src>=srcend) break;
          if(!followUTF16(*src)) break;
          if(ptr+3>=ptrend) break;
          w=SURROGATE_OFFSET+(w<<10)+*src++;
          *ptr++=(w>>18)|0xF0;
          *ptr++=((w>>12)&0x3F)|0x80;
          *ptr++=((w>>6)&0x3F)|0x80;
          *ptr++=(w&0x3F)|0x80;
          continue;
          }
        if(ptr+2>=ptrend) break;
        *ptr++=(w>>12)|0xE0;
        *ptr++=((w>>6)&0x3F)|0x80;
        *ptr++=(w&0x3F)|0x80;
        continue;
        }
      if(ptr+1>=ptrend) break;
      *ptr++=(w>>6)|0xC0;
      *ptr++=(w&0x3F)|0x80;
      continue;
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr='\0';
  return ptr-dst;
  }


// Convert narrow character string to utf8 string
FXival ncs2utf(FXchar *dst,const FXnchar* src,FXival dstlen){
  FXchar* ptrend=dst+dstlen;
  FXchar* ptr=dst;
  FXwchar w;
  while((w=*src++)!=0){
    if(0x80<=w){
      if(0x800<=w){
        if(leadUTF16(w)){
          if(!followUTF16(*src)) break;
          if(ptr+3>=ptrend) break;
          w=SURROGATE_OFFSET+(w<<10)+*src++;
          *ptr++=(w>>18)|0xF0;
          *ptr++=((w>>12)&0x3F)|0x80;
          *ptr++=((w>>6)&0x3F)|0x80;
          *ptr++=(w&0x3F)|0x80;
          continue;
          }
        if(ptr+2>=ptrend) break;
        *ptr++=(w>>12)|0xE0;
        *ptr++=((w>>6)&0x3F)|0x80;
        *ptr++=(w&0x3F)|0x80;
        continue;
        }
      if(ptr+1>=ptrend) break;
      *ptr++=(w>>6)|0xC0;
      *ptr++=(w&0x3F)|0x80;
      continue;
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr='\0';
  return ptr-dst;
  }


// Convert utf8 string to wide character string
FXival utf2wcs(FXwchar *dst,const FXchar* src,FXival dstlen,FXival srclen){
  const FXchar* srcend=src+srclen;
  FXwchar* ptrend=dst+dstlen;
  FXwchar* ptr=dst;
  FXwchar w;
  FXuchar c;
  while(src<srcend){
    w=c=*src++;
    if(0xC0<=w){
      if(src>=srcend) break;
      c=*src++;
      if(!followUTF8(c)) break;
      w=(w<<6) ^ c ^ 0x3080;
      if(0x800<=w){
        if(src>=srcend) break;
        c=*src++;
        if(!followUTF8(c)) break;
        w=(w<<6) ^ c ^ 0x20080;
        if(0x10000<=w){
          if(src>=srcend) break;
          c=*src++;
          if(!followUTF8(c)) break;
          w=(w<<6) ^ c ^ 0x400080;
          }
        }
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr=0;
  return ptr-dst;
  }


// Convert utf8 string to wide character string
FXival utf2wcs(FXwchar *dst,const FXchar* src,FXival dstlen){
  FXwchar* ptrend=dst+dstlen;
  FXwchar* ptr=dst;
  FXwchar w;
  FXuchar c;
  while((w=c=*src++)!=0){
    if(0xC0<=c){
      c=*src++;
      if(!followUTF8(c)) break;
      w=(w<<6) ^ c ^ 0x3080;
      if(0x800<=w){
        c=*src++;
        if(!followUTF8(c)) break;
        w=(w<<6) ^ c ^ 0x20080;
        if(0x10000<=w){
          c=*src++;
          if(!followUTF8(c)) break;
          w=(w<<6) ^ c ^ 0x400080;
          }
        }
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr=0;
  return ptr-dst;
  }


// Convert utf8 string to narrow character string
FXival utf2ncs(FXnchar *dst,const FXchar* src,FXival dstlen,FXival srclen){
  const FXchar* srcend=src+srclen;
  FXnchar* ptrend=dst+dstlen;
  FXnchar* ptr=dst;
  FXwchar w;
  FXuchar c;
  while(src<srcend){
    w=c=*src++;
    if(0xC0<=w){
      if(src>=srcend) break;
      c=*src++;
      if(!followUTF8(c)) break;
      w=(w<<6) ^ c ^ 0x3080;
      if(0x800<=w){
        if(src>=srcend) break;
        c=*src++;
        if(!followUTF8(c)) break;
        w=(w<<6) ^ c ^ 0x20080;
        if(0x10000<=w){
          if(src>=srcend) break;
          c=*src++;
          if(!followUTF8(c)) break;
          if(ptr+1>=ptrend) break;
          w=(w<<6) ^ c ^ 0x400080;
          *ptr++=LEAD_OFFSET+(w>>10);
          *ptr++=TAIL_OFFSET+(w&0x3FF);
          continue;
          }
        }
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr=0;
  return ptr-dst;
  }


// Convert utf8 string to narrow character string
FXival utf2ncs(FXnchar *dst,const FXchar* src,FXival dstlen){
  FXnchar* ptrend=dst+dstlen;
  FXnchar* ptr=dst;
  FXwchar w;
  FXuchar c;
  while((w=c=*src++)!=0){
    if(0xC0<=w){
      c=*src++;
      if(!followUTF8(c)) break;
      w=(w<<6) ^ c ^ 0x3080;
      if(0x800<=w){
        c=*src++;
        if(!followUTF8(c)) break;
        w=(w<<6) ^ c ^ 0x20080;
        if(0x10000<=w){
          c=*src++;
          if(!followUTF8(c)) break;
          if(ptr+1>=ptrend) break;
          w=(w<<6) ^ c ^ 0x400080;
          *ptr++=LEAD_OFFSET+(w>>10);
          *ptr++=TAIL_OFFSET+(w&0x3FF);
          continue;
          }
        }
      }
    if(ptr>=ptrend) break;
    *ptr++=w;
    }
  if(ptr<ptrend) *ptr=0;
  return ptr-dst;
  }

}