This file is indexed.

/usr/include/courier-unicode.h is in libcourier-unicode-dev 1.4-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
#ifndef	courier_unicode_h
#define	courier_unicode_h

/*
** Copyright 2000-2015 Double Precision, Inc.
** See COPYING for distribution information.
**
*/

#ifdef	__cplusplus

#include <string>
#include <vector>
#include <list>

extern "C" {
#endif

#if 0
}
#endif

#include	<stdlib.h>
#include	<stdio.h>
#include	<stdint.h>
#include	<sys/types.h>
#include	<locale.h>

typedef uint32_t unicode_char;

/*
** The system default character set, from the locale.
*/

extern const char *unicode_default_chset();

/*
** The current locale character set.
*/

extern const char *unicode_locale_chset();

#if 1
extern const char *unicode_locale_chset_l(locale_t l);
#endif

/* Unicode upper/lower/title case conversion functions */

extern unicode_char unicode_uc(unicode_char);
extern unicode_char unicode_lc(unicode_char);
extern unicode_char unicode_tc(unicode_char);

/*
** Look up HTML 4.0/XHTML entity.
**
** n="amp", etc...
**
** Returns the unicode entity value, or 0 if no such entity is defined.
*/

unicode_char unicode_html40ent_lookup(const char *n);

/*
**
** Return "width" of unicode character.
**
** This is defined as follows: for characters having the F or W property in
** tr11 (EastAsianWidth), unicode_wcwidth() returns 2.
**
** Otherwise, characters having the BK, CR, LF, CM, NL, WJ, and ZW line
** breaking property as per tr14, unicode_wcwdith() returns 0. For all other
** cases, 1.
**
** This provides a rough estimate of the "width" of the character if its
** shown on a text console.
*/

extern int unicode_wcwidth(unicode_char c);
extern size_t unicode_wcwidth_str(const unicode_char *c);

/* Internal unicode table lookup functions */

extern uint8_t unicode_tab_lookup(unicode_char ch,
				  const size_t *unicode_indextab,
				  size_t unicode_indextab_sizeof,
				  const uint8_t (*unicode_rangetab)[2],
				  const uint8_t *unicode_classtab,
				  uint8_t uclass);

extern uint32_t unicode_tab32_lookup(unicode_char ch,
				     const size_t *unicode_indextab,
				     size_t unicode_indextab_sizeof,
				     const uint8_t (*unicode_rangetab)[2],
				     const uint32_t *unicode_classtab,
				     uint32_t uclass);

/*
** Look up unicode categorization, see http://unicode.org/notes/tn36/
**
** Returns a 32 bit value with four unicode categories encoded in the
** bits defined by UNICODE_CATEGORY_1..4
*/

#define UNICODE_CATEGORY_1   0xFF000000
#define UNICODE_CATEGORY_2   0x00FF0000
#define UNICODE_CATEGORY_3   0x0000FF00
#define UNICODE_CATEGORY_4   0x000000FF

#include <courier-unicode-categories-tab.h>

uint32_t unicode_category_lookup(unicode_char);

/*
** Return non-0 for TAB, and all UNICODE_CATEGORY_2_SPACE.
*/

extern int unicode_isblank(unicode_char ch);

/*
** The unicode-ish isspace(). In addition to return non-0 for
** unicode_isblank(), this also returns non-0 for unicode characters
** with linebreaking properties of BK, CR, LF, NL, and SP.
*/
extern int unicode_isspace(unicode_char ch);

/*
** Return non-0 for all UNICODE_CATEGORY_1_LETTER
*/

extern int unicode_isalpha(unicode_char ch);

/*
** Return non-0 for all UNICODE_CATEGORY_1_NUMBER | UNICODE_CATEGORY_2_DIGIT,
** only (no third categories).
*/
extern int unicode_isdigit(unicode_char ch);

/*
** Return non-0 for all unicode_isalpha() or unicode_isdigit().
*/

extern int unicode_isalnum(unicode_char ch);

/*
** Returns non-0 for all codepoints above SPACE which are not
** unicode_isspace().
*/

extern int unicode_isgraph(unicode_char ch);

/*
** Return non-0 for all UNICODE_CATEGORY_1_PUNCTUATION.
*/

extern int unicode_ispunct(unicode_char ch);

/*
** Return non-0 for all unicode_isalpha() for which the character is
** equal to unicode_lc() of itself.
*/
extern int unicode_islower(unicode_char ch);

/*
** Return non-0 for all unicode_isalpha() for which the character is
** equal to unicode_uc() of itself.
*/
extern int unicode_isupper(unicode_char ch);

/*
** Implementation of grapheme cluster boundary rules, as per
** http://www.unicode.org/reports/tr29/tr29-27.html
** including  GB9a and GB9b.
**
** Returns non-zero if there's a grapheme break between the two referenced
** characters.
*/

int unicode_grapheme_break(unicode_char a, unicode_char b);

typedef enum {

#include <courier-unicode-script-tab.h>

} unicode_script_t;

/*
** Look up the unicode script property, as per
** http://www.unicode.org/reports/tr24/tr24-24.html
*/

unicode_script_t unicode_script(unicode_char a);

/*
** Implementation of line break rules, as per
** http://www.unicode.org/reports/tr14/tr14-35.html
**
** Invoke unicode_lb_init() to initialize the linebreaking algorithm. The
** first parameter is a callback function that gets invoked with two
** arguments: UNICODE_LB_{MANDATORY|NONE|ALLOWED}, and a passthrough argument.
** The second parameter to unicode_lb_init() is the opaque passthrough
** pointer, that is passed as the second argument to the callback function
** with no further interpretation.
**
** unicode_lb_init() returns an opaque handle. Invoke unicode_lb_next(),
** passing the handle and one unicode character. Repeatedly invoke
** unicode_lb_next() to specify the input string for the linebreaking
** algorithm, then invoke unicode_lb_end() to finish calculating the
** linebreaking algorithm, and deallocate the opaque linebreaking handle.
**
** The callback function gets invoked once for each invocation of
** unicode_lb_next(). The contract is that before unicode_lb_end() returns,
** the callback function will get invoked the exact number of times that
** unicode_lb_next(), as long as each invocation of the callback function
** returned 0; nothing more, nothing less. The first parameter to the callback
** function will be one of the following values:
**
** UNICODE_LB_MANDATORY - a linebreak is MANDATORY before the corresponding
** character.
** UNICODE_LB_NONE - a linebreak is PROHIBITED before the corresponding
** character.
** UNICODE_LB_ALLOWED - a linebreak is OPTIONAL before the corresponding
** character (the preceding character is a space, or an equivalent).
**
** The callback function should return 0. A non-zero value indicates an
** error, which gets propagated up to the caller. The contract that the
** callback function gets invoked the same number of times that
** unicode_lb_next() gets invoked is now broken.
*/

#define UNICODE_LB_MANDATORY	-1
#define UNICODE_LB_NONE		0
#define UNICODE_LB_ALLOWED	1

struct unicode_lb_info;

typedef struct unicode_lb_info *unicode_lb_info_t;

/*
** Allocate a linebreaking handle.
*/
extern unicode_lb_info_t unicode_lb_init(int (*cb_func)(int, void *),
					 void *cb_arg);

/*
** Feed the next character through the linebreaking algorithm.
** A non-zero return code indicates that the callback function was invoked
** and it returned a non-zero return code (which is propagated as a return
** value). unicode_lb_end() must still be invoked, in this case.
**
** A zero return code indicates that if the callback function was invoked,
** it returned 0.
*/

extern int unicode_lb_next(unicode_lb_info_t i, unicode_char ch);

/*
** Convenience function that invokes unicode_lb_next() with a list of
** unicode chars. Returns 0 if all invocations of unicode_lb_next() returned
** 0, or the first non-zero return value from unicode_lb_next().
*/

extern int unicode_lb_next_cnt(unicode_lb_info_t i,
			       const unicode_char *chars,
			       size_t cnt);

/*
** Finish the linebreaking algorithm.
**
** A non-zero return code indicates that the callback function was invoked
** and it returned a non-zero return code (which is propagated as a return
** value).
**
** A zero return code indicates that if the callback function was invoked,
** it returned 0, and that the callback function was invoked exactly the same
** number of times that unicode_lb_next() was invoked.
**
** In all case, the linebreak handle will no longer be valid when this
** function returns.
*/

extern int unicode_lb_end(unicode_lb_info_t i);

/*
** An alternative linebreak API where the callback function receives the
** original unicode character in addition to its linebreak value.
**
** User unicode_lbc_init(), unicode_lbc_next(), and unicode_lbc_end(), whose
** semantics are the same as their _lb_ counterparts.
*/

struct unicode_lbc_info;

typedef struct unicode_lbc_info *unicode_lbc_info_t;

extern unicode_lbc_info_t unicode_lbc_init(int (*cb_func)(int, unicode_char,
							  void *),
					   void *cb_arg);
extern int unicode_lbc_next(unicode_lbc_info_t i, unicode_char ch);
extern int unicode_lbc_next_cnt(unicode_lbc_info_t i,
				const unicode_char *chars,
				size_t cnt);
extern int unicode_lbc_end(unicode_lbc_info_t i);

/*
** Set linebreaking options.
**
** OPTIONS SUBJECT TO CHANGE.
*/

extern void unicode_lb_set_opts(unicode_lb_info_t i, int opts);

extern void unicode_lbc_set_opts(unicode_lbc_info_t i, int opts);

/*
** Tailorization of LB24: Prevent pluses, as in "C++", from breaking.
**
** Adds the following to LB24:
**
**            PR x PR
**
**            AL x PR
**
**            ID x PR
**/
#define UNICODE_LB_OPT_PRBREAK 0x0001


/*
** Tailored "/" breaking rules. This prevents breaking after the "/"
** character. And provides an exception to the "x SY" rule in LB13.
**
** Adds the following rule to LB13:
**
**            SY x EX
**
**            SY x AL
**
**            SY x ID
**
**            SP รท SY, which takes precedence over "x SY".
*/
#define UNICODE_LB_OPT_SYBREAK 0x0002

/*
** Tailored / breaking rules.
**
** This reclassifies U+2013 and U+2014 as class WJ, prohibiting breaks before
** and after mdash and ndash.
*/
#define UNICODE_LB_OPT_DASHWJ 0x0004

/*
** Implemention of word break rules, as per
** http://www.unicode.org/reports/tr29/tr29-27.html
**
** Invoke unicode_wb_init() to initialize the wordbreaking algorithm. The
** first parameter is a callback function that gets invoked with two
** arguments: an int flag, and a passthrough argument. The second parameter to
** unicode_wb_init() is the opaque passthrough pointer, that is passed as the
** second argument to the callback function with no further interpretation.
**
** unicode_wb_init() returns an opaque handle. Invoke unicode_wb_next(),
** passing the handle and one unicode character. Repeatedly invoke
** unicode_wb_next() to specify the input string for the wordbreaking
** algorithm, then invoke unicode_wb_end() to finish calculating the
** wordbreaking algorithm, and deallocate the opaque wordbreaking handle.
**
** The callback function gets invoked once for each invocation of
** unicode_wb_next(). The contract is that before unicode_wb_end() returns,
** the callback function will get invoked the exact number of times that
** unicode_wb_next(), as long as each invocation of the callback function
** returned 0; nothing more, nothing less. The first parameter to the callback
** function will be an int. A non-zero value indicates that there is a word
** break between this character and the preceding one.
**
** The callback function should return 0. A non-zero value indicates an
** error, which gets propagated up to the caller. The contract that the
** callback function gets invoked the same number of times that
** unicode_lb_next() gets invoked is now broken.
*/

struct unicode_wb_info;

typedef struct unicode_wb_info *unicode_wb_info_t;

/*
** Allocate a wordbreaking handle.
*/
extern unicode_wb_info_t unicode_wb_init(int (*cb_func)(int, void *),
					 void *cb_arg);

/*
** Feed the next character through the wordbreaking algorithm.
** A non-zero return code indicates that the callback function was invoked
** and it returned a non-zero return code (which is propagated as a return
** value). unicode_wb_end() must still be invoked, in this case.
**
** A zero return code indicates that if the callback function was invoked,
** it returned 0.
*/

extern int unicode_wb_next(unicode_wb_info_t i, unicode_char ch);

/*
** Convenience function that invokes unicode_wb_next() with a list of
** unicode chars. Returns 0 if all invocations of unicode_wb_next() returned
** 0, or the first non-zero return value from unicode_wb_next().
*/

extern int unicode_wb_next_cnt(unicode_wb_info_t i,
			       const unicode_char *chars,
			       size_t cnt);

/*
** Finish the wordbreaking algorithm.
**
** A non-zero return code indicates that the callback function was invoked
** and it returned a non-zero return code (which is propagated as a return
** value).
**
** A zero return code indicates that if the callback function was invoked,
** it returned 0, and that the callback function was invoked exactly the same
** number of times that unicode_wb_next() was invoked.
**
** In all case, the wordbreak handle will no longer be valid when this
** function returns.
*/

extern int unicode_wb_end(unicode_wb_info_t i);

/*
** Search for a word boundary.
**
** Obtain a handle by calling unicode_wbscan_init(), then invoke
** unicode_wbscan_next() to provide a unicode stream, then invoke
** unicode_wbscan_end(). unicode_wbscan_end() returns the number of unicode
** characters from the beginning of the stream until the first word boundary.
**
** You may prematurely stop calling unicode_wbscan_next() once it returns a
** non-0 value, which means that there is sufficient context to compute the
** first word boundary, and all further calls to unicode_wbscan_next() will
** be internal no-ops.
*/

struct unicode_wbscan_info;

typedef struct unicode_wbscan_info *unicode_wbscan_info_t;

unicode_wbscan_info_t unicode_wbscan_init();

int unicode_wbscan_next(unicode_wbscan_info_t i, unicode_char ch);

size_t unicode_wbscan_end(unicode_wbscan_info_t i);

/*
** A buffer that holds unicode characters, and dynamically grows as needed.
*/

struct unicode_buf {
	unicode_char *ptr;	/* The unicode characters */
	size_t size,		/* Buffer size */
		len,		/* How many characters in ptr are initialized */
		max;		/* Maximum size the buffer can grow to */
};

/*
** Initialize a buffer. Constructor.
*/

void unicode_buf_init(/* Initialize this structure. ptr, size, len cleared */
		      struct unicode_buf *p,

		      /*
		      ** Maximum size the buffer can grow to. (size_t)-1
		      ** means unlimited.
		      */
		      size_t max);
/*
** Like unicode_buf_init, and initialize the new buffer with the contents of
** another buffer. The maximum size of the initialized buffer is exactly the
** number of characters in the existing buffer. This copies a buffer using
** the minimum amount of heap space.
*/

#define unicode_buf_init_copy(a,b)				\
	do {							\
		unicode_buf_init((a), unicode_buf_len(b));	\
		unicode_buf_append_buf((a),(b));		\
	} while (0)

/*
** Deinitialize the buffer. Destructor. Frees memory.
*/

void unicode_buf_deinit(struct unicode_buf *p);

/*
** Official way to access the characters in the unicode buffer.
*/
#define unicode_buf_ptr(p) ((p)->ptr)

/*
** Official way of obtaining the number of characters in the unicode buffer.
*/
#define unicode_buf_len(p) ((p)->len)

/*
** Remove all existing characters from an initialized buffer. Sets len to 0.
*/

#define unicode_buf_clear(p) ((p)->len=0)

/*
** Append characters to the existing characters in the unicode buffer.
** The buffer grows, if needed. If the buffer would exceed its maximum size,
** the extra characters get truncated.
**
** Returns 0 if the characters were appended. -1 for a malloc failure.
*/

int unicode_buf_append(struct unicode_buf *p,	/* The buffer */
		       const unicode_char *uc,	/* Characters to append */
		       size_t l);		/* How many of them */

/*
** Convert an iso-8859-1 char string and invoke unicode_buf_append().
*/

void unicode_buf_append_char(struct unicode_buf *dst,
			     const char *str,
			     size_t cnt);

/*
** Remove some portion of the unicode buffer
*/

void unicode_buf_remove(struct unicode_buf *p, /* The buffer */
			size_t pos, /* Offset in buffer */
			size_t cnt); /* How many to remove */

/*
** Append the contents of an existing buffer to another one.
*/

#define unicode_buf_append_buf(a,b)					\
	unicode_buf_append((a), unicode_buf_ptr(b), unicode_buf_len(b))


/*
** The equivalent of strcmp() for unicode buffers.
*/

int unicode_buf_cmp(const struct unicode_buf *a,
		    const struct unicode_buf *b);

/*
** The equivalent of unicode_buf_cmp, except that the second buffer is an
** iso-8859-1 string.
*/

int unicode_buf_cmp_str(const struct unicode_buf *p,
			const char *c,	/* iso-8859-1 string */
			size_t cl);	/* Number of chars in c */

/*
** A wrapper for iconv(3). This wrapper provides a different API for iconv(3).
** A handle gets created by unicode_convert_init().
** unicode_convert_init() receives a pointer to the output function
** which receives converted character text.
**
** The output function receives a pointer to the converted character text, and
** the number of characters in the converted text.
**
** The character text to convert gets passed, repeatedly, to
** unicode_convert(). Each call to unicode_convert() results in
** the output function being invoked, zero or more times, with the converted
** text. Finally, unicode_convert_deinit() stops the conversion and
** deallocates the conversion handle.
**
** Internal buffering takes place. unicode_convert_deinit() may result
** in the output function being called one or more times, to receive the final
** part of the converted character stream.
**
** The output function should return 0. A non-0 value causes
** unicode_convert() and/or unicode_convert_deinit() returning
** non-0.
*/

struct unicode_convert_hdr;

typedef struct unicode_convert_hdr *unicode_convert_handle_t;

/*
** unicode_convert_init() returns a non-NULL handle for the requested
** conversion, or NULL if the requested conversion is not available.
*/

unicode_convert_handle_t
unicode_convert_init(/* Convert from this chset */
		       const char *src_chset,

		       /* Convert to this chset */
		       const char *dst_chset,

		       /* The output function */

		       int (*output_func)(const char *, size_t, void *),

		       /* Passthrough arg */
		       void *convert_arg);

/*
** Repeatedly pass the character text to convert to unicode_convert().
**
** Returns non-0 if the output function returned non-0, or 0 if all invocations
** of the output function returned 0.
*/

int unicode_convert(/* The conversion handle */
		      unicode_convert_handle_t handle,

		      /* Text to convert */
		      const char *text,

		      /* Number of bytes to convert */
		      size_t cnt);

/*
** Finish character set conversion. The handle gets deallocated.
**
** May still result in one or more invocations of the output function.
** Returns non-zero if any previous invocation of the output function returned
** non-zero (this includes any invocations of the output function resulting
** from this call, or prior unicode_convert() calls), or 0 if all
** invocations of the output function returned 0.
**
** If the errptr is not NULL, *errptr is set to non-zero if there were any
** conversion errors -- if there was any text that could not be converted to
** the destination character text.
*/

int unicode_convert_deinit(unicode_convert_handle_t handle,
			     int *errptr);


/*
** Specialization: save converted character text in a buffer.
**
** Implementation: call unicode_convert_tocbuf_init() instead of
** unicode_convert_init(), then call unicode_convert() and
** unicode_convert_deinit(), as usual.
**
** If unicode_convert_deinit() returns 0, *cbufptr_ret gets initialized to a
** malloc()ed buffer, and the number of converted characters, the size of the
** malloc()ed buffer, are placed into *csize_ret arguments, that were passed
** to unicode_convert_tou_init().
**
** Note: if the converted string is an empty string, *cbufsize_ret is set to 0,
** but *cbufptr_ptr still gets initialized (to a dummy malloced buffer).
**
** The optional nullterminate places a trailing \0 character after the
** converted string (this is included in *cbufsize_ret).
*/

unicode_convert_handle_t
unicode_convert_tocbuf_init(/* Convert from this chset */
			      const char *src_chset,

			      /* Convert to this chset */
			      const char *dst_chset,

			      /* malloced buffer */
			      char **cbufptr_ret,

			      /* size of the malloced buffer */
			      size_t *cbufsize_ret,

			      /* null terminate the resulting string */
			      int nullterminate
			      );


/*
** Specialization: convert some character text to a unicode_char array.
**
** This is like unicode_convert_tocbuf_init(), but converts to a unicode_char
** array.
**
** The returned *ucsize_ret is initialized with the number of unicode_chars,
** rather than the byte count.
**
** In all other ways, this function behaves identically to
** unicode_convert_tocbuf_init().
*/

unicode_convert_handle_t
unicode_convert_tou_init(/* Convert from this chset */
			   const char *src_chset,

			   /* malloc()ed buffer pointer, on exit. */
			   unicode_char **ucptr_ret,

			   /* size of the malloc()ed buffer, upon exit */
			   size_t *ucsize_ret,

			   /* If true, terminate with U+0x0000, for convenience */
			   int nullterminate
			   );

/*
** Specialization: convert a unicode_char array to some character text.
**
** This is the opposite of unicode_convert_tou_init(). Call this to
** initialize the conversion handle, then use unicode_convert_uc()
** instead of unicode_convert.
*/

unicode_convert_handle_t
unicode_convert_fromu_init(/* Convert to this chset */
			     const char *dst_chset,

			     /* malloc()ed buffer pointer, on exit. */
			     char **cbufptr_ret,

			     /* size of the malloc()ed buffer, upon exit */
			     size_t *cbufsize_ret,

			     /* If true, terminate with U+0x0000, for convenience */
			     int nullterminate
			     );

int unicode_convert_uc(/* The conversion handle */
			 unicode_convert_handle_t handle,

			 /* Text to convert */
			 const unicode_char *text,

			 /* Number of bytes to convert */
			 size_t cnt);

/*
** Initialize conversion to UTF-8.
**
** This is a wrapper for unicode_convert_tocbuf_init() that specifies the
** destination charset as UTF-8.
*/

unicode_convert_handle_t
unicode_convert_tocbuf_toutf8_init(const char *src_chset,
				     char **cbufptr_ret,
				     size_t *cbufsize_ret,
				     int nullterminate);

/*
** Initialize conversion from UTF-8.
**
** This is a wrapper for unicode_convert_tocbuf_init() that specifies the
** source charset as UTF-8.
*/

unicode_convert_handle_t
unicode_convert_tocbuf_fromutf8_init(const char *dst_chset,
				       char **cbufptr_ret,
				       size_t *cbufsize_ret,
				       int nullterminate);

/*
** Convert a character string to UTF-8.
**
** Returns a malloc-ed buffer holding the UTF-8 string, or NULL if an
** error occured.
*/
char *unicode_convert_toutf8(/* Text to convert to UTF-8 */
			       const char *text,

			       /* Character set to convert to UTF-8 */
			       const char *charset,

			       /*
			       ** If non-NULL, and a non-NULL pointer is
			       ** returned, *error is set to non-zero if
			       ** a character conversion error has occured.
			       */
			       int *error);

/*
** Convert UTF-8 text to another character set.
**
** Returns a malloc-ed buffer holding the string converted to the specified
** character set, or NULL if an error occured.
*/

char *unicode_convert_fromutf8(/* A UTF-8 string */
				 const char *text,

				 /*
				 ** Convert the UTF-8 string to this character
				 ** set.
				 */

				 const char *charset,

				 /*
				 ** If non-NULL, and a non-NULL pointer is
				 ** returned, *error is set to non-zero if
				 ** a character conversion error has occured.
				 */
				 int *error);

/*
** Convert one charset to another charset, placing the result in a malloc-ed
** buffer.
**
** Returns a malloc-ed buffer holding the string converted to the specified
** character set, or NULL if an error occured.
*/

char *unicode_convert_tobuf(/* A string to convert */
			      const char *text,

			      /*
			      ** String's charset.
			      */

			      const char *charset,

			      /*
			      ** Destination charset
			      */
			      const char *dstcharset,

			      /*
			      ** If non-NULL, and a non-NULL pointer is
			      ** returned, *error is set to non-zero if
			      ** a character conversion error has occured.
			      */
			      int *error);

/*
** Convenience function: call unicode_convert_tou_init(), feed the
** character string through unicode_convert(), then call
** unicode_convert_deinit().
**
** If this function returns 0, *uc and *ucsize is set to a malloced buffer+size
** holding the unicode char array.
*/

int unicode_convert_tou_tobuf(/* Character text to convert */
				const char *text,

				/* Number of characters */
				size_t text_l,

				/* text's charset */
				const char *charset,

				/*
				** If this function returns 0, this gets
				** initialized
				*/
				unicode_char **uc,

				/*
				** Size of the allocated buffer
				*/
				size_t *ucsize,

				/*
				** If not null and this function returns 0,
				** this is set to non-0 if there
				** was a conversion error (but the output
				** buffer gets still allocated and
				** initialized)
				*/
				int *err);

/*
** Convenience function: call unicode_convert_fromu_init(), feed the
** unicode_array through unicode_convert_uc(), then call
** unicode_convert_deinit().
**
** If this function returns 0, *uc and *ucsize is set to a malloced buffer+size
** holding the converted character string
*/

int unicode_convert_fromu_tobuf(/* Unicode array to convert to a char str */
				  const unicode_char *utext,

				  /*
				  ** Size of the unicode array.
				  ** If this is (size_t)-1, utext is a
				  ** 0-terminated array.
				  */
				  size_t utext_l,

				  /*
				  ** Convert the unicode array to this charset.
				  */
				  const char *charset,

				  /*
				  ** If unicode_convert_fromu_tobuf()
				  ** returns 0, this is initialized to a
				  ** malloced buffer with a 0-terminated
				  ** string is kept.
				  */
				  char **c,

				  /*
				  ** Size of the initialized array, including
				  ** the 0-terminator.
				  */
				  size_t *csize,

				  /*
				  ** If unicode_convert_fromu_tobuf()
				  ** returns 0 and this is not NULL,
				  ** *err is set to non-0 if there was a
				  ** conversion error to the requested
				  ** character set.
				  */
				  int *err);

/*
** Convenience function: convert a string in a given character set
** to/from uppercase, lowercase, or something else.
**
** This is done by calling unicode_convert_tou_tobuf() first,
** applying the title_func and char_func, then using
** unicode_convert_fromu_tobuf().
**
** A NULL return indicates that the requested conversion cannot be performed.
*/

char *unicode_convert_tocase( /* String to convert */
			       const char *str,

			       /* String's character set */

			       const char *charset,

			       /*
			       ** Conversion of the first character in
			       ** str: unicode_uc, unicode_lc, or unicode_tc:
			       */

			       unicode_char (*first_char_func)(unicode_char),

			       /*
			       ** Conversion of the second and the remaining
			       ** character in str. If NULL, same as
			       ** first_char_func.
			       */
			       unicode_char (*char_func)(unicode_char));



/* Either UCS-4BE or UCS-4LE, matching the native unicode_char endianness */

extern const char unicode_u_ucs4_native[];

/* Either UCS-2BE or UCS-2LE, matching the native unicode_char endianness */

extern const char unicode_u_ucs2_native[];

/*
** Modified-UTF7 encoding used for IMAP folder names. Pass it for a charset
** parameter.
**
** This can be followed by a " " and up to 15 characters to be escaped in
** addition to unicode chars.
*/

#define unicode_x_imap_modutf7 "x-imap-modutf7"

#if 0
{
#endif

#ifdef	__cplusplus
}

extern size_t unicode_wcwidth(const std::vector<unicode_char> &uc);

namespace unicode {

#if 0
};
#endif

/*
** Various character sets
*/
extern const char ucs_4[], ucs_2[], utf_8[], iso_8859_1[];

/*
** Interface to iconv.
**
** Subclass converted(). Invoke begin(), then operator(), repeatedly,
** then end().
**
** converted() receives the converted text.
*/

class iconvert {

	unicode_convert_handle_t handle;

 public:
	iconvert();
	virtual ~iconvert();

	/* Start conversion.
	** Returns false if the requested conversion cannot be done.
	**/

	bool begin(/* Convert from */
		   const std::string &src_chset,

		   /* Convert to */
		   const std::string &dst_chset);

	/* Feed iconv(3). Returns false if the conversion was aborted.
	 */

	bool operator()(const char *, size_t);

	bool operator()(const unicode_char *, size_t);

	/*
	** Get the results here. If the subclass returns a non-0
	** value, the conversion is aborted.
	*/

	virtual int converted(const char *, size_t);

	/*
	** End of conversion.
	**
	** Returns true if all calls to converted() returned 0,
	** false if the conversion was aborted.
	**
	** errflag is set to true if there was a character that could
	** not be converted, and passed to converted().
	*/

	bool end(bool &errflag)
	{
		return end(&errflag);
	}

	bool end()
	{
		return end(NULL);
	}

	/* Convert between two different charsets */

	static std::string convert(const std::string &text,
				   const std::string &charset,
				   const std::string &dstcharset,
				   bool &errflag);

	/* Convert between two different charsets */

	static std::string convert(const std::string &text,
				   const std::string &charset,
				   const std::string &dstcharset)
	{
		bool dummy;

		return convert(text, charset, dstcharset, dummy);
	}

	/* Convert from unicode to a charset */

	static std::string convert(const std::vector<unicode_char> &uc,
				   const std::string &dstcharset,
				   bool &errflag);

	/* Convert from unicode to a charset */

	static std::string convert(const std::vector<unicode_char> &uc,
				   const std::string &dstcharset)
	{
		bool dummy;

		return convert(uc, dstcharset, dummy);
	}

	/* Convert charset to unicode */

	static bool convert(const std::string &text,
			    const std::string &charset,
			    std::vector<unicode_char> &uc);


	/* Convert to upper/lower/title case */

	static std::string
		convert_tocase(/* Text string */
			       const std::string &text,

			       /* Its charset */
			       const std::string &charset,

			       /* First character: unicode_uc, unicode_lc, or unicode_tc */
			       unicode_char (*first_char_func)(unicode_char),

			       /* If not NULL, second and subsequent chars */
			       unicode_char (*char_func)(unicode_char)
			       =NULL)
	{
		bool dummy;

		return convert_tocase(text, charset, dummy,
				      first_char_func,
				      char_func);
	}

	/* Convert to upper/lower/title case */

	static std::string
		convert_tocase(/* Text string */
			       const std::string &text,

			       /* Its charset */
			       const std::string &charset,

			       /* Set if there's a conversion error */
			       bool &err,

			       /* First character: unicode_uc, unicode_lc, or unicode_tc */
			       unicode_char (*first_char_func)(unicode_char),

			       /* If not NULL, second and subsequent chars */
			       unicode_char (*char_func)(unicode_char)
			       =NULL);
 private:
	bool end(bool *);

 public:
	class tou;
	class fromu;
};

/* Convert output of iconvert to unicode_chars. */

class iconvert::tou : public iconvert {

 public:
	bool begin(const std::string &chset);

	virtual int converted(const unicode_char *, size_t);

	using iconvert::operator();
 private:
	int converted(const char *ptr, size_t cnt);

 public:
	template<typename iter_t> class to_iter_class;

	template<typename input_iter_t,
		typename output_iter_t>
		static output_iter_t convert(input_iter_t from_iter,
					     input_iter_t to_iter,
					     const std::string &chset,
					     bool &flag,
					     output_iter_t out_iter);

	template<typename input_iter_t>
		static bool convert(input_iter_t from_iter,
				    input_iter_t to_iter,
				    const std::string &chset,
				    std::vector<unicode_char> &out_buf)
	{
		bool flag;

		out_buf.clear();
		std::back_insert_iterator<std::vector<unicode_char> >
			insert_iter(out_buf);

		convert(from_iter, to_iter, chset, flag, insert_iter);

		return flag;
	}

	static std::pair<std::vector<unicode_char>, bool>
		convert(const std::string &str,
			const std::string &chset);
};

/* Helper class that saves unicode output into an output iterator */

template<typename iter_t>
class iconvert::tou::to_iter_class : public iconvert::tou {

	iter_t iter;
 public:

 to_iter_class(iter_t iterValue)
	 : iter(iterValue) {}

	using tou::operator();

	operator iter_t() const { return iter; }

 private:
	int converted(const unicode_char *ptr, size_t cnt)
	{
		while (cnt)
		{
			*iter=*ptr;

			++iter;
			++ptr;
			--cnt;
		}
		return 0;
	}
};

template<typename input_iter_t,
	typename output_iter_t>
	output_iter_t iconvert::tou::convert(input_iter_t from_iter,
					     input_iter_t to_iter,
					     const std::string &chset,
					     bool &flag,
					     output_iter_t out_iter)
{
	class to_iter_class<output_iter_t> out(out_iter);

	if (!out.begin(chset))
		return out;

	std::vector<char> string;

	while (from_iter != to_iter)
	{
		string.push_back(*from_iter++);

		if (string.size() > 31)
		{
			out(&string[0], string.size());
			string.clear();
		}
	}

	if (string.size() > 0)
		out(&string[0], string.size());

	out.end(flag);
	return out;
}

/* Convert output of iconvert from unicode_chars. */

class iconvert::fromu : public iconvert {

 public:
	bool begin(const std::string &chset);

	using iconvert::operator();

	template<typename iter_t> class to_iter_class;

	template<typename input_iter_t,
		typename output_iter_t>
		static output_iter_t convert(input_iter_t from_iter,
					     input_iter_t to_iter,
					     const std::string &chset,
					     output_iter_t out_iter,
					     bool &errflag);

	template<typename input_iter_t>
		static void convert(input_iter_t from_iter,
				    input_iter_t to_iter,
				    const std::string &chset,
				    std::string &out_buf,
				    bool &errflag)
	{
		out_buf="";
		std::back_insert_iterator<std::string>
			insert_iter(out_buf);

		convert(from_iter, to_iter, chset, insert_iter,
			errflag);
	}

	static std::pair<std::string, bool>
		convert(const std::vector<unicode_char> &ubuf,
			const std::string &chset);
};

/* Helper class that saves unicode output into an output iterator */

template<typename iter_t>
class iconvert::fromu::to_iter_class : public iconvert::fromu {

	iter_t iter;
 public:

 to_iter_class(iter_t iterValue)
	 : iter(iterValue) {}

	using fromu::operator();

	operator iter_t() const { return iter; }

 private:
	int converted(const char *ptr, size_t cnt)
	{
		while (cnt)
		{
			*iter=*ptr;

			++iter;
			++ptr;
			--cnt;
		}
		return 0;
	}
};

template<typename input_iter_t,
	typename output_iter_t>
	output_iter_t iconvert::fromu::convert(input_iter_t from_iter,
					       input_iter_t to_iter,
					       const std::string &chset,
					       output_iter_t out_iter,
					       bool &errflag)
{
	errflag=true;

	class to_iter_class<output_iter_t> out(out_iter);

	if (!out.begin(chset))
		return out;

	std::vector<unicode_char> string;

	while (from_iter != to_iter)
	{
		string.push_back(*from_iter++);

		if (string.size() > 31)
		{
			out(&string[0], string.size());
			string.clear();
		}
	}

	if (string.size() > 0)
		out(&string[0], string.size());

	out.end(errflag);
	return out;
}

/*
** Unicode linebreaking algorithm, tr14.
*/

extern "C" int linebreak_trampoline(int value, void *ptr);
extern "C" int linebreakc_trampoline(int value, unicode_char ch,
				     void *ptr);

/*
** Subclass linebreak_callback_base, implement operator()(int).
**
** Use operator<< or operator()(iterator, iterator) to feed
** unicode_chars into the linebreaking algorithm. The subclass receives
** UNICODE_LB values, as they become available.
*/

class linebreak_callback_base {

	unicode_lb_info_t handle;

	int opts;

#if __cplusplus >= 201103L
 public:
	linebreak_callback_base(const linebreak_callback_base &)=delete;
	linebreak_callback_base &operator=(const
					   linebreak_callback_base &)=delete;
 private:
#else
	linebreak_callback_base(const linebreak_callback_base &);
	/* NOT IMPLEMENTED */

	linebreak_callback_base &operator=(const
					   linebreak_callback_base &);
	/* NOT IMPLEMENTED */
#endif
 public:
	linebreak_callback_base();
	virtual ~linebreak_callback_base();

	void finish();

	void set_opts(int opts);

	friend int linebreak_trampoline(int, void *);

	linebreak_callback_base &operator<<(unicode_char uc);

	template<typename iter_type>
		linebreak_callback_base &operator()(iter_type beg_iter,
						    iter_type end_iter)
	{
		while (beg_iter != end_iter)
			operator<<(*beg_iter++);
		return *this;
	}

	template<typename container_type>
		linebreak_callback_base &operator()(const container_type &vec)
	{
		return operator()(vec.begin(), vec.end());
	}
 private:
	virtual int callback(int);
};

class linebreak_callback_save_buf : public linebreak_callback_base {

 public:
	std::list<int> lb_buf;

	linebreak_callback_save_buf();
	~linebreak_callback_save_buf();

	using linebreak_callback_base::operator<<;
	using linebreak_callback_base::operator();
 private:
	int callback(int value);
};

/*
** Convert an input iterator sequence over unicode_chars into
** an input iterator sequence over linebreak values.
*/

template<typename input_t> class linebreak_iter
: public std::iterator<std::input_iterator_tag, int, void>
{
	mutable input_t iter_value, end_iter_value;

	mutable linebreak_callback_save_buf *buf;

	void fill() const
	{
		if (buf == NULL)
			return;

		while (buf->lb_buf.empty())
		{
			if (iter_value == end_iter_value)
			{
				buf->finish();
				if (buf->lb_buf.empty())
				{
					delete buf;
					buf=NULL;
				}
				break;
			}

			buf->operator<<(*iter_value++);
		}
	}

	mutable value_type bufvalue;

 public:
 linebreak_iter(const input_t &iter_valueArg,
		const input_t &iter_endvalueArg)
	 : iter_value(iter_valueArg),
		end_iter_value(iter_endvalueArg),
		buf(new linebreak_callback_save_buf)
		{
		}

 linebreak_iter() : buf(NULL)
	{
	}

	void set_opts(int opts)
	{
		if (buf)
			buf->set_opts(opts);
	}

	~linebreak_iter()
	{
		if (buf)
			delete buf;
	}

 linebreak_iter(const linebreak_iter<input_t> &v)
	 : buf(NULL)
	{
		operator=(v);
	}

	linebreak_iter<input_t> &operator=(const
					   linebreak_iter<input_t> &v)
		{
			if (buf)
				delete buf;
			buf=v.buf;
			iter_value=v.iter_value;
			end_iter_value=v.end_iter_value;
			v.buf=NULL;
			return *this;
		}

	bool operator==(const linebreak_iter<input_t> &v) const
	{
		fill();
		v.fill();

		return buf == NULL && v.buf == NULL;
	}

	bool operator!=(const linebreak_iter<input_t> &v) const
	{
		return !operator==(v);
	}

	value_type operator*() const
	{
		fill();
		return buf == NULL ? UNICODE_LB_MANDATORY:
			buf->lb_buf.front();
	}

	linebreak_iter<input_t> &operator++()
	{
		bufvalue=operator*();

		if (buf)
			buf->lb_buf.pop_front();
		return *this;
	}

	const value_type *operator++(int)
	{
		operator++();
		return &bufvalue;
	}
};

/*
** Like linebreak_callback_base, except the subclass receives both
** the linebreaking value, and the unicode character.
*/

class linebreakc_callback_base {

	unicode_lbc_info_t handle;

	int opts;

#if __cplusplus >= 201103L
 public:
	linebreakc_callback_base(const linebreakc_callback_base &)
		=delete;

	linebreakc_callback_base &operator=(const
					    linebreakc_callback_base
					    &)=delete;
 private:
#else
	linebreakc_callback_base(const linebreakc_callback_base &);
	/* NOT IMPLEMENTED */

	linebreakc_callback_base &operator=(const
					    linebreakc_callback_base
					    &);
	/* NOT IMPLEMENTED */
#endif

 public:
	linebreakc_callback_base();
	virtual ~linebreakc_callback_base();

	void finish();

	void set_opts(int opts);

	friend int linebreakc_trampoline(int, unicode_char, void *);

	linebreakc_callback_base &operator<<(unicode_char uc);

	template<typename iter_type>
		linebreakc_callback_base &operator()(iter_type beg_iter,
						     iter_type end_iter)
	{
		while (beg_iter != end_iter)
			operator<<(*beg_iter++);
		return *this;
	}

	linebreakc_callback_base &operator<<(const
					     std::vector<unicode_char>
					     &vec)
	{
		return operator()(vec.begin(), vec.end());
	}
 private:
	virtual int callback(int, unicode_char);
};

class linebreakc_callback_save_buf : public linebreakc_callback_base {

 public:
	std::list<std::pair<int, unicode_char> > lb_buf;

	linebreakc_callback_save_buf();
	~linebreakc_callback_save_buf();

	using linebreakc_callback_base::operator<<;
	using linebreakc_callback_base::operator();
 private:
	int callback(int, unicode_char);
};


/*
** Convert an input iterator sequence over unicode_chars into
** an input iterator sequence over std::pair<int, unicode_char>,
** the original unicode character, and the linebreaking value before
** the character.
*/

template<typename input_t> class linebreakc_iter
: public std::iterator<std::input_iterator_tag,
	std::pair<int, unicode_char>, void>
{
	mutable input_t iter_value, end_iter_value;

	mutable linebreakc_callback_save_buf *buf;

	void fill() const
	{
		if (buf == NULL)
			return;

		while (buf->lb_buf.empty())
		{
			if (iter_value == end_iter_value)
			{
				buf->finish();
				if (buf->lb_buf.empty())
				{
					delete buf;
					buf=NULL;
				}
				break;
			}

			buf->operator<<(*iter_value);
			++iter_value;
		}
	}

	mutable value_type bufvalue;

 public:
 linebreakc_iter(const input_t &iter_valueArg,
		 const input_t &iter_endvalueArg)
	 : iter_value(iter_valueArg),
		end_iter_value(iter_endvalueArg),
		buf(new linebreakc_callback_save_buf)
		{
		}

 linebreakc_iter() : buf(NULL)
	{
	}

	~linebreakc_iter()
	{
		if (buf)
			delete buf;
	}

 linebreakc_iter(const linebreakc_iter<input_t> &v)
	 : buf(NULL)
	{
		operator=(v);
	}

	linebreakc_iter<input_t> &operator=(const
					    linebreakc_iter<input_t> &v)
		{
			if (buf)
				delete buf;
			buf=v.buf;
			iter_value=v.iter_value;
			end_iter_value=v.end_iter_value;
			v.buf=NULL;
			return *this;
		}

	bool operator==(const linebreakc_iter<input_t> &v) const
	{
		fill();
		v.fill();

		return buf == NULL && v.buf == NULL;
	}

	bool operator!=(const linebreakc_iter<input_t> &v) const
	{
		return !operator==(v);
	}

	value_type operator*() const
	{
		fill();
		return buf == NULL ?
			std::make_pair(UNICODE_LB_MANDATORY,
				       (unicode_char)0):
			buf->lb_buf.front();
	}

	linebreakc_iter<input_t> &operator++()
	{
		bufvalue=operator*();

		if (buf)
			buf->lb_buf.pop_front();
		return *this;
	}

	const value_type *operator++(int)
	{
		operator++();
		return &bufvalue;
	}
};


/*
** Subclass wordbreak_callback_base, implement operator()(int).
**
** Use operator<< or operator()(iterator, iterator) to feed
** unicode_chars into the wordbreaking algorithm. The subclass receives
** word flags, as they become available.
*/

extern "C" int wordbreak_trampoline(int value, void *ptr);

class wordbreak_callback_base {

	unicode_wb_info_t handle;

#if __cplusplus >= 201103L
 public:
	wordbreak_callback_base(const wordbreak_callback_base &)=delete;

	wordbreak_callback_base &operator=(const
					   wordbreak_callback_base &)
		=delete;
 private:
#else
	wordbreak_callback_base(const wordbreak_callback_base &);
	/* NOT IMPLEMENTED */

	wordbreak_callback_base &operator=(const
					   wordbreak_callback_base &);
	/* NOT IMPLEMENTED */
#endif
 public:
	wordbreak_callback_base();
	virtual ~wordbreak_callback_base();

	void finish();

	friend int wordbreak_trampoline(int, void *);

	wordbreak_callback_base &operator<<(unicode_char uc);

	template<typename iter_type>
		wordbreak_callback_base &operator()(iter_type beg_iter,
						    iter_type end_iter)
	{
		while (beg_iter != end_iter)
			operator<<(*beg_iter++);
		return *this;
	}

	wordbreak_callback_base &operator<<(const
					    std::vector<unicode_char>
					    &vec)
	{
		return operator()(vec.begin(), vec.end());
	}
 private:
	virtual int callback(bool);
};

/*
** A C++ wrapper for unicode_wbscan.
*/

class wordbreakscan {

	unicode_wbscan_info_t handle;

#if __cplusplus >= 201103L
 public:
	wordbreakscan(const wordbreakscan &)=delete;
	wordbreakscan &operator=(const wordbreakscan &)=delete;
 private:
#else
	wordbreakscan(const wordbreakscan &);
	/* NOT IMPLEMENTED */

	wordbreakscan &operator=(const wordbreakscan &);
	/* NOT IMPLEMENTED */
#endif
 public:

	wordbreakscan();
	~wordbreakscan();

	bool operator<<(unicode_char uc);

	size_t finish();
};

//! Convert string in unicode_default_chset() to lowercase

std::string tolower(const std::string &string);

//! Convert string in unicode_default_chset() to uppercase

std::string toupper(const std::string &string);

//! Convert string in the given character set to lowercase

std::string tolower(const std::string &string,
		    const std::string &charset);

//! Convert string in the given character set to uppercase

std::string toupper(const std::string &string,
		    const std::string &charset);

//! Convert unicode to lowercase

std::vector<unicode_char> tolower(const std::vector<unicode_char> &u);

//! Convert unicode to uppercase

std::vector<unicode_char> toupper(const std::vector<unicode_char> &u);

#if 0
{
#endif
}
#endif

#endif