summaryrefslogtreecommitdiffstats
path: root/Objects/unicodectype.c
blob: 40694c619cfda54d495e617285e6deba065939a0 (plain)
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
/*
   Unicode character type helpers.

   Written by Marc-Andre Lemburg (mal@lemburg.com).
   Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)

   Copyright (c) Corporation for National Research Initiatives.

*/

#include "Python.h"
#include "unicodeobject.h"

#define ALPHA_MASK 0x01
#define DECIMAL_MASK 0x02
#define DIGIT_MASK 0x04
#define LOWER_MASK 0x08
#define LINEBREAK_MASK 0x10
#define SPACE_MASK 0x20
#define TITLE_MASK 0x40
#define UPPER_MASK 0x80
#define XID_START_MASK 0x100
#define XID_CONTINUE_MASK 0x200
#define PRINTABLE_MASK 0x400
#define NODELTA_MASK 0x800

typedef struct {
    const Py_UNICODE upper;
    const Py_UNICODE lower;
    const Py_UNICODE title;
    const unsigned char decimal;
    const unsigned char digit;
    const unsigned short flags;
} _PyUnicode_TypeRecord;

#include "unicodetype_db.h"

static const _PyUnicode_TypeRecord *
gettyperecord(Py_UNICODE code)
{
    int index;

#ifdef Py_UNICODE_WIDE
    if (code >= 0x110000)
        index = 0;
    else
#endif
    {
        index = index1[(code>>SHIFT)];
        index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))];
    }

    return &_PyUnicode_TypeRecords[index];
}

/* Returns 1 for Unicode characters having the category 'Zl', 'Zp' or
   type 'B', 0 otherwise. */

int _PyUnicode_IsLinebreak(register const Py_UNICODE ch)
{
    switch (ch) {
    case 0x000A: /* LINE FEED */
    case 0x000D: /* CARRIAGE RETURN */
    case 0x001C: /* FILE SEPARATOR */
    case 0x001D: /* GROUP SEPARATOR */
    case 0x001E: /* RECORD SEPARATOR */
    case 0x0085: /* NEXT LINE */
    case 0x2028: /* LINE SEPARATOR */
    case 0x2029: /* PARAGRAPH SEPARATOR */
	return 1;
    default:
	return 0;
    }
}

/* Returns the titlecase Unicode characters corresponding to ch or just
   ch if no titlecase mapping is known. */

Py_UNICODE _PyUnicode_ToTitlecase(register Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
    int delta;

    if (ctype->title)
        delta = ctype->title;
    else
	delta = ctype->upper;

    if (ctype->flags & NODELTA_MASK)
	return delta;

    if (delta >= 32768)
	    delta -= 65536;

    return ch + delta;
}

/* Returns 1 for Unicode characters having the category 'Lt', 0
   otherwise. */

int _PyUnicode_IsTitlecase(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & TITLE_MASK) != 0;
}

/* Returns 1 for Unicode characters having the XID_Start property, 0
   otherwise. */

int _PyUnicode_IsXidStart(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & XID_START_MASK) != 0;
}

/* Returns 1 for Unicode characters having the XID_Continue property,
   0 otherwise. */

int _PyUnicode_IsXidContinue(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & XID_CONTINUE_MASK) != 0;
}

/* Returns the integer decimal (0-9) for Unicode characters having
   this property, -1 otherwise. */

int _PyUnicode_ToDecimalDigit(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & DECIMAL_MASK) ? ctype->decimal : -1;
}

int _PyUnicode_IsDecimalDigit(Py_UNICODE ch)
{
    if (_PyUnicode_ToDecimalDigit(ch) < 0)
	return 0;
    return 1;
}

/* Returns the integer digit (0-9) for Unicode characters having
   this property, -1 otherwise. */

int _PyUnicode_ToDigit(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & DIGIT_MASK) ? ctype->digit : -1;
}

int _PyUnicode_IsDigit(Py_UNICODE ch)
{
    if (_PyUnicode_ToDigit(ch) < 0)
	return 0;
    return 1;
}

/* Returns the numeric value as double for Unicode characters having
   this property, -1.0 otherwise. */

/* TODO: replace with unicodetype_db.h table */

double _PyUnicode_ToNumeric(Py_UNICODE ch)
{
    switch (ch) {
    case 0x0F33:
        return (double) -1 / 2;
    case 0x17F0:
    case 0x3007:
#ifdef Py_UNICODE_WIDE
    case 0x1018A:
#endif
	return (double) 0;
    case 0x09F4:
    case 0x17F1:
    case 0x215F:
    case 0x2160:
    case 0x2170:
    case 0x3021:
    case 0x3192:
    case 0x3220:
    case 0x3280:
#ifdef Py_UNICODE_WIDE
    case 0x10107:
    case 0x10142:
    case 0x10158:
    case 0x10159:
    case 0x1015A:
    case 0x10320:
    case 0x103D1:
#endif
	return (double) 1;
    case 0x00BD:
    case 0x0F2A:
    case 0x2CFD:
#ifdef Py_UNICODE_WIDE
    case 0x10141:
    case 0x10175:
    case 0x10176:
#endif
	return (double) 1 / 2;
    case 0x2153:
	return (double) 1 / 3;
    case 0x00BC:
#ifdef Py_UNICODE_WIDE
    case 0x10140:
#endif
	return (double) 1 / 4;
    case 0x2155:
	return (double) 1 / 5;
    case 0x2159:
	return (double) 1 / 6;
    case 0x215B:
	return (double) 1 / 8;
    case 0x0BF0:
    case 0x1372:
    case 0x2169:
    case 0x2179:
    case 0x2469:
    case 0x247D:
    case 0x2491:
    case 0x24FE:
    case 0x277F:
    case 0x2789:
    case 0x2793:
    case 0x3038:
    case 0x3229:
    case 0x3289:
#ifdef Py_UNICODE_WIDE
    case 0x10110:
    case 0x10149:
    case 0x10150:
    case 0x10157:
    case 0x10160:
    case 0x10161:
    case 0x10162:
    case 0x10163:
    case 0x10164:
    case 0x10322:
    case 0x103D3:
    case 0x10A44:
#endif
	return (double) 10;
    case 0x0BF1:
    case 0x137B:
    case 0x216D:
    case 0x217D:
#ifdef Py_UNICODE_WIDE
    case 0x10119:
    case 0x1014B:
    case 0x10152:
    case 0x1016A:
    case 0x103D5:
    case 0x10A46:
#endif
	return (double) 100;
    case 0x0BF2:
    case 0x216F:
    case 0x217F:
    case 0x2180:
#ifdef Py_UNICODE_WIDE
    case 0x10122:
    case 0x1014D:
    case 0x10154:
    case 0x10171:
    case 0x10A47:
#endif
	return (double) 1000;
    case 0x137C:
    case 0x2182:
#ifdef Py_UNICODE_WIDE
    case 0x1012B:
    case 0x10155:
#endif
	return (double) 10000;
    case 0x216A:
    case 0x217A:
    case 0x246A:
    case 0x247E:
    case 0x2492:
    case 0x24EB:
	return (double) 11;
    case 0x0F2F:
        return (double) 11 / 2;
    case 0x216B:
    case 0x217B:
    case 0x246B:
    case 0x247F:
    case 0x2493:
    case 0x24EC:
	return (double) 12;
    case 0x246C:
    case 0x2480:
    case 0x2494:
    case 0x24ED:
	return (double) 13;
    case 0x0F30:
        return (double) 13 / 2;
    case 0x246D:
    case 0x2481:
    case 0x2495:
    case 0x24EE:
	return (double) 14;
    case 0x246E:
    case 0x2482:
    case 0x2496:
    case 0x24EF:
	return (double) 15;
    case 0x0F31:
        return (double) 15 / 2;
    case 0x09F9:
    case 0x246F:
    case 0x2483:
    case 0x2497:
    case 0x24F0:
	return (double) 16;
    case 0x16EE:
    case 0x2470:
    case 0x2484:
    case 0x2498:
    case 0x24F1:
	return (double) 17;
    case 0x0F32:
        return (double) 17 / 2;
    case 0x16EF:
    case 0x2471:
    case 0x2485:
    case 0x2499:
    case 0x24F2:
	return (double) 18;
    case 0x16F0:
    case 0x2472:
    case 0x2486:
    case 0x249A:
    case 0x24F3:
	return (double) 19;
    case 0x09F5:
    case 0x17F2:
    case 0x2161:
    case 0x2171:
    case 0x3022:
    case 0x3193:
    case 0x3221:
    case 0x3281:
#ifdef Py_UNICODE_WIDE
    case 0x10108:
    case 0x1015B:
    case 0x1015C:
    case 0x1015D:
    case 0x1015E:
    case 0x103D2:
#endif
	return (double) 2;
    case 0x2154:
#ifdef Py_UNICODE_WIDE
    case 0x10177:
#endif
	return (double) 2 / 3;
    case 0x2156:
        return (double) 2 / 5;
    case 0x1373:
    case 0x2473:
    case 0x2487:
    case 0x249B:
    case 0x24F4:
    case 0x3039:
#ifdef Py_UNICODE_WIDE
    case 0x10111:
    case 0x103D4:
    case 0x10A45:
#endif
        return (double) 20;
#ifdef Py_UNICODE_WIDE
    case 0x1011A:
        return (double) 200;
    case 0x10123:
        return (double) 2000;
    case 0x1012C:
        return (double) 20000;
#endif
    case 0x3251:
        return (double) 21;
    case 0x3252:
        return (double) 22;
    case 0x3253:
        return (double) 23;
    case 0x3254:
        return (double) 24;
    case 0x3255:
        return (double) 25;
    case 0x3256:
        return (double) 26;
    case 0x3257:
        return (double) 27;
    case 0x3258:
        return (double) 28;
    case 0x3259:
        return (double) 29;
    case 0x09F6:
    case 0x17F3:
    case 0x2162:
    case 0x2172:
    case 0x3023:
    case 0x3194:
    case 0x3222:
    case 0x3282:
#ifdef Py_UNICODE_WIDE
    case 0x10109:
#endif
	return (double) 3;
    case 0x0F2B:
        return (double) 3 / 2;
    case 0x00BE:
#ifdef Py_UNICODE_WIDE
    case 0x10178:
#endif
	return (double) 3 / 4;
    case 0x2157:
	return (double) 3 / 5;
    case 0x215C:
	return (double) 3 / 8;
    case 0x1374:
    case 0x303A:
    case 0x325A:
#ifdef Py_UNICODE_WIDE
    case 0x10112:
    case 0x10165:
#endif
	return (double) 30;
#ifdef Py_UNICODE_WIDE
    case 0x1011B:
    case 0x1016B:
        return (double) 300;
    case 0x10124:
        return (double) 3000;
    case 0x1012D:
        return (double) 30000;
#endif
    case 0x325B:
        return (double) 31;
    case 0x325C:
        return (double) 32;
    case 0x325D:
        return (double) 33;
    case 0x325E:
        return (double) 34;
    case 0x325F:
        return (double) 35;
    case 0x32B1:
        return (double) 36;
    case 0x32B2:
        return (double) 37;
    case 0x32B3:
        return (double) 38;
    case 0x32B4:
        return (double) 39;
    case 0x09F7:
    case 0x17F4:
    case 0x2163:
    case 0x2173:
    case 0x3024:
    case 0x3195:
    case 0x3223:
    case 0x3283:
#ifdef Py_UNICODE_WIDE
    case 0x1010A:
#endif
	return (double) 4;
    case 0x2158:
	return (double) 4 / 5;
    case 0x1375:
    case 0x32B5:
#ifdef Py_UNICODE_WIDE
    case 0x10113:
#endif
        return (double) 40;
#ifdef Py_UNICODE_WIDE
    case 0x1011C:
        return (double) 400;
    case 0x10125:
        return (double) 4000;
    case 0x1012E:
        return (double) 40000;
#endif
    case 0x32B6:
        return (double) 41;
    case 0x32B7:
        return (double) 42;
    case 0x32B8:
        return (double) 43;
    case 0x32B9:
        return (double) 44;
    case 0x32BA:
        return (double) 45;
    case 0x32BB:
        return (double) 46;
    case 0x32BC:
        return (double) 47;
    case 0x32BD:
        return (double) 48;
    case 0x32BE:
        return (double) 49;
    case 0x17F5:
    case 0x2164:
    case 0x2174:
    case 0x3025:
    case 0x3224:
    case 0x3284:
#ifdef Py_UNICODE_WIDE
    case 0x1010B:
    case 0x10143:
    case 0x10148:
    case 0x1014F:
    case 0x1015F:
    case 0x10173:
    case 0x10321:
#endif
	return (double) 5;
    case 0x0F2C:
        return (double) 5 / 2;
    case 0x215A:
	return (double) 5 / 6;
    case 0x215D:
	return (double) 5 / 8;
    case 0x1376:
    case 0x216C:
    case 0x217C:
    case 0x32BF:
#ifdef Py_UNICODE_WIDE
    case 0x10114:
    case 0x10144:
    case 0x1014A:
    case 0x10151:
    case 0x10166:
    case 0x10167:
    case 0x10168:
    case 0x10169:
    case 0x10174:
    case 0x10323:
#endif
	return (double) 50;
    case 0x216E:
    case 0x217E:
#ifdef Py_UNICODE_WIDE
    case 0x1011D:
    case 0x10145:
    case 0x1014C:
    case 0x10153:
    case 0x1016C:
    case 0x1016D:
    case 0x1016E:
    case 0x1016F:
    case 0x10170:
#endif
	return (double) 500;
    case 0x2181:
#ifdef Py_UNICODE_WIDE
    case 0x10126:
    case 0x10146:
    case 0x1014E:
    case 0x10172:
#endif
	return (double) 5000;
#ifdef Py_UNICODE_WIDE
    case 0x1012F:
    case 0x10147:
    case 0x10156:
        return (double) 50000;
#endif
    case 0x17F6:
    case 0x2165:
    case 0x2175:
    case 0x3026:
    case 0x3225:
    case 0x3285:
#ifdef Py_UNICODE_WIDE
    case 0x1010C:
#endif
	return (double) 6;
    case 0x1377:
#ifdef Py_UNICODE_WIDE
    case 0x10115:
#endif
	return (double) 60;
#ifdef Py_UNICODE_WIDE
    case 0x1011E:
        return (double) 600;
    case 0x10127:
        return (double) 6000;
    case 0x10130:
        return (double) 60000;
#endif
    case 0x17F7:
    case 0x2166:
    case 0x2176:
    case 0x3027:
    case 0x3226:
    case 0x3286:
#ifdef Py_UNICODE_WIDE
    case 0x1010D:
#endif
	return (double) 7;
    case 0x0F2D:
        return (double) 7 / 2;
    case 0x215E:
	return (double) 7 / 8;
    case 0x1378:
#ifdef Py_UNICODE_WIDE
    case 0x10116:
#endif
	return (double) 70;
#ifdef Py_UNICODE_WIDE
    case 0x1011F:
        return (double) 700;
    case 0x10128:
        return (double) 7000;
    case 0x10131:
        return (double) 70000;
#endif
    case 0x17F8:
    case 0x2167:
    case 0x2177:
    case 0x3028:
    case 0x3227:
    case 0x3287:
#ifdef Py_UNICODE_WIDE
    case 0x1010E:
#endif
	return (double) 8;
    case 0x1379:
#ifdef Py_UNICODE_WIDE
    case 0x10117:
#endif
	return (double) 80;
#ifdef Py_UNICODE_WIDE
    case 0x10120:
        return (double) 800;
    case 0x10129:
        return (double) 8000;
    case 0x10132:
        return (double) 80000;
#endif
    case 0x17F9:
    case 0x2168:
    case 0x2178:
    case 0x3029:
    case 0x3228:
    case 0x3288:
#ifdef Py_UNICODE_WIDE
    case 0x1010F:
#endif
	return (double) 9;
    case 0x0F2E:
        return (double) 9 / 2;
    case 0x137A:
#ifdef Py_UNICODE_WIDE
    case 0x10118:
#endif
	return (double) 90;
#ifdef Py_UNICODE_WIDE
    case 0x10121:
    case 0x1034A:
        return (double) 900;
    case 0x1012A:
        return (double) 9000;
    case 0x10133:
        return (double) 90000;
#endif
    default:
	return (double) _PyUnicode_ToDigit(ch);
    }
}

int _PyUnicode_IsNumeric(Py_UNICODE ch)
{
    return _PyUnicode_ToNumeric(ch) != -1.0;
}

/* Returns 1 for Unicode characters to be hex-escaped when repr()ed,
   0 otherwise.
   All characters except those characters defined in the Unicode character
   database as following categories are considered printable.
      * Cc (Other, Control)
      * Cf (Other, Format)
      * Cs (Other, Surrogate)
      * Co (Other, Private Use)
      * Cn (Other, Not Assigned)
      * Zl Separator, Line ('\u2028', LINE SEPARATOR)
      * Zp Separator, Paragraph ('\u2029', PARAGRAPH SEPARATOR)
      * Zs (Separator, Space) other than ASCII space('\x20').
*/
int _PyUnicode_IsPrintable(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & PRINTABLE_MASK) != 0;
}

#ifndef WANT_WCTYPE_FUNCTIONS

/* Returns 1 for Unicode characters having the bidirectional type
   'WS', 'B' or 'S' or the category 'Zs', 0 otherwise. */

int _PyUnicode_IsWhitespace(register const Py_UNICODE ch)
{
    switch (ch) {
    case 0x0009: /* HORIZONTAL TABULATION */
    case 0x000A: /* LINE FEED */
    case 0x000B: /* VERTICAL TABULATION */
    case 0x000C: /* FORM FEED */
    case 0x000D: /* CARRIAGE RETURN */
    case 0x001C: /* FILE SEPARATOR */
    case 0x001D: /* GROUP SEPARATOR */
    case 0x001E: /* RECORD SEPARATOR */
    case 0x001F: /* UNIT SEPARATOR */
    case 0x0020: /* SPACE */
    case 0x0085: /* NEXT LINE */
    case 0x00A0: /* NO-BREAK SPACE */
    case 0x1680: /* OGHAM SPACE MARK */
    case 0x2000: /* EN QUAD */
    case 0x2001: /* EM QUAD */
    case 0x2002: /* EN SPACE */
    case 0x2003: /* EM SPACE */
    case 0x2004: /* THREE-PER-EM SPACE */
    case 0x2005: /* FOUR-PER-EM SPACE */
    case 0x2006: /* SIX-PER-EM SPACE */
    case 0x2007: /* FIGURE SPACE */
    case 0x2008: /* PUNCTUATION SPACE */
    case 0x2009: /* THIN SPACE */
    case 0x200A: /* HAIR SPACE */
    case 0x200B: /* ZERO WIDTH SPACE */
    case 0x2028: /* LINE SEPARATOR */
    case 0x2029: /* PARAGRAPH SEPARATOR */
    case 0x202F: /* NARROW NO-BREAK SPACE */
    case 0x205F: /* MEDIUM MATHEMATICAL SPACE */
    case 0x3000: /* IDEOGRAPHIC SPACE */
	return 1;
    default:
	return 0;
    }
}

/* Returns 1 for Unicode characters having the category 'Ll', 0
   otherwise. */

int _PyUnicode_IsLowercase(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & LOWER_MASK) != 0;
}

/* Returns 1 for Unicode characters having the category 'Lu', 0
   otherwise. */

int _PyUnicode_IsUppercase(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & UPPER_MASK) != 0;
}

/* Returns the uppercase Unicode characters corresponding to ch or just
   ch if no uppercase mapping is known. */

Py_UNICODE _PyUnicode_ToUppercase(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
    int delta = ctype->upper;
    if (ctype->flags & NODELTA_MASK)
	return delta;
    if (delta >= 32768)
	    delta -= 65536;
    return ch + delta;
}

/* Returns the lowercase Unicode characters corresponding to ch or just
   ch if no lowercase mapping is known. */

Py_UNICODE _PyUnicode_ToLowercase(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
    int delta = ctype->lower;
    if (ctype->flags & NODELTA_MASK)
	return delta;
    if (delta >= 32768)
	    delta -= 65536;
    return ch + delta;
}

/* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt',
   'Lo' or 'Lm',  0 otherwise. */

int _PyUnicode_IsAlpha(Py_UNICODE ch)
{
    const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

    return (ctype->flags & ALPHA_MASK) != 0;
}

#else

/* Export the interfaces using the wchar_t type for portability
   reasons:  */

int _PyUnicode_IsWhitespace(Py_UNICODE ch)
{
    return iswspace(ch);
}

int _PyUnicode_IsLowercase(Py_UNICODE ch)
{
    return iswlower(ch);
}

int _PyUnicode_IsUppercase(Py_UNICODE ch)
{
    return iswupper(ch);
}

Py_UNICODE _PyUnicode_ToLowercase(Py_UNICODE ch)
{
    return towlower(ch);
}

Py_UNICODE _PyUnicode_ToUppercase(Py_UNICODE ch)
{
    return towupper(ch);
}

int _PyUnicode_IsAlpha(Py_UNICODE ch)
{
    return iswalpha(ch);
}

#endif