1 use crate::NewlineStyle;
4 fn auto_detect(raw_input_text: &str) -> NewlineStyle {
5 if let Some(pos) = raw_input_text.find('\n') {
6 let pos = pos.saturating_sub(1);
7 if let Some('\r') = raw_input_text.chars().nth(pos) {
17 fn native() -> NewlineStyle {
25 /// Apply this newline style to the formatted text. When the style is set
26 /// to `Auto`, the `raw_input_text` is used to detect the existing line
29 /// If the style is set to `Auto` and `raw_input_text` contains no
30 /// newlines, the `Native` style will be used.
31 pub(crate) fn apply(self, formatted_text: &mut String, raw_input_text: &str) {
32 use crate::NewlineStyle::*;
35 style = Self::auto_detect(raw_input_text);
38 style = Self::native();
42 let mut transformed = String::with_capacity(2 * formatted_text.capacity());
43 for c in formatted_text.chars() {
45 '\n' => transformed.push_str("\r\n"),
47 c => transformed.push(c),
50 *formatted_text = transformed;
53 Native => unreachable!("NewlineStyle::Native"),
54 Auto => unreachable!("NewlineStyle::Auto"),
64 fn test_newline_style_auto_detect() {
65 let lf = "One\nTwo\nThree";
66 let crlf = "One\r\nTwo\r\nThree";
67 let none = "One Two Three";
69 assert_eq!(NewlineStyle::Unix, NewlineStyle::auto_detect(lf));
70 assert_eq!(NewlineStyle::Windows, NewlineStyle::auto_detect(crlf));
71 assert_eq!(NewlineStyle::Native, NewlineStyle::auto_detect(none));
75 fn test_newline_style_auto_apply() {
76 let auto = NewlineStyle::Auto;
78 let formatted_text = "One\nTwo\nThree";
79 let raw_input_text = "One\nTwo\nThree";
81 let mut out = String::from(formatted_text);
82 auto.apply(&mut out, raw_input_text);
83 assert_eq!("One\nTwo\nThree", &out, "auto should detect 'lf'");
85 let formatted_text = "One\nTwo\nThree";
86 let raw_input_text = "One\r\nTwo\r\nThree";
88 let mut out = String::from(formatted_text);
89 auto.apply(&mut out, raw_input_text);
90 assert_eq!("One\r\nTwo\r\nThree", &out, "auto should detect 'crlf'");
94 let formatted_text = "One\nTwo\nThree";
95 let raw_input_text = "One Two Three";
97 let mut out = String::from(formatted_text);
98 auto.apply(&mut out, raw_input_text);
100 "One\nTwo\nThree", &out,
101 "auto-native-unix should detect 'lf'"
107 let formatted_text = "One\nTwo\nThree";
108 let raw_input_text = "One Two Three";
110 let mut out = String::from(formatted_text);
111 auto.apply(&mut out, raw_input_text);
113 "One\r\nTwo\r\nThree", &out,
114 "auto-native-windows should detect 'crlf'"