Extract literals into constants

This commit is contained in:
Ruben Schmidmeister 2019-04-24 19:34:10 +02:00
parent add473667a
commit 58d7b2cc66
No known key found for this signature in database
GPG Key ID: 29387B5A7AAF863F

View File

@ -11,13 +11,15 @@ pub(crate) fn apply_newline_style(
formatted_text: &mut String,
raw_input_text: &str,
) {
const WINDOWS_NEWLINE: &str = "\r\n";
match effective_newline_style(newline_style, raw_input_text) {
EffectiveNewlineStyle::Windows => {
let mut transformed = String::with_capacity(2 * formatted_text.capacity());
for c in formatted_text.chars() {
match c {
'\n' => transformed.push_str("\r\n"),
'\r' => continue,
LINE_FEED => transformed.push_str(WINDOWS_NEWLINE),
CARRIAGE_RETURN => continue,
c => transformed.push(c),
}
}
@ -27,6 +29,9 @@ pub(crate) fn apply_newline_style(
}
}
const LINE_FEED: char = '\n';
const CARRIAGE_RETURN: char = '\r';
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum EffectiveNewlineStyle {
Windows,
@ -46,9 +51,9 @@ fn effective_newline_style(
}
fn auto_detect_newline_style(raw_input_text: &str) -> EffectiveNewlineStyle {
if let Some(pos) = raw_input_text.find('\n') {
if let Some(pos) = raw_input_text.find(LINE_FEED) {
let pos = pos.saturating_sub(1);
if let Some('\r') = raw_input_text.chars().nth(pos) {
if let Some(CARRIAGE_RETURN) = raw_input_text.chars().nth(pos) {
EffectiveNewlineStyle::Windows
} else {
EffectiveNewlineStyle::Unix