@@ -674,8 +674,10 @@ impl Ini {
674
674
675
675
for line in lines {
676
676
out. push_str ( LINE_ENDING ) ;
677
- out. push_str ( " " . repeat ( indent) . as_ref ( ) ) ;
678
- out. push_str ( line) ;
677
+ if !line. is_empty ( ) {
678
+ out. push_str ( " " . repeat ( indent) . as_ref ( ) ) ;
679
+ out. push_str ( line) ;
680
+ }
679
681
}
680
682
} else {
681
683
out. push_str ( value) ;
@@ -734,6 +736,9 @@ impl Ini {
734
736
}
735
737
} ;
736
738
739
+ // Track blank lines to preserve them in multiline values.
740
+ let mut blank_lines = 0usize ;
741
+
737
742
for ( num, raw_line) in input. lines ( ) . enumerate ( ) {
738
743
let line = match raw_line. find ( |c : char | self . comment_symbols . contains ( & c) ) {
739
744
Some ( idx) => & raw_line[ ..idx] ,
@@ -742,7 +747,13 @@ impl Ini {
742
747
743
748
let trimmed = line. trim ( ) ;
744
749
750
+ // Skip empty lines, but keep track of them for multiline values.
745
751
if trimmed. is_empty ( ) {
752
+ // If a line is _just_ a comment (regardless of whether it's preceded by
753
+ // whitespace), ignore it.
754
+ if line == raw_line {
755
+ blank_lines += 1 ;
756
+ }
746
757
continue ;
747
758
}
748
759
@@ -779,41 +790,52 @@ impl Ini {
779
790
. or_insert_with ( || Some ( String :: new ( ) ) ) ;
780
791
781
792
match val {
782
- Some ( x) => {
783
- x. push_str ( LINE_ENDING ) ;
784
- x. push_str ( trimmed) ;
793
+ Some ( s) => {
794
+ for _ in 0 ..blank_lines {
795
+ s. push_str ( LINE_ENDING ) ;
796
+ }
797
+ s. push_str ( LINE_ENDING ) ;
798
+ s. push_str ( trimmed) ;
785
799
}
786
800
None => {
787
- * val = Some ( format ! ( "{}{}" , LINE_ENDING , trimmed) ) ;
801
+ let mut s = String :: with_capacity (
802
+ ( blank_lines + 1 ) * LINE_ENDING . len ( ) + trimmed. len ( ) ,
803
+ ) ;
804
+ for _ in 0 ..blank_lines {
805
+ s. push_str ( LINE_ENDING ) ;
806
+ }
807
+ s. push_str ( LINE_ENDING ) ;
808
+ s. push_str ( trimmed) ;
809
+ * val = Some ( s) ;
788
810
}
789
811
}
812
+ } else {
813
+ let valmap = map. entry ( section. clone ( ) ) . or_default ( ) ;
790
814
791
- continue ;
792
- }
815
+ match trimmed. find ( & self . delimiters [ ..] ) {
816
+ Some ( delimiter) => {
817
+ let key = caser ( trimmed[ ..delimiter] . trim ( ) ) ;
793
818
794
- let valmap = map. entry ( section. clone ( ) ) . or_default ( ) ;
819
+ if key. is_empty ( ) {
820
+ return Err ( format ! ( "line {}:{}: Key cannot be empty" , num, delimiter) ) ;
821
+ } else {
822
+ current_key = Some ( key. clone ( ) ) ;
795
823
796
- match trimmed. find ( & self . delimiters [ ..] ) {
797
- Some ( delimiter) => {
798
- let key = caser ( trimmed[ ..delimiter] . trim ( ) ) ;
824
+ let value = trimmed[ delimiter + 1 ..] . trim ( ) . to_owned ( ) ;
799
825
800
- if key. is_empty ( ) {
801
- return Err ( format ! ( "line {}:{}: Key cannot be empty" , num, delimiter) ) ;
802
- } else {
826
+ valmap. insert ( key, Some ( value) ) ;
827
+ }
828
+ }
829
+ None => {
830
+ let key = caser ( trimmed) ;
803
831
current_key = Some ( key. clone ( ) ) ;
804
832
805
- let value = trimmed[ delimiter + 1 ..] . trim ( ) . to_owned ( ) ;
806
-
807
- valmap. insert ( key, Some ( value) ) ;
833
+ valmap. insert ( key, None ) ;
808
834
}
809
835
}
810
- None => {
811
- let key = caser ( trimmed) ;
812
- current_key = Some ( key. clone ( ) ) ;
813
-
814
- valmap. insert ( key, None ) ;
815
- }
816
836
}
837
+
838
+ blank_lines = 0 ;
817
839
}
818
840
819
841
Ok ( map)
0 commit comments