1 use crate::NewlineStyle;
3 /// Apply this newline style to the formatted text. When the style is set
4 /// to `Auto`, the `raw_input_text` is used to detect the existing line
7 /// If the style is set to `Auto` and `raw_input_text` contains no
8 /// newlines, the `Native` style will be used.
9 pub(crate) fn apply_newline_style(
10 newline_style: NewlineStyle,
11 formatted_text: &mut String,
14 use crate::NewlineStyle::*;
15 let mut style = newline_style;
17 style = auto_detect_newline_style(raw_input_text);
24 let mut transformed = String::with_capacity(2 * formatted_text.capacity());
25 for c in formatted_text.chars() {
27 '\n' => transformed.push_str("\r\n"),
29 c => transformed.push(c),
32 *formatted_text = transformed;
35 Native => unreachable!("NewlineStyle::Native"),
36 Auto => unreachable!("NewlineStyle::Auto"),
40 fn auto_detect_newline_style(raw_input_text: &str) -> NewlineStyle {
41 if let Some(pos) = raw_input_text.find('\n') {
42 let pos = pos.saturating_sub(1);
43 if let Some('\r') = raw_input_text.chars().nth(pos) {
53 fn native() -> NewlineStyle {
66 fn test_newline_style_auto_detect() {
67 let lf = "One\nTwo\nThree";
68 let crlf = "One\r\nTwo\r\nThree";
69 let none = "One Two Three";
71 assert_eq!(NewlineStyle::Unix, auto_detect_newline_style(lf));
72 assert_eq!(NewlineStyle::Windows, auto_detect_newline_style(crlf));
73 assert_eq!(NewlineStyle::Native, auto_detect_newline_style(none));
77 fn test_newline_style_auto_apply() {
78 let auto = NewlineStyle::Auto;
80 let formatted_text = "One\nTwo\nThree";
81 let raw_input_text = "One\nTwo\nThree";
83 let mut out = String::from(formatted_text);
84 apply_newline_style(auto, &mut out, raw_input_text);
85 assert_eq!("One\nTwo\nThree", &out, "auto should detect 'lf'");
87 let formatted_text = "One\nTwo\nThree";
88 let raw_input_text = "One\r\nTwo\r\nThree";
90 let mut out = String::from(formatted_text);
91 apply_newline_style(auto, &mut out, raw_input_text);
92 assert_eq!("One\r\nTwo\r\nThree", &out, "auto should detect 'crlf'");
96 let formatted_text = "One\nTwo\nThree";
97 let raw_input_text = "One Two Three";
99 let mut out = String::from(formatted_text);
100 apply_newline_style(auto, &mut out, raw_input_text);
102 "One\nTwo\nThree", &out,
103 "auto-native-unix should detect 'lf'"
109 let formatted_text = "One\nTwo\nThree";
110 let raw_input_text = "One Two Three";
112 let mut out = String::from(formatted_text);
113 apply_newline_style(auto, &mut out, raw_input_text);
115 "One\r\nTwo\r\nThree", &out,
116 "auto-native-windows should detect 'crlf'"