-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
bestline.c
3839 lines (3672 loc) · 139 KB
/
bestline.c
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
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ │
│ Bestline ── Library for interactive pseudoteletypewriter command │
│ sessions using ANSI Standard X3.64 control sequences │
│ │
│ OVERVIEW │
│ │
│ Bestline is a fork of linenoise (a popular readline alternative) │
│ that fixes its bugs and adds the missing features while reducing │
│ binary footprint (surprisingly) by removing bloated dependencies │
│ which means you can finally have a permissively-licensed command │
│ prompt w/ a 30kb footprint that's nearly as good as gnu readline │
│ │
│ EXAMPLE │
│ │
│ main() { │
│ char *line; │
│ while ((line = bestlineWithHistory("IN> ", "foo"))) { │
│ fputs("OUT> ", stdout); │
│ fputs(line, stdout); │
│ fputs("\n", stdout); │
│ free(line); │
│ } │
│ } │
│ │
│ CHANGES │
│ │
│ - Remove bell │
│ - Add kill ring │
│ - Fix flickering │
│ - Add UTF-8 editing │
│ - Add CTRL-R search │
│ - Support unlimited lines │
│ - Add parentheses awareness │
│ - React to terminal resizing │
│ - Don't generate .data section │
│ - Support terminal flow control │
│ - Make history loading 10x faster │
│ - Make multiline mode the only mode │
│ - Accommodate O_NONBLOCK file descriptors │
│ - Restore raw mode on process foregrounding │
│ - Make source code compatible with C++ compilers │
│ - Fix corruption issues by using generalized parsing │
│ - Implement nearly all GNU readline editing shortcuts │
│ - Remove heavyweight dependencies like printf/sprintf │
│ - Remove ISIG→^C→EAGAIN hack and use ephemeral handlers │
│ - Support running on Windows in MinTTY or CMD.EXE on Win10+ │
│ - Support diacratics, русский, Ελληνικά, 漢字, 仮名, 한글 │
│ │
│ SHORTCUTS │
│ │
│ CTRL-E END │
│ CTRL-A START │
│ CTRL-B BACK │
│ CTRL-F FORWARD │
│ CTRL-L CLEAR │
│ CTRL-H BACKSPACE │
│ CTRL-D DELETE │
│ CTRL-Y YANK │
│ CTRL-D EOF (IF EMPTY) │
│ CTRL-N NEXT HISTORY │
│ CTRL-P PREVIOUS HISTORY │
│ CTRL-R SEARCH HISTORY │
│ CTRL-G CANCEL SEARCH │
│ CTRL-J INSERT NEWLINE │
│ ALT-< BEGINNING OF HISTORY │
│ ALT-> END OF HISTORY │
│ ALT-F FORWARD WORD │
│ ALT-B BACKWARD WORD │
│ CTRL-ALT-F FORWARD EXPR │
│ CTRL-ALT-B BACKWARD EXPR │
│ ALT-RIGHT FORWARD EXPR │
│ ALT-LEFT BACKWARD EXPR │
│ ALT-SHIFT-B BARF EXPR │
│ ALT-SHIFT-S SLURP EXPR │
│ ALT-SHIFT-R RAISE EXPR │
│ CTRL-K KILL LINE FORWARDS │
│ CTRL-U KILL LINE BACKWARDS │
│ ALT-H KILL WORD BACKWARDS │
│ CTRL-W KILL WORD BACKWARDS │
│ CTRL-ALT-H KILL WORD BACKWARDS │
│ ALT-D KILL WORD FORWARDS │
│ ALT-Y ROTATE KILL RING AND YANK AGAIN │
│ ALT-\ SQUEEZE ADJACENT WHITESPACE │
│ CTRL-T TRANSPOSE │
│ ALT-T TRANSPOSE WORD │
│ ALT-U UPPERCASE WORD │
│ ALT-L LOWERCASE WORD │
│ ALT-C CAPITALIZE WORD │
│ CTRL-C CTRL-C INTERRUPT PROCESS │
│ CTRL-Z SUSPEND PROCESS │
│ CTRL-\ QUIT PROCESS │
│ CTRL-S PAUSE OUTPUT │
│ CTRL-Q UNPAUSE OUTPUT (IF PAUSED) │
│ CTRL-Q ESCAPED INSERT │
│ CTRL-SPACE SET MARK │
│ CTRL-X CTRL-X GOTO MARK │
│ PROTIP REMAP CAPS LOCK TO CTRL │
│ │
╞══════════════════════════════════════════════════════════════════════════════╡
│ │
│ Copyright 2018-2021 Justine Tunney <[email protected]> │
│ Copyright 2010-2016 Salvatore Sanfilippo <[email protected]> │
│ Copyright 2010-2013 Pieter Noordhuis <[email protected]> │
│ │
│ All rights reserved. │
│ │
│ Redistribution and use in source and binary forms, with or without │
│ modification, are permitted provided that the following conditions are │
│ met: │
│ │
│ * Redistributions of source code must retain the above copyright │
│ notice, this list of conditions and the following disclaimer. │
│ │
│ * Redistributions in binary form must reproduce the above copyright │
│ notice, this list of conditions and the following disclaimer in the │
│ documentation and/or other materials provided with the distribution. │
│ │
│ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS │
│ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT │
│ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR │
│ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT │
│ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, │
│ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT │
│ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, │
│ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY │
│ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT │
│ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE │
│ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. │
│ │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "bestline.h"
#define _POSIX_C_SOURCE 1 /* so GCC builds in ANSI mode */
#define _XOPEN_SOURCE 700 /* so GCC builds in ANSI mode */
#define _DARWIN_C_SOURCE 1 /* so SIGWINCH / IUTF8 on XNU */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#ifndef SIGWINCH
#define SIGWINCH 28 /* GNU/Systemd + XNU + FreeBSD + NetBSD + OpenBSD */
#endif
#ifndef IUTF8
#define IUTF8 0
#endif
__asm__(".ident\t\"\\n\\n\
Bestline (BSD-2)\\n\
Copyright 2018-2020 Justine Tunney <[email protected]>\\n\
Copyright 2010-2016 Salvatore Sanfilippo <[email protected]>\\n\
Copyright 2010-2013 Pieter Noordhuis <[email protected]>\"");
#ifndef BESTLINE_MAX_RING
#define BESTLINE_MAX_RING 8
#endif
#ifndef BESTLINE_MAX_HISTORY
#define BESTLINE_MAX_HISTORY 1024
#endif
#define BESTLINE_HISTORY_PREV +1
#define BESTLINE_HISTORY_NEXT -1
#define Ctrl(C) ((C) ^ 0100)
#define Min(X, Y) ((Y) > (X) ? (X) : (Y))
#define Max(X, Y) ((Y) < (X) ? (X) : (Y))
#define Case(X, Y) \
case X: \
Y; \
break
#define Read16le(X) ((255 & (X)[0]) << 000 | (255 & (X)[1]) << 010)
#define Read32le(X) \
((unsigned)(255 & (X)[0]) << 000 | (unsigned)(255 & (X)[1]) << 010 | \
(unsigned)(255 & (X)[2]) << 020 | (unsigned)(255 & (X)[3]) << 030)
struct abuf {
char *b;
unsigned len;
unsigned cap;
};
struct rune {
unsigned c;
unsigned n;
};
struct bestlineRing {
unsigned i;
char *p[BESTLINE_MAX_RING];
};
/* The bestlineState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct bestlineState {
int ifd; /* terminal stdin file descriptor */
int ofd; /* terminal stdout file descriptor */
struct winsize ws; /* rows and columns in terminal */
char *buf; /* edited line buffer */
const char *prompt; /* prompt to display */
int hindex; /* history index */
int rows; /* rows being used */
int oldpos; /* previous refresh cursor position */
unsigned buflen; /* edited line buffer size */
unsigned pos; /* current buffer index */
unsigned len; /* current edited line length */
unsigned mark; /* saved cursor position */
unsigned yi, yj; /* boundaries of last yank */
char seq[2][16]; /* keystroke history for yanking code */
char final; /* set to true on last update */
char dirty; /* if an update was squashed */
struct abuf full; /* used for multiline mode */
};
static const char *const kUnsupported[] = {"dumb", "cons25", "emacs"};
static int gotint;
static int gotcont;
static int gotwinch;
static signed char rawmode;
static char maskmode;
static char emacsmode;
static char llamamode;
static char balancemode;
static char ispaused;
static char iscapital;
static unsigned historylen;
static struct bestlineRing ring;
static struct sigaction orig_cont;
static struct sigaction orig_winch;
static struct termios orig_termios;
static char *history[BESTLINE_MAX_HISTORY];
static bestlineXlatCallback *xlatCallback;
static bestlineHintsCallback *hintsCallback;
static bestlineFreeHintsCallback *freeHintsCallback;
static bestlineCompletionCallback *completionCallback;
static void bestlineAtExit(void);
static void bestlineRefreshLine(struct bestlineState *);
static void bestlineOnInt(int sig) {
gotint = sig;
}
static void bestlineOnCont(int sig) {
gotcont = sig;
}
static void bestlineOnWinch(int sig) {
gotwinch = sig;
}
static char IsControl(unsigned c) {
return c <= 0x1F || (0x7F <= c && c <= 0x9F);
}
static int GetMonospaceCharacterWidth(unsigned c) {
return !IsControl(c) +
(c >= 0x1100 && (c <= 0x115f || c == 0x2329 || c == 0x232a ||
(c >= 0x2e80 && c <= 0xa4cf && c != 0x303f) ||
(c >= 0xac00 && c <= 0xd7a3) || (c >= 0xf900 && c <= 0xfaff) ||
(c >= 0xfe10 && c <= 0xfe19) || (c >= 0xfe30 && c <= 0xfe6f) ||
(c >= 0xff00 && c <= 0xff60) || (c >= 0xffe0 && c <= 0xffe6) ||
(c >= 0x20000 && c <= 0x2fffd) || (c >= 0x30000 && c <= 0x3fffd)));
}
/**
* Returns nonzero if 𝑐 isn't alphanumeric.
*
* Line reading interfaces generally define this operation as UNICODE
* characters that aren't in the letter category (Lu, Ll, Lt, Lm, Lo)
* and aren't in the number categorie (Nd, Nl, No). We also add a few
* other things like blocks and emoji (So).
*/
char bestlineIsSeparator(unsigned c) {
int m, l, r, n;
if (c < 0200) {
return !(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'));
}
if (c <= 0xffff) {
static const unsigned short kGlyphs[][2] = {
{0x00aa, 0x00aa}, /* 1x English */
{0x00b2, 0x00b3}, /* 2x English Arabic */
{0x00b5, 0x00b5}, /* 1x Greek */
{0x00b9, 0x00ba}, /* 2x English Arabic */
{0x00bc, 0x00be}, /* 3x Vulgar English Arabic */
{0x00c0, 0x00d6}, /* 23x Watin */
{0x00d8, 0x00f6}, /* 31x Watin */
{0x0100, 0x02c1}, /* 450x Watin-AB,IPA,Spacemod */
{0x02c6, 0x02d1}, /* 12x Spacemod */
{0x02e0, 0x02e4}, /* 5x Spacemod */
{0x02ec, 0x02ec}, /* 1x Spacemod */
{0x02ee, 0x02ee}, /* 1x Spacemod */
{0x0370, 0x0374}, /* 5x Greek */
{0x0376, 0x0377}, /* 2x Greek */
{0x037a, 0x037d}, /* 4x Greek */
{0x037f, 0x037f}, /* 1x Greek */
{0x0386, 0x0386}, /* 1x Greek */
{0x0388, 0x038a}, /* 3x Greek */
{0x038c, 0x038c}, /* 1x Greek */
{0x038e, 0x03a1}, /* 20x Greek */
{0x03a3, 0x03f5}, /* 83x Greek */
{0x03f7, 0x0481}, /* 139x Greek */
{0x048a, 0x052f}, /* 166x Cyrillic */
{0x0531, 0x0556}, /* 38x Armenian */
{0x0560, 0x0588}, /* 41x Armenian */
{0x05d0, 0x05ea}, /* 27x Hebrew */
{0x0620, 0x064a}, /* 43x Arabic */
{0x0660, 0x0669}, /* 10x Arabic */
{0x0671, 0x06d3}, /* 99x Arabic */
{0x06ee, 0x06fc}, /* 15x Arabic */
{0x0712, 0x072f}, /* 30x Syriac */
{0x074d, 0x07a5}, /* 89x Syriac,Arabic2,Thaana */
{0x07c0, 0x07ea}, /* 43x NKo */
{0x0800, 0x0815}, /* 22x Samaritan */
{0x0840, 0x0858}, /* 25x Mandaic */
{0x0904, 0x0939}, /* 54x Devanagari */
{0x0993, 0x09a8}, /* 22x Bengali */
{0x09e6, 0x09f1}, /* 12x Bengali */
{0x0a13, 0x0a28}, /* 22x Gurmukhi */
{0x0a66, 0x0a6f}, /* 10x Gurmukhi */
{0x0a93, 0x0aa8}, /* 22x Gujarati */
{0x0b13, 0x0b28}, /* 22x Oriya */
{0x0c92, 0x0ca8}, /* 23x Kannada */
{0x0caa, 0x0cb3}, /* 10x Kannada */
{0x0ce6, 0x0cef}, /* 10x Kannada */
{0x0d12, 0x0d3a}, /* 41x Malayalam */
{0x0d85, 0x0d96}, /* 18x Sinhala */
{0x0d9a, 0x0db1}, /* 24x Sinhala */
{0x0de6, 0x0def}, /* 10x Sinhala */
{0x0e01, 0x0e30}, /* 48x Thai */
{0x0e8c, 0x0ea3}, /* 24x Lao */
{0x0f20, 0x0f33}, /* 20x Tibetan */
{0x0f49, 0x0f6c}, /* 36x Tibetan */
{0x109e, 0x10c5}, /* 40x Myanmar,Georgian */
{0x10d0, 0x10fa}, /* 43x Georgian */
{0x10fc, 0x1248}, /* 333x Georgian,Hangul,Ethiopic */
{0x13a0, 0x13f5}, /* 86x Cherokee */
{0x1401, 0x166d}, /* 621x Aboriginal */
{0x16a0, 0x16ea}, /* 75x Runic */
{0x1700, 0x170c}, /* 13x Tagalog */
{0x1780, 0x17b3}, /* 52x Khmer */
{0x1820, 0x1878}, /* 89x Mongolian */
{0x1a00, 0x1a16}, /* 23x Buginese */
{0x1a20, 0x1a54}, /* 53x Tai Tham */
{0x1a80, 0x1a89}, /* 10x Tai Tham */
{0x1a90, 0x1a99}, /* 10x Tai Tham */
{0x1b05, 0x1b33}, /* 47x Balinese */
{0x1b50, 0x1b59}, /* 10x Balinese */
{0x1b83, 0x1ba0}, /* 30x Sundanese */
{0x1bae, 0x1be5}, /* 56x Sundanese */
{0x1c90, 0x1cba}, /* 43x Georgian2 */
{0x1cbd, 0x1cbf}, /* 3x Georgian2 */
{0x1e00, 0x1f15}, /* 278x Watin-C,Greek2 */
{0x2070, 0x2071}, /* 2x Supersub */
{0x2074, 0x2079}, /* 6x Supersub */
{0x207f, 0x2089}, /* 11x Supersub */
{0x2090, 0x209c}, /* 13x Supersub */
{0x2100, 0x2117}, /* 24x Letterlike */
{0x2119, 0x213f}, /* 39x Letterlike */
{0x2145, 0x214a}, /* 6x Letterlike */
{0x214c, 0x218b}, /* 64x Letterlike,Numbery */
{0x21af, 0x21cd}, /* 31x Arrows */
{0x21d5, 0x21f3}, /* 31x Arrows */
{0x230c, 0x231f}, /* 20x Technical */
{0x232b, 0x237b}, /* 81x Technical */
{0x237d, 0x239a}, /* 30x Technical */
{0x23b4, 0x23db}, /* 40x Technical */
{0x23e2, 0x2426}, /* 69x Technical,ControlPictures */
{0x2460, 0x25b6}, /* 343x Enclosed,Boxes,Blocks,Shapes */
{0x25c2, 0x25f7}, /* 54x Shapes */
{0x2600, 0x266e}, /* 111x Symbols */
{0x2670, 0x2767}, /* 248x Symbols,Dingbats */
{0x2776, 0x27bf}, /* 74x Dingbats */
{0x2800, 0x28ff}, /* 256x Braille */
{0x2c00, 0x2c2e}, /* 47x Glagolitic */
{0x2c30, 0x2c5e}, /* 47x Glagolitic */
{0x2c60, 0x2ce4}, /* 133x Watin-D */
{0x2d00, 0x2d25}, /* 38x Georgian2 */
{0x2d30, 0x2d67}, /* 56x Tifinagh */
{0x2d80, 0x2d96}, /* 23x Ethiopic2 */
{0x2e2f, 0x2e2f}, /* 1x Punctuation2 */
{0x3005, 0x3007}, /* 3x CJK Symbols & Punctuation */
{0x3021, 0x3029}, /* 9x CJK Symbols & Punctuation */
{0x3031, 0x3035}, /* 5x CJK Symbols & Punctuation */
{0x3038, 0x303c}, /* 5x CJK Symbols & Punctuation */
{0x3041, 0x3096}, /* 86x Hiragana */
{0x30a1, 0x30fa}, /* 90x Katakana */
{0x3105, 0x312f}, /* 43x Bopomofo */
{0x3131, 0x318e}, /* 94x Hangul Compatibility Jamo */
{0x31a0, 0x31ba}, /* 27x Bopomofo Extended */
{0x31f0, 0x31ff}, /* 16x Katakana Phonetic Extensions */
{0x3220, 0x3229}, /* 10x Enclosed CJK Letters & Months */
{0x3248, 0x324f}, /* 8x Enclosed CJK Letters & Months */
{0x3251, 0x325f}, /* 15x Enclosed CJK Letters & Months */
{0x3280, 0x3289}, /* 10x Enclosed CJK Letters & Months */
{0x32b1, 0x32bf}, /* 15x Enclosed CJK Letters & Months */
{0x3400, 0x4db5}, /* 6582x CJK Unified Ideographs Extension A */
{0x4dc0, 0x9fef}, /* 21040x Yijing Hexagram, CJK Unified Ideographs */
{0xa000, 0xa48c}, /* 1165x Yi Syllables */
{0xa4d0, 0xa4fd}, /* 46x Lisu */
{0xa500, 0xa60c}, /* 269x Vai */
{0xa610, 0xa62b}, /* 28x Vai */
{0xa6a0, 0xa6ef}, /* 80x Bamum */
{0xa80c, 0xa822}, /* 23x Syloti Nagri */
{0xa840, 0xa873}, /* 52x Phags-pa */
{0xa882, 0xa8b3}, /* 50x Saurashtra */
{0xa8d0, 0xa8d9}, /* 10x Saurashtra */
{0xa900, 0xa925}, /* 38x Kayah Li */
{0xa930, 0xa946}, /* 23x Rejang */
{0xa960, 0xa97c}, /* 29x Hangul Jamo Extended-A */
{0xa984, 0xa9b2}, /* 47x Javanese */
{0xa9cf, 0xa9d9}, /* 11x Javanese */
{0xaa00, 0xaa28}, /* 41x Cham */
{0xaa50, 0xaa59}, /* 10x Cham */
{0xabf0, 0xabf9}, /* 10x Meetei Mayek */
{0xac00, 0xd7a3}, /* 11172x Hangul Syllables */
{0xf900, 0xfa6d}, /* 366x CJK Compatibility Ideographs */
{0xfa70, 0xfad9}, /* 106x CJK Compatibility Ideographs */
{0xfb1f, 0xfb28}, /* 10x Alphabetic Presentation Forms */
{0xfb2a, 0xfb36}, /* 13x Alphabetic Presentation Forms */
{0xfb46, 0xfbb1}, /* 108x Alphabetic Presentation Forms */
{0xfbd3, 0xfd3d}, /* 363x Arabic Presentation Forms-A */
{0xfe76, 0xfefc}, /* 135x Arabic Presentation Forms-B */
{0xff10, 0xff19}, /* 10x Dubs */
{0xff21, 0xff3a}, /* 26x Dubs */
{0xff41, 0xff5a}, /* 26x Dubs */
{0xff66, 0xffbe}, /* 89x Dubs */
{0xffc2, 0xffc7}, /* 6x Dubs */
{0xffca, 0xffcf}, /* 6x Dubs */
{0xffd2, 0xffd7}, /* 6x Dubs */
{0xffda, 0xffdc}, /* 3x Dubs */
};
l = 0;
r = n = sizeof(kGlyphs) / sizeof(kGlyphs[0]);
while (l < r) {
m = (l + r) >> 1;
if (kGlyphs[m][1] < c) {
l = m + 1;
} else {
r = m;
}
}
return !(l < n && kGlyphs[l][0] <= c && c <= kGlyphs[l][1]);
} else {
static const unsigned kAstralGlyphs[][2] = {
{0x10107, 0x10133}, /* 45x Aegean */
{0x10140, 0x10178}, /* 57x Ancient Greek Numbers */
{0x1018a, 0x1018b}, /* 2x Ancient Greek Numbers */
{0x10280, 0x1029c}, /* 29x Lycian */
{0x102a0, 0x102d0}, /* 49x Carian */
{0x102e1, 0x102fb}, /* 27x Coptic Epact Numbers */
{0x10300, 0x10323}, /* 36x Old Italic */
{0x1032d, 0x1034a}, /* 30x Old Italic, Gothic */
{0x10350, 0x10375}, /* 38x Old Permic */
{0x10380, 0x1039d}, /* 30x Ugaritic */
{0x103a0, 0x103c3}, /* 36x Old Persian */
{0x103c8, 0x103cf}, /* 8x Old Persian */
{0x103d1, 0x103d5}, /* 5x Old Persian */
{0x10400, 0x1049d}, /* 158x Deseret, Shavian, Osmanya */
{0x104b0, 0x104d3}, /* 36x Osage */
{0x104d8, 0x104fb}, /* 36x Osage */
{0x10500, 0x10527}, /* 40x Elbasan */
{0x10530, 0x10563}, /* 52x Caucasian Albanian */
{0x10600, 0x10736}, /* 311x Linear A */
{0x10800, 0x10805}, /* 6x Cypriot Syllabary */
{0x1080a, 0x10835}, /* 44x Cypriot Syllabary */
{0x10837, 0x10838}, /* 2x Cypriot Syllabary */
{0x1083f, 0x1089e}, /* 86x Cypriot,ImperialAramaic,Palmyrene,Nabataean */
{0x108e0, 0x108f2}, /* 19x Hatran */
{0x108f4, 0x108f5}, /* 2x Hatran */
{0x108fb, 0x1091b}, /* 33x Hatran */
{0x10920, 0x10939}, /* 26x Lydian */
{0x10980, 0x109b7}, /* 56x Meroitic Hieroglyphs */
{0x109bc, 0x109cf}, /* 20x Meroitic Cursive */
{0x109d2, 0x10a00}, /* 47x Meroitic Cursive */
{0x10a10, 0x10a13}, /* 4x Kharoshthi */
{0x10a15, 0x10a17}, /* 3x Kharoshthi */
{0x10a19, 0x10a35}, /* 29x Kharoshthi */
{0x10a40, 0x10a48}, /* 9x Kharoshthi */
{0x10a60, 0x10a7e}, /* 31x Old South Arabian */
{0x10a80, 0x10a9f}, /* 32x Old North Arabian */
{0x10ac0, 0x10ac7}, /* 8x Manichaean */
{0x10ac9, 0x10ae4}, /* 28x Manichaean */
{0x10aeb, 0x10aef}, /* 5x Manichaean */
{0x10b00, 0x10b35}, /* 54x Avestan */
{0x10b40, 0x10b55}, /* 22x Inscriptional Parthian */
{0x10b58, 0x10b72}, /* 27x Inscriptional Parthian and Pahlavi */
{0x10b78, 0x10b91}, /* 26x Inscriptional Pahlavi, Psalter Pahlavi */
{0x10c00, 0x10c48}, /* 73x Old Turkic */
{0x10c80, 0x10cb2}, /* 51x Old Hungarian */
{0x10cc0, 0x10cf2}, /* 51x Old Hungarian */
{0x10cfa, 0x10d23}, /* 42x Old Hungarian, Hanifi Rohingya */
{0x10d30, 0x10d39}, /* 10x Hanifi Rohingya */
{0x10e60, 0x10e7e}, /* 31x Rumi Numeral Symbols */
{0x10f00, 0x10f27}, /* 40x Old Sogdian */
{0x10f30, 0x10f45}, /* 22x Sogdian */
{0x10f51, 0x10f54}, /* 4x Sogdian */
{0x10fe0, 0x10ff6}, /* 23x Elymaic */
{0x11003, 0x11037}, /* 53x Brahmi */
{0x11052, 0x1106f}, /* 30x Brahmi */
{0x11083, 0x110af}, /* 45x Kaithi */
{0x110d0, 0x110e8}, /* 25x Sora Sompeng */
{0x110f0, 0x110f9}, /* 10x Sora Sompeng */
{0x11103, 0x11126}, /* 36x Chakma */
{0x11136, 0x1113f}, /* 10x Chakma */
{0x11144, 0x11144}, /* 1x Chakma */
{0x11150, 0x11172}, /* 35x Mahajani */
{0x11176, 0x11176}, /* 1x Mahajani */
{0x11183, 0x111b2}, /* 48x Sharada */
{0x111c1, 0x111c4}, /* 4x Sharada */
{0x111d0, 0x111da}, /* 11x Sharada */
{0x111dc, 0x111dc}, /* 1x Sharada */
{0x111e1, 0x111f4}, /* 20x Sinhala Archaic Numbers */
{0x11200, 0x11211}, /* 18x Khojki */
{0x11213, 0x1122b}, /* 25x Khojki */
{0x11280, 0x11286}, /* 7x Multani */
{0x11288, 0x11288}, /* 1x Multani */
{0x1128a, 0x1128d}, /* 4x Multani */
{0x1128f, 0x1129d}, /* 15x Multani */
{0x1129f, 0x112a8}, /* 10x Multani */
{0x112b0, 0x112de}, /* 47x Khudawadi */
{0x112f0, 0x112f9}, /* 10x Khudawadi */
{0x11305, 0x1130c}, /* 8x Grantha */
{0x1130f, 0x11310}, /* 2x Grantha */
{0x11313, 0x11328}, /* 22x Grantha */
{0x1132a, 0x11330}, /* 7x Grantha */
{0x11332, 0x11333}, /* 2x Grantha */
{0x11335, 0x11339}, /* 5x Grantha */
{0x1133d, 0x1133d}, /* 1x Grantha */
{0x11350, 0x11350}, /* 1x Grantha */
{0x1135d, 0x11361}, /* 5x Grantha */
{0x11400, 0x11434}, /* 53x Newa */
{0x11447, 0x1144a}, /* 4x Newa */
{0x11450, 0x11459}, /* 10x Newa */
{0x1145f, 0x1145f}, /* 1x Newa */
{0x11480, 0x114af}, /* 48x Tirhuta */
{0x114c4, 0x114c5}, /* 2x Tirhuta */
{0x114c7, 0x114c7}, /* 1x Tirhuta */
{0x114d0, 0x114d9}, /* 10x Tirhuta */
{0x11580, 0x115ae}, /* 47x Siddham */
{0x115d8, 0x115db}, /* 4x Siddham */
{0x11600, 0x1162f}, /* 48x Modi */
{0x11644, 0x11644}, /* 1x Modi */
{0x11650, 0x11659}, /* 10x Modi */
{0x11680, 0x116aa}, /* 43x Takri */
{0x116b8, 0x116b8}, /* 1x Takri */
{0x116c0, 0x116c9}, /* 10x Takri */
{0x11700, 0x1171a}, /* 27x Ahom */
{0x11730, 0x1173b}, /* 12x Ahom */
{0x11800, 0x1182b}, /* 44x Dogra */
{0x118a0, 0x118f2}, /* 83x Warang Citi */
{0x118ff, 0x118ff}, /* 1x Warang Citi */
{0x119a0, 0x119a7}, /* 8x Nandinagari */
{0x119aa, 0x119d0}, /* 39x Nandinagari */
{0x119e1, 0x119e1}, /* 1x Nandinagari */
{0x119e3, 0x119e3}, /* 1x Nandinagari */
{0x11a00, 0x11a00}, /* 1x Zanabazar Square */
{0x11a0b, 0x11a32}, /* 40x Zanabazar Square */
{0x11a3a, 0x11a3a}, /* 1x Zanabazar Square */
{0x11a50, 0x11a50}, /* 1x Soyombo */
{0x11a5c, 0x11a89}, /* 46x Soyombo */
{0x11a9d, 0x11a9d}, /* 1x Soyombo */
{0x11ac0, 0x11af8}, /* 57x Pau Cin Hau */
{0x11c00, 0x11c08}, /* 9x Bhaiksuki */
{0x11c0a, 0x11c2e}, /* 37x Bhaiksuki */
{0x11c40, 0x11c40}, /* 1x Bhaiksuki */
{0x11c50, 0x11c6c}, /* 29x Bhaiksuki */
{0x11c72, 0x11c8f}, /* 30x Marchen */
{0x11d00, 0x11d06}, /* 7x Masaram Gondi */
{0x11d08, 0x11d09}, /* 2x Masaram Gondi */
{0x11d0b, 0x11d30}, /* 38x Masaram Gondi */
{0x11d46, 0x11d46}, /* 1x Masaram Gondi */
{0x11d50, 0x11d59}, /* 10x Masaram Gondi */
{0x11d60, 0x11d65}, /* 6x Gunjala Gondi */
{0x11d67, 0x11d68}, /* 2x Gunjala Gondi */
{0x11d6a, 0x11d89}, /* 32x Gunjala Gondi */
{0x11d98, 0x11d98}, /* 1x Gunjala Gondi */
{0x11da0, 0x11da9}, /* 10x Gunjala Gondi */
{0x11ee0, 0x11ef2}, /* 19x Makasar */
{0x11fc0, 0x11fd4}, /* 21x Tamil Supplement */
{0x12000, 0x12399}, /* 922x Cuneiform */
{0x12400, 0x1246e}, /* 111x Cuneiform Numbers & Punctuation */
{0x12480, 0x12543}, /* 196x Early Dynastic Cuneiform */
{0x13000, 0x1342e}, /* 1071x Egyptian Hieroglyphs */
{0x14400, 0x14646}, /* 583x Anatolian Hieroglyphs */
{0x16800, 0x16a38}, /* 569x Bamum Supplement */
{0x16a40, 0x16a5e}, /* 31x Mro */
{0x16a60, 0x16a69}, /* 10x Mro */
{0x16ad0, 0x16aed}, /* 30x Bassa Vah */
{0x16b00, 0x16b2f}, /* 48x Pahawh Hmong */
{0x16b40, 0x16b43}, /* 4x Pahawh Hmong */
{0x16b50, 0x16b59}, /* 10x Pahawh Hmong */
{0x16b5b, 0x16b61}, /* 7x Pahawh Hmong */
{0x16b63, 0x16b77}, /* 21x Pahawh Hmong */
{0x16b7d, 0x16b8f}, /* 19x Pahawh Hmong */
{0x16e40, 0x16e96}, /* 87x Medefaidrin */
{0x16f00, 0x16f4a}, /* 75x Miao */
{0x16f50, 0x16f50}, /* 1x Miao */
{0x16f93, 0x16f9f}, /* 13x Miao */
{0x16fe0, 0x16fe1}, /* 2x Ideographic Symbols & Punctuation */
{0x16fe3, 0x16fe3}, /* 1x Ideographic Symbols & Punctuation */
{0x17000, 0x187f7}, /* 6136x Tangut */
{0x18800, 0x18af2}, /* 755x Tangut Components */
{0x1b000, 0x1b11e}, /* 287x Kana Supplement */
{0x1b150, 0x1b152}, /* 3x Small Kana Extension */
{0x1b164, 0x1b167}, /* 4x Small Kana Extension */
{0x1b170, 0x1b2fb}, /* 396x Nushu */
{0x1bc00, 0x1bc6a}, /* 107x Duployan */
{0x1bc70, 0x1bc7c}, /* 13x Duployan */
{0x1bc80, 0x1bc88}, /* 9x Duployan */
{0x1bc90, 0x1bc99}, /* 10x Duployan */
{0x1d2e0, 0x1d2f3}, /* 20x Mayan Numerals */
{0x1d360, 0x1d378}, /* 25x Counting Rod Numerals */
{0x1d400, 0x1d454}, /* 85x 𝐀..𝑔 Math */
{0x1d456, 0x1d49c}, /* 71x 𝑖..𝒜 Math */
{0x1d49e, 0x1d49f}, /* 2x 𝒞..𝒟 Math */
{0x1d4a2, 0x1d4a2}, /* 1x 𝒢..𝒢 Math */
{0x1d4a5, 0x1d4a6}, /* 2x 𝒥..𝒦 Math */
{0x1d4a9, 0x1d4ac}, /* 4x 𝒩..𝒬 Math */
{0x1d4ae, 0x1d4b9}, /* 12x 𝒮..𝒹 Math */
{0x1d4bb, 0x1d4bb}, /* 1x 𝒻..𝒻 Math */
{0x1d4bd, 0x1d4c3}, /* 7x 𝒽..𝓃 Math */
{0x1d4c5, 0x1d505}, /* 65x 𝓅..𝔅 Math */
{0x1d507, 0x1d50a}, /* 4x 𝔇..𝔊 Math */
{0x1d50d, 0x1d514}, /* 8x 𝔍..𝔔 Math */
{0x1d516, 0x1d51c}, /* 7x 𝔖..𝔜 Math */
{0x1d51e, 0x1d539}, /* 28x 𝔞..𝔹 Math */
{0x1d53b, 0x1d53e}, /* 4x 𝔻..𝔾 Math */
{0x1d540, 0x1d544}, /* 5x 𝕀..𝕄 Math */
{0x1d546, 0x1d546}, /* 1x 𝕆..𝕆 Math */
{0x1d54a, 0x1d550}, /* 7x 𝕊..𝕐 Math */
{0x1d552, 0x1d6a5}, /* 340x 𝕒..𝚥 Math */
{0x1d6a8, 0x1d6c0}, /* 25x 𝚨..𝛀 Math */
{0x1d6c2, 0x1d6da}, /* 25x 𝛂..𝛚 Math */
{0x1d6dc, 0x1d6fa}, /* 31x 𝛜..𝛺 Math */
{0x1d6fc, 0x1d714}, /* 25x 𝛼..𝜔 Math */
{0x1d716, 0x1d734}, /* 31x 𝜖..𝜴 Math */
{0x1d736, 0x1d74e}, /* 25x 𝜶..𝝎 Math */
{0x1d750, 0x1d76e}, /* 31x 𝝐..𝝮 Math */
{0x1d770, 0x1d788}, /* 25x 𝝰..𝞈 Math */
{0x1d78a, 0x1d7a8}, /* 31x 𝞊..𝞨 Math */
{0x1d7aa, 0x1d7c2}, /* 25x 𝞪..𝟂 Math */
{0x1d7c4, 0x1d7cb}, /* 8x 𝟄..𝟋 Math */
{0x1d7ce, 0x1d9ff}, /* 562x Math, Sutton SignWriting */
{0x1f100, 0x1f10c}, /* 13x Enclosed Alphanumeric Supplement */
{0x20000, 0x2a6d6}, /* 42711x CJK Unified Ideographs Extension B */
{0x2a700, 0x2b734}, /* 4149x CJK Unified Ideographs Extension C */
{0x2b740, 0x2b81d}, /* 222x CJK Unified Ideographs Extension D */
{0x2b820, 0x2cea1}, /* 5762x CJK Unified Ideographs Extension E */
{0x2ceb0, 0x2ebe0}, /* 7473x CJK Unified Ideographs Extension F */
{0x2f800, 0x2fa1d}, /* 542x CJK Compatibility Ideographs Supplement */
};
l = 0;
r = n = sizeof(kAstralGlyphs) / sizeof(kAstralGlyphs[0]);
while (l < r) {
m = (l + r) >> 1;
if (kAstralGlyphs[m][1] < c) {
l = m + 1;
} else {
r = m;
}
}
return !(l < n && kAstralGlyphs[l][0] <= c && c <= kAstralGlyphs[l][1]);
}
}
unsigned bestlineLowercase(unsigned c) {
int m, l, r, n;
if (c < 0200) {
if ('A' <= c && c <= 'Z') {
return c + 32;
} else {
return c;
}
} else if (c <= 0xffff) {
if ((0x0100 <= c && c <= 0x0176) || /* 60x Ā..ā → ā..ŵ Watin-A */
(0x01de <= c && c <= 0x01ee) || /* 9x Ǟ..Ǯ → ǟ..ǯ Watin-B */
(0x01f8 <= c && c <= 0x021e) || /* 20x Ǹ..Ȟ → ǹ..ȟ Watin-B */
(0x0222 <= c && c <= 0x0232) || /* 9x Ȣ..Ȳ → ȣ..ȳ Watin-B */
(0x1e00 <= c && c <= 0x1eff)) { /*256x Ḁ..Ỿ → ḁ..ỿ Watin-C */
if (c == 0x0130)
return c - 199;
if (c == 0x1e9e)
return c;
return c + (~c & 1);
} else if (0x01cf <= c && c <= 0x01db) {
return c + (c & 1); /* 7x Ǐ..Ǜ → ǐ..ǜ Watin-B */
} else if (0x13a0 <= c && c <= 0x13ef) {
return c + 38864; /* 80x Ꭰ ..Ꮿ → ꭰ ..ꮿ Cherokee */
} else {
static const struct {
unsigned short a;
unsigned short b;
short d;
} kLower[] = {
{0x00c0, 0x00d6, +32}, /* 23x À ..Ö → à ..ö Watin */
{0x00d8, 0x00de, +32}, /* 7x Ø ..Þ → ø ..þ Watin */
{0x0178, 0x0178, -121}, /* 1x Ÿ ..Ÿ → ÿ ..ÿ Watin-A */
{0x0179, 0x0179, +1}, /* 1x Ź ..Ź → ź ..ź Watin-A */
{0x017b, 0x017b, +1}, /* 1x Ż ..Ż → ż ..ż Watin-A */
{0x017d, 0x017d, +1}, /* 1x Ž ..Ž → ž ..ž Watin-A */
{0x0181, 0x0181, +210}, /* 1x Ɓ ..Ɓ → ɓ ..ɓ Watin-B */
{0x0182, 0x0182, +1}, /* 1x Ƃ ..Ƃ → ƃ ..ƃ Watin-B */
{0x0184, 0x0184, +1}, /* 1x Ƅ ..Ƅ → ƅ ..ƅ Watin-B */
{0x0186, 0x0186, +206}, /* 1x Ɔ ..Ɔ → ɔ ..ɔ Watin-B */
{0x0187, 0x0187, +1}, /* 1x Ƈ ..Ƈ → ƈ ..ƈ Watin-B */
{0x0189, 0x018a, +205}, /* 2x Ɖ ..Ɗ → ɖ ..ɗ Watin-B */
{0x018b, 0x018b, +1}, /* 1x Ƌ ..Ƌ → ƌ ..ƌ Watin-B */
{0x018e, 0x018e, +79}, /* 1x Ǝ ..Ǝ → ǝ ..ǝ Watin-B */
{0x018f, 0x018f, +202}, /* 1x Ə ..Ə → ə ..ə Watin-B */
{0x0190, 0x0190, +203}, /* 1x Ɛ ..Ɛ → ɛ ..ɛ Watin-B */
{0x0191, 0x0191, +1}, /* 1x Ƒ ..Ƒ → ƒ ..ƒ Watin-B */
{0x0193, 0x0193, +205}, /* 1x Ɠ ..Ɠ → ɠ ..ɠ Watin-B */
{0x0194, 0x0194, +207}, /* 1x Ɣ ..Ɣ → ɣ ..ɣ Watin-B */
{0x0196, 0x0196, +211}, /* 1x Ɩ ..Ɩ → ɩ ..ɩ Watin-B */
{0x0197, 0x0197, +209}, /* 1x Ɨ ..Ɨ → ɨ ..ɨ Watin-B */
{0x0198, 0x0198, +1}, /* 1x Ƙ ..Ƙ → ƙ ..ƙ Watin-B */
{0x019c, 0x019c, +211}, /* 1x Ɯ ..Ɯ → ɯ ..ɯ Watin-B */
{0x019d, 0x019d, +213}, /* 1x Ɲ ..Ɲ → ɲ ..ɲ Watin-B */
{0x019f, 0x019f, +214}, /* 1x Ɵ ..Ɵ → ɵ ..ɵ Watin-B */
{0x01a0, 0x01a0, +1}, /* 1x Ơ ..Ơ → ơ ..ơ Watin-B */
{0x01a2, 0x01a2, +1}, /* 1x Ƣ ..Ƣ → ƣ ..ƣ Watin-B */
{0x01a4, 0x01a4, +1}, /* 1x Ƥ ..Ƥ → ƥ ..ƥ Watin-B */
{0x01a6, 0x01a6, +218}, /* 1x Ʀ ..Ʀ → ʀ ..ʀ Watin-B */
{0x01a7, 0x01a7, +1}, /* 1x Ƨ ..Ƨ → ƨ ..ƨ Watin-B */
{0x01a9, 0x01a9, +218}, /* 1x Ʃ ..Ʃ → ʃ ..ʃ Watin-B */
{0x01ac, 0x01ac, +1}, /* 1x Ƭ ..Ƭ → ƭ ..ƭ Watin-B */
{0x01ae, 0x01ae, +218}, /* 1x Ʈ ..Ʈ → ʈ ..ʈ Watin-B */
{0x01af, 0x01af, +1}, /* 1x Ư ..Ư → ư ..ư Watin-B */
{0x01b1, 0x01b2, +217}, /* 2x Ʊ ..Ʋ → ʊ ..ʋ Watin-B */
{0x01b3, 0x01b3, +1}, /* 1x Ƴ ..Ƴ → ƴ ..ƴ Watin-B */
{0x01b5, 0x01b5, +1}, /* 1x Ƶ ..Ƶ → ƶ ..ƶ Watin-B */
{0x01b7, 0x01b7, +219}, /* 1x Ʒ ..Ʒ → ʒ ..ʒ Watin-B */
{0x01b8, 0x01b8, +1}, /* 1x Ƹ ..Ƹ → ƹ ..ƹ Watin-B */
{0x01bc, 0x01bc, +1}, /* 1x Ƽ ..Ƽ → ƽ ..ƽ Watin-B */
{0x01c4, 0x01c4, +2}, /* 1x DŽ ..DŽ → dž ..dž Watin-B */
{0x01c5, 0x01c5, +1}, /* 1x Dž ..Dž → dž ..dž Watin-B */
{0x01c7, 0x01c7, +2}, /* 1x LJ ..LJ → lj ..lj Watin-B */
{0x01c8, 0x01c8, +1}, /* 1x Lj ..Lj → lj ..lj Watin-B */
{0x01ca, 0x01ca, +2}, /* 1x NJ ..NJ → nj ..nj Watin-B */
{0x01cb, 0x01cb, +1}, /* 1x Nj ..Nj → nj ..nj Watin-B */
{0x01cd, 0x01cd, +1}, /* 1x Ǎ ..Ǎ → ǎ ..ǎ Watin-B */
{0x01f1, 0x01f1, +2}, /* 1x DZ ..DZ → dz ..dz Watin-B */
{0x01f2, 0x01f2, +1}, /* 1x Dz ..Dz → dz ..dz Watin-B */
{0x01f4, 0x01f4, +1}, /* 1x Ǵ ..Ǵ → ǵ ..ǵ Watin-B */
{0x01f6, 0x01f6, -97}, /* 1x Ƕ ..Ƕ → ƕ ..ƕ Watin-B */
{0x01f7, 0x01f7, -56}, /* 1x Ƿ ..Ƿ → ƿ ..ƿ Watin-B */
{0x0220, 0x0220, -130}, /* 1x Ƞ ..Ƞ → ƞ ..ƞ Watin-B */
{0x023b, 0x023b, +1}, /* 1x Ȼ ..Ȼ → ȼ ..ȼ Watin-B */
{0x023d, 0x023d, -163}, /* 1x Ƚ ..Ƚ → ƚ ..ƚ Watin-B */
{0x0241, 0x0241, +1}, /* 1x Ɂ ..Ɂ → ɂ ..ɂ Watin-B */
{0x0243, 0x0243, -195}, /* 1x Ƀ ..Ƀ → ƀ ..ƀ Watin-B */
{0x0244, 0x0244, +69}, /* 1x Ʉ ..Ʉ → ʉ ..ʉ Watin-B */
{0x0245, 0x0245, +71}, /* 1x Ʌ ..Ʌ → ʌ ..ʌ Watin-B */
{0x0246, 0x0246, +1}, /* 1x Ɇ ..Ɇ → ɇ ..ɇ Watin-B */
{0x0248, 0x0248, +1}, /* 1x Ɉ ..Ɉ → ɉ ..ɉ Watin-B */
{0x024a, 0x024a, +1}, /* 1x Ɋ ..Ɋ → ɋ ..ɋ Watin-B */
{0x024c, 0x024c, +1}, /* 1x Ɍ ..Ɍ → ɍ ..ɍ Watin-B */
{0x024e, 0x024e, +1}, /* 1x Ɏ ..Ɏ → ɏ ..ɏ Watin-B */
{0x0386, 0x0386, +38}, /* 1x Ά ..Ά → ά ..ά Greek */
{0x0388, 0x038a, +37}, /* 3x Έ ..Ί → έ ..ί Greek */
{0x038c, 0x038c, +64}, /* 1x Ό ..Ό → ό ..ό Greek */
{0x038e, 0x038f, +63}, /* 2x Ύ ..Ώ → ύ ..ώ Greek */
{0x0391, 0x03a1, +32}, /* 17x Α ..Ρ → α ..ρ Greek */
{0x03a3, 0x03ab, +32}, /* 9x Σ ..Ϋ → σ ..ϋ Greek */
{0x03dc, 0x03dc, +1}, /* 1x Ϝ ..Ϝ → ϝ ..ϝ Greek */
{0x03f4, 0x03f4, -60}, /* 1x ϴ ..ϴ → θ ..θ Greek */
{0x0400, 0x040f, +80}, /* 16x Ѐ ..Џ → ѐ ..џ Cyrillic */
{0x0410, 0x042f, +32}, /* 32x А ..Я → а ..я Cyrillic */
{0x0460, 0x0460, +1}, /* 1x Ѡ ..Ѡ → ѡ ..ѡ Cyrillic */
{0x0462, 0x0462, +1}, /* 1x Ѣ ..Ѣ → ѣ ..ѣ Cyrillic */
{0x0464, 0x0464, +1}, /* 1x Ѥ ..Ѥ → ѥ ..ѥ Cyrillic */
{0x0472, 0x0472, +1}, /* 1x Ѳ ..Ѳ → ѳ ..ѳ Cyrillic */
{0x0490, 0x0490, +1}, /* 1x Ґ ..Ґ → ґ ..ґ Cyrillic */
{0x0498, 0x0498, +1}, /* 1x Ҙ ..Ҙ → ҙ ..ҙ Cyrillic */
{0x049a, 0x049a, +1}, /* 1x Қ ..Қ → қ ..қ Cyrillic */
{0x0531, 0x0556, +48}, /* 38x Ա ..Ֆ → ա ..ֆ Armenian */
{0x10a0, 0x10c5, +7264}, /* 38x Ⴀ ..Ⴥ → ⴀ ..ⴥ Georgian */
{0x10c7, 0x10c7, +7264}, /* 1x Ⴧ ..Ⴧ → ⴧ ..ⴧ Georgian */
{0x10cd, 0x10cd, +7264}, /* 1x Ⴭ ..Ⴭ → ⴭ ..ⴭ Georgian */
{0x13f0, 0x13f5, +8}, /* 6x Ᏸ ..Ᏽ → ᏸ ..ᏽ Cherokee */
{0x1c90, 0x1cba, -3008}, /* 43x Ა ..Ჺ → ა ..ჺ Georgian2 */
{0x1cbd, 0x1cbf, -3008}, /* 3x Ჽ ..Ჿ → ჽ ..ჿ Georgian2 */
{0x1f08, 0x1f0f, -8}, /* 8x Ἀ ..Ἇ → ἀ ..ἇ Greek2 */
{0x1f18, 0x1f1d, -8}, /* 6x Ἐ ..Ἕ → ἐ ..ἕ Greek2 */
{0x1f28, 0x1f2f, -8}, /* 8x Ἠ ..Ἧ → ἠ ..ἧ Greek2 */
{0x1f38, 0x1f3f, -8}, /* 8x Ἰ ..Ἷ → ἰ ..ἷ Greek2 */
{0x1f48, 0x1f4d, -8}, /* 6x Ὀ ..Ὅ → ὀ ..ὅ Greek2 */
{0x1f59, 0x1f59, -8}, /* 1x Ὑ ..Ὑ → ὑ ..ὑ Greek2 */
{0x1f5b, 0x1f5b, -8}, /* 1x Ὓ ..Ὓ → ὓ ..ὓ Greek2 */
{0x1f5d, 0x1f5d, -8}, /* 1x Ὕ ..Ὕ → ὕ ..ὕ Greek2 */
{0x1f5f, 0x1f5f, -8}, /* 1x Ὗ ..Ὗ → ὗ ..ὗ Greek2 */
{0x1f68, 0x1f6f, -8}, /* 8x Ὠ ..Ὧ → ὠ ..ὧ Greek2 */
{0x1f88, 0x1f8f, -8}, /* 8x ᾈ ..ᾏ → ᾀ ..ᾇ Greek2 */
{0x1f98, 0x1f9f, -8}, /* 8x ᾘ ..ᾟ → ᾐ ..ᾗ Greek2 */
{0x1fa8, 0x1faf, -8}, /* 8x ᾨ ..ᾯ → ᾠ ..ᾧ Greek2 */
{0x1fb8, 0x1fb9, -8}, /* 2x Ᾰ ..Ᾱ → ᾰ ..ᾱ Greek2 */
{0x1fba, 0x1fbb, -74}, /* 2x Ὰ ..Ά → ὰ ..ά Greek2 */
{0x1fbc, 0x1fbc, -9}, /* 1x ᾼ ..ᾼ → ᾳ ..ᾳ Greek2 */
{0x1fc8, 0x1fcb, -86}, /* 4x Ὲ ..Ή → ὲ ..ή Greek2 */
{0x1fcc, 0x1fcc, -9}, /* 1x ῌ ..ῌ → ῃ ..ῃ Greek2 */
{0x1fd8, 0x1fd9, -8}, /* 2x Ῐ ..Ῑ → ῐ ..ῑ Greek2 */
{0x1fda, 0x1fdb, -100}, /* 2x Ὶ ..Ί → ὶ ..ί Greek2 */
{0x1fe8, 0x1fe9, -8}, /* 2x Ῠ ..Ῡ → ῠ ..ῡ Greek2 */
{0x1fea, 0x1feb, -112}, /* 2x Ὺ ..Ύ → ὺ ..ύ Greek2 */
{0x1fec, 0x1fec, -7}, /* 1x Ῥ ..Ῥ → ῥ ..ῥ Greek2 */
{0x1ff8, 0x1ff9, -128}, /* 2x Ὸ ..Ό → ὸ ..ό Greek2 */
{0x1ffa, 0x1ffb, -126}, /* 2x Ὼ ..Ώ → ὼ ..ώ Greek2 */
{0x1ffc, 0x1ffc, -9}, /* 1x ῼ ..ῼ → ῳ ..ῳ Greek2 */
{0x2126, 0x2126, -7517}, /* 1x Ω ..Ω → ω ..ω Letterlike */
{0x212a, 0x212a, -8383}, /* 1x K ..K → k ..k Letterlike */
{0x212b, 0x212b, -8262}, /* 1x Å ..Å → å ..å Letterlike */
{0x2132, 0x2132, +28}, /* 1x Ⅎ ..Ⅎ → ⅎ ..ⅎ Letterlike */
{0x2160, 0x216f, +16}, /* 16x Ⅰ ..Ⅿ → ⅰ ..ⅿ Numbery */
{0x2183, 0x2183, +1}, /* 1x Ↄ ..Ↄ → ↄ ..ↄ Numbery */
{0x24b6, 0x24cf, +26}, /* 26x Ⓐ ..Ⓩ → ⓐ ..ⓩ Enclosed */
{0x2c00, 0x2c2e, +48}, /* 47x Ⰰ ..Ⱞ → ⰰ ..ⱞ Glagolitic */
{0xff21, 0xff3a, +32}, /* 26x A..Z → a..z Dubs */
};
l = 0;
r = n = sizeof(kLower) / sizeof(kLower[0]);
while (l < r) {
m = (l + r) >> 1;
if (kLower[m].b < c) {
l = m + 1;
} else {
r = m;
}
}
if (l < n && kLower[l].a <= c && c <= kLower[l].b) {
return c + kLower[l].d;
} else {
return c;
}
}
} else {
static struct {
unsigned a;
unsigned b;
short d;
} kAstralLower[] = {
{0x10400, 0x10427, +40}, /* 40x 𐐀 ..𐐧 → 𐐨 ..𐑏 Deseret */
{0x104b0, 0x104d3, +40}, /* 36x 𐒰 ..𐓓 → 𐓘 ..𐓻 Osage */
{0x1d400, 0x1d419, +26}, /* 26x 𝐀 ..𝐙 → 𝐚 ..𝐳 Math */
{0x1d43c, 0x1d44d, +26}, /* 18x 𝐼 ..𝑍 → 𝑖 ..𝑧 Math */
{0x1d468, 0x1d481, +26}, /* 26x 𝑨 ..𝒁 → 𝒂 ..𝒛 Math */
{0x1d4ae, 0x1d4b5, +26}, /* 8x 𝒮 ..𝒵 → 𝓈 ..𝓏 Math */
{0x1d4d0, 0x1d4e9, +26}, /* 26x 𝓐 ..𝓩 → 𝓪 ..𝔃 Math */
{0x1d50d, 0x1d514, +26}, /* 8x 𝔍 ..𝔔 → 𝔧 ..𝔮 Math */
{0x1d56c, 0x1d585, +26}, /* 26x 𝕬 ..𝖅 → 𝖆 ..𝖟 Math */
{0x1d5a0, 0x1d5b9, +26}, /* 26x 𝖠 ..𝖹 → 𝖺 ..𝗓 Math */
{0x1d5d4, 0x1d5ed, +26}, /* 26x 𝗔 ..𝗭 → 𝗮 ..𝘇 Math */
{0x1d608, 0x1d621, +26}, /* 26x 𝘈 ..𝘡 → 𝘢 ..𝘻 Math */
{0x1d63c, 0x1d655, -442}, /* 26x 𝘼 ..𝙕 → 𝒂 ..𝒛 Math */
{0x1d670, 0x1d689, +26}, /* 26x 𝙰 ..𝚉 → 𝚊 ..𝚣 Math */
{0x1d6a8, 0x1d6b8, +26}, /* 17x 𝚨 ..𝚸 → 𝛂 ..𝛒 Math */
{0x1d6e2, 0x1d6f2, +26}, /* 17x 𝛢 ..𝛲 → 𝛼 ..𝜌 Math */
{0x1d71c, 0x1d72c, +26}, /* 17x 𝜜 ..𝜬 → 𝜶 ..𝝆 Math */
{0x1d756, 0x1d766, +26}, /* 17x 𝝖 ..𝝦 → 𝝰 ..𝞀 Math */
{0x1d790, 0x1d7a0, -90}, /* 17x 𝞐 ..𝞠 → 𝜶 ..𝝆 Math */
};
l = 0;
r = n = sizeof(kAstralLower) / sizeof(kAstralLower[0]);
while (l < r) {
m = (l + r) >> 1;
if (kAstralLower[m].b < c) {
l = m + 1;
} else {
r = m;
}
}
if (l < n && kAstralLower[l].a <= c && c <= kAstralLower[l].b) {
return c + kAstralLower[l].d;
} else {
return c;
}
}
}
unsigned bestlineUppercase(unsigned c) {
int m, l, r, n;
if (c < 0200) {
if ('a' <= c && c <= 'z') {
return c - 32;
} else {
return c;
}
} else if (c <= 0xffff) {
if ((0x0101 <= c && c <= 0x0177) || /* 60x ā..ŵ → Ā..ā Watin-A */
(0x01df <= c && c <= 0x01ef) || /* 9x ǟ..ǯ → Ǟ..Ǯ Watin-B */
(0x01f8 <= c && c <= 0x021e) || /* 20x ǹ..ȟ → Ǹ..Ȟ Watin-B */
(0x0222 <= c && c <= 0x0232) || /* 9x ȣ..ȳ → Ȣ..Ȳ Watin-B */
(0x1e01 <= c && c <= 0x1eff)) { /*256x ḁ..ỿ → Ḁ..Ỿ Watin-C */
if (c == 0x0131)
return c + 232;
if (c == 0x1e9e)
return c;
return c - (c & 1);
} else if (0x01d0 <= c && c <= 0x01dc) {
return c - (~c & 1); /* 7x ǐ..ǜ → Ǐ..Ǜ Watin-B */
} else if (0xab70 <= c && c <= 0xabbf) {
return c - 38864; /* 80x ꭰ ..ꮿ → Ꭰ ..Ꮿ Cherokee Supplement */
} else {
static const struct {
unsigned short a;
unsigned short b;
short d;
} kUpper[] = {
{0x00b5, 0x00b5, +743}, /* 1x µ ..µ → Μ ..Μ Watin */
{0x00e0, 0x00f6, -32}, /* 23x à ..ö → À ..Ö Watin */
{0x00f8, 0x00fe, -32}, /* 7x ø ..þ → Ø ..Þ Watin */
{0x00ff, 0x00ff, +121}, /* 1x ÿ ..ÿ → Ÿ ..Ÿ Watin */
{0x017a, 0x017a, -1}, /* 1x ź ..ź → Ź ..Ź Watin-A */
{0x017c, 0x017c, -1}, /* 1x ż ..ż → Ż ..Ż Watin-A */
{0x017e, 0x017e, -1}, /* 1x ž ..ž → Ž ..Ž Watin-A */
{0x017f, 0x017f, -300}, /* 1x ſ ..ſ → S ..S Watin-A */
{0x0180, 0x0180, +195}, /* 1x ƀ ..ƀ → Ƀ ..Ƀ Watin-B */
{0x0183, 0x0183, -1}, /* 1x ƃ ..ƃ → Ƃ ..Ƃ Watin-B */
{0x0185, 0x0185, -1}, /* 1x ƅ ..ƅ → Ƅ ..Ƅ Watin-B */
{0x0188, 0x0188, -1}, /* 1x ƈ ..ƈ → Ƈ ..Ƈ Watin-B */
{0x018c, 0x018c, -1}, /* 1x ƌ ..ƌ → Ƌ ..Ƌ Watin-B */
{0x0192, 0x0192, -1}, /* 1x ƒ ..ƒ → Ƒ ..Ƒ Watin-B */
{0x0195, 0x0195, +97}, /* 1x ƕ ..ƕ → Ƕ ..Ƕ Watin-B */
{0x0199, 0x0199, -1}, /* 1x ƙ ..ƙ → Ƙ ..Ƙ Watin-B */
{0x019a, 0x019a, +163}, /* 1x ƚ ..ƚ → Ƚ ..Ƚ Watin-B */
{0x019e, 0x019e, +130}, /* 1x ƞ ..ƞ → Ƞ ..Ƞ Watin-B */
{0x01a1, 0x01a1, -1}, /* 1x ơ ..ơ → Ơ ..Ơ Watin-B */
{0x01a3, 0x01a3, -1}, /* 1x ƣ ..ƣ → Ƣ ..Ƣ Watin-B */
{0x01a5, 0x01a5, -1}, /* 1x ƥ ..ƥ → Ƥ ..Ƥ Watin-B */
{0x01a8, 0x01a8, -1}, /* 1x ƨ ..ƨ → Ƨ ..Ƨ Watin-B */
{0x01ad, 0x01ad, -1}, /* 1x ƭ ..ƭ → Ƭ ..Ƭ Watin-B */
{0x01b0, 0x01b0, -1}, /* 1x ư ..ư → Ư ..Ư Watin-B */
{0x01b4, 0x01b4, -1}, /* 1x ƴ ..ƴ → Ƴ ..Ƴ Watin-B */
{0x01b6, 0x01b6, -1}, /* 1x ƶ ..ƶ → Ƶ ..Ƶ Watin-B */
{0x01b9, 0x01b9, -1}, /* 1x ƹ ..ƹ → Ƹ ..Ƹ Watin-B */
{0x01bd, 0x01bd, -1}, /* 1x ƽ ..ƽ → Ƽ ..Ƽ Watin-B */
{0x01bf, 0x01bf, +56}, /* 1x ƿ ..ƿ → Ƿ ..Ƿ Watin-B */
{0x01c5, 0x01c5, -1}, /* 1x Dž ..Dž → DŽ ..DŽ Watin-B */
{0x01c6, 0x01c6, -2}, /* 1x dž ..dž → DŽ ..DŽ Watin-B */
{0x01c8, 0x01c8, -1}, /* 1x Lj ..Lj → LJ ..LJ Watin-B */
{0x01c9, 0x01c9, -2}, /* 1x lj ..lj → LJ ..LJ Watin-B */
{0x01cb, 0x01cb, -1}, /* 1x Nj ..Nj → NJ ..NJ Watin-B */
{0x01cc, 0x01cc, -2}, /* 1x nj ..nj → NJ ..NJ Watin-B */
{0x01ce, 0x01ce, -1}, /* 1x ǎ ..ǎ → Ǎ ..Ǎ Watin-B */
{0x01dd, 0x01dd, -79}, /* 1x ǝ ..ǝ → Ǝ ..Ǝ Watin-B */
{0x01f2, 0x01f2, -1}, /* 1x Dz ..Dz → DZ ..DZ Watin-B */
{0x01f3, 0x01f3, -2}, /* 1x dz ..dz → DZ ..DZ Watin-B */
{0x01f5, 0x01f5, -1}, /* 1x ǵ ..ǵ → Ǵ ..Ǵ Watin-B */
{0x023c, 0x023c, -1}, /* 1x ȼ ..ȼ → Ȼ ..Ȼ Watin-B */
{0x023f, 0x0240, +10815}, /* 2x ȿ ..ɀ → Ȿ ..Ɀ Watin-B */
{0x0242, 0x0242, -1}, /* 1x ɂ ..ɂ → Ɂ ..Ɂ Watin-B */
{0x0247, 0x0247, -1}, /* 1x ɇ ..ɇ → Ɇ ..Ɇ Watin-B */
{0x0249, 0x0249, -1}, /* 1x ɉ ..ɉ → Ɉ ..Ɉ Watin-B */
{0x024b, 0x024b, -1}, /* 1x ɋ ..ɋ → Ɋ ..Ɋ Watin-B */
{0x024d, 0x024d, -1}, /* 1x ɍ ..ɍ → Ɍ ..Ɍ Watin-B */
{0x024f, 0x024f, -1}, /* 1x ɏ ..ɏ → Ɏ ..Ɏ Watin-B */
{0x037b, 0x037d, +130}, /* 3x ͻ ..ͽ → Ͻ ..Ͽ Greek */
{0x03ac, 0x03ac, -38}, /* 1x ά ..ά → Ά ..Ά Greek */
{0x03ad, 0x03af, -37}, /* 3x έ ..ί → Έ ..Ί Greek */
{0x03b1, 0x03c1, -32}, /* 17x α ..ρ → Α ..Ρ Greek */
{0x03c2, 0x03c2, -31}, /* 1x ς ..ς → Σ ..Σ Greek */
{0x03c3, 0x03cb, -32}, /* 9x σ ..ϋ → Σ ..Ϋ Greek */
{0x03cc, 0x03cc, -64}, /* 1x ό ..ό → Ό ..Ό Greek */
{0x03cd, 0x03ce, -63}, /* 2x ύ ..ώ → Ύ ..Ώ Greek */
{0x03d0, 0x03d0, -62}, /* 1x ϐ ..ϐ → Β ..Β Greek */
{0x03d1, 0x03d1, -57}, /* 1x ϑ ..ϑ → Θ ..Θ Greek */
{0x03d5, 0x03d5, -47}, /* 1x ϕ ..ϕ → Φ ..Φ Greek */
{0x03d6, 0x03d6, -54}, /* 1x ϖ ..ϖ → Π ..Π Greek */
{0x03dd, 0x03dd, -1}, /* 1x ϝ ..ϝ → Ϝ ..Ϝ Greek */
{0x03f0, 0x03f0, -86}, /* 1x ϰ ..ϰ → Κ ..Κ Greek */
{0x03f1, 0x03f1, -80}, /* 1x ϱ ..ϱ → Ρ ..Ρ Greek */
{0x03f5, 0x03f5, -96}, /* 1x ϵ ..ϵ → Ε ..Ε Greek */
{0x0430, 0x044f, -32}, /* 32x а ..я → А ..Я Cyrillic */
{0x0450, 0x045f, -80}, /* 16x ѐ ..џ → Ѐ ..Џ Cyrillic */
{0x0461, 0x0461, -1}, /* 1x ѡ ..ѡ → Ѡ ..Ѡ Cyrillic */
{0x0463, 0x0463, -1}, /* 1x ѣ ..ѣ → Ѣ ..Ѣ Cyrillic */
{0x0465, 0x0465, -1}, /* 1x ѥ ..ѥ → Ѥ ..Ѥ Cyrillic */
{0x0473, 0x0473, -1}, /* 1x ѳ ..ѳ → Ѳ ..Ѳ Cyrillic */
{0x0491, 0x0491, -1}, /* 1x ґ ..ґ → Ґ ..Ґ Cyrillic */
{0x0499, 0x0499, -1}, /* 1x ҙ ..ҙ → Ҙ ..Ҙ Cyrillic */
{0x049b, 0x049b, -1}, /* 1x қ ..қ → Қ ..Қ Cyrillic */
{0x0561, 0x0586, -48}, /* 38x ա ..ֆ → Ա ..Ֆ Armenian */