# lib.rs - source
Source of the Rust file `/home/justine/.cargo/registry/src/github.com-1ecc6299db9ec823/rust-ini-0.18.0/src/lib.rs`.
Keywords: rust, rustlang, rust-lang

    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
    1869
    1870
    1871
    1872
    1873
    1874
    1875
    1876
    1877
    1878
    1879
    1880
    1881
    1882
    1883
    1884
    1885
    1886
    1887
    1888
    1889
    1890
    1891
    1892
    1893
    1894
    1895
    1896
    1897
    1898
    1899
    1900
    1901
    1902
    1903
    1904
    1905
    1906
    1907
    1908
    1909
    1910
    1911
    1912
    1913
    1914
    1915
    1916
    1917
    1918
    1919
    1920
    1921
    1922
    1923
    1924
    1925
    1926
    1927
    1928
    1929
    1930
    1931
    1932
    1933
    1934
    1935
    1936
    1937
    1938
    1939
    1940
    1941
    1942
    1943
    1944
    1945
    1946
    1947
    1948
    1949
    1950
    1951
    1952
    1953
    1954
    1955
    1956
    1957
    1958
    1959
    1960
    1961
    1962
    1963
    1964
    1965
    1966
    1967
    1968
    1969
    1970
    1971
    1972
    1973
    1974
    1975
    1976
    1977
    1978
    1979
    1980
    1981
    1982
    1983
    1984
    1985
    1986
    1987
    1988
    1989
    1990
    1991
    1992
    1993
    1994
    1995
    1996
    1997
    1998
    1999
    2000
    2001
    2002
    2003
    2004
    2005
    2006
    2007
    2008
    2009
    2010
    2011
    2012
    2013
    2014
    2015
    2016
    2017
    2018
    2019
    2020
    2021
    2022
    2023
    2024
    2025
    2026
    2027
    2028
    2029
    2030
    2031
    2032
    2033
    2034
    2035
    2036
    2037
    2038

    // The MIT License (MIT)

    // Copyright (c) 2014 Y. T. CHUNG

    // Permission is hereby granted, free of charge, to any person obtaining a copy of
    // this software and associated documentation files (the "Software"), to deal in
    // the Software without restriction, including without limitation the rights to
    // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    // the Software, and to permit persons to whom the Software is furnished to do so,
    // subject to the following conditions:

    // The above copyright notice and this permission notice shall be included in all
    // copies or substantial portions of the Software.

    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    //! Ini parser for Rust
    //!
    //! ```no_run
    //! use ini::Ini;
    //!
    //! let mut conf = Ini::new();
    //! conf.with_section(Some("User"))
    //!     .set("name", "Raspberry树莓")
    //!     .set("value", "Pi");
    //! conf.with_section(Some("Library"))
    //!     .set("name", "Sun Yat-sen U")
    //!     .set("location", "Guangzhou=world");
    //! conf.write_to_file("conf.ini").unwrap();
    //!
    //! let i = Ini::load_from_file("conf.ini").unwrap();
    //! for (sec, prop) in i.iter() {
    //!     println!("Section: {:?}", sec);
    //!     for (k, v) in prop.iter() {
    //!         println!("{}:{}", k, v);
    //!     }
    //! }
    //! ```

    use std::char;
    use std::error;
    use std::fmt::{self, Display};
    use std::fs::{File, OpenOptions};
    use std::io::{self, Read, Write};
    use std::io::{Seek, SeekFrom};
    use std::ops::{Index, IndexMut};
    use std::path::Path;
    use std::str::Chars;

    use cfg_if::cfg_if;
    use ordered_multimap::list_ordered_multimap::{Entry, Iter, IterMut, OccupiedEntry, VacantEntry};
    use ordered_multimap::ListOrderedMultimap;
    #[cfg(feature = "case-insensitive")]
    use unicase::UniCase;

    /// Policies for escaping logic
    #[derive(Debug, PartialEq, Copy, Clone)]
    pub enum EscapePolicy {
        /// escape absolutely nothing (dangerous)
        Nothing,
        /// only escape the most necessary things
        Basics,
        /// escape basics and non-ascii characters
        BasicsUnicode,
        /// Escape reserved symbols.
        Reserved,
        /// Escape reserved symbols and non-ascii characters
        ReservedUnicode,
        /// Escape everything that some INI implementations assume
        Everything,
    }

    impl EscapePolicy {
        fn escape_basics(self) -> bool {
            match self {
                EscapePolicy::Nothing => false,
                _ => true,
            }
        }

        fn escape_reserved(self) -> bool {
            match self {
                EscapePolicy::Reserved => true,
                EscapePolicy::ReservedUnicode => true,
                EscapePolicy::Everything => true,
                _ => false,
            }
        }

        fn escape_unicode(self) -> bool {
            match self {
                EscapePolicy::BasicsUnicode => true,
                EscapePolicy::ReservedUnicode => true,
                EscapePolicy::Everything => true,
                _ => false,
            }
        }

        /// Given a character this returns true if it should be escaped as
        /// per this policy or false if not.
        pub fn should_escape(self, c: char) -> bool {
            match c {
                // A single backslash, must be escaped
                // ASCII control characters, U+0000 NUL..= U+001F UNIT SEPARATOR, or U+007F DELETE. The same as char::is_ascii_control()
                '\\' | '\x00'..='\x1f' | '\x7f' => self.escape_basics(),
                ';' | '#' | '=' | ':' => self.escape_reserved(),
                '\u{0080}'..='\u{FFFF}' => self.escape_unicode(),
                _ => false,
            }
        }
    }

    // Escape non-INI characters
    //
    // Common escape sequences: https://en.wikipedia.org/wiki/INI_file#Escape_characters
    //
    // * `\\` \ (a single backslash, escaping the escape character)
    // * `\0` Null character
    // * `\a` Bell/Alert/Audible
    // * `\b` Backspace, Bell character for some applications
    // * `\t` Tab character
    // * `\r` Carriage return
    // * `\n` Line feed
    // * `\;` Semicolon
    // * `\#` Number sign
    // * `\=` Equals sign
    // * `\:` Colon
    // * `\x????` Unicode character with hexadecimal code point corresponding to ????
    fn escape_str(s: &str, policy: EscapePolicy) -> String {
        let mut escaped: String = String::with_capacity(s.len());
        for c in s.chars() {
            // if we know this is not something to escape as per policy, we just
            // write it and continue.
            if !policy.should_escape(c) {
                escaped.push(c);
                continue;
            }

            match c {
                '\\' => escaped.push_str("\\\\"),
                '\0' => escaped.push_str("\\0"),
                '\x01'..='\x06' | '\x0e'..='\x1f' | '\x7f'..='\u{00ff}' => {
                    escaped.push_str(&format!("\\x{:04x}", c as isize)[..])
                }
                '\x07' => escaped.push_str("\\a"),
                '\x08' => escaped.push_str("\\b"),
                '\x0c' => escaped.push_str("\\f"),
                '\x0b' => escaped.push_str("\\v"),
                '\n' => escaped.push_str("\\n"),
                '\t' => escaped.push_str("\\t"),
                '\r' => escaped.push_str("\\r"),
                '\u{0080}'..='\u{FFFF}' => escaped.push_str(&format!("\\x{:04x}", c as isize)[..]),
                _ => {
                    escaped.push('\\');
                    escaped.push(c);
                }
            }
        }
        escaped
    }

    /// Parsing configuration
    pub struct ParseOption {
        /// Allow quote (`"` or `'`) in value
        /// For example
        /// ```ini
        /// [Section]
        /// Key1="Quoted value"
        /// Key2='Single Quote' with extra value
        /// ```
        ///
        /// In this example, Value of `Key1` is `Quoted value`,
        /// and value of `Key2` is `Single Quote with extra value`
        /// if `enabled_quote` is set to `true`.
        pub enabled_quote: bool,

        /// Interpret `\` as an escape character
        /// For example
        /// ```ini
        /// [Section]
        /// Key1=C:\Windows
        /// ```
        ///
        /// If `enabled_escape` is true, then the value of `Key` will become `C:Windows` (`\W` equals to `W`).
        pub enabled_escape: bool,
    }

    impl Default for ParseOption {
        fn default() -> ParseOption {
            ParseOption { enabled_quote: true,
                          enabled_escape: true }
        }
    }

    /// Newline style
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub enum LineSeparator {
        /// System-dependent line separator
        ///
        /// On UNIX system, uses "\n"
        /// On Windows system, uses "\r\n"
        SystemDefault,

        /// Uses "\n" as new line separator
        CR,

        /// Uses "\r\n" as new line separator
        CRLF,
    }

    #[cfg(not(windows))]
    static DEFAULT_LINE_SEPARATOR: &str = "\n";

    #[cfg(windows)]
    static DEFAULT_LINE_SEPARATOR: &str = "\r\n";

    impl fmt::Display for LineSeparator {
        fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            f.write_str(self.as_str())
        }
    }

    impl LineSeparator {
        /// String representation
        pub fn as_str(self) -> &'static str {
            match self {
                LineSeparator::SystemDefault => DEFAULT_LINE_SEPARATOR,
                LineSeparator::CR => "\n",
                LineSeparator::CRLF => "\r\n",
            }
        }
    }

    /// Writing configuration
    #[derive(Debug, Clone)]
    pub struct WriteOption {
        /// Policies about how to escape characters
        pub escape_policy: EscapePolicy,

        /// Newline style
        pub line_separator: LineSeparator,
    }

    impl Default for WriteOption {
        fn default() -> WriteOption {
            WriteOption { escape_policy: EscapePolicy::Basics,
                          line_separator: LineSeparator::SystemDefault }
        }
    }

    cfg_if! {
        if #[cfg(feature = "case-insensitive")] {
            /// Internal storage of section's key
            pub type SectionKey = Option<UniCase<String>>;
            /// Internal storage of property's key
            pub type PropertyKey = UniCase<String>;

            macro_rules! property_get_key {
                ($s:expr) => {
                    &UniCase::from($s)
                };
            }

            macro_rules! property_insert_key {
                ($s:expr) => {
                    UniCase::from($s)
                };
            }

            macro_rules! section_key {
                ($s:expr) => {
                    $s.map(|s| UniCase::from(s.into()))
                };
            }

        } else {
            /// Internal storage of section's key
            pub type SectionKey = Option<String>;
            /// Internal storage of property's key
            pub type PropertyKey = String;

            macro_rules! property_get_key {
                ($s:expr) => {
                    $s
                };
            }

            macro_rules! property_insert_key {
                ($s:expr) => {
                    $s
                };
            }

            macro_rules! section_key {
                ($s:expr) => {
                    $s.map(Into::into)
                };
            }
        }
    }

    /// A setter which could be used to set key-value pair in a specified section
    pub struct SectionSetter<'a> {
        ini: &'a mut Ini,
        section_name: Option<String>,
    }

    impl<'a> SectionSetter<'a> {
        fn new(ini: &'a mut Ini, section_name: Option<String>) -> SectionSetter<'a> {
            SectionSetter { ini, section_name }
        }

        /// Set (replace) key-value pair in this section (all with the same name)
        pub fn set<K, V>(&'a mut self, key: K, value: V) -> &'a mut SectionSetter<'a>
            where K: Into<String>,
                  V: Into<String>
        {
            self.ini
                .entry(self.section_name.clone())
                .or_insert_with(Default::default)
                .insert(key, value);

            self
        }

        /// Delete the first entry in this section with `key`
        pub fn delete<K: AsRef<str>>(&'a mut self, key: &K) -> &'a mut SectionSetter<'a> {
            for prop in self.ini.section_all_mut(self.section_name.as_ref()) {
                prop.remove(key);
            }

            self
        }

        /// Get the entry in this section with `key`
        pub fn get<K: AsRef<str>>(&'a mut self, key: K) -> Option<&'a str> {
            self.ini
                .section(self.section_name.as_ref())
                .and_then(|prop| prop.get(key))
                .map(AsRef::as_ref)
        }
    }

    /// Properties type (key-value pairs)
    #[derive(Clone, Default, Debug, PartialEq)]
    pub struct Properties {
        data: ListOrderedMultimap<PropertyKey, String>,
    }

    impl Properties {
        /// Create an instance
        pub fn new() -> Properties {
            Default::default()
        }

        /// Get the number of the properties
        pub fn len(&self) -> usize {
            self.data.keys_len()
        }

        /// Check if properties has 0 elements
        pub fn is_empty(&self) -> bool {
            self.data.is_empty()
        }

        /// Get an iterator of the properties
        pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&str, &str)> {
            self.data.iter().map(|(k, v)| (k.as_ref(), v.as_str()))
        }

        /// Return true if property exist
        pub fn contains_key<S: AsRef<str>>(&self, s: S) -> bool {
            self.data.contains_key(property_get_key!(s.as_ref()))
        }

        /// Insert (key, value) pair by replace
        pub fn insert<K, V>(&mut self, k: K, v: V)
            where K: Into<String>,
                  V: Into<String>
        {
            self.data.insert(property_insert_key!(k.into()), v.into());
        }

        /// Append key with (key, value) pair
        pub fn append<K, V>(&mut self, k: K, v: V)
            where K: Into<String>,
                  V: Into<String>
        {
            self.data.append(property_insert_key!(k.into()), v.into());
        }

        /// Get the first value associate with the key
        pub fn get<S: AsRef<str>>(&self, s: S) -> Option<&str> {
            self.data.get(property_get_key!(s.as_ref())).map(|v| v.as_str())
        }

        /// Get all values associate with the key
        pub fn get_all<S: AsRef<str>>(&self, s: S) -> impl DoubleEndedIterator<Item = &str> {
            self.data.get_all(property_get_key!(s.as_ref())).map(|v| v.as_str())
        }

        /// Remove the property with the first value of the key
        pub fn remove<S: AsRef<str>>(&mut self, s: S) -> Option<String> {
            self.data.remove(property_get_key!(s.as_ref()))
        }

        /// Remove the property with all values with the same key
        pub fn remove_all<'a, S: AsRef<str>>(&'a mut self, s: S) -> impl DoubleEndedIterator<Item = String> + 'a {
            self.data.remove_all(property_get_key!(s.as_ref()))
        }

        fn get_mut<S: AsRef<str>>(&mut self, s: S) -> Option<&mut str> {
            self.data.get_mut(property_get_key!(s.as_ref())).map(|v| v.as_mut_str())
        }
    }

    impl<S: AsRef<str>> Index<S> for Properties {
        type Output = str;

        fn index(&self, index: S) -> &str {
            let s = index.as_ref();
            match self.get(s) {
                Some(p) => p,
                None => panic!("Key `{}` does not exist", s),
            }
        }
    }

    /// A view into a vacant entry in a `Ini`
    pub struct SectionVacantEntry<'a> {
        inner: VacantEntry<'a, SectionKey, Properties>,
    }

    impl<'a> SectionVacantEntry<'a> {
        /// Insert one new section
        pub fn insert(self, value: Properties) -> &'a mut Properties {
            self.inner.insert(value)
        }
    }

    /// A view into a occupied entry in a `Ini`
    pub struct SectionOccupiedEntry<'a> {
        inner: OccupiedEntry<'a, SectionKey, Properties>,
    }

    impl<'a> SectionOccupiedEntry<'a> {
        /// Into the first internal mutable properties
        pub fn into_mut(self) -> &'a mut Properties {
            self.inner.into_mut()
        }

        /// Append a new section
        pub fn append(&mut self, prop: Properties) {
            self.inner.append(prop);
        }

        fn last_mut(&'a mut self) -> &'a mut Properties {
            self.inner
                .iter_mut()
                .next_back()
                .expect("occupied section shouldn't have 0 property")
        }
    }

    /// A view into an `Ini`, which may either be vacant or occupied.
    pub enum SectionEntry<'a> {
        Vacant(SectionVacantEntry<'a>),
        Occupied(SectionOccupiedEntry<'a>),
    }

    impl<'a> SectionEntry<'a> {
        /// Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
        pub fn or_insert(self, properties: Properties) -> &'a mut Properties {
            match self {
                SectionEntry::Occupied(e) => e.into_mut(),
                SectionEntry::Vacant(e) => e.insert(properties),
            }
        }

        /// Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
        pub fn or_insert_with<F: FnOnce() -> Properties>(self, default: F) -> &'a mut Properties {
            match self {
                SectionEntry::Occupied(e) => e.into_mut(),
                SectionEntry::Vacant(e) => e.insert(default()),
            }
        }
    }

    impl<'a> From<Entry<'a, SectionKey, Properties>> for SectionEntry<'a> {
        fn from(e: Entry<'a, SectionKey, Properties>) -> SectionEntry<'a> {
            match e {
                Entry::Occupied(inner) => SectionEntry::Occupied(SectionOccupiedEntry { inner }),
                Entry::Vacant(inner) => SectionEntry::Vacant(SectionVacantEntry { inner }),
            }
        }
    }

    /// Ini struct
    #[derive(Clone, Default)]
    pub struct Ini {
        sections: ListOrderedMultimap<SectionKey, Properties>,
    }

    impl Ini {
        /// Create an instance
        pub fn new() -> Ini {
            Default::default()
        }

        /// Set with a specified section, `None` is for the general section
        pub fn with_section<S>(&mut self, section: Option<S>) -> SectionSetter
            where S: Into<String>
        {
            SectionSetter::new(self, section.map(Into::into))
        }

        /// Set with general section, a simple wrapper of `with_section(None::<String>)`
        pub fn with_general_section(&mut self) -> SectionSetter {
            self.with_section(None::<String>)
        }

        /// Get the immmutable general section
        pub fn general_section(&self) -> &Properties {
            self.section(None::<String>)
                .expect("There is no general section in this Ini")
        }

        /// Get the mutable general section
        pub fn general_section_mut(&mut self) -> &mut Properties {
            self.section_mut(None::<String>)
                .expect("There is no general section in this Ini")
        }

        /// Get a immutable section
        pub fn section<S>(&self, name: Option<S>) -> Option<&Properties>
            where S: Into<String>
        {
            self.sections.get(&section_key!(name))
        }

        /// Get a mutable section
        pub fn section_mut<S>(&mut self, name: Option<S>) -> Option<&mut Properties>
            where S: Into<String>
        {
            self.sections.get_mut(&section_key!(name))
        }

        /// Get all sections immutable with the same key
        pub fn section_all<S>(&self, name: Option<S>) -> impl DoubleEndedIterator<Item = &Properties>
            where S: Into<String>
        {
            self.sections.get_all(&section_key!(name))
        }

        /// Get all sections mutable with the same key
        pub fn section_all_mut<S>(&mut self, name: Option<S>) -> impl DoubleEndedIterator<Item = &mut Properties>
            where S: Into<String>
        {
            self.sections.get_all_mut(&section_key!(name))
        }

        /// Get the entry
        #[cfg(not(feature = "case-insensitive"))]
        pub fn entry(&mut self, name: Option<String>) -> SectionEntry<'_> {
            SectionEntry::from(self.sections.entry(name.map(|s| s)))
        }

        /// Get the entry
        #[cfg(feature = "case-insensitive")]
        pub fn entry(&mut self, name: Option<String>) -> SectionEntry<'_> {
            SectionEntry::from(self.sections.entry(name.map(UniCase::from)))
        }

        /// Clear all entries
        pub fn clear(&mut self) {
            self.sections.clear()
        }

        /// Iterate with sections
        pub fn sections(&self) -> impl DoubleEndedIterator<Item = Option<&str>> {
            self.sections.keys().map(|s| s.as_ref().map(AsRef::as_ref))
        }

        /// Set key-value to a section
        pub fn set_to<S>(&mut self, section: Option<S>, key: String, value: String)
            where S: Into<String>
        {
            self.with_section(section).set(key, value);
        }

        /// Get the first value from the sections with key
        ///
        /// Example:
        ///
        /// ```
        /// use ini::Ini;
        /// let input = "[sec]\nabc = def\n";
        /// let ini = Ini::load_from_str(input).unwrap();
        /// assert_eq!(ini.get_from(Some("sec"), "abc"), Some("def"));
        /// ```
        pub fn get_from<'a, S>(&'a self, section: Option<S>, key: &str) -> Option<&'a str>
            where S: Into<String>
        {
            self.sections.get(&section_key!(section)).and_then(|prop| prop.get(key))
        }

        /// Get the first value from the sections with key, return the default value if it does not exist
        ///
        /// Example:
        ///
        /// ```
        /// use ini::Ini;
        /// let input = "[sec]\n";
        /// let ini = Ini::load_from_str(input).unwrap();
        /// assert_eq!(ini.get_from_or(Some("sec"), "key", "default"), "default");
        /// ```
        pub fn get_from_or<'a, S>(&'a self, section: Option<S>, key: &str, default: &'a str) -> &'a str
            where S: Into<String>
        {
            self.get_from(section, key).unwrap_or(default)
        }

        /// Get the first mutable value from the sections with key
        pub fn get_from_mut<'a, S>(&'a mut self, section: Option<S>, key: &str) -> Option<&'a mut str>
            where S: Into<String>
        {
            self.sections
                .get_mut(&section_key!(section))
                .and_then(|prop| prop.get_mut(key))
        }

        /// Delete the first section with key, return the properties if it exists
        pub fn delete<S>(&mut self, section: Option<S>) -> Option<Properties>
            where S: Into<String>
        {
            let key = section_key!(section);
            self.sections.remove(&key)
        }

        /// Delete the key from the section, return the value if key exists or None
        pub fn delete_from<S>(&mut self, section: Option<S>, key: &str) -> Option<String>
            where S: Into<String>
        {
            self.section_mut(section).and_then(|prop| prop.remove(key))
        }

        /// Total sections count
        pub fn len(&self) -> usize {
            self.sections.keys_len()
        }

        /// Check if object coutains no section
        pub fn is_empty(&self) -> bool {
            self.sections.is_empty()
        }
    }

    impl<S: Into<String>> Index<Option<S>> for Ini {
        type Output = Properties;

        fn index(&self, index: Option<S>) -> &Properties {
            match self.section(index) {
                Some(p) => p,
                None => panic!("Section does not exist"),
            }
        }
    }

    impl<S: Into<String>> IndexMut<Option<S>> for Ini {
        fn index_mut(&mut self, index: Option<S>) -> &mut Properties {
            match self.section_mut(index) {
                Some(p) => p,
                None => panic!("Section does not exist"),
            }
        }
    }

    impl<'q> Index<&'q str> for Ini {
        type Output = Properties;

        fn index<'a>(&'a self, index: &'q str) -> &'a Properties {
            match self.section(Some(index)) {
                Some(p) => p,
                None => panic!("Section `{}` does not exist", index),
            }
        }
    }

    impl<'q> IndexMut<&'q str> for Ini {
        fn index_mut<'a>(&'a mut self, index: &'q str) -> &'a mut Properties {
            match self.section_mut(Some(index)) {
                Some(p) => p,
                None => panic!("Section `{}` does not exist", index),
            }
        }
    }

    impl Ini {
        /// Write to a file
        pub fn write_to_file<P: AsRef<Path>>(&self, filename: P) -> io::Result<()> {
            self.write_to_file_policy(filename, EscapePolicy::Basics)
        }

        /// Write to a file
        pub fn write_to_file_policy<P: AsRef<Path>>(&self, filename: P, policy: EscapePolicy) -> io::Result<()> {
            let mut file = OpenOptions::new().write(true)
                                             .truncate(true)
                                             .create(true)
                                             .open(filename.as_ref())?;
            self.write_to_policy(&mut file, policy)
        }

        /// Write to a file with options
        pub fn write_to_file_opt<P: AsRef<Path>>(&self, filename: P, opt: WriteOption) -> io::Result<()> {
            let mut file = OpenOptions::new().write(true)
                                             .truncate(true)
                                             .create(true)
                                             .open(filename.as_ref())?;
            self.write_to_opt(&mut file, opt)
        }

        /// Write to a writer
        pub fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()> {
            self.write_to_opt(writer, Default::default())
        }

        /// Write to a writer
        pub fn write_to_policy<W: Write>(&self, writer: &mut W, policy: EscapePolicy) -> io::Result<()> {
            self.write_to_opt(writer,
                              WriteOption { escape_policy: policy,
                                            ..Default::default() })
        }

        /// Write to a writer with options
        pub fn write_to_opt<W: Write>(&self, writer: &mut W, opt: WriteOption) -> io::Result<()> {
            let mut firstline = true;

            if let Some(props) = self.sections.get(&None) {
                for (k, v) in props.iter() {
                    let k_str = escape_str(&k[..], opt.escape_policy);
                    let v_str = escape_str(&v[..], opt.escape_policy);
                    write!(writer, "{}={}{}", k_str, v_str, opt.line_separator)?;

                    firstline = false;
                }
            }

            for (section, props) in &self.sections {
                if let Some(ref section) = *section {
                    if firstline {
                        firstline = false;
                    } else {
                        // Write an empty line between sections
                        writer.write_all(opt.line_separator.as_str().as_bytes())?;
                    }

                    write!(writer,
                           "[{}]{}",
                           escape_str(&section[..], opt.escape_policy),
                           opt.line_separator)?;

                    for (k, v) in props.iter() {
                        let k_str = escape_str(&k[..], opt.escape_policy);
                        let v_str = escape_str(&v[..], opt.escape_policy);
                        write!(writer, "{}={}{}", k_str, v_str, opt.line_separator)?;
                    }
                }
            }
            Ok(())
        }
    }

    impl Ini {
        /// Load from a string
        pub fn load_from_str(buf: &str) -> Result<Ini, ParseError> {
            Ini::load_from_str_opt(buf, ParseOption::default())
        }

        /// Load from a string, but do not interpret '\' as an escape character
        pub fn load_from_str_noescape(buf: &str) -> Result<Ini, ParseError> {
            Ini::load_from_str_opt(buf,
                                   ParseOption { enabled_escape: false,
                                                 ..ParseOption::default() })
        }

        /// Load from a string with options
        pub fn load_from_str_opt(buf: &str, opt: ParseOption) -> Result<Ini, ParseError> {
            let mut parser = Parser::new(buf.chars(), opt);
            parser.parse()
        }

        /// Load from a reader
        pub fn read_from<R: Read>(reader: &mut R) -> Result<Ini, Error> {
            Ini::read_from_opt(reader, ParseOption::default())
        }

        /// Load from a reader, but do not interpret '\' as an escape character
        pub fn read_from_noescape<R: Read>(reader: &mut R) -> Result<Ini, Error> {
            Ini::read_from_opt(reader,
                               ParseOption { enabled_escape: false,
                                             ..ParseOption::default() })
        }

        /// Load from a reader with options
        pub fn read_from_opt<R: Read>(reader: &mut R, opt: ParseOption) -> Result<Ini, Error> {
            let mut s = String::new();
            reader.read_to_string(&mut s).map_err(Error::Io)?;
            let mut parser = Parser::new(s.chars(), opt);
            match parser.parse() {
                Err(e) => Err(Error::Parse(e)),
                Ok(success) => Ok(success),
            }
        }

        /// Load from a file
        pub fn load_from_file<P: AsRef<Path>>(filename: P) -> Result<Ini, Error> {
            Ini::load_from_file_opt(filename, ParseOption::default())
        }

        /// Load from a file, but do not interpret '\' as an escape character
        pub fn load_from_file_noescape<P: AsRef<Path>>(filename: P) -> Result<Ini, Error> {
            Ini::load_from_file_opt(filename,
                                    ParseOption { enabled_escape: false,
                                                  ..ParseOption::default() })
        }

        /// Load from a file with options
        pub fn load_from_file_opt<P: AsRef<Path>>(filename: P, opt: ParseOption) -> Result<Ini, Error> {
            let mut reader = match File::open(filename.as_ref()) {
                Err(e) => {
                    return Err(Error::Io(e));
                }
                Ok(r) => r,
            };

            let mut with_bom = false;

            // Check if file starts with a BOM marker
            // UTF-8: EF BB BF
            let mut bom = [0u8; 3];
            if let Ok(..) = reader.read_exact(&mut bom) {
                if &bom == b"\xEF\xBB\xBF" {
                    with_bom = true;
                }
            }

            if !with_bom {
                // Reset file pointer
                reader.seek(SeekFrom::Start(0))?;
            }

            Ini::read_from_opt(&mut reader, opt)
        }
    }

    /// Iterator for traversing sections
    pub struct SectionIter<'a> {
        inner: Iter<'a, SectionKey, Properties>,
    }

    impl<'a> Iterator for SectionIter<'a> {
        type Item = (Option<&'a str>, &'a Properties);

        fn next(&mut self) -> Option<Self::Item> {
            self.inner.next().map(|(k, v)| (k.as_ref().map(|s| s.as_str()), v))
        }

        fn size_hint(&self) -> (usize, Option<usize>) {
            self.inner.size_hint()
        }
    }

    impl DoubleEndedIterator for SectionIter<'_> {
        fn next_back(&mut self) -> Option<Self::Item> {
            self.inner.next_back().map(|(k, v)| (k.as_ref().map(|s| s.as_str()), v))
        }
    }

    /// Iterator for traversing sections
    pub struct SectionIterMut<'a> {
        inner: IterMut<'a, SectionKey, Properties>,
    }

    impl<'a> Iterator for SectionIterMut<'a> {
        type Item = (Option<&'a str>, &'a mut Properties);

        fn next(&mut self) -> Option<Self::Item> {
            self.inner.next().map(|(k, v)| (k.as_ref().map(|s| s.as_str()), v))
        }

        fn size_hint(&self) -> (usize, Option<usize>) {
            self.inner.size_hint()
        }
    }

    impl DoubleEndedIterator for SectionIterMut<'_> {
        fn next_back(&mut self) -> Option<Self::Item> {
            self.inner.next_back().map(|(k, v)| (k.as_ref().map(|s| s.as_str()), v))
        }
    }

    impl<'a> Ini {
        /// Immutable iterate though sections
        pub fn iter(&'a self) -> SectionIter<'a> {
            SectionIter { inner: self.sections.iter() }
        }

        /// Mutable iterate though sections
        #[deprecated(note = "Use `iter_mut` instead!")]
        pub fn mut_iter(&'a mut self) -> SectionIterMut<'a> {
            self.iter_mut()
        }

        /// Mutable iterate though sections
        pub fn iter_mut(&'a mut self) -> SectionIterMut<'a> {
            SectionIterMut { inner: self.sections.iter_mut() }
        }
    }

    impl<'a> IntoIterator for &'a Ini {
        type Item = (Option<&'a str>, &'a Properties);
        type IntoIter = SectionIter<'a>;

        fn into_iter(self) -> Self::IntoIter {
            self.iter()
        }
    }

    impl<'a> IntoIterator for &'a mut Ini {
        type Item = (Option<&'a str>, &'a mut Properties);
        type IntoIter = SectionIterMut<'a>;

        fn into_iter(self) -> Self::IntoIter {
            self.iter_mut()
        }
    }

    // Ini parser
    struct Parser<'a> {
        ch: Option<char>,
        rdr: Chars<'a>,
        line: usize,
        col: usize,
        opt: ParseOption,
    }

    #[derive(Debug)]
    /// Parse error
    pub struct ParseError {
        pub line: usize,
        pub col: usize,
        pub msg: String,
    }

    impl Display for ParseError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}:{} {}", self.line, self.col, self.msg)
        }
    }

    impl error::Error for ParseError {}

    /// Error while parsing an INI document
    #[derive(Debug)]
    pub enum Error {
        Io(io::Error),
        Parse(ParseError),
    }

    impl Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match *self {
                Error::Io(ref err) => err.fmt(f),
                Error::Parse(ref err) => err.fmt(f),
            }
        }
    }

    impl error::Error for Error {
        fn source(&self) -> Option<&(dyn error::Error + 'static)> {
            match *self {
                Error::Io(ref err) => err.source(),
                Error::Parse(ref err) => err.source(),
            }
        }
    }

    impl From<io::Error> for Error {
        fn from(err: io::Error) -> Self {
            Error::Io(err)
        }
    }

    impl<'a> Parser<'a> {
        // Create a parser
        pub fn new(rdr: Chars<'a>, opt: ParseOption) -> Parser<'a> {
            let mut p = Parser { ch: None,
                                 line: 0,
                                 col: 0,
                                 rdr,
                                 opt };
            p.bump();
            p
        }

        fn eof(&self) -> bool {
            self.ch.is_none()
        }

        fn bump(&mut self) {
            self.ch = self.rdr.next();
            match self.ch {
                Some('\n') => {
                    self.line += 1;
                    self.col = 0;
                }
                Some(..) => {
                    self.col += 1;
                }
                None => {}
            }
        }

        fn error<U, M: Into<String>>(&self, msg: M) -> Result<U, ParseError> {
            Err(ParseError { line: self.line,
                             col: self.col,
                             msg: msg.into() })
        }

        /// Consume all the white space until the end of the line or a tab
        fn parse_whitespace(&mut self) {
            while let Some(c) = self.ch {
                if !c.is_whitespace() && c != '\n' && c != '\t' && c != '\r' {
                    break;
                }
                self.bump();
            }
        }

        /// Consume all the white space except line break
        fn parse_whitespace_except_line_break(&mut self) {
            while let Some(c) = self.ch {
                if (c == '\n' || c == '\r' || !c.is_whitespace()) && c != '\t' {
                    break;
                }
                self.bump();
            }
        }

        /// Parse the whole INI input
        pub fn parse(&mut self) -> Result<Ini, ParseError> {
            let mut result = Ini::new();
            let mut curkey: String = "".into();
            let mut cursec: Option<String> = None;

            self.parse_whitespace();
            while let Some(cur_ch) = self.ch {
                match cur_ch {
                    ';' | '#' => {
                        if cfg!(not(feature = "inline-comment")) {
                            // Inline comments is not supported, so comments must starts from a new line
                            //
                            // https://en.wikipedia.org/wiki/INI_file#Comments
                            if self.col > 1 {
                                return self.error("doesn't support inline comment");
                            }
                        }

                        self.parse_comment();
                    }
                    '[' => match self.parse_section() {
                        Ok(sec) => {
                            let msec = sec[..].trim();
                            cursec = Some((*msec).to_string());
                            match result.entry(cursec.clone()) {
                                SectionEntry::Vacant(v) => {
                                    v.insert(Default::default());
                                }
                                SectionEntry::Occupied(mut o) => {
                                    o.append(Default::default());
                                }
                            }
                            self.bump();
                        }
                        Err(e) => return Err(e),
                    },
                    '=' | ':' => {
                        if (&curkey[..]).is_empty() {
                            return self.error("missing key");
                        }
                        match self.parse_val() {
                            Ok(val) => {
                                let mval = val[..].trim().to_owned();
                                match result.entry(cursec.clone()) {
                                    SectionEntry::Vacant(v) => {
                                        // cursec must be None (the General Section)
                                        let mut prop = Properties::new();
                                        prop.insert(curkey, mval);
                                        v.insert(prop);
                                    }
                                    SectionEntry::Occupied(mut o) => {
                                        // Insert into the last (current) section
                                        o.last_mut().append(curkey, mval);
                                    }
                                }
                                curkey = "".into();
                            }
                            Err(e) => return Err(e),
                        }
                    }
                    _ => match self.parse_key() {
                        Ok(key) => {
                            let mkey: String = key[..].trim().to_owned();
                            curkey = mkey;
                        }
                        Err(e) => return Err(e),
                    },
                }

                self.parse_whitespace();
            }

            Ok(result)
        }

        fn parse_comment(&mut self) {
            while let Some(c) = self.ch {
                self.bump();
                if c == '\n' {
                    break;
                }
            }
        }

        fn parse_str_until(&mut self, endpoint: &[Option<char>]) -> Result<String, ParseError> {
            let mut result: String = String::new();

            while !endpoint.contains(&self.ch) {
                match self.ch {
                    None => {
                        return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint));
                    }
                    Some('\\') if self.opt.enabled_escape => {
                        self.bump();
                        if self.eof() {
                            return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint));
                        }
                        match self.ch.unwrap() {
                            '0' => result.push('\0'),
                            'a' => result.push('\x07'),
                            'b' => result.push('\x08'),
                            't' => result.push('\t'),
                            'r' => result.push('\r'),
                            'n' => result.push('\n'),
                            '\n' => (),
                            'x' => {
                                // Unicode 4 character
                                let mut code: String = String::with_capacity(4);
                                for _ in 0..4 {
                                    self.bump();
                                    if self.eof() {
                                        return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint));
                                    } else if let Some('\\') = self.ch {
                                        self.bump();
                                        if self.ch != Some('\n') {
                                            return self.error(format!("expecting \"\\\\n\" but \
                                                                       found \"{:?}\".",
                                                                      self.ch));
                                        }
                                    }
                                    code.push(self.ch.unwrap());
                                }
                                let r = u32::from_str_radix(&code[..], 16);
                                match r {
                                    Ok(c) => match char::from_u32(c) {
                                        Some(c) => result.push(c),
                                        None => {
                                            return self.error("unknown character in \\xHH form");
                                        }
                                    },
                                    Err(_) => return self.error("unknown character in \\xHH form"),
                                }
                            }
                            c => result.push(c),
                        }
                    }
                    Some(c) => {
                        result.push(c);
                    }
                }
                self.bump();
            }
            Ok(result)
        }

        fn parse_section(&mut self) -> Result<String, ParseError> {
            cfg_if! {
                if #[cfg(feature = "brackets-in-section-names")] {
                    // Skip [
                    self.bump();
                    let endpoint;
                    cfg_if! {
                        if #[cfg(feature = "inline-comment")] {
                            endpoint = &[Some('\r'), Some('\n'), Some('#'), Some(';')]
                        } else {
                            endpoint = &[Some('\r'), Some('\n')];
                        }
                    }

                    let result = self.parse_str_until(endpoint);
                    if result.is_err() {
                        return result;
                    }

                    cfg_if! {
                        if #[cfg(feature = "inline-comment")] {
                            match self.ch {
                                Some('#') | Some(';') => {
                                    let r = self.parse_str_until(&[Some('\r'), Some('\n')]);
                                    if r.is_err() {
                                        return r;
                                    }
                                },
                                _ => {}
                            }
                        }
                    }

                    let s = result.unwrap();
                    let cut= s.chars().rev().take_while(|c| *c != ']').count();
                    let len = s.len() - cut - 1;
                    if cut > 0 {
                        let all_acceptable = s[len+1..].chars().all(|c| c == ' ' || c == '\t');
                        if !all_acceptable {
                            return self.error("not acceptable character after section end");
                        }
                    }
                    Ok(String::from(&s[0..len]))
                } else {
                    // Skip [
                    self.bump();
                    self.parse_str_until(&[Some(']')])
                }
            }
        }

        fn parse_key(&mut self) -> Result<String, ParseError> {
            self.parse_str_until(&[Some('='), Some(':')])
        }

        fn parse_val(&mut self) -> Result<String, ParseError> {
            self.bump();
            // Issue #35: Allow empty value
            self.parse_whitespace_except_line_break();

            match self.ch {
                None => Ok(String::new()),
                Some('"') if self.opt.enabled_quote => {
                    self.bump();
                    self.parse_str_until(&[Some('"')]).and_then(|s| {
                                                          self.bump(); // Eats the last "
                                                                       // Parse until EOL
                                                          self.parse_str_until_eol().map(|x| s + &x)
                                                      })
                }
                Some('\'') if self.opt.enabled_quote => {
                    self.bump();
                    self.parse_str_until(&[Some('\'')]).and_then(|s| {
                                                           self.bump(); // Eats the last '
                                                                        // Parse until EOL
                                                           self.parse_str_until_eol().map(|x| s + &x)
                                                       })
                }
                _ => self.parse_str_until_eol(),
            }
        }

        #[cfg(not(feature = "inline-comment"))]
        fn parse_str_until_eol(&mut self) -> Result<String, ParseError> {
            self.parse_str_until(&[Some('\n'), Some('\r'), None])
        }

        #[cfg(feature = "inline-comment")]
        fn parse_str_until_eol(&mut self) -> Result<String, ParseError> {
            self.parse_str_until(&[Some('\n'), Some('\r'), Some(';'), Some('#'), None])
        }
    }

    // ------------------------------------------------------------------------------

    #[cfg(test)]
    mod test {
        use std::env::temp_dir;

        use super::*;

        #[test]
        fn property_replace() {
            let mut props = Properties::new();
            props.insert("k1", "v1");

            assert_eq!(Some("v1"), props.get("k1"));
            let res = props.get_all("k1").collect::<Vec<&str>>();
            assert_eq!(res, vec!["v1"]);

            props.insert("k1", "v2");
            assert_eq!(Some("v2"), props.get("k1"));

            let res = props.get_all("k1").collect::<Vec<&str>>();
            assert_eq!(res, vec!["v2"]);
        }

        #[test]
        fn property_get_vec() {
            let mut props = Properties::new();
            props.append("k1", "v1");

            assert_eq!(Some("v1"), props.get("k1"));

            props.append("k1", "v2");

            assert_eq!(Some("v1"), props.get("k1"));

            let res = props.get_all("k1").collect::<Vec<&str>>();
            assert_eq!(res, vec!["v1", "v2"]);

            let res = props.get_all("k2").collect::<Vec<&str>>();
            assert!(res.is_empty());
        }

        #[test]
        fn property_remove() {
            let mut props = Properties::new();
            props.append("k1", "v1");
            props.append("k1", "v2");

            let res = props.remove_all("k1").collect::<Vec<String>>();
            assert_eq!(res, vec!["v1", "v2"]);
            assert!(!props.contains_key("k1"));
        }

        #[test]
        #[cfg(not(feature = "brackets-in-section-names"))]
        fn load_from_str_with_valid_input() {
            let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]foo=bar\n";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 2);
            assert!(output.section(Some("sec1")).is_some());

            let sec1 = output.section(Some("sec1")).unwrap();
            assert_eq!(sec1.len(), 2);
            let key1: String = "key1".into();
            assert!(sec1.contains_key(&key1));
            let key2: String = "key2".into();
            assert!(sec1.contains_key(&key2));
            let val1: String = "val1".into();
            assert_eq!(sec1[&key1], val1);
            let val2: String = "377".into();
            assert_eq!(sec1[&key2], val2);
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn load_from_str_with_valid_input() {
            let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]\nfoo=bar\n";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 2);
            assert!(output.section(Some("sec1")).is_some());

            let sec1 = output.section(Some("sec1")).unwrap();
            assert_eq!(sec1.len(), 2);
            let key1: String = "key1".into();
            assert!(sec1.contains_key(&key1));
            let key2: String = "key2".into();
            assert!(sec1.contains_key(&key2));
            let val1: String = "val1".into();
            assert_eq!(sec1[&key1], val1);
            let val2: String = "377".into();
            assert_eq!(sec1[&key2], val2);
        }

        #[test]
        #[cfg(not(feature = "brackets-in-section-names"))]
        fn load_from_str_without_ending_newline() {
            let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]foo=bar";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn load_from_str_without_ending_newline() {
            let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]\nfoo=bar";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());
        }

        #[test]
        fn parse_comment() {
            let input = "; abcdefghijklmn\n";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());
        }

        #[cfg(not(feature = "inline-comment"))]
        #[test]
        fn inline_comment_not_supported() {
            let input = "
    [section name]
    name = hello # abcdefg
    gender = mail ; abdddd
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello # abcdefg");
            assert_eq!(ini.get_from(Some("section name"), "gender").unwrap(), "mail ; abdddd");
        }

        #[test]
        #[cfg_attr(not(feature = "inline-comment"), should_panic)]
        fn inline_comment() {
            let input = "
    [section name] # comment in section line
    name = hello # abcdefg
    gender = mail ; abdddd
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello");
            assert_eq!(ini.get_from(Some("section name"), "gender").unwrap(), "mail");
        }

        #[test]
        fn sharp_comment() {
            let input = "
    [section name]
    name = hello
    # abcdefg
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello");
        }

        #[test]
        fn iter() {
            let input = "
    [section name]
    name = hello # abcdefg
    gender = mail ; abdddd
    ";
            let mut ini = Ini::load_from_str(input).unwrap();

            for _ in &mut ini {}
            for _ in &ini {}
            // for _ in ini {}
        }

        #[test]
        fn colon() {
            let input = "
    [section name]
    name: hello
    gender : mail
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello");
            assert_eq!(ini.get_from(Some("section name"), "gender").unwrap(), "mail");
        }

        #[test]
        fn string() {
            let input = "
    [section name]
    # This is a comment
    Key = \"Value\"
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value");
        }

        #[test]
        fn string_multiline() {
            let input = "
    [section name]
    # This is a comment
    Key = \"Value
    Otherline\"
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value\nOtherline");
        }

        #[test]
        fn string_comment() {
            let input = "
    [section name]
    # This is a comment
    Key = \"Value   # This is not a comment ; at all\"
    Stuff = Other
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(),
                       "Value   # This is not a comment ; at all");
        }

        #[test]
        fn string_single() {
            let input = "
    [section name]
    # This is a comment
    Key = 'Value'
    Stuff = Other
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value");
        }

        #[test]
        fn string_includes_quote() {
            let input = "
    [Test]
    Comment[tr]=İnternet'e erişin
    Comment[uk]=Доступ до Інтернету
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("Test"), "Comment[tr]").unwrap(), "İnternet'e erişin");
        }

        #[test]
        fn string_single_multiline() {
            let input = "
    [section name]
    # This is a comment
    Key = 'Value
    Otherline'
    Stuff = Other
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value\nOtherline");
        }

        #[test]
        fn string_single_comment() {
            let input = "
    [section name]
    # This is a comment
    Key = 'Value   # This is not a comment ; at all'
    ";
            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(),
                       "Value   # This is not a comment ; at all");
        }

        #[test]
        fn load_from_str_with_valid_empty_input() {
            let input = "key1=\nkey2=val2\n";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 1);
            assert!(output.section(None::<String>).is_some());

            let sec1 = output.section(None::<String>).unwrap();
            assert_eq!(sec1.len(), 2);
            let key1: String = "key1".into();
            assert!(sec1.contains_key(&key1));
            let key2: String = "key2".into();
            assert!(sec1.contains_key(&key2));
            let val1: String = "".into();
            assert_eq!(sec1[&key1], val1);
            let val2: String = "val2".into();
            assert_eq!(sec1[&key2], val2);
        }

        #[test]
        fn load_from_str_with_crlf() {
            let input = "key1=val1\r\nkey2=val2\r\n";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 1);
            assert!(output.section(None::<String>).is_some());
            let sec1 = output.section(None::<String>).unwrap();
            assert_eq!(sec1.len(), 2);
            let key1: String = "key1".into();
            assert!(sec1.contains_key(&key1));
            let key2: String = "key2".into();
            assert!(sec1.contains_key(&key2));
            let val1: String = "val1".into();
            assert_eq!(sec1[&key1], val1);
            let val2: String = "val2".into();
            assert_eq!(sec1[&key2], val2);
        }

        #[test]
        fn load_from_str_with_cr() {
            let input = "key1=val1\rkey2=val2\r";
            let opt = Ini::load_from_str(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 1);
            assert!(output.section(None::<String>).is_some());
            let sec1 = output.section(None::<String>).unwrap();
            assert_eq!(sec1.len(), 2);
            let key1: String = "key1".into();
            assert!(sec1.contains_key(&key1));
            let key2: String = "key2".into();
            assert!(sec1.contains_key(&key2));
            let val1: String = "val1".into();
            assert_eq!(sec1[&key1], val1);
            let val2: String = "val2".into();
            assert_eq!(sec1[&key2], val2);
        }

        #[test]
        #[cfg(not(feature = "brackets-in-section-names"))]
        fn load_from_file_with_bom() {
            let file_name = temp_dir().join("rust_ini_load_from_file_with_bom");

            let file_content = b"\xEF\xBB\xBF[Test]Key=Value\n";

            {
                let mut file = File::create(&file_name).expect("create");
                file.write_all(file_content).expect("write");
            }

            let ini = Ini::load_from_file(&file_name).unwrap();
            assert_eq!(ini.get_from(Some("Test"), "Key"), Some("Value"));
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn load_from_file_with_bom() {
            let file_name = temp_dir().join("rust_ini_load_from_file_with_bom");

            let file_content = b"\xEF\xBB\xBF[Test]\nKey=Value\n";

            {
                let mut file = File::create(&file_name).expect("create");
                file.write_all(file_content).expect("write");
            }

            let ini = Ini::load_from_file(&file_name).unwrap();
            assert_eq!(ini.get_from(Some("Test"), "Key"), Some("Value"));
        }

        #[test]
        #[cfg(not(feature = "brackets-in-section-names"))]
        fn load_from_file_without_bom() {
            let file_name = temp_dir().join("rust_ini_load_from_file_without_bom");

            let file_content = b"[Test]Key=Value\n";

            {
                let mut file = File::create(&file_name).expect("create");
                file.write_all(file_content).expect("write");
            }

            let ini = Ini::load_from_file(&file_name).unwrap();
            assert_eq!(ini.get_from(Some("Test"), "Key"), Some("Value"));
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn load_from_file_without_bom() {
            let file_name = temp_dir().join("rust_ini_load_from_file_without_bom");

            let file_content = b"[Test]\nKey=Value\n";

            {
                let mut file = File::create(&file_name).expect("create");
                file.write_all(file_content).expect("write");
            }

            let ini = Ini::load_from_file(&file_name).unwrap();
            assert_eq!(ini.get_from(Some("Test"), "Key"), Some("Value"));
        }

        #[test]
        fn get_with_non_static_key() {
            let input = "key1=val1\nkey2=val2\n";
            let opt = Ini::load_from_str(input).unwrap();

            let sec1 = opt.section(None::<String>).unwrap();

            let key = "key1".to_owned();
            sec1.get(&key).unwrap();
        }

        #[test]
        fn load_from_str_noescape() {
            let input = "path=C:\\Windows\\Some\\Folder\\";
            let opt = Ini::load_from_str_noescape(input);
            assert!(opt.is_ok());

            let output = opt.unwrap();
            assert_eq!(output.len(), 1);
            let sec = output.section(None::<String>).unwrap();
            assert_eq!(sec.len(), 1);
            assert!(sec.contains_key("path"));
            assert_eq!(&sec["path"], "C:\\Windows\\Some\\Folder\\");
        }

        #[test]
        fn partial_quoting_double() {
            let input = "
    [Section]
    A=\"quote\" arg0
    B=b";

            let opt = Ini::load_from_str(input).unwrap();
            let sec = opt.section(Some("Section")).unwrap();
            assert_eq!(&sec["A"], "quote arg0");
            assert_eq!(&sec["B"], "b");
        }

        #[test]
        fn partial_quoting_single() {
            let input = "
    [Section]
    A='quote' arg0
    B=b";

            let opt = Ini::load_from_str(input).unwrap();
            let sec = opt.section(Some("Section")).unwrap();
            assert_eq!(&sec["A"], "quote arg0");
            assert_eq!(&sec["B"], "b");
        }

        #[test]
        fn parse_without_quote() {
            let input = "
    [Desktop Entry]
    Exec = \"/path/to/exe with space\" arg
    ";

            let opt = Ini::load_from_str_opt(input,
                                             ParseOption { enabled_quote: false,
                                                           ..ParseOption::default() }).unwrap();
            let sec = opt.section(Some("Desktop Entry")).unwrap();
            assert_eq!(&sec["Exec"], "\"/path/to/exe with space\" arg");
        }

        #[test]
        #[cfg(feature = "case-insensitive")]
        fn case_insensitive() {
            let input = "
    [SecTION]
    KeY=value
    ";

            let ini = Ini::load_from_str(input).unwrap();
            let section = ini.section(Some("section")).unwrap();
            let val = section.get("key").unwrap();
            assert_eq!("value", val);
        }

        #[test]
        fn preserve_order_section() {
            let input = r"
    none2 = n2
    [SB]
    p2 = 2
    [SA]
    x2 = 2
    [SC]
    cd1 = x
    [xC]
    xd = x
            ";

            let data = Ini::load_from_str(input).unwrap();
            let keys: Vec<Option<&str>> = data.iter().map(|(k, _)| k).collect();

            assert_eq!(keys.len(), 5);
            assert_eq!(keys[0], None);
            assert_eq!(keys[1], Some("SB"));
            assert_eq!(keys[2], Some("SA"));
            assert_eq!(keys[3], Some("SC"));
            assert_eq!(keys[4], Some("xC"));
        }

        #[test]
        fn preserve_order_property() {
            let input = r"
    x2 = n2
    x1 = n2
    x3 = n2
    ";
            let data = Ini::load_from_str(input).unwrap();
            let section = data.general_section();
            let keys: Vec<&str> = section.iter().map(|(k, _)| k).collect();
            assert_eq!(keys, vec!["x2", "x1", "x3"]);
        }

        #[test]
        fn preserve_order_property_in_section() {
            let input = r"
    [s]
    x2 = n2
    xb = n2
    a3 = n3
    ";
            let data = Ini::load_from_str(input).unwrap();
            let section = data.section(Some("s")).unwrap();
            let keys: Vec<&str> = section.iter().map(|(k, _)| k).collect();
            assert_eq!(keys, vec!["x2", "xb", "a3"])
        }

        #[test]
        fn preserve_order_write() {
            let input = r"
    x2 = n2
    x1 = n2
    x3 = n2
    [s]
    x2 = n2
    xb = n2
    a3 = n3
    ";
            let data = Ini::load_from_str(input).unwrap();
            let mut buf = vec![];
            data.write_to(&mut buf).unwrap();
            let new_data = Ini::load_from_str(&String::from_utf8(buf).unwrap()).unwrap();

            let sec0 = new_data.general_section();
            let keys0: Vec<&str> = sec0.iter().map(|(k, _)| k).collect();
            assert_eq!(keys0, vec!["x2", "x1", "x3"]);

            let sec1 = new_data.section(Some("s")).unwrap();
            let keys1: Vec<&str> = sec1.iter().map(|(k, _)| k).collect();
            assert_eq!(keys1, vec!["x2", "xb", "a3"]);
        }

        #[test]
        fn write_line_separator() {
            use std::str;

            let mut ini = Ini::new();
            ini.with_section(Some("Section1"))
               .set("Key1", "Value")
               .set("Key2", "Value");
            ini.with_section(Some("Section2"))
               .set("Key1", "Value")
               .set("Key2", "Value");

            {
                let mut buf = Vec::new();
                ini.write_to_opt(&mut buf,
                                 WriteOption { line_separator: LineSeparator::CR,
                                               ..Default::default() })
                   .unwrap();

                assert_eq!("[Section1]\nKey1=Value\nKey2=Value\n\n[Section2]\nKey1=Value\nKey2=Value\n",
                           str::from_utf8(&buf).unwrap());
            }

            {
                let mut buf = Vec::new();
                ini.write_to_opt(&mut buf,
                                 WriteOption { line_separator: LineSeparator::CRLF,
                                               ..Default::default() })
                   .unwrap();

                assert_eq!("[Section1]\r\nKey1=Value\r\nKey2=Value\r\n\r\n[Section2]\r\nKey1=Value\r\nKey2=Value\r\n",
                           str::from_utf8(&buf).unwrap());
            }

            {
                let mut buf = Vec::new();
                ini.write_to_opt(&mut buf,
                                 WriteOption { line_separator: LineSeparator::SystemDefault,
                                               ..Default::default() })
                   .unwrap();

                if cfg!(windows) {
                    assert_eq!("[Section1]\r\nKey1=Value\r\nKey2=Value\r\n\r\n[Section2]\r\nKey1=Value\r\nKey2=Value\r\n",
                               str::from_utf8(&buf).unwrap());
                } else {
                    assert_eq!("[Section1]\nKey1=Value\nKey2=Value\n\n[Section2]\nKey1=Value\nKey2=Value\n",
                               str::from_utf8(&buf).unwrap());
                }
            }
        }

        #[test]
        fn duplicate_sections() {
            // https://github.com/zonyitoo/rust-ini/issues/49

            let input = r"
    [Peer]
    foo = a
    bar = b

    [Peer]
    foo = c
    bar = d

    [Peer]
    foo = e
    bar = f
    ";

            let ini = Ini::load_from_str(input).unwrap();
            assert_eq!(3, ini.section_all(Some("Peer")).count());

            let mut iter = ini.iter();
            let (k1, p1) = iter.next().unwrap();
            assert_eq!(Some("Peer"), k1);
            assert_eq!(Some("a"), p1.get("foo"));
            assert_eq!(Some("b"), p1.get("bar"));
            let (k2, p2) = iter.next().unwrap();
            assert_eq!(Some("Peer"), k2);
            assert_eq!(Some("c"), p2.get("foo"));
            assert_eq!(Some("d"), p2.get("bar"));
            let (k3, p3) = iter.next().unwrap();
            assert_eq!(Some("Peer"), k3);
            assert_eq!(Some("e"), p3.get("foo"));
            assert_eq!(Some("f"), p3.get("bar"));

            assert_eq!(None, iter.next());
        }

        #[test]
        fn fix_issue63() {
            let section = "PHP";
            let key = "engine";
            let value = "On";
            let new_value = "Off";

            // create a new configuration
            let mut conf = Ini::new();
            conf.with_section(Some(section)).set(key, value);

            // assert the value is the one expected
            let v = conf.get_from(Some(section), key).unwrap();
            assert_eq!(v, value);

            // update the section/key with a new value
            conf.set_to(Some(section), key.to_string(), new_value.to_string());

            // assert the new value was set
            let v = conf.get_from(Some(section), key).unwrap();
            assert_eq!(v, new_value);
        }

        #[test]
        fn fix_issue64() {
            let input = format!("some-key=åäö{}", super::DEFAULT_LINE_SEPARATOR);

            let conf = Ini::load_from_str(&input).unwrap();

            let mut output = Vec::new();
            conf.write_to_policy(&mut output, EscapePolicy::Basics).unwrap();

            assert_eq!(input, String::from_utf8(output).unwrap());
        }

        #[test]
        fn invalid_codepoint() {
            use std::io::Cursor;

            let d = vec![10, 8, 68, 8, 61, 10, 126, 126, 61, 49, 10, 62, 8, 8, 61, 10, 91, 93, 93, 36, 91, 61, 10, 75, 91,
                         10, 10, 10, 61, 92, 120, 68, 70, 70, 70, 70, 70, 126, 61, 10, 0, 0, 61, 10, 38, 46, 49, 61, 0,
                         39, 0, 0, 46, 92, 120, 46, 36, 91, 91, 1, 0, 0, 16, 0, 0, 0, 0, 0, 0];
            let mut file = Cursor::new(d);
            assert!(Ini::read_from(&mut file).is_err());
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn fix_issue84() {
            let input = "
    [[*]]
    a = b
    c = d
    ";
            let ini = Ini::load_from_str(input).unwrap();
            let sect = ini.section(Some("[*]"));
            assert!(sect.is_some());
            assert!(sect.unwrap().contains_key("a"));
            assert!(sect.unwrap().contains_key("c"));
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn fix_issue84_brackets_inside() {
            let input = "
    [a[b]c]
    a = b
    c = d
    ";
            let ini = Ini::load_from_str(input).unwrap();
            let sect = ini.section(Some("a[b]c"));
            assert!(sect.is_some());
            assert!(sect.unwrap().contains_key("a"));
            assert!(sect.unwrap().contains_key("c"));
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn fix_issue84_whitespaces_after_bracket() {
            let input = "
    [[*]]\t\t
    a = b
    c = d
    ";
            let ini = Ini::load_from_str(input).unwrap();
            let sect = ini.section(Some("[*]"));
            assert!(sect.is_some());
            assert!(sect.unwrap().contains_key("a"));
            assert!(sect.unwrap().contains_key("c"));
        }

        #[test]
        #[cfg(feature = "brackets-in-section-names")]
        fn fix_issue84_not_whitespaces_after_bracket() {
            let input = "
    [[*]]xx
    a = b
    c = d
    ";
            let ini = Ini::load_from_str(input);
            assert!(ini.is_err());
        }
    }