forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_unicode.cpp
18128 lines (17628 loc) · 493 KB
/
opennurbs_unicode.cpp
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
int ON_IsValidUnicodeCodePoint(ON__UINT32 u)
{
if (u < 0xD800)
return true;
if (u < 0xE000)
{
// 0xD800 <= u < 0xDC00 is used for the first value in a UTF-16 surrogate pair.
// 0xDC00 <= u < 0xE000 is used for the second value in a UTF-16 surrogate pair.
return false;
}
if (u < 0xFFFE)
return true;
if (u == 0xFFFE || u == 0xFFFF)
{
// The Unicode specification states these values are <not a character>.
// 0xFFFE is a swapped Unicode byte order mark (U+FEFF).
// https://www.unicode.org/charts/PDF/UFFF0.pdf
return false;
}
if (u < 0xFFFFE)
return true;
if (u == 0xFFFFE || u == 0xFFFFF)
{
// The Unicode specification states these values are <not a character>.
// https://www.unicode.org/charts/PDF/UFFF80.pdf
return false;
}
if (u < 0x10FFFE)
return true;
if (u == 0x10FFFE || u == 0x10FFFF)
{
// The Unicode specification states these values are <not a character>.
// https://www.unicode.org/charts/PDF/U10FF80.pdf
return false;
}
// All valid Unicode code points are < 0x110000
return false;
}
bool ON_IsPrivateUseUnicodeCodePoint(ON__UINT32 unicode_code_point)
{
// Unicode code points in the ranges U+E000–U+F8FF, U+F0000–U+FFFFD,
// and U+100000–U+10FFFD are for private use (user defined characters).
if (unicode_code_point < 0xE000U)
return false;
if (unicode_code_point <= 0xF8FFU)
return true; // Private use area E000-F8FF
if (unicode_code_point < 0xF0000U)
return false;
if (unicode_code_point <= 0xFFFFDU)
return true; // Supplementary Private Use Area-A F0000-FFFFD
// FFFFE and FFFFF are <not a character>
if (unicode_code_point < 0x100000U)
return false;
if (unicode_code_point <= 0x10FFFDU)
return true; // Supplementary Private Use Area-B 100000-10FFFD
// 10FFFE and 10FFFF are <not a character>
return false;
}
bool ON_IsStandardUnicodeCodePoint(ON__UINT32 unicode_code_point)
{
return
0 != ON_IsValidUnicodeCodePoint(unicode_code_point)
&& ON_UnicodeCodePoint::ON_ByteOrderMark != unicode_code_point
&& false == ON_IsPrivateUseUnicodeCodePoint(unicode_code_point)
;
}
int ON_IsUnicodeSpaceCodePoint(
ON__UINT32 u
)
{
// Additional code points may be added in the future.
// The goal is to detect code points that typically separate words
// and which should not be at the beginning or end of a word.
return
ON_UnicodeCodePoint::ON_Space == u
|| ON_UnicodeCodePoint::ON_NoBreakSpace == u
|| ON_UnicodeCodePoint::ON_NarrowNoBreakSpace == u
|| (u >= ON_UnicodeCodePoint::ON_EnQuad && u <= ON_UnicodeCodePoint::ON_ZeroWidthSpace)
;
}
int ON_IsUnicodeSpaceOrControlCodePoint(
ON__UINT32 u
)
{
// Additional space code points may be added in the future.
// The goal is to detect code points that should be trimmed
// when isolating text tokens in a string.
if (u > 0 && u <= ON_UnicodeCodePoint::ON_Space)
return true;
if (u < ON_UnicodeCodePoint::ON_Delete)
return false;
if (u == ON_UnicodeCodePoint::ON_Delete)
return true;
if (ON_IsUnicodeSpaceCodePoint(u))
return true;
if (ON_IsUnicodeC1ControlCodePoint(u))
return true;
if (u < 0x2000)
return false;
// Bidirectional text control
if (u >= 0x200E && u <= 0x200F)
return true;
// Separators, Format characters, and NNBSP
if (u >= 0x2028 && u <= 0x202F)
return true;
// Bidirectional text control
if (u >= 0x202A && u <= 0x202E)
return true;
// Bidirectional text control
if (u >= 0x2066 && u <= 0x2069)
return true;
return false;
}
int ON_IsUnicodeC1ControlCodePoint(
ON__UINT32 u
)
{
return (u >= 0x0080 && u <= 0x009F);
}
int ON_IsValidUTF32Value(
ON__UINT32 c
)
{
return (c < 0xD800 || (c >= 0xE000 && c <= 0x10FFFF));
}
int ON_IsValidSingleElementUTF16Value(ON__UINT32 c)
{
return ((c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFF));
}
int ON_IsValidUTF16Singleton(ON__UINT32 c)
{
return ((c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFF));
}
enum ON_UnicodeEncoding ON_UnicodeNativeCPU_UTF16()
{
return (ON::endian::little_endian== ON::Endian()) ? ON_UTF_16LE : ON_UTF_16BE;
}
enum ON_UnicodeEncoding ON_UnicodeNativeCPU_UTF32()
{
return (ON::endian::little_endian== ON::Endian()) ? ON_UTF_32LE : ON_UTF_32BE;
}
int ON_IsValidSingleByteUTF8CharValue(
char c
)
{
return (c >= 0 && c <= 0x7F);
}
int ON_IsValidUTF8SingletonChar(
char c
)
{
return (c >= 0 && c <= 0x7F);
}
int ON_IsValidSingleElementUTF8Value(
ON__UINT32 c
)
{
return (c <= 0x7F);
}
int ON_IsValidUTF8Singleton(
ON__UINT32 c
)
{
return (c <= 0x7FU);
}
int ON_IsValidUTF16SurrogatePair(
unsigned int w1,
unsigned int w2
)
{
return ( w1 >= 0xD800U && w1 < 0xDC00 && w2 >= 0xDC00 && w2 < 0xE000 );
}
unsigned int ON_DecodeUTF16SurrogatePair(
unsigned int u1,
unsigned int u2,
unsigned int error_code_point
)
{
if (u1 >= 0xD800U && u1 < 0xDC00 && u2 >= 0xDC00 && u2 < 0xE000)
{
return ((u1-0xD800)*0x400 + (u2-0xDC00) + 0x10000);
}
return error_code_point;
}
int ON_IsValidSingleElementWideCharValue(
wchar_t w
)
{
#pragma ON_PRAGMA_WARNING_PUSH
// warning C4127: conditional expression is constant
#pragma ON_PRAGMA_WARNING_DISABLE_MSC( 4127 )
if (1 == sizeof(w))
return ON_IsValidSingleElementUTF8Value((ON__UINT32)w);
if (2 == sizeof(w))
return ON_IsValidSingleElementUTF16Value((ON__UINT32)w);
return ON_IsValidUTF32Value((ON__UINT32)w);
#pragma ON_PRAGMA_WARNING_POP
}
enum ON_UnicodeEncoding ON_IsUTFByteOrderMark(
const void* buffer,
size_t sizeof_buffer
)
{
if ( 0 != buffer && sizeof_buffer >= 2 )
{
const unsigned char* b = static_cast<const unsigned char*>(buffer);
if ( 0 == b[0] )
{
if ( sizeof_buffer >= 4 && 0 == b[1] && 0xFE == b[2] && 0xFF == b[3] )
return ON_UTF_32BE;
}
else if ( 0xEF == b[0] )
{
if ( sizeof_buffer >= 3 && 0xBB == b[1] && 0xBF == b[2] )
return ON_UTF_8;
}
else if ( 0xFE == b[0] )
{
if ( 0xFF == b[1] )
return ON_UTF_16BE;
}
else if ( 0xFF == b[0] && 0xFE == b[1] )
{
return ( sizeof_buffer >= 4 && 0 == b[2] && 0 == b[3] )
? ON_UTF_32LE
: ON_UTF_16LE;
}
}
return ON_UTF_unset;
}
unsigned int ON_UTFSizeofByteOrderMark(
ON_UnicodeEncoding e
)
{
unsigned int sizeof_bom;
switch (e)
{
case ON_UTF_8:
sizeof_bom = 3;
break;
case ON_UTF_16:
case ON_UTF_16BE:
case ON_UTF_16LE:
sizeof_bom = 2;
break;
case ON_UTF_32:
case ON_UTF_32BE:
case ON_UTF_32LE:
sizeof_bom = 4;
break;
default:
sizeof_bom = 0;
break;
}
return sizeof_bom;
}
static int ON_IsUTF8ByteOrderMark(
const char* sUTF8,
int sUTF8_count
)
{
if ( 0 == sUTF8 )
return 0;
if (sUTF8_count < 3 )
return 0;
return (0xEF == (unsigned char)(sUTF8[0]) && 0xBB == (unsigned char)(sUTF8[1]) && 0xBF == (unsigned char)(sUTF8[2]));
}
bool ON_IsUnicodeControlCodePoint(
ON__UINT32 code_point,
bool bNullReturnValue
)
{
if (0 == code_point)
return bNullReturnValue ? true : false;
if (code_point < 0x0020)
return true; // below space
if (code_point < 0x007f)
return false;
if (code_point <= 0x00A0)
return true; // del to 0xA0
if (code_point < 0x00AD)
return false;
if (code_point == 0x00AD)
return true; // soft hyphen
return false;
}
int ON_EncodeUTF8( ON__UINT32 u, char sUTF8[6] )
{
ON__UINT32 c;
if ( u <= 0x7F )
{
// 1 byte UTF8 encoding: 0xxxxxxx (7 bits of u)
sUTF8[0] = (char)u;
return 1;
}
if ( u <= 0x7FF )
{
// 2 byte UTF8 encoding: 110xxxxx, 10xxxxxx (11 bits of u)
c = (u / 0x40); // c = 000xxxxx
c |= 0xC0; // |= 11000000
sUTF8[0] = (char)c;
c = (u & 0x3F);
c |= 0x80;
sUTF8[1] = (char)c;
return 2;
}
if ( u <= 0xFFFF )
{
// 3 byte UTF8 encoding: 1110xxxx, 10xxxxxx, 10xxxxxx (16 bits of u)
c = (u / 0x1000); // c = 0000xxxx
c |= 0xE0; // |= 11100000
sUTF8[0] = (char)c;
c = ((u & 0xFFF) / 0x40);
c |= 0x80;
sUTF8[1] = (char)c;
c = u & 0x3F;
c |= 0x80;
sUTF8[2] = (char)c;
return 3;
}
if ( u <= 0x1FFFFF )
{
// (maximum valid unicode codepoint is 0x10FFFF)
// 4 byte UTF8 encoding: 11110xxx, 10xxxxxx, 10xxxxxx, 10xxxxxx (21 bits of u)
// Note: 0x10FFFF is the maximum valid unicode code point.
// For u > 0x10FFFF and u <= 0x1FFFFF, this calculation encodes the low 21 bits of u.
c = (u / 0x40000); // c = 00000xxx
c |= 0xF0; // |= 11110000
sUTF8[0] = (char)c;
c = ((u & 0x3FFFF)/0x1000);
c |= 0x80;
sUTF8[1] = (char)c;
c = ((u & 0xFFF) / 0x40);
c |= 0x80;
sUTF8[2] = (char)c;
c = u & 0x3F;
c |= 0x80;
sUTF8[3] = (char)c;
return 4;
}
if ( u <= 0x3FFFFFF )
{
// 5 byte encoding: 111110xx, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx (26 bits of u)
// Note: 0x10FFFF is the maximum valid unicode code point.
c = (u / 0x1000000); // c = 000000xx
c |= 0xF8; // |= 11111000
sUTF8[0] = (char)c;
c = ((u & 0xFFFFFF)/0x40000);
c |= 0x80;
sUTF8[1] = (char)c;
c = ((u & 0x3FFFF)/0x1000);
c |= 0x80;
sUTF8[2] = (char)c;
c = ((u & 0xFFF) / 0x40);
c |= 0x80;
sUTF8[3] = (char)c;
c = u & 0x3F;
c |= 0x80;
sUTF8[4] = (char)c;
return 5;
}
if ( u <= 0x7FFFFFFF )
{
// 6 byte encoding: 1111110x, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx (31 bits of u)
// Note: 0x10FFFF is the maximum valid unicode code point.
c = (u / 0x40000000); // c = 00000000x
c |= 0xFC; // |= 11111100
sUTF8[0] = (char)c;
c = ((u & 0x3FFFFFFF)/0x1000000);
c |= 0x80;
sUTF8[1] = (char)c;
c = ((u & 0xFFFFFF)/0x40000);
c |= 0x80;
sUTF8[2] = (char)c;
c = ((u & 0x3FFFF)/0x1000);
c |= 0x80;
sUTF8[3] = (char)c;
c = ((u & 0xFFF) / 0x40);
c |= 0x80;
sUTF8[4] = (char)c;
c = u & 0x3F;
c |= 0x80;
sUTF8[5] = (char)c;
return 6;
}
return 0;
}
static int ON_DecodeUTF8Helper(
const char* sUTF8,
int sUTF8_count,
ON__UINT32* value,
unsigned int* error_status
)
{
#define INPUT_BUFFER_TOO_SHORT 16
#define INVALID_CONTINUATION_VALUE 16
#define OVERLONG_ENCODING 8
ON__UINT32 u;
char c;
c = sUTF8[0];
if ( 0 == (0x80 & c) )
{
// 1 byte ASCII encoding: 0xxxxxxx
*value = c;
return 1;
}
if ( 0xC0 == ( 0xE0 & c) )
{
// 2 byte character encoding: 10xxxxxx, 10xxxxxx
if ( sUTF8_count < 2 )
{
*error_status |= INPUT_BUFFER_TOO_SHORT; // input buffer too short
return 0;
}
u = (0x1F & c);
c = sUTF8[1];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
if ( u <= 0x7F )
{
*error_status |= OVERLONG_ENCODING; // overlong 2 byte character encoding
}
*value = u;
return 2;
}
if ( 0xE0 == ( 0xF0 & c) )
{
// 3 byte character encoding: 110xxxxx, 10xxxxxx, 10xxxxxx
if ( sUTF8_count < 3 )
{
*error_status |= INPUT_BUFFER_TOO_SHORT; // input buffer too short
return 0;
}
u = (0x0F & c);
c = sUTF8[1];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
if ( u <= 0x7FF )
{
*error_status |= OVERLONG_ENCODING; // overlong 3 byte character encoding
}
*value = u;
return 3;
}
if ( 0xF0 == ( 0xF8 & c) )
{
// 4 byte character encoding: 11110xxx, 10xxxxxx, 10xxxxxx, 10xxxxxx
if ( sUTF8_count < 4 )
{
*error_status |= INPUT_BUFFER_TOO_SHORT; // input buffer too short
return 0;
}
u = (0x07 & c);
c = sUTF8[1];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[3];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
if ( u <= 0xFFFF )
{
*error_status |= OVERLONG_ENCODING; // overlong 4 byte character encoding
}
*value = u;
return 4;
}
if ( 0xF8 == ( 0xFC & c) )
{
// 5 byte character encoding: 111110xx, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx
if ( sUTF8_count < 5 )
{
*error_status |= INPUT_BUFFER_TOO_SHORT; // input buffer too short
return 0;
}
u = (0x03 & c);
c = sUTF8[1];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[3];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[4];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
if ( u <= 0x1FFFFF )
{
*error_status |= OVERLONG_ENCODING; // overlong 5 byte character encoding
}
*value = u;
return 5;
}
if ( 0xFC == ( 0xFE & c) )
{
// 6 byte character encoding: 110xxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx, 10xxxxxx
if ( sUTF8_count < 6 )
{
*error_status |= INPUT_BUFFER_TOO_SHORT; // input buffer too short
return 0;
}
u = (0x01 & c);
c = sUTF8[1];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[3];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[4];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
c = sUTF8[5];
if ( 0x80 != ( 0xC0 & c) )
{
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 continuation value
return 0;
}
u *= 64;
u |= (0x3F & c);
if ( u <= 0x3FFFFFF )
{
*error_status |= OVERLONG_ENCODING; // overlong 6 byte character encoding
}
*value = u;
return 6;
}
*error_status |= INVALID_CONTINUATION_VALUE; // invalid UTF=8 start value
return 0;
#undef INPUT_BUFFER_TOO_SHORT
#undef INVALID_CONTINUATION_VALUE
#undef OVERLONG_ENCODING
}
int ON_DecodeUTF8(
const char* sUTF8,
int sUTF8_count,
struct ON_UnicodeErrorParameters* e,
ON__UINT32* unicode_code_point
)
{
ON__UINT32 u0, u1;
int i0, i1;
unsigned int error_status;
ON__UINT16 sUTF16[2];
char c;
ON_UnicodeErrorParameters local_e = ON_UnicodeErrorParameters::MaskErrors;
if (nullptr == e)
e = &local_e;
if ( 0 == sUTF8 || sUTF8_count <= 0 || 0 == unicode_code_point )
{
if ( e )
e->m_error_status |= 1;
return 0;
}
// special cases for most common unicode values
// If any error conditions exist, then ON_DecodeUTF8Helper()
// is used.
if ( 0 == (0x80 & sUTF8[0]) )
{
*unicode_code_point = sUTF8[0];
return 1;
}
c = sUTF8[0];
if ( 0xC0 == ( 0xE0 & c) && sUTF8_count >= 2 )
{
// 2 byte character encoding: 10xxxxxx, 10xxxxxx
u0 = (0x1F & c);
c = sUTF8[1];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
if ( u0 > 0x7F )
{
*unicode_code_point = u0;
return 2;
}
}
}
else if ( 0xE0 == ( 0xF0 & c) && sUTF8_count >= 3 )
{
// 3 byte character encoding: 110xxxxx, 10xxxxxx, 10xxxxxx
u0 = (0x0F & c);
c = sUTF8[1];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
if ( u0 >= 0x0800 && (u0 <= 0xD800 || u0 >= 0xE000) )
{
*unicode_code_point = u0;
return 3;
}
}
}
}
else if ( 0xF0 == ( 0xF8 & c) && sUTF8_count >= 4 )
{
// 4 byte character encoding: 11110xxx, 10xxxxxx, 10xxxxxx, 10xxxxxx
u0 = (0x07 & c);
c = sUTF8[1];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
c = sUTF8[2];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
c = sUTF8[3];
if ( 0x80 == ( 0xC0 & c) )
{
u0 *= 64;
u0 |= (0x3F & c);
if ( u0 >= 0x010000 && u0 <= 0x10FFFF )
{
*unicode_code_point = u0;
return 4;
}
}
}
}
}
error_status = 0;
u0 = 0xFFFFFFFF;
i0 = ON_DecodeUTF8Helper(sUTF8,sUTF8_count,&u0,&error_status);
if ( i0 > 0 && 0 == error_status && (u0 < 0xD800 || (u0 >= 0xE000 && u0 <= 0x10FFFF) ) )
{
// valid UTF-8 multibyte encoding parsed
*unicode_code_point = u0;
return i0;
}
// handle errors
if ( 0 == e )
{
// no errors are masked.
return 0;
}
// report error condition
e->m_error_status |= error_status;
if ( error_status != (error_status & e->m_error_mask) )
{
// this error is not masked
return 0;
}
if ( i0 <= 0 )
{
i0 = 1;
if ( ON_IsValidUnicodeCodePoint(e->m_error_code_point) )
{
// skip to next UTF-8 start element
for ( /*empty for initializer*/; i0 < sUTF8_count; i0++ )
{
// Search for the next element of sUTF8[] that is the
// start of a UTF-8 encoding sequence.
c = sUTF8[i0];
if ( 0 == (0x80 & c) // ASCII 0 - 127
|| 0xC0 == ( 0xE0 & c) // 2 byte encoding first character
|| 0xE0 == ( 0xF0 & c) // 3 byte encoding first character
|| 0xF0 == ( 0xF8 & c) // 4 byte encoding first character
|| 0xF8 == ( 0xFC & c) // 5 byte encoding first character
|| 0xFC == ( 0xFE & c) // 6 byte encoding first character
)
{
// resume parsing at this character
break;
}
}
*unicode_code_point = e->m_error_code_point;
}
return i0;
}
if ( ON_IsValidUnicodeCodePoint(u0) && 8 == error_status )
{
// overlong UTF-8 multibyte encoding of valid unicode code point
*unicode_code_point = u0;
return i0;
}
if ( i0 < sUTF8_count
&& u0 >= 0xD800 && u0 <= 0xDBFF
&& (0 == error_status || 8 == error_status)
&& 0 != (4 & e->m_error_mask)
)
{
// See if a UFT-16 surrogate pair was incorrectly encoded
// as two consecutive UTF-8 sequences.
u1 = 0xFFFFFFFF;
i1 = ON_DecodeUTF8Helper(sUTF8+i0,sUTF8_count-i0,&u1,&error_status);
if ( i1 > 0 && (0 == error_status || 8 == error_status) )
{
error_status = 0;
sUTF16[0] = (ON__UINT16)u0;
sUTF16[1] = (ON__UINT16)u1;
u0 = 0xFFFFFFFF;
if ( 2 == ON_ConvertUTF16ToUTF32(false,sUTF16,2,&u0,1,&error_status,0,0,0)
&& 0 == error_status
&& ON_IsValidUnicodeCodePoint(u0)
)
{
*unicode_code_point = u0;
e->m_error_status |= 4;
return i0+i1;
}
}
}
if ( ON_IsValidUnicodeCodePoint(e->m_error_code_point) )
{
*unicode_code_point = e->m_error_code_point;
return i0;
}
return 0;
}
int ON_EncodeUTF16( ON__UINT32 unicode_code_point, ON__UINT16 sUTF16[2] )
{
// put the most common case first
if ( unicode_code_point < 0xD800 )
{
// code point values U+0000 ... U+D7FF
// = UTF-16 values
sUTF16[0] = (ON__UINT16)unicode_code_point;
return 1;
}
if ( unicode_code_point < 0xE000 )
{
// 0xD800 ... 0xDFFF are invalid unicode code point values
return 0;
}
if ( unicode_code_point <= 0xFFFF )
{
// code point values U+E000 ... U+FFFF
// = UTF-16 values
sUTF16[0] = (ON__UINT16)unicode_code_point;
return 1;
}
if ( unicode_code_point <= 0x10FFFF )
{
// code point values U+10000 ... U+10FFFF
// = surrogate pair UTF-16 values
unicode_code_point -= 0x10000;
sUTF16[0] = (ON__UINT16)(0xD800 + (unicode_code_point / 0x400)); // high surrogate value (0xD800 ... 0xDBFF)
sUTF16[1] = (ON__UINT16)(0xDC00 + (unicode_code_point & 0x3FF)); // low surrogate value (0xDC00 ... 0xDFFF)
return 2;
}
// 0x110000 ... 0xFFFFFFFF are invalid unicode code point values
return 0;
}
int ON_DecodeUTF16(
const ON__UINT16* sUTF16,
int sUTF16_count,
struct ON_UnicodeErrorParameters* e,
ON__UINT32* unicode_code_point
)
{
ON__UINT32 uhi, ulo;
ON_UnicodeErrorParameters local_e = ON_UnicodeErrorParameters::MaskErrors;
if (nullptr == e)
e = &local_e;
if ( 0 == sUTF16 || sUTF16_count <= 0 || 0 == unicode_code_point )
{
if ( e )
e->m_error_status |= 1;
return 0;
}
// special case for most common UTF-16 single element values
if ( ( sUTF16[0] < 0xD800 ) || ( sUTF16[0] >= 0xE000 ) )
{
*unicode_code_point = sUTF16[0];
return 1;
}
if ( sUTF16_count >= 2 && sUTF16[0] < 0xDC00 && sUTF16[1] >= 0xDC00 && sUTF16[1] < 0xE000 )
{
// UTF-16 surrogate pair
uhi = sUTF16[0];
ulo = sUTF16[1];
*unicode_code_point = (uhi-0xD800)*0x400 + (ulo-0xDC00) + 0x10000;
return 2;
}
// handle errors
if ( 0 == e )
{
// no errors are masked.
return 0;
}