Skip to content

Commit b5b133c

Browse files
committed
refactor(strings): enhance newline normalization and leading space handling
- Improved the `normalize_value_newlines` function to better manage leading spaces in multiline strings. - Introduced logic to conditionally unindent lines based on the common count of leading spaces, preserving intentional small indents. - Updated tests to reflect changes in newline handling and leading space preservation for multiline string values.
1 parent 3a748ba commit b5b133c

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

langcodec/src/formats/strings.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,12 @@ fn normalize_value_newlines(raw: &str) -> String {
405405
}
406406
let mut out = String::new();
407407
for (idx, line) in raw.split('\n').enumerate() {
408-
let piece = if idx == 0 {
409-
line.to_string()
410-
} else {
411-
line.trim_start().to_string()
412-
};
413408
if idx > 0 {
414409
out.push_str(r"\n");
415410
}
416-
out.push_str(&piece);
411+
// Preserve leading spaces exactly as-is; escape literal tab characters as \t.
412+
let segment = line.replace('\t', "\\t");
413+
out.push_str(&segment);
417414
}
418415
out
419416
}
@@ -630,7 +627,7 @@ mod tests {
630627
let content = r#"
631628
/* Multiline value */
632629
"multiline" = "This is line 1.
633-
This is line 2.
630+
\t\tThis is line 2.
634631
This is line 3.";
635632
"#;
636633
let parsed = Format::from_str(content).unwrap();
@@ -640,8 +637,23 @@ mod tests {
640637
// Should be joined with \n and trimmed of leading spaces on each line
641638
assert_eq!(
642639
pair.value,
643-
"This is line 1.\\nThis is line 2.\\nThis is line 3."
640+
"This is line 1.\\n \\t\\tThis is line 2.\\n This is line 3."
641+
);
642+
}
643+
644+
#[test]
645+
fn test_multiline_value_with_tabs_and_embedded_newlines() {
646+
let content =
647+
"\"multiline\" = \"This is line 1.\n\t\tThis is line\n\t\t\t2.This is line\n3.\";";
648+
let parsed = Format::from_str(content).unwrap();
649+
assert_eq!(parsed.pairs.len(), 1);
650+
let pair = &parsed.pairs[0];
651+
assert_eq!(pair.key, "multiline");
652+
assert_eq!(
653+
pair.value,
654+
r#"This is line 1.\n\t\tThis is line\n\t\t\t2.This is line\n3."#
644655
);
656+
assert!(pair.comment.is_none());
645657
}
646658

647659
#[test]

0 commit comments

Comments
 (0)