@@ -137,16 +137,16 @@ void file_settings::from_string(const xtd::ustring& text) {
137
137
// comment = ustring::empty_string;
138
138
auto key_value = line.split ({key_value_separator});
139
139
if (key_value.size () == 1 ) {
140
- if (!ustring::is_empty (comment)) before_key_comment_[unescaping (key_value[0 ].trim ().trim (string_delimiter))] = comment;
140
+ if (!ustring::is_empty (comment)) before_key_comment_[section][ unescaping (key_value[0 ].trim ().trim (string_delimiter))] = comment;
141
141
section_key_values_[section][unescaping (key_value[0 ].trim ().trim (string_delimiter))] = " " ;
142
142
} else {
143
143
auto key_comment = ustring::empty_string;
144
144
auto value = separate_comment (ustring::join (ustring::format (" {}" , key_value_separator), key_value, 1 ), key_comment);
145
145
if (value.starts_with (string_delimiter) && value.ends_with (string_delimiter)) value = value.trim (string_delimiter);
146
146
if (value.starts_with (alt_string_delimiter) && value.ends_with (alt_string_delimiter)) value = value.trim (alt_string_delimiter);
147
- if (!ustring::is_empty (key_comment)) key_comment_[unescaping (key_value[0 ].trim ().trim (string_delimiter))] = key_comment;
147
+ if (!ustring::is_empty (key_comment)) key_comment_[section][ unescaping (key_value[0 ].trim ().trim (string_delimiter))] = key_comment;
148
148
section_key_values_[section][unescaping (key_value[0 ].trim ().trim (string_delimiter))] = unescaping (value);
149
- if (!ustring::is_empty (comment)) before_key_comment_[unescaping (key_value[0 ].trim ().trim (string_delimiter))] = comment;
149
+ if (!ustring::is_empty (comment)) before_key_comment_[section][ unescaping (key_value[0 ].trim ().trim (string_delimiter))] = comment;
150
150
}
151
151
comment = ustring::empty_string;
152
152
}
@@ -233,12 +233,21 @@ ustring file_settings::to_string() const noexcept {
233
233
auto as_it = after_section_comment_.find (section);
234
234
if (as_it != after_section_comment_.end () && !ustring::is_empty (as_it->second )) text += split_comment (as_it->second );
235
235
for (auto [key, value] : key_value) {
236
- auto bk_it = before_key_comment_.find (key);
237
- if (bk_it != before_key_comment_.end () && !ustring::is_empty (bk_it->second )) text += split_comment (bk_it->second );
238
- auto k_it = key_comment_.find (key);
239
- text += ustring::format (" {} {} {}{}\n " , key, key_value_separator, value.starts_with (' ' ) || value.starts_with (' \t ' ) || value.ends_with (' ' ) || value.ends_with (' \t ' ) || value.contains (comment_delimiter) || value.contains (alt_comment_delimiter) || value.contains (key_value_separator) ? ustring::format (" \" {}\" " , value) : value, k_it != key_comment_.end () && !ustring::is_empty (k_it->second ) ? ustring::format (" {}" , k_it->second ) : " " );
240
- auto ak_it = after_key_comment_.find (key);
241
- if (ak_it != after_key_comment_.end () && !ustring::is_empty (ak_it->second )) text += split_comment (ak_it->second );
236
+ if (before_key_comment_.find (section) != before_key_comment_.end ()) {
237
+ auto bk_it = before_key_comment_.at (section).find (key);
238
+ if (bk_it != before_key_comment_.at (section).end () && !ustring::is_empty (bk_it->second )) text += split_comment (bk_it->second );
239
+ }
240
+ if (key_comment_.find (section) == key_comment_.end ())
241
+ text += ustring::format (" {} {} {}\n " , key, key_value_separator, value.starts_with (' ' ) || value.starts_with (' \t ' ) || value.ends_with (' ' ) || value.ends_with (' \t ' ) || value.contains (comment_delimiter) || value.contains (alt_comment_delimiter) || value.contains (key_value_separator) ? ustring::format (" \" {}\" " , value) : value);
242
+ else {
243
+ auto k_it = key_comment_.at (section).find (key);
244
+ text += ustring::format (" {} {} {}{}\n " , key, key_value_separator, value.starts_with (' ' ) || value.starts_with (' \t ' ) || value.ends_with (' ' ) || value.ends_with (' \t ' ) || value.contains (comment_delimiter) || value.contains (alt_comment_delimiter) || value.contains (key_value_separator) ? ustring::format (" \" {}\" " , value) : value, k_it != key_comment_.at (section).end () && !ustring::is_empty (k_it->second ) ? ustring::format (" {}" , k_it->second ) : " " );
245
+ }
246
+
247
+ if (after_key_comment_.find (section) != after_key_comment_.end ()) {
248
+ auto ak_it = after_key_comment_.at (section).find (key);
249
+ if (ak_it != after_key_comment_.at (section).end () && !ustring::is_empty (ak_it->second )) text += split_comment (ak_it->second );
250
+ }
242
251
}
243
252
}
244
253
if (!ustring::is_empty (bottom_file_comment_)) text += environment::new_line () + split_comment (bottom_file_comment_);
0 commit comments